test_failure_reason_vocabulary_migration.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. """Migration tests for issue #2974 — one vocabulary for ``failure_reason``.
  2. Three writers used to put three spellings of one cause into the column: the
  3. backend wrote English display labels ("Layer shift"), older builds of the
  4. archive editor wrote the *translated* label in whatever locale that user was
  5. running, and two stale-archive paths wrote English prose sentences. The Failure
  6. Analysis widget groups on the raw column, so one real cause occupied several
  7. buckets.
  8. Measured on a live install before this landed: ``print_log_entries`` held 91
  9. rows reading ``"User cancelled"`` beside 1 reading ``"userCancelled"``. In an
  10. English UI those render as the same words twice with different counts, which is
  11. why nobody spotted it; in any other locale one of the two stays English.
  12. """
  13. import pytest
  14. from sqlalchemy import select
  15. from sqlalchemy.ext.asyncio import AsyncSession
  16. from backend.app.core.database import (
  17. _LEGACY_FAILURE_REASON_LABELS,
  18. _migrate_failure_reason_vocabulary,
  19. )
  20. from backend.app.models.archive import PrintArchive
  21. from backend.app.models.print_log import PrintLogEntry
  22. # The columns each model needs beyond ``failure_reason``. Rows stay minimal on
  23. # purpose -- these tests exercise the UPDATE, not the schema.
  24. _REQUIRED = {
  25. "PrintArchive": {"filename": "x.3mf", "file_path": "/tmp/x.3mf", "file_size": 1},
  26. "PrintLogEntry": {},
  27. }
  28. async def _seed(session: AsyncSession, model, values: list[str | None]) -> list[int]:
  29. """Insert one row per value through the ORM and return their ids, in order."""
  30. required = _REQUIRED[model.__name__]
  31. rows = [model(status="failed", failure_reason=v, **required) for v in values]
  32. session.add_all(rows)
  33. await session.commit()
  34. ids = []
  35. for row in rows:
  36. await session.refresh(row)
  37. ids.append(row.id)
  38. return ids
  39. async def _read(session: AsyncSession, model, ids: list[int]) -> list[str | None]:
  40. """Read ``failure_reason`` back for ``ids``, in the order given."""
  41. session.expire_all()
  42. result = await session.execute(select(model.id, model.failure_reason).where(model.id.in_(ids)))
  43. got = {row[0]: row[1] for row in result.fetchall()}
  44. return [got[i] for i in ids]
  45. async def _run(session: AsyncSession) -> None:
  46. """Drive the migration over the session's own connection.
  47. TEST_DATABASE_URL is in-memory SQLite on a shared pool, so opening a second
  48. connection would not see the seeded rows -- and production calls this with
  49. an ``AsyncConnection`` inside an open transaction anyway, which is exactly
  50. what ``session.connection()`` hands over.
  51. """
  52. await _migrate_failure_reason_vocabulary(await session.connection())
  53. await session.commit()
  54. # ---------------------------------------------------------------------------
  55. # The map itself
  56. # ---------------------------------------------------------------------------
  57. def test_every_mapped_value_is_a_canonical_key() -> None:
  58. """A label may only ever fold onto a key the rest of the stack accepts."""
  59. from backend.app.api.routes.print_log import _FAILURE_REASON_KEYS
  60. offenders = sorted(set(_LEGACY_FAILURE_REASON_LABELS.values()) - _FAILURE_REASON_KEYS)
  61. assert not offenders, f"map targets values nothing else recognises: {offenders}"
  62. def test_the_map_is_unambiguous() -> None:
  63. """No label may resolve to two different keys.
  64. This is what makes the conversion exact rather than a guess, and it is the
  65. property that let the migration be written at all -- the reporter's open
  66. question was what to do with a value matching no key.
  67. """
  68. assert len(_LEGACY_FAILURE_REASON_LABELS) == len(set(_LEGACY_FAILURE_REASON_LABELS))
  69. def test_the_map_covers_every_writer_that_ever_existed() -> None:
  70. """The three historical vocabularies, by example."""
  71. m = _LEGACY_FAILURE_REASON_LABELS
  72. # 1. Backend English display labels.
  73. assert m["Layer shift"] == "layerShift"
  74. assert m["Filament runout"] == "filamentRunout"
  75. assert m["Clogged nozzle"] == "cloggedNozzle"
  76. assert m["User cancelled"] == "userCancelled"
  77. # 2. Legacy archive-editor writes of a *translated* label. Not English --
  78. # that is the whole reason a locale-dependent reverse lookup could not
  79. # fix this on read.
  80. assert m["Schichtversatz"] == "layerShift"
  81. assert m["Сдвиг слоёв"] == "layerShift"
  82. # 3. The two stale-path prose sentences.
  83. assert m["Stale - print likely cancelled or failed without status update"] == "noStatusUpdate"
  84. assert m["Stale - reconciled after reconnect, end time unknown"] == "noStatusUpdate"
  85. # ---------------------------------------------------------------------------
  86. # The migration
  87. # ---------------------------------------------------------------------------
  88. @pytest.mark.parametrize("model", [PrintArchive, PrintLogEntry])
  89. async def test_labels_fold_onto_keys(db_session: AsyncSession, model) -> None:
  90. ids = await _seed(db_session, model, ["Layer shift", "Schichtversatz", "layerShift"])
  91. await _run(db_session)
  92. assert await _read(db_session, model, ids) == ["layerShift", "layerShift", "layerShift"]
  93. @pytest.mark.parametrize("model", [PrintArchive, PrintLogEntry])
  94. async def test_the_live_split_collapses(db_session: AsyncSession, model) -> None:
  95. """The exact shape measured on the maintainer's instance."""
  96. ids = await _seed(db_session, model, ["User cancelled"] * 3 + ["userCancelled"])
  97. await _run(db_session)
  98. assert set(await _read(db_session, model, ids)) == {"userCancelled"}
  99. @pytest.mark.parametrize("model", [PrintArchive, PrintLogEntry])
  100. async def test_both_stale_sentences_become_one_key(db_session: AsyncSession, model) -> None:
  101. ids = await _seed(
  102. db_session,
  103. model,
  104. [
  105. "Stale - print likely cancelled or failed without status update",
  106. "Stale - reconciled after reconnect, end time unknown",
  107. ],
  108. )
  109. await _run(db_session)
  110. assert await _read(db_session, model, ids) == ["noStatusUpdate", "noStatusUpdate"]
  111. @pytest.mark.parametrize("model", [PrintArchive, PrintLogEntry])
  112. async def test_unrecognised_values_are_left_alone(db_session: AsyncSession, model) -> None:
  113. """Free text and NULL survive untouched.
  114. Guessing at a value the map does not know would be worse than leaving one
  115. honest string in its own bucket -- it still renders through the
  116. ``defaultValue`` fallback in the editor and the Statistics breakdown.
  117. """
  118. ids = await _seed(db_session, model, ["Custom legacy reason", None, ""])
  119. await _run(db_session)
  120. assert await _read(db_session, model, ids) == ["Custom legacy reason", None, ""]
  121. @pytest.mark.parametrize("model", [PrintArchive, PrintLogEntry])
  122. async def test_running_twice_changes_nothing(db_session: AsyncSession, model) -> None:
  123. """Self-terminating, which is why it carries no one-shot settings flag.
  124. A user restoring an older database, or upgrading through this version
  125. twice, must still get their legacy rows converted -- a flag would skip them
  126. forever.
  127. """
  128. ids = await _seed(db_session, model, ["Layer shift", "Custom legacy reason"])
  129. await _run(db_session)
  130. first = await _read(db_session, model, ids)
  131. await _run(db_session)
  132. assert await _read(db_session, model, ids) == first == ["layerShift", "Custom legacy reason"]