test_qrcode_channel_api.py
4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import pytest
from app.infrastructure.file_parser import InMemoryOSSClient
from app.model.agent import Agent, AgentPosition, AgentSkill, AgentStatus
from app.model.agent_group import QrcodeChannel
from app.model.position_tag import PositionTag, PositionTagUser
from app.model.wecom_sync import WeComBot
from app.schema.qrcode_channel import QrcodeCreateRequest, QrcodeExportRequest, QrcodeUpdateRequest
from app.service.agent_group_helper_service import InMemoryWeComClient
from app.service.qrcode_channel_service import QrcodeChannelService
from tests.conftest import TestingSessionLocal
@pytest.fixture
def db():
session = TestingSessionLocal()
session.query(AgentSkill).delete()
session.query(AgentPosition).delete()
session.query(Agent).delete()
session.query(QrcodeChannel).delete()
session.query(PositionTagUser).delete()
session.query(WeComBot).delete()
session.query(PositionTag).delete()
session.commit()
yield session
session.close()
def _seed_position_with_bot(db, name="驻店销售岗", code="store_sales", bot_row_id=1, bot_id="emp_1001", bot_name="张三机器人"):
pos = PositionTag(name=name, code=code)
db.add(pos)
db.flush()
bot = WeComBot(id=bot_row_id, channel="wt_ww", bot_id=bot_id, bot_code=f"wt_ww:{bot_id}", bot_name=bot_name)
db.add(bot)
db.flush()
db.add(PositionTagUser(position_tag_id=pos.id, wecom_bot_id=bot.id))
db.commit()
db.refresh(pos)
return pos, bot
def _seed_agent(db, name="管家Agent", code="steward", status=AgentStatus.enable):
a = Agent(name=name, code=code, status=status)
db.add(a)
db.commit()
db.refresh(a)
return a
class TestQrcodeChannel:
def test_create_success_with_applicant_bot(self, db):
pos, bot = _seed_position_with_bot(db)
_seed_agent(db)
svc = QrcodeChannelService(db, InMemoryWeComClient())
result = svc.create(QrcodeCreateRequest(qrcode_name="销售岗活码", position_name=pos.name, applicant_bot_id=bot.id))
assert result.qrcode_id > 0
assert result.position.position_name == pos.name
assert result.employee.employee_name == bot.bot_name
def test_create_conflict_when_position_has_qrcode(self, db):
pos, bot = _seed_position_with_bot(db)
_seed_agent(db)
svc = QrcodeChannelService(db, InMemoryWeComClient())
svc.create(QrcodeCreateRequest(qrcode_name="销售岗活码1", position_name=pos.name, applicant_bot_id=bot.id))
with pytest.raises(ValueError, match="岗位已存在活码"):
svc.create(QrcodeCreateRequest(qrcode_name="销售岗活码2", position_name=pos.name, applicant_bot_id=bot.id))
def test_create_error_when_applicant_not_in_position(self, db):
pos, _ = _seed_position_with_bot(db, bot_row_id=1, bot_id="emp_1")
other = WeComBot(id=2, channel="wt_ww", bot_id="emp_2", bot_code="wt_ww:emp_2", bot_name="李四机器人")
db.add(other)
db.commit()
svc = QrcodeChannelService(db, InMemoryWeComClient())
with pytest.raises(ValueError, match="未绑定到该岗位"):
svc.create(QrcodeCreateRequest(qrcode_name="x", position_name=pos.name, applicant_bot_id=2))
def test_create_error_when_applicant_already_bound(self, db):
pos1, bot = _seed_position_with_bot(db, name="销售岗A", code="a")
pos2 = PositionTag(name="销售岗B", code="b")
db.add(pos2)
db.flush()
db.add(PositionTagUser(position_tag_id=pos2.id, wecom_bot_id=bot.id))
db.commit()
svc = QrcodeChannelService(db, InMemoryWeComClient())
_seed_agent(db)
svc.create(QrcodeCreateRequest(qrcode_name="A活码", position_name=pos1.name, applicant_bot_id=bot.id))
with pytest.raises(ValueError, match="员工已绑定活码"):
svc.create(QrcodeCreateRequest(qrcode_name="B活码", position_name=pos2.name, applicant_bot_id=bot.id))
def test_update_list_disable_enable_delete_export(self, db):
pos, bot = _seed_position_with_bot(db)
_seed_agent(db)
svc = QrcodeChannelService(db, InMemoryWeComClient(), InMemoryOSSClient())
created = svc.create(QrcodeCreateRequest(qrcode_name="原始", position_name=pos.name, applicant_bot_id=bot.id))
svc.update(created.qrcode_id, QrcodeUpdateRequest(qrcode_name="更新后", welcome_message="hi"))
detail = svc.get_detail(created.qrcode_id)
assert detail.qrcode_name == "更新后"
listed = svc.list_qrcodes()
assert listed.total == 1
row = listed.list[0]
assert row.position_id == pos.id
assert row.employee_id == bot.id
assert row.qrcode_name == "更新后"
assert isinstance(row.agents, list)
assert svc.disable(created.qrcode_id, "x").success is True
assert svc.enable(created.qrcode_id).success is True
assert svc.export(QrcodeExportRequest()).total_count >= 1
assert svc.delete(created.qrcode_id).success is True