askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_backup_index_torn_tail.py · 3.0 KB · 77 lines
| 1 | """The restore path's torn-tail check, which had drifted and had no test. |
| 2 | |
| 3 | `object_backup_index._drop_torn_tail` carried a docstring saying it |
| 4 | "mirrors object_records._drop_torn_tail". That mirror was upgraded to be |
| 5 | quote-aware; this copy was not, silently, for months — and nothing caught |
| 6 | it because **it had no test at all**. |
| 7 | |
| 8 | It was worse than the read-side original had been. The old body returned |
| 9 | `text` unchanged whenever it ended with a newline — including a newline |
| 10 | sitting INSIDE an unclosed quote. So a backup archive holding a row torn |
| 11 | mid-quoted-field was folded as if complete, and a restore could resurrect |
| 12 | a garbled row that a live read of the identical bytes handles correctly. |
| 13 | |
| 14 | The fix is not a second correct copy. It CALLS |
| 15 | `object_records.committed_prefix_len`, which is exported for this reason: |
| 16 | two implementations of one invariant drift, one cannot. |
| 17 | """ |
| 18 | |
| 19 | import object_backup_index |
| 20 | import object_records |
| 21 | |
| 22 | |
| 23 | def drop(text): |
| 24 | return object_backup_index._drop_torn_tail(text) |
| 25 | |
| 26 | |
| 27 | def test_the_two_modules_now_share_one_implementation(): |
| 28 | """The actual fix. A private copy is what failed here, so the property |
| 29 | worth pinning is that there is no longer a copy to drift.""" |
| 30 | assert object_records.committed_prefix_len is object_records._committed_prefix_len |
| 31 | source = ( |
| 32 | __import__("pathlib").Path(object_backup_index.__file__).read_text() |
| 33 | ) |
| 34 | assert "object_records.committed_prefix_len(text)" in source |
| 35 | assert 'text.rfind("\\n")' not in source |
| 36 | |
| 37 | |
| 38 | def test_a_newline_inside_a_quote_is_not_a_row_boundary(): |
| 39 | """THE regression. The old body returned this untouched because the |
| 40 | file 'ends with a newline' — that newline is inside an unclosed quote, |
| 41 | so the row is torn and must go.""" |
| 42 | torn = 'id\tnote\n1\t"line one\n' |
| 43 | assert drop(torn) == "id\tnote\n" |
| 44 | |
| 45 | |
| 46 | def test_a_complete_multiline_row_survives_intact(): |
| 47 | """The other half: a quoted newline in a CLOSED field is content, and |
| 48 | dropping it would destroy committed data.""" |
| 49 | whole = 'id\tnote\n1\t"line one\nline two"\n' |
| 50 | assert drop(whole) == whole |
| 51 | |
| 52 | |
| 53 | def test_an_ordinary_torn_tail_still_goes(): |
| 54 | assert drop("a\tb\nc\td\nhalf-writ") == "a\tb\nc\td\n" |
| 55 | |
| 56 | |
| 57 | def test_a_quote_free_file_behaves_exactly_as_before(): |
| 58 | """The cheap common case must be byte-identical to the old rfind, or |
| 59 | this 'fix' is a behaviour change to every backup ever taken.""" |
| 60 | for text in ("", "a\tb\n", "a\tb\nc\td\n", "a\tb\ntorn", "no newline at all"): |
| 61 | old = (text if text == "" or text.endswith("\n") |
| 62 | else (text[: text.rfind("\n") + 1] if "\n" in text else "")) |
| 63 | assert drop(text) == old, repr(text) |
| 64 | |
| 65 | |
| 66 | def test_a_torn_backup_folds_without_the_garbled_row(tmp_path): |
| 67 | """End to end through the parser that actually reads an archive: the |
| 68 | torn row must not appear among the folded records.""" |
| 69 | header = ["_op", "id", "note"] |
| 70 | text = ( |
| 71 | "_op\tid\tnote\n" |
| 72 | "\t1\tfine\n" |
| 73 | '\t2\t"torn mid-field\n' |
| 74 | ) |
| 75 | folded = object_backup_index._parse_append_tsv_by_id(text, header) |
| 76 | assert set(folded) == {"1"} |
| 77 | assert "2" not in folded |