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_object_source_changes.py · 4.4 KB · 136 lines
| 1 | from __future__ import annotations |
| 2 | |
| 3 | import json |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | import object_source_changes |
| 8 | from object_versions import InvalidObjectIdError |
| 9 | |
| 10 | |
| 11 | def test_append_and_list_source_changes_newest_first(tmp_path): |
| 12 | first = object_source_changes.append_source_change( |
| 13 | object_id="basics_counter", |
| 14 | action="source_update", |
| 15 | version_id=1, |
| 16 | actor="tester", |
| 17 | message="first edit", |
| 18 | correlation_id="123e4567-e89b-42d3-a456-426614174000", |
| 19 | base_dir=tmp_path, |
| 20 | ) |
| 21 | second = object_source_changes.append_source_change( |
| 22 | object_id="basics_counter", |
| 23 | action="source_rollback", |
| 24 | version_id=2, |
| 25 | from_version_id=1, |
| 26 | actor="tester", |
| 27 | message="rollback", |
| 28 | details={"reason": "bad output", "nested": {"ok": True}}, |
| 29 | base_dir=tmp_path, |
| 30 | ) |
| 31 | |
| 32 | payload = object_source_changes.list_source_changes( |
| 33 | "basics_counter", |
| 34 | base_dir=tmp_path, |
| 35 | limit=10, |
| 36 | ) |
| 37 | |
| 38 | assert payload["object_id"] == "basics_counter" |
| 39 | assert payload["count"] == 2 |
| 40 | assert payload["total"] == 2 |
| 41 | assert payload["has_more"] is False |
| 42 | assert [entry["change_id"] for entry in payload["changes"]] == [ |
| 43 | second["change_id"], |
| 44 | first["change_id"], |
| 45 | ] |
| 46 | assert payload["changes"][0]["action"] == "source_rollback" |
| 47 | assert payload["changes"][0]["from_version_id"] == 1 |
| 48 | assert payload["changes"][0]["details"] == {"reason": "bad output", "nested": {"ok": True}} |
| 49 | assert payload["changes"][1]["correlation_id"] == "123e4567-e89b-42d3-a456-426614174000" |
| 50 | |
| 51 | changes_path = tmp_path / "source_changes" / "basics_counter" / "changes.jsonl" |
| 52 | lines = changes_path.read_text(encoding="utf-8").splitlines() |
| 53 | assert len(lines) == 2 |
| 54 | assert json.loads(lines[0])["action"] == "source_update" |
| 55 | |
| 56 | |
| 57 | def test_source_changes_support_limit_and_offset(tmp_path): |
| 58 | for version_id in range(1, 5): |
| 59 | object_source_changes.append_source_change( |
| 60 | object_id="basics_counter", |
| 61 | action="source_update", |
| 62 | version_id=version_id, |
| 63 | message=f"edit {version_id}", |
| 64 | base_dir=tmp_path, |
| 65 | ) |
| 66 | |
| 67 | payload = object_source_changes.list_source_changes( |
| 68 | "basics_counter", |
| 69 | base_dir=tmp_path, |
| 70 | limit=2, |
| 71 | offset=1, |
| 72 | ) |
| 73 | |
| 74 | assert payload["count"] == 2 |
| 75 | assert payload["total"] == 4 |
| 76 | assert payload["has_more"] is True |
| 77 | assert [entry["version_id"] for entry in payload["changes"]] == [3, 2] |
| 78 | |
| 79 | |
| 80 | def test_source_changes_accept_source_create_action(tmp_path): |
| 81 | entry = object_source_changes.append_source_change( |
| 82 | object_id="site_home", |
| 83 | action="source_create", |
| 84 | version_id=1, |
| 85 | actor="alice", |
| 86 | message="Create home", |
| 87 | details={"description": "Home page"}, |
| 88 | base_dir=tmp_path, |
| 89 | ) |
| 90 | |
| 91 | payload = object_source_changes.list_source_changes("site_home", base_dir=tmp_path) |
| 92 | |
| 93 | assert entry["action"] == "source_create" |
| 94 | assert payload["changes"][0]["action"] == "source_create" |
| 95 | assert payload["changes"][0]["message"] == "Create home" |
| 96 | assert payload["changes"][0]["details"] == {"description": "Home page"} |
| 97 | |
| 98 | |
| 99 | def test_source_changes_reject_invalid_inputs(tmp_path): |
| 100 | with pytest.raises(InvalidObjectIdError): |
| 101 | object_source_changes.append_source_change( |
| 102 | object_id="../bad", |
| 103 | action="source_update", |
| 104 | version_id=1, |
| 105 | base_dir=tmp_path, |
| 106 | ) |
| 107 | |
| 108 | with pytest.raises(object_source_changes.InvalidSourceChangeError): |
| 109 | object_source_changes.append_source_change( |
| 110 | object_id="basics_counter", |
| 111 | action="delete_everything", |
| 112 | version_id=1, |
| 113 | base_dir=tmp_path, |
| 114 | ) |
| 115 | |
| 116 | with pytest.raises(object_source_changes.InvalidSourceChangeError): |
| 117 | object_source_changes.append_source_change( |
| 118 | object_id="basics_counter", |
| 119 | action="source_update", |
| 120 | version_id=0, |
| 121 | base_dir=tmp_path, |
| 122 | ) |
| 123 | |
| 124 | |
| 125 | def test_list_source_changes_ignores_corrupt_lines(tmp_path): |
| 126 | changes_path = tmp_path / "source_changes" / "basics_counter" / "changes.jsonl" |
| 127 | changes_path.parent.mkdir(parents=True) |
| 128 | changes_path.write_text( |
| 129 | '{"action":"source_update","version_id":1}\nnot-json\n[]\n', |
| 130 | encoding="utf-8", |
| 131 | ) |
| 132 | |
| 133 | payload = object_source_changes.list_source_changes("basics_counter", base_dir=tmp_path) |
| 134 | |
| 135 | assert payload["count"] == 1 |
| 136 | assert payload["changes"][0]["version_id"] == 1 |