Files
xieyi-weixin-/test_candidate_matching.py
T
2026-11-02 14:05:03 +08:00

83 lines
4.3 KiB
Python

"""Real local recipient search against a synthetic customer directory."""
import sqlite3
from contextlib import closing
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
import review_assistant_contacts as contacts
class CustomerSearchAccuracyTest(unittest.TestCase):
def setUp(self):
self.root = self.enterContext(tempfile.TemporaryDirectory())
directory = Path(self.root) / '1000'
directory.mkdir()
with closing(sqlite3.connect(directory / 'user.db')) as db, db:
db.executescript('''
CREATE TABLE user_table(id TEXT, name TEXT, real_name TEXT);
CREATE TABLE external_user_relation_v3(user_id TEXT, status INTEGER,
stranger_type INTEGER, add_customer_time INTEGER, remarks TEXT, real_remarks TEXT);
''')
db.executemany('INSERT INTO user_table VALUES(?,?,?)', [
('2001', '旧昵称', ''), ('2002', '小高', '高瑞'),
('2003', 'Amy Chen', ''), ('2004', '小林', '')])
db.executemany('INSERT INTO external_user_relation_v3 VALUES(?,2049,0,100,?,?)', [
('2001', '高瑞的家属', ''), ('2002', '高瑞', '高先生'),
('2003', 'Amy Chen', ''), ('2004', '林女士', '')])
with closing(sqlite3.connect(directory / 'message.db')) as db, db:
db.execute('CREATE TABLE message_table(conversation_id TEXT, send_time INTEGER)')
db.executemany('INSERT INTO message_table VALUES(?,?)', [
('S:1000_2001', 900), ('S:1000_2002', 100),
('M:2003', 800), ('S:1000_2004', 700)])
with closing(sqlite3.connect(directory / 'session.db')) as db, db:
db.execute('CREATE TABLE conversation_table(id TEXT,name TEXT,roomname_remark TEXT,is_blocked INTEGER)')
db.execute('INSERT INTO conversation_table VALUES(?,?,?,0)', ('S:1000_2004', '小林', '复诊客户'))
self.enterContext(patch.object(contacts, 'active_context', return_value={
'ready': True, 'account_id': '1000', 'reason': ''}))
def search(self, query, **kwargs):
result = contacts.list_recipients('1000', query, cache_roots=[self.root], status='sendable', **kwargs)
self.assertTrue(result['ready'], result.get('reason'))
return result
def test_exact_name_precedes_more_recent_partial_match_before_pagination(self):
page = self.search('高瑞', limit=1)
self.assertEqual(page['items'][0]['id'], 'S:1000_2002')
self.assertEqual(page['total'], 2)
self.assertTrue(page['has_more'])
self.assertEqual(self.search('高瑞', limit=1, offset=1)['items'][0]['id'], 'S:1000_2001')
def test_sender_account_digits_do_not_match_every_customer(self):
self.assertEqual(self.search('1000')['total'], 0)
self.assertEqual(self.search('2002')['items'][0]['id'], 'S:1000_2002')
self.assertEqual(self.search('002')['total'], 1)
self.assertEqual(self.search('S:1000_2002')['items'][0]['id'], 'S:1000_2002')
def test_nickname_customer_remark_and_session_remark_are_searchable(self):
for query, expected in [('小高','S:1000_2002'), ('高先生','S:1000_2002'), ('复诊客户','S:1000_2004')]:
with self.subTest(query=query):
row = self.search(query)['items'][0]
self.assertEqual(row['id'], expected)
self.assertEqual(row['matchedName'], query)
def test_whitespace_fullwidth_case_and_invisible_paste_characters(self):
for query in [' AMY CHEN ', 'amy chen', 'AMY\u200b CHEN']:
self.assertEqual(self.search(query)['items'][0]['id'], 'M:2003')
def test_multiple_terms_must_all_match_same_customer(self):
self.assertEqual(self.search('高瑞 2002')['items'][0]['id'], 'S:1000_2002')
self.assertEqual(self.search('高瑞 2003')['total'], 0)
self.assertEqual(self.search('高瑞 不存在')['total'], 0)
def test_empty_query_keeps_recency_order_and_search_does_not_mutate_cache(self):
self.search('小高')
self.assertEqual([r['id'] for r in self.search('')['items']],
['S:1000_2001','M:2003','S:1000_2004','S:1000_2002'])
self.assertTrue(all(not r['matchedName'] for r in self.search('')['items']))
if __name__ == '__main__':
unittest.main()