synology_link_manipultor/tests/test_media.py

67 lines
1.8 KiB
Python
Raw Normal View History

2021-07-17 21:34:36 +10:00
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
def test_media_symlink_to_hdlink(create_one_pair_links):
p = Media('./tests/file/file1.txt')
t = Media('./tests/target/target1.txt')
text_in = 'hello, world'
p.convert_symlink_to_hdlink()
assert not p.is_symlink()
with open(p.path, 'w') as writer:
writer.write(text_in)
with open(t.path, 'r') as reader:
text_out = reader.read()
assert text_out == text_in