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.py · 7.4 KB · 238 lines
| 1 | import pytest |
| 2 | |
| 3 | import object_source |
| 4 | import object_versions |
| 5 | |
| 6 | |
| 7 | def write_source(path, content): |
| 8 | path.parent.mkdir(parents=True, exist_ok=True) |
| 9 | path.write_text(content) |
| 10 | return path |
| 11 | |
| 12 | |
| 13 | def test_get_object_source_reads_existing_object(tmp_path): |
| 14 | root = tmp_path / "objects" |
| 15 | write_source(root / "basics" / "counter.py", "def GET(request):\n return {'count': 1}\n") |
| 16 | |
| 17 | source = object_source.get_object_source("basics_counter", roots=[root]) |
| 18 | |
| 19 | assert source == "def GET(request):\n return {'count': 1}\n" |
| 20 | |
| 21 | |
| 22 | def test_create_object_source_writes_file_and_saves_first_version(tmp_path): |
| 23 | root = tmp_path / "objects" |
| 24 | manager = object_versions.VersionManager(tmp_path / "data") |
| 25 | code = "def GET(request):\n return {'created': True}\n" |
| 26 | |
| 27 | version_id = object_source.create_object_source( |
| 28 | "site_home", |
| 29 | code, |
| 30 | author="alice", |
| 31 | message="Create home", |
| 32 | roots=[root], |
| 33 | version_manager=manager, |
| 34 | correlation_id="123e4567-e89b-42d3-a456-426614174001", |
| 35 | ) |
| 36 | |
| 37 | assert version_id == 1 |
| 38 | assert (root / "site" / "home.py").read_text() == code |
| 39 | saved = manager.get_version("site_home", version_id=1) |
| 40 | assert saved is not None |
| 41 | assert saved["content"] == code |
| 42 | assert saved["author"] == "alice" |
| 43 | assert saved["message"] == "Create home" |
| 44 | assert saved["correlation_id"] == "123e4567-e89b-42d3-a456-426614174001" |
| 45 | |
| 46 | |
| 47 | def test_create_object_source_places_user_objects_in_user_tree(tmp_path): |
| 48 | root = tmp_path / "objects" |
| 49 | |
| 50 | object_source.create_object_source( |
| 51 | "u_42_deals", |
| 52 | "def GET(request):\n return {}\n", |
| 53 | author="alice", |
| 54 | message="Create deals", |
| 55 | roots=[root], |
| 56 | version_manager=object_versions.VersionManager(tmp_path / "data"), |
| 57 | ) |
| 58 | |
| 59 | assert (root / "users" / "42" / "deals.py").exists() |
| 60 | |
| 61 | |
| 62 | def test_create_object_source_rejects_existing_object(tmp_path): |
| 63 | root = tmp_path / "objects" |
| 64 | write_source(root / "basics" / "counter.py", "old\n") |
| 65 | |
| 66 | with pytest.raises(object_source.ObjectSourceExistsError): |
| 67 | object_source.create_object_source( |
| 68 | "basics_counter", |
| 69 | "new\n", |
| 70 | author="alice", |
| 71 | message="Create duplicate", |
| 72 | roots=[root], |
| 73 | version_manager=object_versions.VersionManager(tmp_path / "data"), |
| 74 | ) |
| 75 | |
| 76 | assert (root / "basics" / "counter.py").read_text() == "old\n" |
| 77 | |
| 78 | |
| 79 | def test_update_object_source_writes_file_and_saves_version(tmp_path): |
| 80 | root = tmp_path / "objects" |
| 81 | source_path = write_source(root / "users" / "42" / "deals.py", "old code\n") |
| 82 | manager = object_versions.VersionManager(tmp_path / "data") |
| 83 | |
| 84 | version_id = object_source.update_object_source( |
| 85 | "u_42_deals", |
| 86 | "new code\n", |
| 87 | author="alice", |
| 88 | message="Update deals", |
| 89 | roots=[root], |
| 90 | version_manager=manager, |
| 91 | ) |
| 92 | |
| 93 | assert version_id == 1 |
| 94 | assert source_path.read_text() == "new code\n" |
| 95 | saved = manager.get_version("u_42_deals", version_id=1) |
| 96 | assert saved is not None |
| 97 | assert saved["content"] == "new code\n" |
| 98 | assert saved["author"] == "alice" |
| 99 | assert saved["message"] == "Update deals" |
| 100 | |
| 101 | |
| 102 | def test_update_object_source_appends_versions(tmp_path): |
| 103 | root = tmp_path / "objects" |
| 104 | source_path = write_source(root / "basics" / "counter.py", "v0\n") |
| 105 | manager = object_versions.VersionManager(tmp_path / "data") |
| 106 | |
| 107 | v1 = object_source.update_object_source( |
| 108 | "basics_counter", |
| 109 | "v1\n", |
| 110 | author="api", |
| 111 | message="first", |
| 112 | roots=[root], |
| 113 | version_manager=manager, |
| 114 | ) |
| 115 | v2 = object_source.update_object_source( |
| 116 | "basics_counter", |
| 117 | "v2\n", |
| 118 | author="api", |
| 119 | message="second", |
| 120 | roots=[root], |
| 121 | version_manager=manager, |
| 122 | ) |
| 123 | |
| 124 | assert [v1, v2] == [1, 2] |
| 125 | assert source_path.read_text() == "v2\n" |
| 126 | assert [row["version_id"] for row in manager.get_history("basics_counter")] == [2, 1] |
| 127 | assert (tmp_path / "data" / "versions" / "basics_counter" / "metadata.tsv").exists() |
| 128 | assert (tmp_path / "data" / "versions" / "basics_counter" / "v1.txt").read_text() == "v1\n" |
| 129 | assert (tmp_path / "data" / "versions" / "basics_counter" / "v2.txt").read_text() == "v2\n" |
| 130 | |
| 131 | |
| 132 | def test_rollback_object_source_writes_old_content_as_new_latest_version(tmp_path): |
| 133 | root = tmp_path / "objects" |
| 134 | source_path = write_source(root / "basics" / "counter.py", "v0\n") |
| 135 | manager = object_versions.VersionManager(tmp_path / "data") |
| 136 | object_source.update_object_source( |
| 137 | "basics_counter", |
| 138 | "v1\n", |
| 139 | author="api", |
| 140 | message="first", |
| 141 | roots=[root], |
| 142 | version_manager=manager, |
| 143 | ) |
| 144 | object_source.update_object_source( |
| 145 | "basics_counter", |
| 146 | "v2\n", |
| 147 | author="api", |
| 148 | message="second", |
| 149 | roots=[root], |
| 150 | version_manager=manager, |
| 151 | ) |
| 152 | |
| 153 | new_version_id = object_source.rollback_object_source( |
| 154 | "basics_counter", |
| 155 | to_version=1, |
| 156 | author="rollback", |
| 157 | message="Rollback to v1", |
| 158 | roots=[root], |
| 159 | version_manager=manager, |
| 160 | ) |
| 161 | |
| 162 | assert new_version_id == 3 |
| 163 | assert source_path.read_text() == "v1\n" |
| 164 | latest = manager.get_version("basics_counter") |
| 165 | assert latest is not None |
| 166 | assert latest["version_id"] == 3 |
| 167 | assert latest["content"] == "v1\n" |
| 168 | assert latest["author"] == "rollback" |
| 169 | assert [row["version_id"] for row in manager.get_history("basics_counter")] == [3, 2, 1] |
| 170 | |
| 171 | |
| 172 | def test_rollback_missing_version_raises_and_leaves_source_unchanged(tmp_path): |
| 173 | root = tmp_path / "objects" |
| 174 | source_path = write_source(root / "basics" / "counter.py", "current\n") |
| 175 | manager = object_versions.VersionManager(tmp_path / "data") |
| 176 | manager.save_version("basics_counter", "v1\n", "api", "first") |
| 177 | |
| 178 | with pytest.raises(object_versions.VersionNotFoundError): |
| 179 | object_source.rollback_object_source( |
| 180 | "basics_counter", |
| 181 | to_version=99, |
| 182 | author="rollback", |
| 183 | message="bad rollback", |
| 184 | roots=[root], |
| 185 | version_manager=manager, |
| 186 | ) |
| 187 | |
| 188 | assert source_path.read_text() == "current\n" |
| 189 | |
| 190 | |
| 191 | @pytest.mark.parametrize( |
| 192 | "object_id", |
| 193 | [ |
| 194 | "", |
| 195 | "../outside", |
| 196 | "basics/counter", |
| 197 | "basics.counter", |
| 198 | "object id", |
| 199 | "object@station", |
| 200 | "object;drop", |
| 201 | "object\x00id", |
| 202 | "a" * 65, |
| 203 | ], |
| 204 | ) |
| 205 | def test_invalid_object_ids_are_rejected(tmp_path, object_id): |
| 206 | root = tmp_path / "objects" |
| 207 | manager = object_versions.VersionManager(tmp_path / "data") |
| 208 | |
| 209 | with pytest.raises(object_versions.InvalidObjectIdError): |
| 210 | object_source.get_object_source(object_id, roots=[root]) |
| 211 | |
| 212 | with pytest.raises(object_versions.InvalidObjectIdError): |
| 213 | object_source.update_object_source( |
| 214 | object_id, |
| 215 | "code", |
| 216 | author="api", |
| 217 | message="bad", |
| 218 | roots=[root], |
| 219 | version_manager=manager, |
| 220 | ) |
| 221 | |
| 222 | |
| 223 | def test_missing_object_source_raises(tmp_path): |
| 224 | root = tmp_path / "objects" |
| 225 | manager = object_versions.VersionManager(tmp_path / "data") |
| 226 | |
| 227 | with pytest.raises(object_source.ObjectSourceNotFoundError): |
| 228 | object_source.get_object_source("missing_object", roots=[root]) |
| 229 | |
| 230 | with pytest.raises(object_source.ObjectSourceNotFoundError): |
| 231 | object_source.update_object_source( |
| 232 | "missing_object", |
| 233 | "code", |
| 234 | author="api", |
| 235 | message="missing", |
| 236 | roots=[root], |
| 237 | version_manager=manager, |
| 238 | ) |