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_namespace.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_namespace.py · 4.7 KB · 158 lines
1from pathlib import Path
2
3import pytest
4
5import object_namespace
6
7
8def write_object(path: Path):
9 path.parent.mkdir(parents=True, exist_ok=True)
10 path.write_text("def GET(request):\n return {'status': 'ok'}\n")
11 return path
12
13
14def test_get_object_roots_defaults_to_objects(monkeypatch):
15 monkeypatch.delenv("DBBASIC_OBJECTS_DIR", raising=False)
16
17 assert object_namespace.get_object_roots() == [Path("objects")]
18
19
20def test_get_object_roots_honors_env_override(tmp_path, monkeypatch):
21 custom_root = tmp_path / "custom_objects"
22 monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(custom_root))
23
24 assert object_namespace.get_object_roots() == [custom_root]
25
26
27def test_missing_source_root_returns_no_objects(tmp_path):
28 missing_root = tmp_path / "missing"
29
30 assert object_namespace.iter_object_sources([missing_root]) == []
31 assert object_namespace.resolve_object_id("basics_counter", [missing_root]) is None
32
33
34def test_resolve_system_object_from_category_name(tmp_path):
35 root = tmp_path / "objects"
36 expected = write_object(root / "basics" / "counter.py")
37
38 found = object_namespace.resolve_object_id("basics_counter", [root])
39
40 assert found == expected
41
42
43def test_resolve_system_object_with_numbered_filename(tmp_path):
44 root = tmp_path / "objects"
45 expected = write_object(root / "tutorial" / "01_hello.py")
46
47 found = object_namespace.resolve_object_id("tutorial_01_hello", [root])
48
49 assert found == expected
50
51
52def test_resolve_user_object(tmp_path):
53 root = tmp_path / "objects"
54 expected = write_object(root / "users" / "42" / "deals.py")
55
56 found = object_namespace.resolve_object_id("u_42_deals", [root])
57
58 assert found == expected
59
60
61def test_object_id_from_system_path(tmp_path):
62 root = tmp_path / "objects"
63 source = write_object(root / "basics" / "counter.py")
64
65 assert object_namespace.object_id_from_path(source, root) == "basics_counter"
66
67
68def test_object_id_from_user_path(tmp_path):
69 root = tmp_path / "objects"
70 source = write_object(root / "users" / "42" / "deals.py")
71
72 assert object_namespace.object_id_from_path(source, root) == "u_42_deals"
73
74
75def test_object_id_from_path_rejects_escaped_path(tmp_path):
76 root = tmp_path / "objects"
77 outside = write_object(tmp_path / "outside.py")
78
79 with pytest.raises(ValueError):
80 object_namespace.object_id_from_path(outside, root)
81
82
83def test_iter_object_sources_ignores_private_and_cache_files(tmp_path):
84 root = tmp_path / "objects"
85 public = write_object(root / "basics" / "counter.py")
86 write_object(root / "basics" / "__init__.py")
87 write_object(root / "basics" / "_private.py")
88 write_object(root / "basics" / ".hidden.py")
89 write_object(root / "__pycache__" / "cached.py")
90
91 sources = object_namespace.iter_object_sources([root])
92
93 assert sources == [
94 object_namespace.ObjectSource(
95 object_id="basics_counter",
96 path=public,
97 relative_path=Path("basics") / "counter.py",
98 kind="system",
99 )
100 ]
101
102
103def test_iter_object_sources_keeps_user_objects_separate(tmp_path):
104 root = tmp_path / "objects"
105 write_object(root / "basics" / "counter.py")
106 write_object(root / "users" / "42" / "deals.py")
107
108 sources = object_namespace.iter_object_sources([root])
109
110 assert [source.object_id for source in sources] == ["basics_counter", "u_42_deals"]
111 assert [source.kind for source in sources] == ["system", "user"]
112 assert "users_42_deals" not in [source.object_id for source in sources]
113
114
115@pytest.mark.parametrize(
116 "object_id",
117 [
118 "",
119 "../outside",
120 "basics/counter",
121 "basics.counter",
122 "object id",
123 "object@station",
124 "object;drop",
125 "object\x00id",
126 "a" * 65,
127 ],
128)
129def test_invalid_object_ids_are_rejected(object_id):
130 assert not object_namespace.validate_object_id(object_id)
131 assert object_namespace.resolve_object_id(object_id, [Path("objects")]) is None
132
133
134def test_duplicate_ids_resolve_from_first_root(tmp_path):
135 root_one = tmp_path / "one"
136 root_two = tmp_path / "two"
137 first = write_object(root_one / "basics" / "counter.py")
138 write_object(root_two / "basics" / "counter.py")
139
140 found = object_namespace.resolve_object_id("basics_counter", [root_one, root_two])
141
142 assert found == first
143
144
145def test_find_trigger_file_uses_triggers_directory(tmp_path):
146 root = tmp_path / "objects"
147 expected = write_object(root / "triggers" / "queue.py")
148
149 found = object_namespace.find_trigger_file("queue", [root])
150
151 assert found == expected
152
153
154def test_find_trigger_file_rejects_unsafe_name(tmp_path):
155 root = tmp_path / "objects"
156 write_object(root / "triggers" / "queue.py")
157
158 assert object_namespace.find_trigger_file("../queue", [root]) is None