diff --git a/photograb/store.py b/photograb/store.py index 8161933..017cdae 100644 --- a/photograb/store.py +++ b/photograb/store.py @@ -74,7 +74,7 @@ class Store: count = len(content) start_time = time.process_time() for name, path in content.items(): - shutil.rm(path) + path.unlink() elapsed = time.process_time() - start_time print(f"{count} in {elapsed:.2f}s.") diff --git a/photograb/test/test_store.py b/photograb/test/test_store.py new file mode 100644 index 0000000..99b0ebe --- /dev/null +++ b/photograb/test/test_store.py @@ -0,0 +1,157 @@ +# -*- codinf:utf-8 -*- + +import os +import unittest +from pathlib import Path +from ..store import Content, Store, Backup + + +class TestContent(unittest.TestCase): + def setUp(self): + self.first = Content(( + ('test1.jpg', Path("~/test1.jpg")), + ('test2.jpg', Path("~/test2.jpg")), + ('test3.mp4', Path("~/test3.mp4")), + ('test4.mp4', Path("~/test4.mp4")), + )) + self.second = Content(( + ('test1.jpg', Path("~/test1.jpg")), + ('test3.mp4', Path("~/test3.mp4")), + )) + + def test_content_difference(self): + diff = self.first.difference(self.second) + self.assertEqual( + diff, + Content(( + ('test2.jpg', Path("~/test2.jpg")), + ('test4.mp4', Path("~/test4.mp4")), + ))) + + def test_filter_by_ext_that_exists(self): + self.assertEqual( + self.first.filter_by_ext('jpg'), + Content(( + ('test1.jpg', Path("~/test1.jpg")), + ('test2.jpg', Path("~/test2.jpg")), + ))) + self.assertEqual( + self.second.filter_by_ext('mp4'), + Content(( + ('test3.mp4', Path("~/test3.mp4")), + ))) + + def test_filter_by_ext_doesnt_exists(self): + self.assertEqual( + self.second.filter_by_ext('nop'), + Content() + ) + + +class TestStore(unittest.TestCase): + + _test_files = set(("test1", "test2", "test3")) + test_folder = Path("./sandbox/") + test_subdir = test_folder / "subdir" + + @classmethod + def _get_test_files_paths(cls): + for f in cls._test_files: + if f == "test1": + yield cls.test_subdir / f + else: + yield cls.test_folder / f + + @classmethod + def setUpClass(cls): + if not cls.test_folder.exists(): + os.mkdir(cls.test_folder) + if not cls.test_subdir.exists(): + os.mkdir(cls.test_subdir) + for path in cls._get_test_files_paths(): + # Create an empty file + open(path, 'w').close() + + @classmethod + def tearDownClass(cls): + for path in cls._get_test_files_paths(): + os.remove(path) + cls.test_subdir.rmdir() + cls.test_folder.rmdir() + + def test_creates_empty_folder_on_init(self): + test_path = Path("./sandbox2") + if test_path.exists(): + test_path.rmdir() + Store(test_path) + self.assertTrue(test_path.exists()) + test_path.rmdir() + + def test_checks_valid_dir_on_init(self): + test_path = Path("./sandbox/test2") + test_path.touch() + with self.assertRaises(ValueError): + Store(test_path) + + def test_read_content_without_exclude(self): + content = Store(self.test_folder).read_content(exclude=None) + self.assertEqual(set(content.keys()), self._test_files) + + def test_read_content_with_exclude(self): + content = Store(self.test_folder).read_content(exclude="subdir") + self.assertEqual(set(content.keys()), set(("test2", "test3"))) + + +class TestStoreWriteMethods(unittest.TestCase): + + def setUp(self): + if not os.path.exists("sandbox"): + os.mkdir("sandbox") + if not os.path.exists("sandbox/subdir"): + os.mkdir("sandbox/subdir") + for i in range(3): + open(f"sandbox/test_file{i}", "w").close() + + def tearDown(self): + for i in range(3): + for p in [f"sandbox/test_file{i}", + f"sandbox/subdir/test_file{i}"]: + if os.path.exists(p): + os.remove(p) + os.removedirs("sandbox/subdir") + + def test_copy_content(self): + store = Store("sandbox/subdir") + store.copy_content( + Content( + ((f"test_file{i}", Path(f"sandbox/test_file{i}")) + for i in range(3)) + )) + for i in range(3): + self.assertTrue(os.path.exists(f"sandbox/subdir/test_file{i}")) + self.assertTrue(os.path.exists(f"sandbox/test_file{i}")) + + def test_move_content(self): + store = Store("sandbox/subdir") + store.move_content( + Content( + ((f"test_file{i}", Path(f"sandbox/test_file{i}")) + for i in range(3)) + )) + for i in range(3): + self.assertTrue(os.path.exists(f"sandbox/subdir/test_file{i}")) + self.assertFalse(os.path.exists(f"sandbox/test_file{i}")) + + def test_remove_content(self): + store = Store("sandbox") + store.remove_content(Content( + ((f"test_file{i}", Path(f"sandbox/test_file{i}")) + for i in range(2)) + )) + + for i in range(3): + exists = os.path.exists(f"sandbox/test_file{i}") + if i < 2: + self.assertFalse(exists) + else: + self.assertTrue(exists) diff --git a/photograb/test/test_utils.py b/photograb/test/test_utils.py deleted file mode 100644 index a69b011..0000000 --- a/photograb/test/test_utils.py +++ /dev/null @@ -1,114 +0,0 @@ -# -*- codinf:utf-8 -*- - -import os -import unittest -from pathlib import Path -from ..store import Content, Store, Backup - - -class TestContent(unittest.TestCase): - - _test_files = set(("test1", "test2", "test3")) - test_folder = Path("./sandbox/") - test_subdir = test_folder / "subdir" - - @classmethod - def _get_test_files_paths(cls): - for f in cls._test_files: - if f == "test1": - yield cls.test_subdir / f - else: - yield cls.test_folder / f - - @classmethod - def setUpClass(cls): - if not cls.test_folder.exists(): - os.mkdir(cls.test_folder) - if not cls.test_subdir.exists(): - os.mkdir(cls.test_subdir) - for path in cls._get_test_files_paths(): - # Create an empty file - open(path, 'w').close() - - @classmethod - def tearDownClass(cls): - for path in cls._get_test_files_paths(): - os.remove(path) - cls.test_subdir.rmdir() - cls.test_folder.rmdir() - - def test_content_difference(self): - first = Content(((1, 0), (2, 0), (3, 0), (4, 0))) - second = Content(((1, 0), (3, 0))) - result = first.difference(second) - self.assertEqual(result, Content(((2, 0), (4, 0)))) - - def test_get_content_without_exclude(self): - content = Store(self.test_folder).read_content(exclude=None) - self.assertEqual(set(content.keys()), self._test_files) - - def test_get_content_with_exclude(self): - content = Store(self.test_folder).read_content(exclude="subdir") - self.assertEqual(set(content.keys()), set(("test2", "test3"))) - - -class TestCopyFile(unittest.TestCase): - - def setUp(self): - if not os.path.exists("sandbox"): - os.mkdir("sandbox") - if not os.path.exists("sandbox/subdir"): - os.mkdir("sandbox/subdir") - for i in range(3): - open(f"sandbox/test_copy_file{i}", "w").close() - - def tearDown(self): - for i in range(3): - for p in [f"sandbox/test_copy_file{i}", - f"sandbox/subdir/test_copy_file{i}"]: - if os.path.exists(p): - os.remove(p) - os.removedirs("sandbox/subdir") - - def test_copy_files(self): - copy_files( - Content( - ((f"test_copy_file{i}", Path(f"sandbox/test_copy_file{i}")) - for i in range(3)) - ), - "sandbox/subdir" - ) - for i in range(3): - self.assertTrue(os.path.exists(f"sandbox/subdir/test_copy_file{i}")) - self.assertTrue(os.path.exists(f"sandbox/test_copy_file{i}")) - - -class TestMoveFiles(unittest.TestCase): - - def setUp(self): - if not os.path.exists("sandbox"): - os.mkdir("sandbox") - if not os.path.exists("sandbox/subdir"): - os.mkdir("sandbox/subdir") - for i in range(3): - open(f"sandbox/test_move_file{i}", "w").close() - - def tearDown(self): - for i in range(3): - for p in [f"sandbox/test_move_file{i}", - f"sandbox/subdir/test_move_file{i}"]: - if os.path.exists(p): - os.remove(p) - os.removedirs("sandbox/subdir") - - def test_move_files(self): - move_files( - Content( - ((f"test_move_file{i}", Path(f"sandbox/test_move_file{i}")) - for i in range(3)) - ), - "sandbox/subdir", - ) - for i in range(3): - self.assertTrue(os.path.exists(f"sandbox/subdir/test_move_file{i}")) - self.assertFalse(os.path.exists(f"sandbox/test_move_file{i}"))