104 lines
3.5 KiB
Python
104 lines
3.5 KiB
Python
"""Dual sync: loading_report доходит до обоих терминалов."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app import db
|
|
from app.models import LoadingReport
|
|
from app.services.sync_manager import enqueue_sync_queue_task
|
|
from tests.helpers.mill_recipe_fixtures import create_recipe_with_children
|
|
from tests.helpers.sync_dual_harness import SyncDualInstanceHarness
|
|
|
|
SCENARIO_DUAL_REPORTS_SYNC = "DUAL_REPORTS_SYNC"
|
|
|
|
|
|
class SyncDualReportsTests(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.harness = SyncDualInstanceHarness()
|
|
self.harness.start()
|
|
|
|
def tearDown(self) -> None:
|
|
self.harness.stop()
|
|
|
|
def test_loading_report_reaches_both_terminals(self) -> None:
|
|
with self.harness.server_ctx():
|
|
recipe_id, _, _ = create_recipe_with_children(name="Report recipe")
|
|
report_id = "dual-lr-1"
|
|
db.session.add(
|
|
LoadingReport(
|
|
id=report_id,
|
|
recipe_id=recipe_id,
|
|
recipe_name="Report recipe",
|
|
start_time=datetime.now(),
|
|
dispenser_type="dispenser",
|
|
total_weight=100.0,
|
|
)
|
|
)
|
|
db.session.commit()
|
|
enqueue_sync_queue_task(
|
|
"loading_report", report_id, "create", priority=2, target_node_id=None
|
|
)
|
|
db.session.commit()
|
|
|
|
self.harness.drain_sync()
|
|
|
|
for term in ("a", "b"):
|
|
with self.harness.terminal_ctx(term):
|
|
row = db.session.get(LoadingReport, report_id)
|
|
self.assertIsNotNone(row, msg=f"terminal {term}")
|
|
self.assertEqual(float(row.total_weight or 0), 100.0)
|
|
|
|
def test_unloading_report_chain_listed_on_server(self) -> None:
|
|
client = self.harness.server_test_client()
|
|
with self.harness.server_ctx():
|
|
recipe_id, _, _ = create_recipe_with_children(name="Unload R")
|
|
save_loading = client.post(
|
|
"/api/save_report",
|
|
json={
|
|
"recipe_id": recipe_id,
|
|
"total_weight": 50.0,
|
|
"target_mixing_time": 5,
|
|
"actual_mixing_time": 5,
|
|
"components": [
|
|
{"name": "C1", "target_weight": 50, "actual_weight": 50, "overload": 0}
|
|
],
|
|
},
|
|
)
|
|
self.assertEqual(save_loading.status_code, 200)
|
|
report_id = save_loading.get_json()["report_id"]
|
|
|
|
save_unloading = client.post(
|
|
"/api/save_unloading_report",
|
|
json={
|
|
"loading_report_id": report_id,
|
|
"recipe_id": recipe_id,
|
|
"total_weight": 50.0,
|
|
"total_unloaded_weight": 48.0,
|
|
"remaining_weight": 2.0,
|
|
"unloading_groups": [
|
|
{
|
|
"name": "G1",
|
|
"target_weight": 50.0,
|
|
"unloaded_weight": 48.0,
|
|
"remaining_weight": 2.0,
|
|
"distribution_type": "percent",
|
|
"distribution_value": 100,
|
|
"order": 1,
|
|
}
|
|
],
|
|
},
|
|
)
|
|
self.assertEqual(save_unloading.status_code, 200)
|
|
|
|
listed = client.get("/api/unloading_reports")
|
|
self.assertEqual(listed.status_code, 200)
|
|
self.assertGreaterEqual(len(listed.get_json()), 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|