started testing

This commit is contained in:
artus
2018-11-17 15:35:21 +01:00
parent 9aca0584ee
commit 6773d593b8
5 changed files with 143 additions and 8 deletions

Binary file not shown.

View File

@@ -0,0 +1,11 @@
import os
def load_tests(loader, std_tests, pattern):
pkg_tests = loader.discover(
start_dir=os.path.dirname(__file__),
pattern="test_*.py"
)
std_tests.addTests(pkg_tests)
return std_tests

View File

@@ -0,0 +1,14 @@
# -*- coding:utf-8 -*-
import unittest
class TestBackup(unittest.TestCase):
def test_add(self):
pass
def test_list(self):
pass

View File

@@ -0,0 +1,111 @@
# -*- codinf:utf-8 -*-
import os
import unittest
from pathlib import Path
from ..utils import Content, get_content, copy_files, move_files
class TestContent(unittest.TestCase):
_test_files = set(("test1", "test2", "test3"))
def _get_test_files_paths(self):
for f in self._test_files:
if f == "test1":
yield self.test_subdir / f
else:
yield self.test_folder / f
def setUp(self):
self.test_folder = Path("./sandbox/")
if not self.test_folder.exists():
os.mkdir(self.test_folder)
self.test_subdir = self.test_folder / "subdir"
if not self.test_subdir.exists():
os.mkdir(self.test_subdir)
for path in self._get_test_files_paths():
# Create an empty file
open(path, 'w').close()
def tearDown(self):
for path in self._get_test_files_paths():
os.remove(path)
self.test_subdir.rmdir()
self.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 = get_content(self.test_folder, exclude=None)
self.assertEqual(set(content.keys()), self._test_files)
def test_get_content_with_exclude(self):
content = get_content(self.test_folder, 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}"))

View File

@@ -2,15 +2,16 @@
import itertools
import os
import time
# import shutil
from pathlib import Path
import shutil
def copy_files(content, target_dir):
print(f"Copy files to {target_dir} : ", end="")
count = len(content)
start_time = time.process_time()
# for name,path in content.items():
# shutil.copy(path, target_dir)
for name, path in content.items():
shutil.copy2(str(path), target_dir)
elapsed = time.process_time() - start_time
print(f"{count} in {elapsed:.2f}s.")
@@ -19,8 +20,8 @@ def move_files(content, target_dir):
print(f"Move files to {target_dir} : ", end="")
count = len(content)
start_time = time.process_time()
# for name, path in content.items():
# shutil.mv(path, target_dir)
for name, path in content.items():
shutil.move(str(path), target_dir)
elapsed = time.process_time() - start_time
print(f"{count} in {elapsed:.2f}s.")
@@ -38,12 +39,10 @@ class Content(dict):
def get_content(folder, exclude=None):
# TODO: exact location of files is lost, but will be used !
# Could return a dict{ filename: full_path }
result = Content()
for path, _, filenames in os.walk(folder):
# Ignore excluded folders
if path.split("/")[-1] == exclude:
if exclude and Path(path).absolute().name == exclude:
continue
for name in filenames: