AutoRpa/rpa_cart/tests/test_repo.py

131 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 支付落表仓库:假连接录 SQL。不连真库。
from __future__ import annotations
from contextlib import contextmanager
from repo import OraclePayRepo
# 只交出一条连接。
class _FakePool:
# 挂上要借出的那条假连接。
def __init__(self, conn):
self._conn = conn
# 借出连接。
@contextmanager
def acquire(self):
yield self._conn
# 两行支付一次 executemany,带 operate_type=2 和订单号。
def test_save_pay_rows_inserts_operate_type_2():
many: list[tuple[str, list]] = []
# 录 SQL 的假游标。
class FakeCursor:
# 进入 with,返回自身。
def __enter__(self):
return self
# 退出 with,不吞异常。
def __exit__(self, *args):
return False
# 记下一次批量 SQL 和全部 bind。
def executemany(self, sql, params):
many.append((sql, [dict(item) for item in params]))
# 提交后记下 committed。
class FakeConn:
committed = False
# 新开录 SQL 的游标。
def cursor(self):
return FakeCursor()
# 记下已提交。
def commit(self):
self.committed = True
# 本套用例不应回滚。
def rollback(self):
raise AssertionError("不应 rollback")
# 假连接不计数。
def close(self):
return None
conn = FakeConn()
repo = OraclePayRepo(_FakePool(conn))
row = {
"operate_type": 2,
"oms_id": 0,
"platform_type": 66,
"supplier_name": "广东壹号药业有限公司",
"bar_code": "6921",
"purchase_price": 9.85,
"purchase_qty": 10,
"purchase_total": 98.5,
"status": None,
"fail_reason": None,
"pay_order_no": "ZQD1",
"pay_order_dtl_id": "a1b2c3d4e5f6789012345678901234ab",
"push_status": 0,
"org_code": None,
"multi_platform_flag": 0,
}
second = dict(row)
second["pay_order_no"] = "ZQD2"
second["pay_order_dtl_id"] = "b1b2c3d4e5f6789012345678901234ab"
repo.save_pay_rows([row, second])
assert conn.committed is True
assert len(many) == 1
sql, binds = many[0]
assert "rpa_cart_settlement_goods_data" in sql.lower()
assert [item["operate_type"] for item in binds] == [2, 2]
assert [item["oms_id"] for item in binds] == [0, 0]
assert [item["pay_order_no"] for item in binds] == ["ZQD1", "ZQD2"]
assert [item["pay_order_dtl_id"] for item in binds] == [
"a1b2c3d4e5f6789012345678901234ab",
"b1b2c3d4e5f6789012345678901234ab",
]
assert [item["create_user_name"] for item in binds] == ["rpa-cart", "rpa-cart"]
# 已有支付单号去重时丢掉空值。
def test_load_existing_pay_order_nos():
# 返回预设支付单号。
class FakeCursor:
# 进入 with,返回自身。
def __enter__(self):
return self
# 退出 with,不吞异常。
def __exit__(self, *args):
return False
# 记下 SQL 和 bind。
def execute(self, sql, params=None):
self.sql = sql
self.params = params
# 返回含重复和空值的单号。
def fetchall(self):
return [("ZQD1",), ("ZQD1",), (None,)]
# 只提供游标。
class FakeConn:
# 新开返回预设单号的游标。
def cursor(self):
return FakeCursor()
# 假连接不计数。
def close(self):
return None
repo = OraclePayRepo(_FakePool(FakeConn()))
keys = repo.load_existing_pay_order_nos()
assert keys == {"ZQD1"}