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_package_changes.py · 5.6 KB · 179 lines
| 1 | import json |
| 2 | from uuid import UUID |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | import object_package_changes |
| 7 | import object_packages |
| 8 | |
| 9 | |
| 10 | def test_append_package_change_writes_jsonl_and_lists_newest_first(tmp_path): |
| 11 | data_dir = tmp_path / "data" |
| 12 | |
| 13 | first = object_package_changes.append_package_change( |
| 14 | package_id="hello-world", |
| 15 | action="dry_run", |
| 16 | package_version="0.1.0", |
| 17 | actor="admin", |
| 18 | details={"safe_to_install": True, "objects": {"create": 1}}, |
| 19 | base_dir=data_dir, |
| 20 | ) |
| 21 | second = object_package_changes.append_package_change( |
| 22 | package_id="hello-world", |
| 23 | action="install_requested", |
| 24 | package_version="0.1.0", |
| 25 | actor="admin", |
| 26 | base_dir=data_dir, |
| 27 | ) |
| 28 | |
| 29 | path = data_dir / "package_changes" / "hello-world" / "changes.jsonl" |
| 30 | rows = [json.loads(line) for line in path.read_text().splitlines()] |
| 31 | |
| 32 | assert rows == [first, second] |
| 33 | assert first["message"] == "Dry run package install" |
| 34 | assert first["details"] == {"safe_to_install": True, "objects": {"create": 1}} |
| 35 | assert UUID(first["change_id"]).version == 4 |
| 36 | assert UUID(second["change_id"]).version == 4 |
| 37 | assert "+" not in first["change_id"] |
| 38 | assert ":" not in first["change_id"] |
| 39 | assert "." not in first["change_id"] |
| 40 | |
| 41 | payload = object_package_changes.list_package_changes("hello-world", base_dir=data_dir) |
| 42 | |
| 43 | assert payload["package_id"] == "hello-world" |
| 44 | assert payload["count"] == 2 |
| 45 | assert payload["total"] == 2 |
| 46 | assert [change["action"] for change in payload["changes"]] == [ |
| 47 | "install_requested", |
| 48 | "dry_run", |
| 49 | ] |
| 50 | |
| 51 | |
| 52 | def test_list_package_changes_paginates(tmp_path): |
| 53 | data_dir = tmp_path / "data" |
| 54 | for action in ("dry_run", "install_requested", "failed"): |
| 55 | object_package_changes.append_package_change( |
| 56 | package_id="hello-world", |
| 57 | action=action, |
| 58 | package_version="0.1.0", |
| 59 | base_dir=data_dir, |
| 60 | ) |
| 61 | |
| 62 | payload = object_package_changes.list_package_changes( |
| 63 | "hello-world", |
| 64 | base_dir=data_dir, |
| 65 | limit=1, |
| 66 | offset=1, |
| 67 | ) |
| 68 | |
| 69 | assert payload["count"] == 1 |
| 70 | assert payload["total"] == 3 |
| 71 | assert payload["has_more"] is True |
| 72 | assert payload["changes"][0]["action"] == "install_requested" |
| 73 | |
| 74 | |
| 75 | def test_get_package_change_returns_one_entry_by_id(tmp_path): |
| 76 | data_dir = tmp_path / "data" |
| 77 | first = object_package_changes.append_package_change( |
| 78 | package_id="hello-world", |
| 79 | action="restore_requested", |
| 80 | package_version="0.1.0", |
| 81 | base_dir=data_dir, |
| 82 | ) |
| 83 | object_package_changes.append_package_change( |
| 84 | package_id="hello-world", |
| 85 | action="rolled_back", |
| 86 | package_version="0.1.0", |
| 87 | base_dir=data_dir, |
| 88 | ) |
| 89 | |
| 90 | assert ( |
| 91 | object_package_changes.get_package_change( |
| 92 | "hello-world", |
| 93 | first["change_id"], |
| 94 | base_dir=data_dir, |
| 95 | ) |
| 96 | == first |
| 97 | ) |
| 98 | assert object_package_changes.get_package_change( |
| 99 | "hello-world", |
| 100 | "missing-change", |
| 101 | base_dir=data_dir, |
| 102 | ) is None |
| 103 | |
| 104 | |
| 105 | def test_package_changes_return_empty_for_valid_package_without_log(tmp_path): |
| 106 | payload = object_package_changes.list_package_changes( |
| 107 | "hello-world", |
| 108 | base_dir=tmp_path / "data", |
| 109 | ) |
| 110 | |
| 111 | assert payload["changes"] == [] |
| 112 | assert payload["count"] == 0 |
| 113 | assert payload["total"] == 0 |
| 114 | |
| 115 | |
| 116 | def test_package_changes_reject_invalid_names_actions_and_details(tmp_path): |
| 117 | with pytest.raises(object_packages.InvalidPackageIdError): |
| 118 | object_package_changes.list_package_changes("../bad", base_dir=tmp_path / "data") |
| 119 | |
| 120 | with pytest.raises(object_package_changes.InvalidPackageChangeError): |
| 121 | object_package_changes.append_package_change( |
| 122 | package_id="hello-world", |
| 123 | action="publish", |
| 124 | base_dir=tmp_path / "data", |
| 125 | ) |
| 126 | |
| 127 | with pytest.raises(object_package_changes.InvalidPackageChangeError): |
| 128 | object_package_changes.append_package_change( |
| 129 | package_id="hello-world", |
| 130 | action="dry_run", |
| 131 | details=[], |
| 132 | base_dir=tmp_path / "data", |
| 133 | ) |
| 134 | |
| 135 | |
| 136 | def test_package_changes_ignore_corrupt_lines(tmp_path): |
| 137 | data_dir = tmp_path / "data" |
| 138 | path = data_dir / "package_changes" / "hello-world" / "changes.jsonl" |
| 139 | path.parent.mkdir(parents=True) |
| 140 | path.write_text( |
| 141 | "not-json\n" |
| 142 | '{"action":"dry_run","package_id":"hello-world"}\n' |
| 143 | "[]\n" |
| 144 | ) |
| 145 | |
| 146 | payload = object_package_changes.list_package_changes("hello-world", base_dir=data_dir) |
| 147 | |
| 148 | assert payload["changes"] == [{"action": "dry_run", "package_id": "hello-world"}] |
| 149 | |
| 150 | |
| 151 | def test_dry_run_change_details_compacts_plan(): |
| 152 | details = object_package_changes.dry_run_change_details( |
| 153 | { |
| 154 | "package": {"id": "hello-world", "name": "Hello World", "version": "0.1.0"}, |
| 155 | "safe_to_install": True, |
| 156 | "install_enabled": False, |
| 157 | "objects": [{"action": "create"}, {"action": "replace"}], |
| 158 | "schemas": [{"action": "create"}], |
| 159 | "permissions": [], |
| 160 | "seed": [{"action": "merge"}], |
| 161 | "migrations": [{"action": "apply"}, {"action": "apply"}], |
| 162 | "warnings": ["review first"], |
| 163 | } |
| 164 | ) |
| 165 | |
| 166 | assert details == { |
| 167 | "package": {"id": "hello-world", "name": "Hello World", "version": "0.1.0"}, |
| 168 | "safe_to_install": True, |
| 169 | "install_enabled": False, |
| 170 | "objects": {"create": 1, "replace": 1}, |
| 171 | "schemas": {"create": 1}, |
| 172 | "permissions": {}, |
| 173 | "seed": {"merge": 1}, |
| 174 | "migrations": {"apply": 2}, |
| 175 | "schedules": {}, |
| 176 | "nav": {}, |
| 177 | "attention": {}, |
| 178 | "warnings": ["review first"], |
| 179 | } |