test_github_backup_spool_fields.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """The spool collector's field whitelist for Git backup (#2870).
  2. ``_collect_spools`` builds an explicit dict per spool, so anything missing
  3. from it is silently absent from the backup and therefore lost on restore.
  4. The owner's own bookkeeping — purchasing number (#2870), category and
  5. low-stock override (#729), free-text storage — belongs in the file.
  6. """
  7. import pytest
  8. from backend.app.models.spool import Spool
  9. from backend.app.services.github_backup import GitHubBackupService
  10. @pytest.mark.asyncio
  11. async def test_collects_the_owners_own_bookkeeping_fields(db_session):
  12. db_session.add(
  13. Spool(
  14. material="PLA",
  15. material_number="15",
  16. category="Production",
  17. low_stock_threshold_pct=40,
  18. storage_location="Shelf B",
  19. )
  20. )
  21. await db_session.commit()
  22. files: dict = {}
  23. await GitHubBackupService()._collect_spools(db_session, files)
  24. entry = files["spools/inventory.json"]["spools"][0]
  25. assert entry["material_number"] == "15"
  26. assert entry["category"] == "Production"
  27. assert entry["low_stock_threshold_pct"] == 40
  28. assert entry["storage_location"] == "Shelf B"
  29. @pytest.mark.asyncio
  30. async def test_location_id_is_left_out(db_session):
  31. """The locations table is not backed up, so the ID has nothing to mean."""
  32. db_session.add(Spool(material="PLA"))
  33. await db_session.commit()
  34. files: dict = {}
  35. await GitHubBackupService()._collect_spools(db_session, files)
  36. assert "location_id" not in files["spools/inventory.json"]["spools"][0]
  37. @pytest.mark.asyncio
  38. async def test_collects_the_drying_record(db_session):
  39. """#2863 — written with str(), the form _parse_dt reads back."""
  40. from datetime import datetime
  41. db_session.add(
  42. Spool(material="PLA", last_dried_at=datetime(2026, 10, 6, 12, 30), last_dried_temp=55, last_dried_hours=7.5)
  43. )
  44. await db_session.commit()
  45. files: dict = {}
  46. await GitHubBackupService()._collect_spools(db_session, files)
  47. entry = files["spools/inventory.json"]["spools"][0]
  48. assert entry["last_dried_at"] == "2026-10-06 12:30:00"
  49. assert entry["last_dried_temp"] == 55
  50. assert entry["last_dried_hours"] == 7.5