feat: implement automatic margin pricing and margin reverse engineering in sync

This commit is contained in:
SmartUp Developer
2026-07-02 21:22:37 +08:00
parent 9011c68b9d
commit 0e77de31ae
5 changed files with 106 additions and 34 deletions
+46
View File
@@ -387,3 +387,49 @@ def test_create_binding_does_not_notify_when_website_rate_unchanged(monkeypatch,
assert result.target_group_id == "target"
assert db_session.query(NotificationLog).count() == 0
def test_sync_binding_auto_percent_writeback(monkeypatch, db_session):
website, upstream = seed_rows(db_session)
binding = WebsiteGroupBinding(
website_id=website.id,
target_group_id="target",
target_group_name="Target group",
source_groups_json=json.dumps([{
"upstream_id": upstream.id,
"upstream_name": "Upstream",
"group_id": "source",
"group_name": "Source",
}], ensure_ascii=False),
percent="0.00",
algorithm="priority_weighted_plus_percent",
enabled=True,
)
db_session.add(binding)
db_session.commit()
db_session.refresh(binding)
class FakeClient:
def __init__(self, **kwargs):
pass
def __enter__(self):
return self
def __exit__(self, *args):
pass
def get_groups(self, endpoint):
return [{"id": "target", "name": "Target group", "rate_multiplier": "2.0"}]
def update_group_rate(self, endpoint, group_id, rate):
pass
monkeypatch.setattr(websites_router, "Sub2ApiWebsiteClient", FakeClient)
monkeypatch.setattr("app.services.website_sync.Sub2ApiWebsiteClient", FakeClient)
from app.services.website_sync import sync_binding
sync_binding(db_session, binding, write=True)
db_session.refresh(binding)
assert binding.percent == "50.00"
log = db_session.query(WebsiteSyncLog).filter(WebsiteSyncLog.binding_id == binding.id).one()
assert log.percent == "50.00"
assert log.new_rate == "3.00"