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_metadata.py · 2.7 KB · 92 lines
| 1 | import pytest |
| 2 | |
| 3 | import object_metadata |
| 4 | import object_source |
| 5 | import object_versions |
| 6 | |
| 7 | |
| 8 | def write_file(path, content): |
| 9 | path.parent.mkdir(parents=True, exist_ok=True) |
| 10 | path.write_text(content) |
| 11 | return path |
| 12 | |
| 13 | |
| 14 | def test_get_object_metadata_summarizes_existing_object(tmp_path): |
| 15 | root = tmp_path / "objects" |
| 16 | data_dir = tmp_path / "data" |
| 17 | source = write_file(root / "basics" / "counter.py", "def GET(request):\n return {}\n") |
| 18 | write_file( |
| 19 | data_dir / "state" / "basics_counter" / "state.tsv", |
| 20 | "key\tvalue\ttimestamp\ncount\t3\t1710000000.1\nname\tcounter\t1710000000.2\n", |
| 21 | ) |
| 22 | write_file( |
| 23 | data_dir / "logs" / "basics_counter" / "log.tsv", |
| 24 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 25 | "a1\t2026-01-01T00:00:00\tINFO\tstarted\n" |
| 26 | "a2\t2026-01-01T00:00:01\tERROR\tboom\n", |
| 27 | ) |
| 28 | manager = object_versions.VersionManager(data_dir) |
| 29 | manager.save_version("basics_counter", "v1\n", author="test", message="first") |
| 30 | manager.save_version("basics_counter", "v2\n", author="test", message="second") |
| 31 | |
| 32 | metadata = object_metadata.get_object_metadata( |
| 33 | "basics_counter", |
| 34 | base_dir=data_dir, |
| 35 | roots=[root], |
| 36 | ) |
| 37 | |
| 38 | assert metadata == { |
| 39 | "object_id": "basics_counter", |
| 40 | "source_path": "basics/counter.py", |
| 41 | "owner": "system", |
| 42 | "kind": "system", |
| 43 | "last_modified": source.stat().st_mtime, |
| 44 | "state_count": 2, |
| 45 | "state_keys": ["count", "name"], |
| 46 | "log_count": 2, |
| 47 | "file_count": 0, |
| 48 | "version_count": 2, |
| 49 | } |
| 50 | |
| 51 | |
| 52 | def test_get_object_metadata_reports_user_owner(tmp_path): |
| 53 | root = tmp_path / "objects" |
| 54 | write_file(root / "users" / "42" / "deals.py", "def GET(request):\n return {}\n") |
| 55 | |
| 56 | metadata = object_metadata.get_object_metadata( |
| 57 | "u_42_deals", |
| 58 | base_dir=tmp_path / "data", |
| 59 | roots=[root], |
| 60 | ) |
| 61 | |
| 62 | assert metadata["source_path"] == "users/42/deals.py" |
| 63 | assert metadata["owner"] == "42" |
| 64 | assert metadata["kind"] == "user" |
| 65 | |
| 66 | |
| 67 | @pytest.mark.parametrize( |
| 68 | "object_id", |
| 69 | [ |
| 70 | "", |
| 71 | "../outside", |
| 72 | "basics/counter", |
| 73 | "basics.counter", |
| 74 | "object id", |
| 75 | "object@station", |
| 76 | "object;drop", |
| 77 | "object\x00id", |
| 78 | "a" * 65, |
| 79 | ], |
| 80 | ) |
| 81 | def test_get_object_metadata_rejects_invalid_object_ids(tmp_path, object_id): |
| 82 | with pytest.raises(object_versions.InvalidObjectIdError): |
| 83 | object_metadata.get_object_metadata(object_id, base_dir=tmp_path / "data") |
| 84 | |
| 85 | |
| 86 | def test_get_object_metadata_raises_for_missing_object(tmp_path): |
| 87 | with pytest.raises(object_source.ObjectSourceNotFoundError): |
| 88 | object_metadata.get_object_metadata( |
| 89 | "missing_object", |
| 90 | base_dir=tmp_path / "data", |
| 91 | roots=[tmp_path / "objects"], |
| 92 | ) |