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_file_changes.py · 3.8 KB · 121 lines
| 1 | import pytest |
| 2 | |
| 3 | import object_file_changes |
| 4 | from object_versions import InvalidObjectIdError |
| 5 | |
| 6 | |
| 7 | def test_append_file_change_writes_jsonl_and_lists_newest_first(tmp_path): |
| 8 | data_dir = tmp_path / "data" |
| 9 | |
| 10 | first = object_file_changes.append_file_change( |
| 11 | object_id="site_home", |
| 12 | action="file_create", |
| 13 | file_name="assets/report.txt", |
| 14 | file_size=5, |
| 15 | actor="admin", |
| 16 | message="created report", |
| 17 | correlation_id="first-correlation", |
| 18 | details={"method": "POST"}, |
| 19 | base_dir=data_dir, |
| 20 | ) |
| 21 | second = object_file_changes.append_file_change( |
| 22 | object_id="site_home", |
| 23 | action="file_update", |
| 24 | file_name="assets/report.txt", |
| 25 | file_size=7, |
| 26 | actor="admin", |
| 27 | base_dir=data_dir, |
| 28 | ) |
| 29 | |
| 30 | path = data_dir / "file_changes" / "site_home" / "changes.jsonl" |
| 31 | assert path.exists() |
| 32 | |
| 33 | payload = object_file_changes.list_file_changes("site_home", base_dir=data_dir) |
| 34 | |
| 35 | assert payload["count"] == 2 |
| 36 | assert payload["total"] == 2 |
| 37 | assert payload["changes"][0]["change_id"] == second["change_id"] |
| 38 | assert payload["changes"][1]["change_id"] == first["change_id"] |
| 39 | assert payload["changes"][1]["message"] == "created report" |
| 40 | assert payload["changes"][1]["correlation_id"] == "first-correlation" |
| 41 | assert payload["changes"][1]["details"] == {"method": "POST"} |
| 42 | |
| 43 | |
| 44 | def test_list_file_changes_filters_file_and_paginates(tmp_path): |
| 45 | data_dir = tmp_path / "data" |
| 46 | for index in range(3): |
| 47 | object_file_changes.append_file_change( |
| 48 | object_id="site_home", |
| 49 | action="file_create", |
| 50 | file_name=f"assets/{index}.txt", |
| 51 | file_size=index, |
| 52 | base_dir=data_dir, |
| 53 | ) |
| 54 | |
| 55 | payload = object_file_changes.list_file_changes( |
| 56 | "site_home", |
| 57 | file_name="assets/1.txt", |
| 58 | base_dir=data_dir, |
| 59 | limit=1, |
| 60 | offset=0, |
| 61 | ) |
| 62 | |
| 63 | assert payload["count"] == 1 |
| 64 | assert payload["total"] == 1 |
| 65 | assert payload["file_name"] == "assets/1.txt" |
| 66 | assert payload["changes"][0]["file_name"] == "assets/1.txt" |
| 67 | |
| 68 | |
| 69 | def test_list_file_changes_returns_empty_for_valid_object_without_log(tmp_path): |
| 70 | payload = object_file_changes.list_file_changes("site_home", base_dir=tmp_path / "data") |
| 71 | |
| 72 | assert payload["count"] == 0 |
| 73 | assert payload["total"] == 0 |
| 74 | assert payload["changes"] == [] |
| 75 | |
| 76 | |
| 77 | def test_file_changes_reject_invalid_inputs(tmp_path): |
| 78 | with pytest.raises(InvalidObjectIdError): |
| 79 | object_file_changes.list_file_changes("../bad", base_dir=tmp_path / "data") |
| 80 | |
| 81 | with pytest.raises(object_file_changes.InvalidFileChangeError): |
| 82 | object_file_changes.append_file_change( |
| 83 | object_id="site_home", |
| 84 | action="wrong", |
| 85 | file_name="assets/report.txt", |
| 86 | base_dir=tmp_path / "data", |
| 87 | ) |
| 88 | |
| 89 | with pytest.raises(object_file_changes.InvalidFileChangeError): |
| 90 | object_file_changes.append_file_change( |
| 91 | object_id="site_home", |
| 92 | action="file_create", |
| 93 | file_name="../secret.txt", |
| 94 | base_dir=tmp_path / "data", |
| 95 | ) |
| 96 | |
| 97 | with pytest.raises(ValueError): |
| 98 | object_file_changes.list_file_changes( |
| 99 | "site_home", |
| 100 | base_dir=tmp_path / "data", |
| 101 | limit=object_file_changes.MAX_CHANGE_LIMIT + 1, |
| 102 | ) |
| 103 | |
| 104 | |
| 105 | def test_file_changes_ignore_corrupt_lines(tmp_path): |
| 106 | data_dir = tmp_path / "data" |
| 107 | path = data_dir / "file_changes" / "site_home" / "changes.jsonl" |
| 108 | path.parent.mkdir(parents=True) |
| 109 | path.write_text("{bad json}\n") |
| 110 | |
| 111 | object_file_changes.append_file_change( |
| 112 | object_id="site_home", |
| 113 | action="file_create", |
| 114 | file_name="assets/report.txt", |
| 115 | base_dir=data_dir, |
| 116 | ) |
| 117 | |
| 118 | payload = object_file_changes.list_file_changes("site_home", base_dir=data_dir) |
| 119 | |
| 120 | assert payload["count"] == 1 |
| 121 | assert payload["changes"][0]["file_name"] == "assets/report.txt" |