def test_locate_success(self, test_fs: tempfile.TemporaryDirectory, path_list: List[str], expected_result: str): root: Path = Path(test_fs.name) dir: FileRepoDir = FileRepoDir(root) obj_path: RepoObjectPath = RepoObjectPath.from_object( dir.get(path_list), self.REPO_FORMAT) assert str(obj_path) == expected_result
def test_lastmodified(self, dir_prepped: tempfile.TemporaryDirectory) -> None: dir: FileRepoDir = FileRepoDir(Path(dir_prepped.name)) (timestamp, path) = dir.lastmodified() now = datetime.today() assert path.name == str(max(y for y in YEAR_LIST)) assert now > timestamp and timestamp > now - timedelta(seconds = 10)
def test_inp(self, dir_empty: tempfile.TemporaryDirectory, fake: Faker, content_size: int, chunk_size: int) -> None: name: str = fake.file_name(extension='xml') content: str = ''.join( fake.random_elements(elements=('a', 'b', 'c', 'd'), length=content_size, unique=False)) dir: FileRepoDir = FileRepoDir(Path(dir_empty.name)) obj: FileRepoObject = FileRepoObject(dir, name) with obj.path.open(mode="w", buffering=2048) as f: f.write(content) count: int = 0 result: str = "" for chunk in obj.inp(chunk_size): assert content[count * chunk_size:(count + 1) * chunk_size] == chunk result += chunk count += 1 assert count == content_size // chunk_size + ( 1 if content_size % chunk_size > 0 else 0) assert content == result
def test_new_dir_success(self, dir_empty: tempfile.TemporaryDirectory, fake: Faker) -> None: name: str = fake.file_name(extension="") dir: FileRepoDir = FileRepoDir(Path(dir_empty.name)) subdir = dir.new_dir(name) assert len(dir) == 1 assert name in dir assert subdir.exists()
def test_init_dir_prepped(self, dir_prepped: tempfile.TemporaryDirectory) -> None: dir: FileRepoDir = FileRepoDir(Path(dir_prepped.name)) subdirs: Dict[str,int] = {} for (name, _) in dir: subdirs[name] = 1 for i in YEAR_LIST: assert str(i) in subdirs
def test_inp_no_file(self, dir_empty: tempfile.TemporaryDirectory, fake: Faker) -> None: dir: FileRepoDir = FileRepoDir(Path(dir_empty.name)) obj: FileRepoObject = FileRepoObject(dir, fake.file_name(extension='xml')) with pytest.raises(FileNotFoundError): for _ in obj.inp(512): assert False
def test_new_object_success(self, dir_empty: tempfile.TemporaryDirectory, fake: Faker) -> None: name: str = fake.file_name() dir: FileRepoDir = FileRepoDir(Path(dir_empty.name)) obj: FileRepoObject = dir.new_object(name) assert obj.path.name == name assert len(dir) == 1 for (_, o) in dir: assert o == obj
def test_init(self, dir_prepped: tempfile.TemporaryDirectory, fake: Faker) -> None: name: str = fake.file_name() dir: FileRepoDir = FileRepoDir(Path(dir_prepped.name)) obj: FileRepoObject = FileRepoObject(dir, name) assert obj.path.name == name assert obj.parent == dir assert not obj.exists() assert name in dir assert len(dir) == len(YEAR_LIST) + 1
def test_visit_one_object(self, test_fs: tempfile.TemporaryDirectory) -> None: dir: FileRepoDir = FileRepoDir(Path(test_fs.name)) mock: MagicMock = MagicMock() mock.visit.return_value = False dir.visit(mock) c = mock.mock_calls[0] assert c[0] == 'visit' assert isinstance(c[1][0], FileRepoObject) assert c[1][0].subpath(4) == ['Q', str(max(YEAR_LIST)), 'QTR4', 'file-2.txt'] assert len(mock.mock_calls) == 1
def test_refresh(self, dir_prepped: tempfile.TemporaryDirectory, fake: Faker) -> None: dir: FileRepoDir = FileRepoDir(Path(dir_prepped.name)) assert len(dir) == len(YEAR_LIST) name: str = fake.file_name() path: Path = dir.path / name path.mkdir() for i in range(2): assert len(dir) == len(YEAR_LIST) + i assert (lambda x: (x == 0 and name not in dir) or (x == 1 and name in dir))(i) dir.refresh()
def test_out(self, dir_empty: tempfile.TemporaryDirectory, fake: Faker) -> None: name: str = fake.file_name(extension='csv') content: List[str] = fake.random_elements(elements=('a', 'b', 'c', 'd'), length=20, unique=False) input: MagicMock = MagicMock() input.__iter__.return_value = content dir: FileRepoDir = FileRepoDir(Path(dir_empty.name)) obj: FileRepoObject = FileRepoObject(dir, name) obj.out(iter(input)) assert obj.exists() with open(obj.path, "r") as f: assert ''.join(content) == f.read()
def test_out_file_exists(self, dir_empty: tempfile.TemporaryDirectory, fake: Faker) -> None: name: str = fake.file_name(extension='csv') content: List[str] = fake.random_elements(elements=('a', 'b', 'c', 'd'), length=20, unique=False) input: MagicMock = MagicMock() input.__iter__.return_value = content dir: FileRepoDir = FileRepoDir(Path(dir_empty.name)) obj: FileRepoObject = FileRepoObject(dir, name) with obj.path.open(mode="w", buffering=2048) as f: f.write(''.join(content)) with pytest.raises(FileExistsError): obj.out(iter(input)) assert False
def test_visit_all_objects(self, test_fs: tempfile.TemporaryDirectory) -> None: dir: FileRepoDir = FileRepoDir(Path(test_fs.name)) mock: MagicMock = MagicMock() mock.visit.return_value = True dir.visit(mock) l: int = len(YEAR_LIST) m: int = max(YEAR_LIST) i: int = 0 for c in mock.mock_calls: assert c[0] == 'visit' assert isinstance(c[1][0], FileRepoObject) assert c[1][0].subpath(4) == [ "QD"[i // (FILE_PER_DIR * 4 * l)], "{year}".format(year = m - (i // (FILE_PER_DIR * 4)) % l), "QTR{quarter}".format(quarter = 4 - (i // FILE_PER_DIR) % 4), "file-{file}.txt".format(file = FILE_PER_DIR - (i % FILE_PER_DIR) - 1) ] i += 1 assert len(mock.mock_calls) == FILE_PER_DIR * 4 * l * 2
def test_out_overwrite(self, dir_empty: tempfile.TemporaryDirectory, fake: Faker) -> None: name: str = fake.file_name(extension='csv') input: MagicMock = MagicMock() dir: FileRepoDir = FileRepoDir(Path(dir_empty.name)) obj: FileRepoObject = FileRepoObject(dir, name) input.__iter__.return_value = fake.random_elements(elements=('a', 'b', 'c', 'd'), length=20, unique=False) obj.out(input) input.__iter__.return_value = fake.random_elements(elements=('e', 'f', 'g', 'k'), length=20, unique=False) obj.out(input, override=True) assert obj.exists() assert obj.path.name == name with open(obj.path, "r") as f: assert ''.join(input.__iter__.return_value) == f.read()
def test_get_success(self, test_fs: tempfile.TemporaryDirectory, path_list: List[str], expected_path: str): root: Path = Path(test_fs.name) dir: FileRepoDir = FileRepoDir(root) obj: FileRepoObject = dir.get(path_list) assert (root / expected_path).resolve() == obj.path
def test_sort(self, dir_prepped: tempfile.TemporaryDirectory) -> None: dir: FileRepoDir = FileRepoDir(Path(dir_prepped.name)) objects: List[str] = dir.sort() expected: Iterator = iter(sorted(YEAR_LIST, reverse = True)) for actual in objects: assert actual == str(next(expected))
def test_get_failure(self, test_fs: tempfile.TemporaryDirectory, path_list: List[str]): root: Path = Path(test_fs.name) dir: FileRepoDir = FileRepoDir(root) obj: FileRepoObject = dir.get(path_list) assert obj is None
def test_init_dir_empty(self, dir_empty: tempfile.TemporaryDirectory) -> None: dir: FileRepoDir = FileRepoDir(Path(dir_empty.name)) assert dir.exists() assert len(dir) == 0
def __init__(self, root: Path, repo_format: RepoFormat) -> None: self.__root: FileRepoDir = FileRepoDir(root) self.__format: RepoFormat = repo_format self.__index: Dict[str, RepoObject] = {}