Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_object_package_baselines.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_object_package_baselines.py · 6.6 KB · 187 lines
1import json
2
3import pytest
4
5import object_package_baselines
6import object_packages
7import object_schemas
8import object_source
9
10
11def write_package(root, package_id, payload, files=()):
12 package_dir = root / package_id
13 package_dir.mkdir(parents=True)
14 (package_dir / object_packages.MANIFEST_FILE).write_text(json.dumps(payload))
15 for relative_path, content in files:
16 path = package_dir / relative_path
17 path.parent.mkdir(parents=True, exist_ok=True)
18 path.write_text(content)
19 return package_dir
20
21
22def manifest(package_id="hello-world", **overrides):
23 payload = {
24 "id": package_id,
25 "name": "Hello World",
26 "version": "0.1.0",
27 "objects": [{"id": "hello_world", "path": "objects/hello/world.py"}],
28 "schemas": [{"collection": "contacts", "path": "schemas/contacts.json"}],
29 }
30 payload.update(overrides)
31 return payload
32
33
34def install_hello_world(tmp_path, *, version="0.1.0", allow_replace=False):
35 packages_root = tmp_path / "packages"
36 data_dir = tmp_path / "data"
37 object_root = tmp_path / "objects"
38 object_root.mkdir(exist_ok=True)
39
40 write_package(
41 packages_root,
42 "hello-world",
43 manifest(version=version),
44 files=(
45 ("objects/hello/world.py", f"def GET(request): return {{'version': '{version}'}}\n"),
46 ("schemas/contacts.json", '{"name":"contacts","fields":[{"name":"id","type":"text"}]}\n'),
47 ),
48 )
49
50 result = object_packages.install_package(
51 "hello-world",
52 root=packages_root,
53 base_dir=data_dir,
54 object_roots=[object_root],
55 allow_replace=allow_replace,
56 )
57 return packages_root, data_dir, object_root, result
58
59
60def test_record_and_load_baseline_roundtrip(tmp_path):
61 assert object_package_baselines.load_baseline("hello-world", base_dir=tmp_path) is None
62
63 baseline = object_package_baselines.record_baseline(
64 "hello-world",
65 version="0.1.0",
66 objects={"hello_world": "abc123"},
67 schemas={"contacts": "def456"},
68 base_dir=tmp_path,
69 )
70
71 assert baseline == {
72 "package": "hello-world",
73 "version": "0.1.0",
74 "installed_at": None,
75 "objects": {"hello_world": "abc123"},
76 "schemas": {"contacts": "def456"},
77 "schema_bodies": {},
78 }
79
80 loaded = object_package_baselines.load_baseline("hello-world", base_dir=tmp_path)
81 assert loaded == baseline
82
83 path = object_package_baselines.baseline_path("hello-world", base_dir=tmp_path)
84 assert path == tmp_path / "package_baselines" / "hello-world.json"
85 assert path.is_file()
86
87
88def test_install_package_writes_baseline_with_shipped_hashes(tmp_path):
89 packages_root, data_dir, object_root, result = install_hello_world(tmp_path)
90
91 baseline = object_package_baselines.load_baseline("hello-world", base_dir=data_dir)
92 assert baseline is not None
93 assert baseline["package"] == "hello-world"
94 assert baseline["version"] == "0.1.0"
95
96 live_object = object_source.get_object_source("hello_world", [object_root])
97 assert baseline["objects"]["hello_world"] == object_package_baselines.sha256_text(live_object)
98
99 live_schema = object_schemas.get_schema("contacts", base_dir=data_dir, roots=[object_root])
100 assert baseline["schemas"]["contacts"] == object_package_baselines.canonical_schema_hash(live_schema)
101
102
103def test_package_status_after_fresh_install_is_pristine(tmp_path):
104 packages_root, data_dir, object_root, result = install_hello_world(tmp_path)
105
106 status = object_packages.package_status(
107 "hello-world", root=packages_root, base_dir=data_dir, object_roots=[object_root]
108 )
109
110 assert status["installed"] is True
111 assert status["installed_version"] == "0.1.0"
112 assert status["customized"] is False
113 assert len(status["artifacts"]) == 2
114 assert all(artifact["state"] == "pristine" for artifact in status["artifacts"])
115
116
117def test_package_status_marks_edited_object_customized(tmp_path):
118 packages_root, data_dir, object_root, result = install_hello_world(tmp_path)
119
120 (object_root / "hello" / "world.py").write_text("def GET(request): return {'edited': True}\n")
121
122 status = object_packages.package_status(
123 "hello-world", root=packages_root, base_dir=data_dir, object_roots=[object_root]
124 )
125
126 assert status["customized"] is True
127 object_artifacts = [a for a in status["artifacts"] if a["kind"] == "object"]
128 assert object_artifacts == [
129 {"kind": "object", "id": "hello_world", "state": "customized", "overridden": False}
130 ]
131 schema_artifacts = [a for a in status["artifacts"] if a["kind"] == "schema"]
132 assert schema_artifacts == [{"kind": "schema", "collection": "contacts", "state": "pristine"}]
133
134
135def test_package_status_never_installed_reports_not_installed(tmp_path):
136 packages_root = tmp_path / "packages"
137 data_dir = tmp_path / "data"
138 write_package(
139 packages_root,
140 "hello-world",
141 manifest(),
142 files=(
143 ("objects/hello/world.py", "def GET(request): return {'status': 'ok'}\n"),
144 ("schemas/contacts.json", '{"name":"contacts","fields":[]}\n'),
145 ),
146 )
147
148 status = object_packages.package_status(
149 "hello-world", root=packages_root, base_dir=data_dir, object_roots=[tmp_path / "objects"]
150 )
151
152 assert status["installed"] is False
153 assert status["customized"] is False
154 assert status["artifacts"] == []
155
156
157def test_reinstall_upgrade_restamps_baseline_to_new_version(tmp_path):
158 packages_root, data_dir, object_root, first_install = install_hello_world(tmp_path, version="0.1.0")
159
160 baseline_v1 = object_package_baselines.load_baseline("hello-world", base_dir=data_dir)
161 assert baseline_v1["version"] == "0.1.0"
162
163 package_dir = packages_root / "hello-world"
164 (package_dir / object_packages.MANIFEST_FILE).write_text(json.dumps(manifest(version="0.2.0")))
165 (package_dir / "objects" / "hello" / "world.py").write_text(
166 "def GET(request): return {'version': '0.2.0'}\n"
167 )
168
169 object_packages.install_package(
170 "hello-world",
171 root=packages_root,
172 base_dir=data_dir,
173 object_roots=[object_root],
174 allow_replace=True,
175 )
176
177 baseline_v2 = object_package_baselines.load_baseline("hello-world", base_dir=data_dir)
178 assert baseline_v2["version"] == "0.2.0"
179
180 live_object = object_source.get_object_source("hello_world", [object_root])
181 assert baseline_v2["objects"]["hello_world"] == object_package_baselines.sha256_text(live_object)
182
183 status = object_packages.package_status(
184 "hello-world", root=packages_root, base_dir=data_dir, object_roots=[object_root]
185 )
186 assert status["installed_version"] == "0.2.0"
187 assert status["customized"] is False