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

0 downloads

dbbasic-object-server/tests/test_object_record_changes.py
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.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_object_record_changes.py · 4.0 KB · 126 lines
1import json
2from uuid import UUID
3
4import pytest
5
6import object_collections
7import object_record_changes
8import object_records
9
10
11def test_append_record_change_writes_jsonl_and_lists_newest_first(tmp_path):
12 data_dir = tmp_path / "data"
13
14 first = object_record_changes.append_record_change(
15 collection="contacts",
16 record_id="c1",
17 action="create",
18 before=None,
19 after={"id": "c1", "name": "Ada"},
20 actor="admin",
21 message="created from test",
22 base_dir=data_dir,
23 )
24 second = object_record_changes.append_record_change(
25 collection="contacts",
26 record_id="c1",
27 action="update",
28 before={"id": "c1", "name": "Ada"},
29 after={"id": "c1", "name": "Ada Lovelace"},
30 actor="admin",
31 base_dir=data_dir,
32 )
33
34 path = data_dir / "record_changes" / "contacts" / "changes.jsonl"
35 rows = [json.loads(line) for line in path.read_text().splitlines()]
36
37 assert rows == [first, second]
38 assert UUID(first["change_id"]).version == 4
39 assert UUID(second["change_id"]).version == 4
40 assert first["changed_fields"] == ["id", "name"]
41 assert second["changed_fields"] == ["name"]
42 assert second["message"] == "Updated record"
43
44 payload = object_record_changes.list_record_changes("contacts", base_dir=data_dir)
45
46 assert payload["collection"] == "contacts"
47 assert payload["count"] == 2
48 assert payload["total"] == 2
49 assert [change["action"] for change in payload["changes"]] == ["update", "create"]
50
51
52def test_list_record_changes_filters_record_and_paginates(tmp_path):
53 data_dir = tmp_path / "data"
54 for record_id in ("c1", "c2", "c1"):
55 object_record_changes.append_record_change(
56 collection="contacts",
57 record_id=record_id,
58 action="update",
59 before={"id": record_id, "name": "old"},
60 after={"id": record_id, "name": "new"},
61 base_dir=data_dir,
62 )
63
64 payload = object_record_changes.list_record_changes(
65 "contacts",
66 record_id="c1",
67 base_dir=data_dir,
68 limit=1,
69 offset=1,
70 )
71
72 assert payload["record_id"] == "c1"
73 assert payload["count"] == 1
74 assert payload["total"] == 2
75 assert payload["has_more"] is False
76 assert payload["changes"][0]["record_id"] == "c1"
77
78
79def test_list_record_changes_returns_empty_for_valid_collection_without_log(tmp_path):
80 payload = object_record_changes.list_record_changes("contacts", base_dir=tmp_path / "data")
81
82 assert payload["changes"] == []
83 assert payload["count"] == 0
84 assert payload["total"] == 0
85
86
87def test_record_changes_reject_invalid_names_and_actions(tmp_path):
88 with pytest.raises(object_collections.InvalidCollectionNameError):
89 object_record_changes.list_record_changes("bad.name", base_dir=tmp_path / "data")
90
91 with pytest.raises(object_records.InvalidRecordIdError):
92 object_record_changes.append_record_change(
93 collection="contacts",
94 record_id="bad.name",
95 action="create",
96 before=None,
97 after={"id": "bad.name"},
98 base_dir=tmp_path / "data",
99 )
100
101 with pytest.raises(object_record_changes.InvalidRecordChangeError):
102 object_record_changes.append_record_change(
103 collection="contacts",
104 record_id="c1",
105 action="publish",
106 before=None,
107 after={"id": "c1"},
108 base_dir=tmp_path / "data",
109 )
110
111
112def test_record_changes_ignore_corrupt_lines(tmp_path):
113 data_dir = tmp_path / "data"
114 path = data_dir / "record_changes" / "contacts" / "changes.jsonl"
115 path.parent.mkdir(parents=True)
116 path.write_text(
117 "not-json\n"
118 '{"action":"create","collection":"contacts","record_id":"c1"}\n'
119 "[]\n"
120 )
121
122 payload = object_record_changes.list_record_changes("contacts", base_dir=data_dir)
123
124 assert payload["changes"] == [
125 {"action": "create", "collection": "contacts", "record_id": "c1"}
126 ]