Created helper and media files
parent
2b8fdd8660
commit
c4dde2cc3f
|
@ -13,6 +13,6 @@
|
|||
"python.testing.nosetestsEnabled": false,
|
||||
"python.testing.unittestEnabled": false,
|
||||
"python.testing.pytestArgs": [
|
||||
"."
|
||||
"tests"
|
||||
]
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
import os
|
||||
import pandas as pd
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def append_col_inode(df: pd.DataFrame) -> pd.DataFrame:
|
||||
col_inode = df.apply(lambda x: os.stat(x).st_nlink,
|
||||
axis=1,
|
||||
columns=['path'])
|
||||
return df.assign(inode = col_inode)
|
|
@ -0,0 +1,30 @@
|
|||
import pathlib
|
||||
import typing
|
||||
import os
|
||||
|
||||
class Media:
|
||||
|
||||
def __init__(self, path: typing.Union[str, pathlib.Path]):
|
||||
self.path = pathlib.Path(path)
|
||||
|
||||
def exists(self) -> bool:
|
||||
return self.path.exists()
|
||||
|
||||
def is_symlink(self) -> bool:
|
||||
return self.path.is_symlink()
|
||||
|
||||
def number_of_links(self) -> int:
|
||||
return self.path.lstat().st_nlink
|
||||
|
||||
def inode(self) -> int:
|
||||
return self.path.lstat().st_ino
|
||||
|
||||
def relative_to_parents(self, target: pathlib.Path) -> str:
|
||||
path_parent = self.path.resolve().parent
|
||||
target_parent = target.resolve().parent
|
||||
|
||||
return os.path.relpath(path=target_parent, start=path_parent)
|
||||
|
||||
def symlink_to(self, target: pathlib.Path):
|
||||
self.path.symlink_to(target=os.path.join(self.relative_to_parents(target=target), target.name),
|
||||
target_is_directory=False)
|
|
@ -0,0 +1,2 @@
|
|||
file/
|
||||
target/
|
|
@ -0,0 +1,33 @@
|
|||
import pytest
|
||||
from pathlib import Path
|
||||
import os
|
||||
import pandas as pd
|
||||
import shutil
|
||||
|
||||
@pytest.fixture
|
||||
def cleanup():
|
||||
dir_delete = [Path('./tests/file/'), Path('./tests/target/')]
|
||||
|
||||
for d in dir_delete:
|
||||
shutil.rmtree(str(d), ignore_errors=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def create_test_files(cleanup) -> pd.DataFrame:
|
||||
|
||||
df = pd.DataFrame(columns=['path','target'])
|
||||
Path('./tests/file/').mkdir(parents=True, exist_ok=False)
|
||||
Path('./tests/target/').mkdir(parents=True, exist_ok=False)
|
||||
|
||||
for x in range(1,6):
|
||||
p = Path('./tests/file/file' + str(x) + '.txt')
|
||||
t = Path('./tests/target/target' + str(x) + '.txt')
|
||||
t.touch(exist_ok=False)
|
||||
p.symlink_to(t,target_is_directory=False)
|
||||
df.append({'path': p, 'target': t} , ignore_index=True)
|
||||
|
||||
return df
|
||||
|
||||
def test1(create_test_files):
|
||||
p = Path('./tests/file/')
|
||||
assert p.exists()
|
|
@ -0,0 +1,50 @@
|
|||
import pytest
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import os, sys
|
||||
|
||||
sys.path.insert(1, os.path.abspath('.'))
|
||||
|
||||
from test_helper import create_test_files, cleanup
|
||||
from media import Media
|
||||
|
||||
@pytest.fixture
|
||||
def create_one_pair_links(cleanup):
|
||||
Path('./tests/target/').mkdir(parents=True, exist_ok=False)
|
||||
Path('./tests/file').mkdir(parents=True, exist_ok=True)
|
||||
p = Path('./tests/file/file1.txt')
|
||||
t = Path('./tests/target/target1.txt')
|
||||
t.touch(exist_ok=False)
|
||||
p.symlink_to(Path('../target/target1.txt'))
|
||||
|
||||
def test_media_symlink_exist(create_one_pair_links):
|
||||
p = Media('./tests/file/file1.txt')
|
||||
assert p.exists()
|
||||
assert p.is_symlink()
|
||||
|
||||
def test_media_target_exist(create_one_pair_links):
|
||||
t = Media('./tests/target/target1.txt')
|
||||
assert t.exists()
|
||||
assert not t.is_symlink()
|
||||
assert t.number_of_links() == 1
|
||||
|
||||
def test_media_symlink(create_one_pair_links):
|
||||
|
||||
p = Media('./tests/file/file1.txt')
|
||||
t = Media('./tests/target/target1.txt')
|
||||
|
||||
text_in = 'hello, world'
|
||||
|
||||
with open(p.path, 'w') as writer:
|
||||
writer.write(text_in)
|
||||
|
||||
with open(t.path, 'r') as reader:
|
||||
text_out = reader.read()
|
||||
assert text_in == text_out
|
||||
|
||||
p2 = Media('./tests/file/file2.txt')
|
||||
assert p2.relative_to_parents(target=t.path) == '../target'
|
||||
p2.symlink_to(target=t.path)
|
||||
with open(p2.path, 'r') as reader:
|
||||
text_out = reader.read()
|
||||
assert text_in == text_out
|
Loading…
Reference in New Issue