Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/test_latest_record_change.py
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 19 minutes ago History
test_latest_record_change.py · 3.8 KB · 83 lines
1"""latest_record_change must return exactly what
2list_record_changes(record_id=..., limit=1) returns -- it exists only to skip
3parsing the whole (uncapped) change log on every write -- and the
4_coerce_value shortcut must not change what any state value parses to."""
5import json
6
7import pytest
8
9import object_record_changes
10import object_records
11import object_state
12
13
14def _write_log(tmp_path, lines):
15 path = object_record_changes.record_changes_file("tasks", base_dir=tmp_path)
16 path.parent.mkdir(parents=True, exist_ok=True)
17 path.write_text("\n".join(lines) + "\n", encoding="utf-8")
18
19
20def _entry(change_id, record_id, pad=""):
21 return json.dumps({"change_id": change_id, "record_id": record_id, "action": "update", "pad": pad})
22
23
24def _expected(tmp_path, record_id):
25 found = object_record_changes.list_record_changes(
26 "tasks", record_id=record_id, limit=1, base_dir=tmp_path
27 )["changes"]
28 return found[0] if found else None
29
30
31def test_returns_the_newest_entry_for_the_record(tmp_path):
32 _write_log(tmp_path, [_entry("c1", "t1"), _entry("c2", "t2"), _entry("c3", "t1"), _entry("c4", "t2")])
33 got = object_record_changes.latest_record_change("tasks", "t1", base_dir=tmp_path)
34 assert got["change_id"] == "c3"
35 assert got == _expected(tmp_path, "t1")
36
37
38def test_missing_log_and_missing_record_give_none(tmp_path):
39 assert object_record_changes.latest_record_change("tasks", "t1", base_dir=tmp_path) is None
40 _write_log(tmp_path, [_entry("c1", "t2")])
41 assert object_record_changes.latest_record_change("tasks", "t1", base_dir=tmp_path) is None
42
43
44def test_an_old_record_is_found_by_widening_past_the_first_window(tmp_path):
45 # 500 later lines from other records: the target is far outside the first 64.
46 _write_log(tmp_path, [_entry("first", "old")] + [_entry(f"c{i}", "busy") for i in range(500)])
47 got = object_record_changes.latest_record_change("tasks", "old", base_dir=tmp_path)
48 assert got["change_id"] == "first"
49 assert got == _expected(tmp_path, "old")
50
51
52def test_an_old_record_is_found_in_a_log_larger_than_the_tail_block(tmp_path):
53 pad = "x" * 70_000
54 lines = [_entry("first", "old")] + [_entry(f"c{i}", "busy", pad) for i in range(6)]
55 _write_log(tmp_path, lines)
56 assert object_record_changes.record_changes_file("tasks", base_dir=tmp_path).stat().st_size > object_record_changes._TAIL_BLOCK
57 got = object_record_changes.latest_record_change("tasks", "old", base_dir=tmp_path)
58 assert got["change_id"] == "first"
59 assert got == _expected(tmp_path, "old")
60 assert object_record_changes.latest_record_change("tasks", "busy", base_dir=tmp_path)["change_id"] == "c5"
61
62
63def test_junk_lines_are_skipped_like_the_full_read_skips_them(tmp_path):
64 _write_log(tmp_path, [_entry("c1", "t1"), "{not json", "", "[1, 2]", "42", '{"record_id": "t1"', _entry("c2", "t1")])
65 assert object_record_changes.latest_record_change("tasks", "t1", base_dir=tmp_path)["change_id"] == "c2"
66 _write_log(tmp_path, [_entry("c1", "t1"), "{not json", "[1, 2]"])
67 assert object_record_changes.latest_record_change("tasks", "t1", base_dir=tmp_path)["change_id"] == "c1"
68
69
70def test_invalid_record_id_raises_like_list_record_changes(tmp_path):
71 with pytest.raises(object_records.InvalidRecordIdError):
72 object_record_changes.latest_record_change("tasks", "bad id/..", base_dir=tmp_path)
73
74
75@pytest.mark.parametrize(
76 "value, expected",
77 [("12", 12), ("-3", -3), ("1.5", 1.5), ("1e3", 1000.0), (" 7 ", 7), ("1_0", 10),
78 ("hello", "hello"), ("", ""), ("nan-ish", "nan-ish"),
79 ('{"a": 1}', '{"a": 1}'), ('["x"]', '["x"]'), ('"1"', '"1"'), ("{", "{")],
80)
81def test_coerce_value_unchanged_by_the_json_shortcut(value, expected):
82 assert object_state._coerce_value(value) == expected
83 assert type(object_state._coerce_value(value)) is type(expected)