70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
"""Dual sync: устаревший push на master не перезаписывает сервер."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from app import db
|
|
from app.models import Recipe
|
|
from app.services.recipe_update_service import update_recipe_from_payload
|
|
from app.services.sync_manager import SyncManager
|
|
from tests.helpers.mill_recipe_fixtures import (
|
|
create_recipe_with_children,
|
|
recipe_update_payload,
|
|
)
|
|
from tests.helpers.sync_dual_harness import SyncDualInstanceHarness
|
|
|
|
SCENARIO_DUAL_CONCURRENT_RECIPE = "DUAL_CONCURRENT_RECIPE"
|
|
|
|
|
|
class SyncDualConcurrentRecipeTests(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.harness = SyncDualInstanceHarness()
|
|
self.harness.start()
|
|
|
|
def tearDown(self) -> None:
|
|
self.harness.stop()
|
|
|
|
def test_stale_push_does_not_overwrite_server_master(self) -> None:
|
|
with self.harness.server_ctx():
|
|
recipe_id, _, _ = create_recipe_with_children(name="Initial")
|
|
self.harness.drain_sync()
|
|
|
|
with self.harness.server_ctx():
|
|
recipe = db.session.get(Recipe, recipe_id)
|
|
update_recipe_from_payload(
|
|
recipe_id,
|
|
recipe_update_payload(recipe, ingredients=[], groups=[])
|
|
| {"name": "Server authoritative"},
|
|
)
|
|
db.session.commit()
|
|
server_version = int(recipe.version or 1)
|
|
|
|
self.harness.drain_sync()
|
|
|
|
with self.harness.server_ctx():
|
|
result = SyncManager.process_push(
|
|
client_id=self.harness.NODE_A,
|
|
changes=[
|
|
{
|
|
"table_name": "recipe",
|
|
"record_id": recipe_id,
|
|
"action": "update",
|
|
"data": {
|
|
"id": recipe_id,
|
|
"name": "Stale from client",
|
|
"version": max(1, server_version - 1),
|
|
},
|
|
}
|
|
],
|
|
)
|
|
self.assertEqual(result["status_code"], 200)
|
|
|
|
with self.harness.server_ctx():
|
|
row = db.session.get(Recipe, recipe_id)
|
|
self.assertEqual(row.name, "Server authoritative")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|