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.
test_object_files_listing_fast_path.py · 5.8 KB · 161 lines
| 1 | """list_object_files answers the same as the per-file check it used to run. |
| 2 | |
| 3 | The listing asked every file "is your real path inside this object's files |
| 4 | directory" with three realpath() calls, which made /collections, |
| 5 | /admin/status and /admin/files take seconds on a store of 11,000 files. |
| 6 | Plain files now skip the question; these tests hold the answer to the old |
| 7 | code, kept here as the reference, on trees built to disagree. |
| 8 | """ |
| 9 | |
| 10 | import os |
| 11 | from pathlib import Path |
| 12 | |
| 13 | import pytest |
| 14 | |
| 15 | import object_collections |
| 16 | import object_files |
| 17 | |
| 18 | |
| 19 | def reference_listing(object_id, base_dir): |
| 20 | """list_object_files as it was before the fast path, line for line.""" |
| 21 | files_dir = object_files.object_files_dir(object_id, base_dir) |
| 22 | if not files_dir.exists(): |
| 23 | return [] |
| 24 | if not files_dir.is_dir(): |
| 25 | raise OSError(f"Object files path is not a directory: {files_dir}") |
| 26 | |
| 27 | files = [] |
| 28 | for path in sorted(files_dir.rglob("*")): |
| 29 | if not path.is_file(): |
| 30 | continue |
| 31 | relative_name = path.relative_to(files_dir).as_posix() |
| 32 | try: |
| 33 | safe_path = object_files._safe_file_path(files_dir, relative_name) |
| 34 | except object_files.InvalidObjectFilenameError: |
| 35 | continue |
| 36 | if path.resolve() != safe_path: |
| 37 | continue |
| 38 | files.append(object_files._metadata(files_dir, path).to_dict()) |
| 39 | return files |
| 40 | |
| 41 | |
| 42 | def write(path, text="x"): |
| 43 | path.parent.mkdir(parents=True, exist_ok=True) |
| 44 | path.write_text(text) |
| 45 | return path |
| 46 | |
| 47 | |
| 48 | def build_awkward_tree(tmp_path): |
| 49 | """Every case the old check distinguished, under one object.""" |
| 50 | data = tmp_path / "data" |
| 51 | root = data / "files" / "notes_inbox" |
| 52 | outside = write(tmp_path / "outside" / "secret.txt", "outside") |
| 53 | write(root / "top.txt", "1") |
| 54 | write(root / "a" / "one.txt", "22") |
| 55 | write(root / "a" / "b" / "c" / "deep.txt", "333") |
| 56 | write(root / "a" / "odd..name.txt", "dots") |
| 57 | write(root / "dotted..dir" / "inner.txt", "dots") |
| 58 | write(root / ".hidden", "h") |
| 59 | write(root / "empty.txt", "") |
| 60 | (root / "empty_dir").mkdir() |
| 61 | os.symlink(root / "a" / "one.txt", root / "link_inside.txt") |
| 62 | os.symlink(outside, root / "link_outside.txt") |
| 63 | os.symlink(root / "missing.txt", root / "link_dangling.txt") |
| 64 | os.symlink(root / "a", root / "dir_link_inside") |
| 65 | os.symlink(outside.parent, root / "dir_link_outside") |
| 66 | os.symlink(outside.parent, root / "a" / "nested_link_outside") |
| 67 | if hasattr(os, "mkfifo"): |
| 68 | os.mkfifo(root / "a" / "pipe") |
| 69 | return data |
| 70 | |
| 71 | |
| 72 | def test_awkward_tree_lists_exactly_as_before(tmp_path): |
| 73 | data = build_awkward_tree(tmp_path) |
| 74 | |
| 75 | listed = object_files.list_object_files("notes_inbox", base_dir=data) |
| 76 | |
| 77 | assert listed == reference_listing("notes_inbox", data) |
| 78 | names = [item["name"] for item in listed] |
| 79 | assert "top.txt" in names and "a/b/c/deep.txt" in names |
| 80 | assert not any("outside" in name for name in names) |
| 81 | assert "a/odd..name.txt" not in names |
| 82 | |
| 83 | |
| 84 | def test_files_directory_that_is_a_symlink_lists_as_before(tmp_path): |
| 85 | data = tmp_path / "data" |
| 86 | real = tmp_path / "elsewhere" |
| 87 | write(real / "one.txt", "1") |
| 88 | write(real / "sub" / "two.txt", "22") |
| 89 | (data / "files").mkdir(parents=True) |
| 90 | os.symlink(real, data / "files" / "notes_inbox") |
| 91 | |
| 92 | listed = object_files.list_object_files("notes_inbox", base_dir=data) |
| 93 | |
| 94 | assert listed == reference_listing("notes_inbox", data) |
| 95 | assert [item["name"] for item in listed] == ["one.txt", "sub/two.txt"] |
| 96 | |
| 97 | |
| 98 | def test_relative_base_dir_lists_as_before(tmp_path, monkeypatch): |
| 99 | build_awkward_tree(tmp_path) |
| 100 | monkeypatch.chdir(tmp_path) |
| 101 | |
| 102 | assert object_files.list_object_files("notes_inbox", base_dir="data") == reference_listing( |
| 103 | "notes_inbox", "data") |
| 104 | |
| 105 | |
| 106 | def test_missing_and_non_directory_answers_are_unchanged(tmp_path): |
| 107 | data = tmp_path / "data" |
| 108 | assert object_files.list_object_files("notes_inbox", base_dir=data) == [] |
| 109 | write(data / "files" / "notes_inbox", "a file, not a directory") |
| 110 | with pytest.raises(OSError): |
| 111 | object_files.list_object_files("notes_inbox", base_dir=data) |
| 112 | |
| 113 | |
| 114 | def build_many(tmp_path, per_object=400): |
| 115 | data = tmp_path / "data" |
| 116 | objects = tmp_path / "objects" |
| 117 | for object_id in ("notes_inbox", "notes_archive", "photos_album"): |
| 118 | group, name = object_id.split("_", 1) |
| 119 | write(objects / group / f"{name}.py", "def GET(request):\n return {}\n") |
| 120 | for index in range(per_object): |
| 121 | write(data / "files" / object_id / f"d{index % 7}" / f"f{index:04d}.txt", |
| 122 | "x" * (index % 13)) |
| 123 | return data, objects |
| 124 | |
| 125 | |
| 126 | def test_all_files_and_collection_counts_are_identical(tmp_path, monkeypatch): |
| 127 | data, objects = build_many(tmp_path) |
| 128 | os.symlink(tmp_path / "objects", data / "files" / "notes_inbox" / "escape") |
| 129 | |
| 130 | listed = object_files.list_all_object_files(data) |
| 131 | expected = [] |
| 132 | for object_id in ("notes_archive", "notes_inbox", "photos_album"): |
| 133 | expected.extend({"object_id": object_id, **item} |
| 134 | for item in reference_listing(object_id, data)) |
| 135 | expected.sort(key=lambda item: (-float(item["modified"]), item["object_id"], item["name"])) |
| 136 | assert listed == expected |
| 137 | assert len(listed) == 1200 |
| 138 | |
| 139 | collections = { |
| 140 | item["name"]: item |
| 141 | for item in object_collections.list_collections(base_dir=data, roots=[objects]) |
| 142 | } |
| 143 | assert collections["notes"]["file_count"] == 800 |
| 144 | assert collections["photos"]["file_count"] == 400 |
| 145 | |
| 146 | |
| 147 | def test_plain_files_are_not_resolved_one_by_one(tmp_path, monkeypatch): |
| 148 | data, _ = build_many(tmp_path, per_object=50) |
| 149 | calls = [] |
| 150 | real_resolve = Path.resolve |
| 151 | |
| 152 | def counting_resolve(self, *args, **kwargs): |
| 153 | calls.append(self) |
| 154 | return real_resolve(self, *args, **kwargs) |
| 155 | |
| 156 | monkeypatch.setattr(Path, "resolve", counting_resolve) |
| 157 | |
| 158 | listed = object_files.list_object_files("notes_inbox", base_dir=data) |
| 159 | |
| 160 | assert len(listed) == 50 |
| 161 | assert calls == [] |