Feat: start testing fs_repository
This commit is contained in:
parent
08c7fbe4c5
commit
5450de8628
@ -10,55 +10,60 @@ class FSRepository(AbstractRepository):
|
|||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
self.basepath = Path(basepath)
|
self.basepath = Path(basepath)
|
||||||
|
assert self.basepath.exists()
|
||||||
self._metadata_engine = metadata_engine
|
self._metadata_engine = metadata_engine
|
||||||
|
|
||||||
def ls(self, dir, only_files=False, only_directories=False, recursive=False) -> list[str]:
|
def ls(
|
||||||
dirpath = Path(dir)
|
self, dir="", only_files=False, only_directories=False, recursive=False
|
||||||
|
) -> list[str]:
|
||||||
|
dirpath = self.basepath / dir
|
||||||
|
|
||||||
if only_files:
|
if only_files:
|
||||||
return [str(f.relative_to(dirpath)) for f in dirpath.iterdir() if not f.is_dir()]
|
return [
|
||||||
|
str(f.relative_to(dirpath)) for f in dirpath.iterdir() if not f.is_dir()
|
||||||
|
]
|
||||||
|
|
||||||
if only_directories:
|
if only_directories:
|
||||||
if recursive:
|
if recursive:
|
||||||
return [str(f[0].relative_to(dirpath)) for f in dirpath.walk()]
|
return [str(f[0].relative_to(dirpath)) for f in dirpath.walk()]
|
||||||
|
|
||||||
return [str(f.relative_to(dirpath)) for f in dirpath.iterdir() if f.is_dir()]
|
return [
|
||||||
|
str(f.relative_to(dirpath)) for f in dirpath.iterdir() if f.is_dir()
|
||||||
|
]
|
||||||
|
|
||||||
return [str(f.relative_to(dirpath)) for f in dirpath.iterdir()]
|
return [str(f.relative_to(dirpath)) for f in dirpath.iterdir()]
|
||||||
|
|
||||||
def schemas(self, recursive=True) -> list[str]:
|
def schemas(self, recursive=True) -> list[str]:
|
||||||
dirpath = self.basepath
|
return self.ls("", only_directories=True, recursive=True)
|
||||||
return self.ls(dirpath, only_directories=True, recursive=True)
|
|
||||||
|
|
||||||
def tables(self, schema:str) -> list[str]:
|
def tables(self, schema: str = ".") -> list[str]:
|
||||||
dirpath = self.basepath / schema
|
return self.ls(schema, only_files=True)
|
||||||
return self.ls(dirpath, only_files=True)
|
|
||||||
|
|
||||||
def build_table_path(self, table: str, schema: str):
|
def build_table_path(self, table: str, schema: str):
|
||||||
table_path = self.basepath
|
table_path = self.basepath
|
||||||
if schema == '.':
|
if schema == ".":
|
||||||
return table_path / table
|
return table_path / table
|
||||||
return table_path / schema / table
|
return table_path / schema / table
|
||||||
|
|
||||||
def info(self, table:str, schema:str='.'):
|
def infos(self, table: str, schema: str = "."):
|
||||||
table_path = self.build_table_path(table, schema)
|
table_path = self.build_table_path(table, schema)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def read(self, table:str, schema:str='.', read_options={}):
|
def read(self, table: str, schema: str = ".", read_options={}):
|
||||||
table_path = self.build_table_path(table, schema)
|
table_path = self.build_table_path(table, schema)
|
||||||
extension = table_path.suffix
|
extension = table_path.suffix
|
||||||
if extension == '.csv':
|
if extension == ".csv":
|
||||||
return pd.read_csv(table_path, **read_options)
|
return pd.read_csv(table_path, **read_options)
|
||||||
|
|
||||||
if extension == '.xlsx':
|
if extension == ".xlsx":
|
||||||
return pd.read_excel(table_path, **read_options)
|
return pd.read_excel(table_path, **read_options)
|
||||||
|
|
||||||
raise ValueError("Can't open the table")
|
raise ValueError("Can't open the table")
|
||||||
|
|
||||||
def write(self, content, table:str, schema:str='.'):
|
def write(self, content, table: str, schema: str = "."):
|
||||||
table_path = self.build_table_path(table, schema)
|
table_path = self.build_table_path(table, schema)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def delete(self, table:str, schema:str='.'):
|
def delete_table(self, table: str, schema: str = "."):
|
||||||
table_path = self.build_table_path(table, schema)
|
table_path = self.build_table_path(table, schema)
|
||||||
pass
|
pass
|
||||||
|
0
tests/repository/__init__.py
Normal file
0
tests/repository/__init__.py
Normal file
BIN
tests/repository/fs_examples/salary.pdf
Normal file
BIN
tests/repository/fs_examples/salary.pdf
Normal file
Binary file not shown.
Binary file not shown.
7
tests/repository/fs_examples/username.csv
Normal file
7
tests/repository/fs_examples/username.csv
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
Username; Identifier;First name;Last name
|
||||||
|
booker12;9012;Rachel;Booker
|
||||||
|
grey07;2070;Laura;Grey
|
||||||
|
johnson81;4081;Craig;Johnson
|
||||||
|
jenkins46;9346;Mary;Jenkins
|
||||||
|
smith79;5079;Jamie;Smith
|
||||||
|
|
|
46
tests/repository/test_fs_repository.py
Normal file
46
tests/repository/test_fs_repository.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import shutil
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from dashboard.libs.repository.fs_repository import FSRepository
|
||||||
|
|
||||||
|
EXAMPLE_DIR = "./tests/repository/fs_examples/"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def location(tmp_path):
|
||||||
|
loc = tmp_path
|
||||||
|
username_loc = loc / "username"
|
||||||
|
username_loc.mkdir()
|
||||||
|
salary_loc = loc / "salary"
|
||||||
|
salary_loc.mkdir()
|
||||||
|
example_src = Path(EXAMPLE_DIR)
|
||||||
|
|
||||||
|
for f in example_src.glob("*"):
|
||||||
|
if "username" in str(f):
|
||||||
|
shutil.copy(f, username_loc)
|
||||||
|
else:
|
||||||
|
shutil.copy(f, salary_loc)
|
||||||
|
|
||||||
|
return loc
|
||||||
|
|
||||||
|
|
||||||
|
def test_init(location):
|
||||||
|
repo = FSRepository("example", location)
|
||||||
|
assert repo.ls() == [
|
||||||
|
"username",
|
||||||
|
"salary",
|
||||||
|
]
|
||||||
|
assert repo.schemas() == [
|
||||||
|
".",
|
||||||
|
"username",
|
||||||
|
"salary",
|
||||||
|
]
|
||||||
|
|
||||||
|
assert repo.tables() == []
|
||||||
|
assert repo.tables("username") == [
|
||||||
|
"username.csv",
|
||||||
|
"username-password-recovery-code.xlsx",
|
||||||
|
]
|
||||||
|
assert repo.tables("salary") == ["salary.pdf"]
|
Loading…
Reference in New Issue
Block a user