Skip to content

Commit

Permalink
WIP media magic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
e3rd committed Nov 26, 2024
1 parent 7ee56cf commit fe468f5
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 280 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
- name: Install dependencies
run: pip install -e .
- name: Run tests
run: python3 tests.py
run: python3 -m unittest discover tests
1 change: 1 addition & 0 deletions deduplidog/deduplidog.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ def start(self, interface=None):
self.reset()
self.check()
self.perform()
return self

def perform(self):
# build file list of the originals
Expand Down
184 changes: 0 additions & 184 deletions tests.py

This file was deleted.

123 changes: 120 additions & 3 deletions tests/setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,132 @@
import os
import random
import string
from collections.abc import Mapping
from dataclasses import MISSING, dataclass, field
from itertools import chain
from pathlib import Path
from tempfile import TemporaryDirectory, mkdtemp
from typing import Self
from unittest import TestCase, main

from deduplidog import Deduplidog
from deduplidog.deduplidog import Action, Execution, Match, Media, Helper
from deduplidog.deduplidog import Action, Execution, Helper, Match, Media


def drun(action=None, execution=None, match=None, media=None, helper=None, **kw):
def drun(action=None, execution=None, match=None, media=None, helper=None, confirm_one_by_one=False, **kw):
def _(l: list | dict):
if isinstance(l, list):
return {k: True for k in l}
return l

# as confirm_one_by_one affects the testing, this option is lifted up here
exec = {"confirm_one_by_one": confirm_one_by_one} if confirm_one_by_one is not None else {}

return Deduplidog(Action(**_(action or [])),
Execution(**_(execution or [])),
Execution(**_(execution or {}) | exec),
Match(**_(match or [])),
Media(**_(media or [])),
Helper(**_(helper or [])),
**kw).start()


@dataclass
class FileReal:
path: Path

def __post_init__(self):
self._mtime = self.path.stat().st_mtime

def check(self, test: TestCase):
"Checks the disk whether it contains the file represented."
test.assertTrue(self.path.exists(), msg=self.path)
test.assertEqual(self._mtime, self.path.stat().st_mtime, msg=self.path)

def prefixed(self):
self.path = self.path.with_name("✓" + self.path.name)

def suck(self, other: Self):
"Use the other file. Use its name, however stays in the current directory."
self.path = self.path.with_name(other.path.name)
self._mtime = other._mtime


@dataclass
class FileRepresentation(FileReal):
# path: Path

mtime: int = 0
"relative mtime"
text_seed: int = 1

def __post_init__(self):
self._mtime = round(self.path.parent.parent.stat().st_mtime + self.mtime)

def write(self):
"Writes the representation to the disk."
self.path.write_text(self.get_text())
os.utime(self.path, (self._mtime,)*2)
return self

def check(self, test: TestCase):
super().check(test)
if self.path.suffix not in (".jpeg",):
test.assertEqual(self.get_text(), self.path.read_text(), msg=self.path)

def get_text(self):
random.seed(self.text_seed)
return ''.join(random.choices(string.ascii_letters + string.digits, k=10+self.text_seed*10))

def suck(self, other: Self):
super().suck(other)
self.text_seed = other.text_seed


@dataclass
class FolderState(Mapping):
test_case: TestCase
_work_dir: Path
_original_dir: Path
work_files: dict[str, FileReal] = field(default_factory=lambda: {})
originals: dict[str, FileReal] = field(default_factory=lambda: {})

def __post_init__(self):
def _(dir_: Path, files_: dict):
for file in dir_.rglob('*'):
if file.is_file():
files_[str(file)] = FileReal(path=file)

if not self.work_files:
_(self._work_dir, self.work_files)
if not self.originals:
_(self._original_dir, self.originals)

def __iter__(self):
yield from ('work_dir', 'original_dir')

def __len__(self):
return 2

def __getitem__(self, key):
if key == 'work_dir':
return self._work_dir
elif key == 'original_dir':
return self._original_dir
else:
raise KeyError(key)

def check(self, prefixed: tuple[str] = None, suck: tuple[str] = None, prefixed_i: tuple[int] = None, suck_i: tuple[int] = None):
"""Checks the file changes
:param prefixed: These files in the work dir are expected to be prefixed
:param suck: These files in the work dir are expected to be sucked from the originals
:param prefixed_i: These file_{i} in the work dir are expected to be prefixed
:param suck_i: These file_{i} in the work dir are expected to be sucked from the originals
"""
[self.work_files[str(self._work_dir / f)].prefixed() for f in prefixed or ()]
[self.work_files[str(self._work_dir / f)].suck(self.originals[str(self._original_dir / f)]) for f in suck or ()]

[self.work_files[f"file_{i}"].prefixed() for i in prefixed_i or ()]
[self.work_files[f"file_{i}"].suck(self.originals[f"file_{i}"]) for i in suck_i or ()]

[f.check(self.test_case) for f in chain(self.work_files.values(), self.originals.values())]
1 change: 1 addition & 0 deletions tests/test_data/disk/folder1/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
one
1 change: 1 addition & 0 deletions tests/test_data/disk/folder1/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
two
Binary file added tests/test_data/disk/folder1/TODO_licence.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/test_data/disk/folder1/unique.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this file is unique
1 change: 1 addition & 0 deletions tests/test_data/disk/folder2/1-different-name.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
one
1 change: 1 addition & 0 deletions tests/test_data/disk/folder2/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
two
Binary file added tests/test_data/disk/folder2/TODO_licence.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/test_data/disk/folder2/folder2.1/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
one
Loading

0 comments on commit fe468f5

Please sign in to comment.