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_namespace.py · 4.7 KB · 158 lines
| 1 | from pathlib import Path |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | import object_namespace |
| 6 | |
| 7 | |
| 8 | def 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 | |
| 14 | def 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 | |
| 20 | def 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 | |
| 27 | def 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 | |
| 34 | def 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 | |
| 43 | def 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 | |
| 52 | def 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 | |
| 61 | def 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 | |
| 68 | def 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 | |
| 75 | def 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 | |
| 83 | def 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 | |
| 103 | def 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 | ) |
| 129 | def 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 | |
| 134 | def 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 | |
| 145 | def 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 | |
| 154 | def 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 |