def test_registry_resolve_unknown(): """ Try to access unregistered file from the registry """ pro = MagicMock() reg = Registry(pro) with pytest.raises(PrologueError) as excinfo: reg.resolve("bad_file.txt") assert "No registry entry found for 'bad_file.txt'" in str(excinfo.value)
def test_registry_bad_insert(): """ Attempt to insert a bad object type into the Registry """ pro = MagicMock() reg = Registry(pro) for item in ["a string", 123, True, {"hi": "bye"}, [1, 2, 3]]: with pytest.raises(PrologueError) as excinfo: reg.insert_entry(item) assert f"Entry is not a recognised type {type(item).__name__}" in str( excinfo.value)
def test_registry_recurse(tmp_path): """ Test adding a folder recursively """ pro = MagicMock() reg = Registry(pro) # Create nested folders folder_a = tmp_path / "folder_a" folder_b = folder_a / "folder_b" folder_c = folder_b / "folder_c" folder_c.mkdir(parents=True) # Create a file at each level file_a = folder_a / "file_a.txt" file_b = folder_b / "file_b.txt" file_c = folder_c / "file_c.txt" with open(file_a, "w") as fh: fh.write("dummy file a") with open(file_b, "w") as fh: fh.write("dummy file b") with open(file_c, "w") as fh: fh.write("dummy file c") # Add root folder recursively reg.add_folder(folder_a, recursive=True) r_file_a = reg.resolve("folder_a/file_a.txt") r_file_b = reg.resolve("folder_b/file_b.txt") r_file_c = reg.resolve("folder_c/file_c.txt") assert isinstance(r_file_a, RegistryFile) assert isinstance(r_file_b, RegistryFile) assert isinstance(r_file_c, RegistryFile) assert r_file_a.path == file_a assert r_file_b.path == file_b assert r_file_c.path == file_c # Check registered entries assert reg.has_entry("folder_a") assert reg.has_entry("folder_b") assert reg.has_entry("folder_c") assert len(reg.list_entries()) == 3
def test_registry_flat(tmp_path): """ Test a 'flat' registry sticks all files in the root """ pro = MagicMock() reg = Registry(pro, flat=True) folder_a = tmp_path / "folder_a" folder_b = folder_a / "folder_b" file_a = folder_a / "file_a.txt" file_b = folder_b / "file_b.txt" folder_b.mkdir(parents=True) with open(file_a, "w") as fh: fh.write("dummy content A") with open(file_b, "w") as fh: fh.write("dummy content B") reg.add_folder(folder_a) r_file_a = reg.resolve("file_a.txt") r_file_b = reg.resolve("file_b.txt") assert isinstance(r_file_a, RegistryFile) assert isinstance(r_file_b, RegistryFile) assert r_file_a.path == file_a assert r_file_b.path == file_b assert reg.has_entry("file_a.txt") assert reg.has_entry("file_b.txt") assert len(reg.list_entries()) == 2 assert r_file_a in reg.list_entries() assert r_file_b in reg.list_entries()
def test_registry_selective(tmp_path): """ Recurse selectively through folder """ pro = MagicMock() reg = Registry(pro) # Create nested folders folder_a = tmp_path / "folder_a" folder_b = folder_a / "folder_b" folder_c = folder_b / "folder_c" folder_c.mkdir(parents=True) # Create a file at each level file_a = folder_a / "file_a.txt" file_b = folder_b / "file_b.yml" file_c = folder_c / "file_c.txt" with open(file_a, "w") as fh: fh.write("dummy file a") with open(file_b, "w") as fh: fh.write("dummy file b") with open(file_c, "w") as fh: fh.write("dummy file c") # Perform a selective add and check reg.add_folder(folder_a, search_for=".txt", recursive=True) r_file_a = reg.resolve("file_a.txt") r_file_c = reg.resolve("file_c.txt") assert isinstance(r_file_a, RegistryFile) assert isinstance(r_file_c, RegistryFile) assert r_file_a.path == file_a assert r_file_c.path == file_c # Check that file B was not imported with pytest.raises(PrologueError) as excinfo: reg.resolve("file_b.yml") assert f"No registry entry found for 'file_b.yml'" in str(excinfo.value)
def test_registry_add_file(tmp_path): """ Add a file to the registry and check it can be resolved """ pro = MagicMock() reg = Registry(pro) path = tmp_path / "file_a.txt" with open(path, "w") as fh: fh.write("dummy content") reg.add_file(path) r_file = reg.resolve("file_a.txt") assert isinstance(r_file, RegistryFile) assert r_file.path == path assert reg.has_entry("file_a.txt") assert len(reg.list_entries()) == 1 assert r_file in reg.list_entries()
def test_registry_resolve_bad_file(tmp_path): """ Try to access a file matching a folder's name """ pro = MagicMock() reg = Registry(pro) the_path = tmp_path / "some_name" the_path.mkdir() reg.add_folder(the_path) with pytest.raises(PrologueError) as excinfo: reg.resolve("some_name") assert "Failed to resolve some_name to a file" in str(excinfo.value)
def test_registry_resolve_bad_folder(tmp_path): """ Try to access a folder matching a file's name """ pro = MagicMock() reg = Registry(pro) the_path = tmp_path / "some_name" with open(the_path, "w") as fh: fh.write("dummy content") reg.add_file(the_path) with pytest.raises(PrologueError) as excinfo: reg.resolve("some_name/some_file.txt") assert "Only a file is registered for path some_name" in str(excinfo.value)
def test_registry_add_folder(tmp_path): """ Add a folder to the registry and check files can be resolved """ pro = MagicMock() reg = Registry(pro) folder = tmp_path / "folder" path = folder / "test_a.txt" folder.mkdir() with open(path, "w") as fh: fh.write("dummy content") reg.add_folder(folder) r_file = reg.resolve("folder/test_a.txt") assert isinstance(r_file, RegistryFile) assert r_file.path == path assert reg.has_entry("folder") assert len(reg.list_entries()) == 1
def test_registry_add_bad_file(tmp_path): """ Add a bad file path into the registry """ pro = MagicMock() reg = Registry(pro) # Try a file that doesn't exist with pytest.raises(PrologueError) as excinfo: bad_path = tmp_path / "bad_file.txt" reg.add_file(bad_path) assert f"File does not exist at path {bad_path}" in str(excinfo.value) # Try adding a folder with pytest.raises(PrologueError) as excinfo: bad_path = tmp_path / "bad_folder" bad_path.mkdir() reg.add_file(bad_path) assert f"Path provided is not a file {bad_path}" in str(excinfo.value)
def test_registry_add_bad_folder(tmp_path): """ Add a bad folder into the registry """ pro = MagicMock() reg = Registry(pro) # Try a folder that doesn't exist with pytest.raises(PrologueError) as excinfo: bad_path = tmp_path / "bad_folder" reg.add_folder(bad_path) assert f"Folder does not exist at path {bad_path}" in str(excinfo.value) # Try adding a file with pytest.raises(PrologueError) as excinfo: bad_path = tmp_path / "bad_file.txt" with open(bad_path, "w") as fh: fh.write("dummy content") reg.add_folder(bad_path) assert f"Path provided is not a folder {bad_path}" in str(excinfo.value)
def test_registry_clash(tmp_path, ignore_duplicate): """ Try to register the same file or folder twice """ pro = MagicMock() reg = Registry(pro) # Insert baseline file and folder file_a = tmp_path / "file_a.txt" folder_a = tmp_path / "folder_a" with open(file_a, "w") as fh: fh.write("dummy content") folder_a.mkdir() reg.add_file(file_a) reg.add_folder(folder_a) # Attempt to insert file a second time if ignore_duplicate: reg.add_file(file_a, ignore_duplicate=ignore_duplicate) else: with pytest.raises(PrologueError) as excinfo: reg.add_file(file_a) assert f"Entry already exists in registry with name {file_a.parts[-1]}" in str( excinfo.value) # Attempt to insert folder a second time if ignore_duplicate: reg.add_folder(folder_a, ignore_duplicate=ignore_duplicate) else: with pytest.raises(PrologueError) as excinfo: reg.add_folder(folder_a) assert f"Entry already exists in registry with name {folder_a.parts[-1]}" in str( excinfo.value)