|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path, PurePosixPath |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from mega.data_structures import Node |
| 9 | +from mega.errors import MultipleNodesFoundError |
| 10 | +from mega.filesystem import UserFileSystem |
| 11 | + |
| 12 | +NODE_ID = "0fPFklV3" |
| 13 | +FIND_NODE_ID = "0fPFklV3" |
| 14 | +DELETED_NODE_IDS = "t8HkzBH2", "8EwHVJna" |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(name="fs") |
| 18 | +async def filesystem() -> UserFileSystem: |
| 19 | + return UserFileSystem.from_dump(json.loads(Path("fake_fs.json").read_text())) |
| 20 | + |
| 21 | + |
| 22 | +def test_filesystem_has_root(fs: UserFileSystem) -> None: |
| 23 | + assert isinstance(fs, UserFileSystem) |
| 24 | + assert fs.root |
| 25 | + assert fs.inbox |
| 26 | + assert fs.trash_bin |
| 27 | + |
| 28 | + |
| 29 | +def test_magic_methods(fs: UserFileSystem) -> None: |
| 30 | + assert len(fs) == 20 |
| 31 | + assert len(fs) == fs.file_count + fs.folder_count + 3 |
| 32 | + assert isinstance(next(iter(fs)), Node) |
| 33 | + node = fs[NODE_ID] |
| 34 | + assert isinstance(node, Node) |
| 35 | + assert node.id == NODE_ID |
| 36 | + assert node.id in fs |
| 37 | + assert node not in fs |
| 38 | + |
| 39 | + |
| 40 | +def test_deleted(fs: UserFileSystem) -> None: |
| 41 | + deleted = set(fs.deleted) |
| 42 | + assert deleted == {fs[DELETED_NODE_IDS[0]]} |
| 43 | + all_deleted = set(fs.iterdir(fs.trash_bin.id, recursive=True)) |
| 44 | + assert all_deleted == { |
| 45 | + fs[DELETED_NODE_IDS[0]], |
| 46 | + fs[DELETED_NODE_IDS[1]], |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | +def test_path_resolve(fs: UserFileSystem) -> None: |
| 51 | + path = "tests/scripts/script.js" |
| 52 | + rel_path = PurePosixPath(path) |
| 53 | + node = fs.find(path) |
| 54 | + assert node is fs.find(rel_path) |
| 55 | + assert fs.relative_path(node.id) == rel_path |
| 56 | + assert fs.resolve(node.id) == PurePosixPath("/") / rel_path |
| 57 | + |
| 58 | + |
| 59 | +def test_search(fs: UserFileSystem) -> None: |
| 60 | + query = "tests/script" |
| 61 | + results = dict(fs.search(query)) |
| 62 | + expected = ( |
| 63 | + "/tests/script.js", |
| 64 | + "/tests/scripts", |
| 65 | + "/tests/scripts/notes.txt", |
| 66 | + "/tests/scripts/script.js", |
| 67 | + "/tests/scripts/styles.css", |
| 68 | + ) |
| 69 | + |
| 70 | + assert sorted(results.values()) == sorted(PurePosixPath(v) for v in expected) |
| 71 | + |
| 72 | + |
| 73 | +def test_find(fs: UserFileSystem) -> None: |
| 74 | + node = fs.find("/tests/scripts/notes.txt") |
| 75 | + assert node is fs[FIND_NODE_ID] |
| 76 | + |
| 77 | + with pytest.raises(FileNotFoundError): |
| 78 | + fs.find("/this/path/does/not/exists") |
| 79 | + |
| 80 | + with pytest.raises(FileNotFoundError): |
| 81 | + fs.find("/tests/script") |
| 82 | + |
| 83 | + with pytest.raises(MultipleNodesFoundError): |
| 84 | + fs.find("/tests/logo.png") |
| 85 | + |
| 86 | + |
| 87 | +def test_iter_dir(fs: UserFileSystem) -> None: |
| 88 | + children = ( |
| 89 | + "/tests/logo.png", |
| 90 | + "/tests/logo.png", |
| 91 | + "/tests/script.js", |
| 92 | + "/tests/scripts", |
| 93 | + "/tests/setup.sh", |
| 94 | + "/tests/utils.py", |
| 95 | + ) |
| 96 | + |
| 97 | + recursive_children = ( |
| 98 | + *children, |
| 99 | + "/tests/scripts/notes.txt", |
| 100 | + "/tests/scripts/script.js", |
| 101 | + "/tests/scripts/styles.css", |
| 102 | + ) |
| 103 | + |
| 104 | + node = fs.find("/tests") |
| 105 | + |
| 106 | + def get_path(recursive: bool) -> list[str]: |
| 107 | + return sorted(str(fs.resolve(n.id)) for n in fs.iterdir(node.id, recursive=recursive)) |
| 108 | + |
| 109 | + assert get_path(recursive=False) == sorted(children) |
| 110 | + assert get_path(recursive=True) == sorted(recursive_children) |
0 commit comments