def test_filename_multiple(): filename = Filename(startswith="begin", contains="con", endswith="end") assert filename.matches(Path("~/here/begin_somethgin_con_end.pdf")) assert not filename.matches(Path("~/here/beginend.pdf")) assert not filename.matches(Path("~/here/begincon.begin")) assert not filename.matches(Path("~/here/conend.begin")) assert filename.matches(Path("~/here/beginconend.begin"))
def test_min(): now = datetime.now() created = Created(days=10, hours=12, mode="older") with patch.object(created, "_created") as mock_cr: mock_cr.return_value = now - timedelta(days=10, hours=0) assert created.run(path=Path("~")) is None mock_cr.return_value = now - timedelta(days=10, hours=13) assert created.run(path=Path("~"))
def test_min(): now = pendulum.now() last_modified = LastModified(days=10, hours=12, mode="older") with patch.object(last_modified, "_last_modified") as mock_lm: mock_lm.return_value = now - pendulum.duration(days=10, hours=0) assert not last_modified.run(path=Path("~")) mock_lm.return_value = now - pendulum.duration(days=10, hours=13) assert last_modified.run(path=Path("~"))
def test_min(): now = datetime.now() last_modified = LastModified(days=10, hours=12, mode="older") with patch.object(last_modified, "_last_modified") as mock_lm: mock_lm.return_value = now - timedelta(days=10, hours=0) assert not last_modified.run(path=Path("~")) mock_lm.return_value = now - timedelta(days=10, hours=13) assert last_modified.run(path=Path("~"))
def test_min(): now = pendulum.now() created = Created(days=10, hours=12, mode="older") with patch.object(created, "_created") as mock_cr: mock_cr.return_value = now - pendulum.duration(days=10, hours=0) assert created.run(path=Path("~")) is None mock_cr.return_value = now - pendulum.duration(days=10, hours=13) assert created.run(path=Path("~"))
def test_filename_case(): filename = Filename(startswith="star", contains="con", endswith="end", case_sensitive=False) assert filename.matches(Path("~/STAR_conEnD.dpf")) assert not filename.matches(Path("~/here/STAREND.pdf")) assert not filename.matches(Path("~/here/STARCON.begin")) assert not filename.matches(Path("~/here/CONEND.begin")) assert filename.matches(Path("~/here/STARCONEND.begin"))
def test_extension(): extension = Extension("JPG", ".gif", "pdf") testpathes = [ (Path("~/somefile.pdf"), True), (Path("/home/test/somefile.pdf.jpeg"), False), (Path("/home/test/gif.TXT"), False), (Path("/home/test/txt.GIF"), True), (Path("~/somefile.pdf"), True), ] for path, match in testpathes: assert bool(extension.matches(path)) == match
def test_shell_path(): with patch("subprocess.call") as m: shell = Shell("echo {path.stem} for {year}") shell.run(path=Path("/") / "this" / "isafile.txt", year=2017, simulate=False) m.assert_called_with("echo isafile for 2017", shell=True)
def test_code_execution(): with patch.object(Python, "print") as mock_print: path = Path("/some/folder") python = Python("print(x)\nprint(path)") python.run(path=path, x=42, simulate=False) mock_print.assert_any_call(42) mock_print.assert_any_call(path)
def test_filename_list(): filename = Filename( startswith="_", contains=["1", "A", "3", "6"], endswith=["5", "6"], case_sensitive=False, ) assert filename.matches(Path("~/_15.dpf")) assert filename.matches(Path("~/_A5.dpf")) assert filename.matches(Path("~/_A6.dpf")) assert filename.matches(Path("~/_a6.dpf")) assert filename.matches(Path("~/_35.dpf")) assert filename.matches(Path("~/_36.dpf")) assert filename.matches(Path("~/_somethinga56")) assert filename.matches(Path("~/_6")) assert not filename.matches(Path("~/")) assert not filename.matches(Path("~/a_5"))
def test_overwrite(mock_exists, mock_samefile, mock_rename, mock_trash): mock_exists.return_value = True mock_samefile.return_value = False rename = Rename(name="{path.stem} Kopie.py", overwrite=True) new_path = rename.run(**ARGS) assert mock_exists.call_count > 0 mock_trash.assert_called_with(os.path.join(USER_DIR, "test Kopie.py")) mock_rename.assert_called_with(Path("~/test Kopie.py").expanduser()) assert new_path is not None
def test_already_exists(mock_exists, mock_samefile, mock_rename, mock_trash): mock_exists.side_effect = [True, False] mock_samefile.return_value = False rename = Rename(name="asd.txt", overwrite=False) new_path = rename.run(**ARGS) assert mock_exists.call_count > 0 mock_trash.assert_not_called() mock_rename.assert_called_with(Path("~/asd 2.txt").expanduser()) assert new_path is not None
def test_already_exists_multiple_separator(mock_exists, mock_samefile, mock_rename, mock_trash): mock_exists.side_effect = [True, True, True, False] mock_samefile.return_value = False rename = Rename(name="asd.txt", overwrite=False, counter_separator="-") new_path = rename.run(**ARGS) mock_exists.assert_called() mock_trash.assert_not_called() mock_rename.assert_called_with(Path("~/asd-4.txt").expanduser()) assert new_path is not None
def test_tilde_expansion(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): mock_exists.return_value = False mock_samefile.return_value = False move = Move(dest="~/newname.py", overwrite=False) updates = move.run(**ARGS) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with(src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "newname.py")) assert updates == {"path": Path("~/newname.py").expanduser()}
def test_into_folder(mock_exists, mock_samefile, mock_move, mock_trash, mock_mkdir): mock_exists.return_value = False mock_samefile.return_value = False move = Move(dest="~/somefolder/", overwrite=False) updates = move.run(**ARGS) mock_mkdir.assert_called_with(exist_ok=True, parents=True) mock_exists.assert_called_with() mock_trash.assert_not_called() mock_move.assert_called_with( src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "somefolder", "test.py"), ) assert updates == {"path": Path(USER_DIR) / "somefolder" / "test.py"}
def test_extension_result(): path = Path("~/somefile.TxT") extension = Extension("txt") assert extension.matches(path) result = extension.run(path=path)["extension"] assert str(result) == "TxT" assert result.lower == "txt" assert result.upper == "TXT" extension = Extension(".txt") assert extension.matches(path) result = extension.run(path=path)["extension"] assert str(result) == "TxT" assert result.lower == "txt" assert result.upper == "TXT"
def create_filesystem(tmp_path, files, config): # create files for f in files: try: name, content = f except Exception: name = f content = "" p = tmp_path / "files" / Path(name) p.parent.mkdir(parents=True, exist_ok=True) with p.open("w") as ptr: ptr.write(content) # create config with (tmp_path / "config.yaml").open("w") as f: f.write(config) # change working directory os.chdir(str(tmp_path))
def test_args(mock_exists, mock_samefile, mock_rename, mock_trash): args = { "basedir": Path.home(), "path": Path.home() / "test.py", "nr": { "upper": 1 }, "simulate": False, } mock_exists.return_value = False mock_samefile.return_value = False rename = Rename(name="{nr.upper}-{path.stem} Kopie.py") new_path = rename.run(**args) assert mock_exists.call_count > 0 mock_trash.assert_not_called() mock_rename.assert_called_with(Path("~/1-test Kopie.py").expanduser()) assert new_path is not None
def test_echo_basic(): echo = Echo("Hello World") with patch.object(echo, "print") as m: echo.run(path=Path('~'), simulate=False) m.assert_called_with("Hello World")
def test_echo_path(): echo = Echo("{path.stem} for {year}") with patch.object(echo, "print") as m: echo.run(simulate=False, path=Path("/this/isafile.txt"), year=2017) m.assert_called_with("isafile for 2017")
def test_echo_args(): echo = Echo("This is the year {year}") with patch.object(echo, "print") as m: echo.run(path=Path('~'), simulate=False, year=2017) m.assert_called_with("This is the year 2017")
def assertdir(path, *files): os.chdir(str(path / "files")) assert set(files) == set( str(x) for x in Path(".").glob("**/*") if x.is_file())
def test_filename_startswith(): filename = Filename(startswith="begin") assert filename.matches(Path("~/here/beginhere.pdf")) assert not filename.matches(Path("~/here/.beginhere.pdf")) assert not filename.matches(Path("~/here/herebegin.begin"))
from organize.compat import Path from organize.filters import Regex from organize.utils import DotDict TESTDATA = [ (Path("~/Invoices/RG123456123456-sig.pdf"), True, "123456123456"), (Path("~/Invoices/RG002312321542-sig.pdf"), True, "002312321542"), (Path("~/Invoices/RG002312321542.pdf"), False, None), ] def test_regex_backslash(): regex = Regex(r"^\.pdf$") assert regex.matches(Path(".pdf")) assert not regex.matches(Path("+pdf")) assert not regex.matches(Path("/pdf")) assert not regex.matches(Path("\\pdf")) def test_regex_basic(): regex = Regex(r"^RG(\d{12})-sig\.pdf$") for path, match, _ in TESTDATA: assert bool(regex.matches(path)) == match def test_regex_return(): regex = Regex(r"^RG(?P<the_number>\d{12})-sig\.pdf$") for path, valid, result in TESTDATA: if valid: dct = regex.run(path=path) assert dct == {"regex": {"the_number": result}}
def test_regex_backslash(): regex = Regex(r"^\.pdf$") assert regex.matches(Path(".pdf")) assert not regex.matches(Path("+pdf")) assert not regex.matches(Path("/pdf")) assert not regex.matches(Path("\\pdf"))
def test_filename_contains(): filename = Filename(contains="begin") assert filename.matches(Path("~/here/beginhere.pdf")) assert filename.matches(Path("~/here/.beginhere.pdf")) assert filename.matches(Path("~/here/herebegin.begin")) assert not filename.matches(Path("~/here/other.begin"))
def test_extension_empty(): extension = Extension() assert extension.matches(Path("~/test.txt"))
def test_filename_endswith(): filename = Filename(endswith="end") assert filename.matches(Path("~/here/hereend.pdf")) assert not filename.matches(Path("~/here/end.tar.gz")) assert not filename.matches(Path("~/here/theendishere.txt"))
def test_regex_umlaut(): regex = Regex(r"^Erträgnisaufstellung-(?P<year>\d*)\.pdf") doc = Path("~/Documents/Erträgnisaufstellung-1998.pdf") assert regex.matches(doc) dct = regex.run(path=doc) assert dct == {"regex": {"year": "1998"}}