fix: delay binding.percent update until remote sync succeeds and update UI labels to 自动保底盈利
This commit is contained in:
@@ -433,3 +433,49 @@ def test_sync_binding_auto_percent_writeback(monkeypatch, db_session):
|
||||
log = db_session.query(WebsiteSyncLog).filter(WebsiteSyncLog.binding_id == binding.id).one()
|
||||
assert log.percent == "50.00"
|
||||
assert log.new_rate == "3.00"
|
||||
|
||||
|
||||
def test_sync_binding_auto_percent_failure_does_not_save(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="10.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):
|
||||
raise Exception("connection timeout")
|
||||
|
||||
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 == "10.00"
|
||||
|
||||
log = db_session.query(WebsiteSyncLog).filter(WebsiteSyncLog.binding_id == binding.id).one()
|
||||
assert log.status == "failed"
|
||||
assert log.percent == "50.00"
|
||||
|
||||
Reference in New Issue
Block a user