From 41c194faad7cc48acff4e143cc52a1a18add2e8f Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Sat, 16 Mar 2019 07:46:03 -0700 Subject: [PATCH] Split test_hierarchy into 3 modules and make tests a package --- tests/__init__.py | 0 tests/test_hierarchy.py | 112 +++------------------------------------ tests/test_repository.py | 65 +++++++++++++++++++++++ tests/test_snapshot.py | 53 ++++++++++++++++++ 4 files changed, 124 insertions(+), 106 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_repository.py create mode 100644 tests/test_snapshot.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_hierarchy.py b/tests/test_hierarchy.py index 2a87513..45dc537 100644 --- a/tests/test_hierarchy.py +++ b/tests/test_hierarchy.py @@ -1,110 +1,10 @@ -import rbackup.hierarchy as hierarchy -import unittest - -from unittest.mock import patch +import doctest # ========== Constants ========== +TESTING_MODULE = "rbackup.hierarchy.hierarchy" -# ========== Test Cases ========== -class TestHierarchy(unittest.TestCase): - @patch("rbackup.hierarchy.os.path.isdir") - def test_base_path(self, mocked_isdir): - mocked_isdir.return_value = True - - self.assertEqual(hierarchy.Hierarchy("directory").base_path, "directory") - - @patch("rbackup.hierarchy.os.path.isdir") - def test_invalid_dir(self, mocked_isdir): - mocked_isdir.return_value = False - - with self.assertRaises(NotADirectoryError): - hierarchy.Hierarchy("notadirectory") - - -class TestRepository(unittest.TestCase): - def setUp(self): - self.patched_isdir = patch("rbackup.hierarchy.os.path.isdir") - self.mocked_isdir = self.patched_isdir.start() - - self.mocked_isdir.return_value = True - - self.patched_datetime = patch("rbackup.hierarchy.datetime") - self.mocked_datetime = self.patched_datetime.start() - - self.mocked_datetime.datetime.utcnow.return_value.isoformat.return_value = ( - "utcnow" - ) - - self.patched_glob = patch("rbackup.hierarchy.glob.glob") - self.mocked_glob = self.patched_glob.start() - - self.mocked_glob.return_value = [ - "backup/data/snapshot-first", - "backup/data/snapshot-second", - "backup/data/snapshot-last", - ] - - self.repo = hierarchy.Repository("backup") - - @patch("rbackup.hierarchy.glob.glob") - def test_first_snapshot(self, mocked_glob): - mocked_glob.return_value = [] - - empty_repo = hierarchy.Repository("backup") - - self.assertIsNone(empty_repo.snapshots) - - @patch("rbackup.hierarchy.os.path.realpath") - def test_prev_snapshot(self, mocked_realpath): - mocked_realpath.return_value = "backup/data/snapshot-last" - - self.assertEqual(self.repo.prev_snapshot.path, "backup/data/snapshot-last") - - self.assertIsInstance(self.repo.prev_snapshot, hierarchy.Snapshot) - - def test_curr_snapshot(self): - self.assertEqual(self.repo.curr_snapshot.path, "backup/data/snapshot-utcnow") - - def tearDown(self): - self.patched_isdir.stop() - self.patched_datetime.stop() - self.patched_glob.stop() - - -class TestSnapshot(unittest.TestCase): - def setUp(self): - self.patched_isdir = patch("rbackup.hierarchy.os.path.isdir") - self.mocked_isdir = self.patched_isdir.start() - - self.mocked_isdir.return_value = True - - self.patched_datetime = patch("rbackup.hierarchy.datetime") - self.mocked_datetime = self.patched_datetime.start() - - self.mocked_datetime.datetime.utcnow.return_value.isoformat.return_value = ( - "utcnow" - ) - - self.snapshot = hierarchy.Snapshot("backup/data/snapshot-utcnow") - - def test_name(self): - self.assertEqual(self.snapshot.name, "snapshot-utcnow") - - def test_boot_dir(self): - self.assertEqual(self.snapshot.boot_dir, "backup/data/snapshot-utcnow/boot") - - def test_etc_dir(self): - self.assertEqual(self.snapshot.etc_dir, "backup/data/snapshot-utcnow/etc") - - def test_home_dir(self): - self.assertEqual(self.snapshot.home_dir, "backup/data/snapshot-utcnow/home") - - def test_root_home_dir(self): - self.assertEqual( - self.snapshot.root_home_dir, "backup/data/snapshot-utcnow/root" - ) - - def tearDown(self): - self.patched_isdir.stop() - self.patched_datetime.stop() +# ========== Functions ========== +def load_tests(loader, tests, ignore): + tests.addTests(doctest.DocTestSuite(TESTING_MODULE)) + return tests diff --git a/tests/test_repository.py b/tests/test_repository.py new file mode 100644 index 0000000..654a761 --- /dev/null +++ b/tests/test_repository.py @@ -0,0 +1,65 @@ +import doctest +import unittest + +from rbackup.hierarchy.repository import Repository +from rbackup.hierarchy.snapshot import Snapshot +from unittest.mock import patch + +# ========== Constants ========== +TESTING_MODULE = "rbackup.hierarchy.repository" +OS_PATH = f"{TESTING_MODULE}.os.path" +OS_PATH_ISDIR = f"{OS_PATH}.isdir" +GLOB_GLOB = f"{TESTING_MODULE}.glob.glob" + + +# ========== Functions ========== +def load_tests(loader, tests, ignore): + tests.addTests(doctest.DocTestSuite(TESTING_MODULE)) + return tests + + +# ========== Integration Tests ========== +class TestRepository(unittest.TestCase): + def setUp(self): + self.patched_isdir = patch(OS_PATH_ISDIR) + self.mocked_isdir = self.patched_isdir.start() + + self.mocked_isdir.return_value = True + + self.patched_glob = patch(GLOB_GLOB) + self.mocked_glob = self.patched_glob.start() + + self.snapshots = [ + "backup/data/snapshot-first", + "backup/data/snapshot-second", + "backup/data/snapshot-third", + ] + + self.mocked_glob.return_value = self.snapshots + + self.repo_basepath = "backup" + + self.repo = Repository(self.repo_basepath) + + def test_snapshots(self): + found_snapshots = [s.path for s in self.repo.snapshots] + self.assertListEqual(found_snapshots, self.snapshots) + + def test_curr_snapshot_pre_create(self): + snapshot_name = "third" + snapshot_path = f"backup/data/snapshot-{snapshot_name}" + + self.assertEqual(self.repo.curr_snapshot.path, snapshot_path) + self.assertIsInstance(self.repo.curr_snapshot, Snapshot) + + def test_curr_snapshot_post_create(self): + snapshot_name = "new" + snapshot_path = f"backup/data/snapshot-{snapshot_name}" + + self.repo.create_snapshot(snapshot_name) + self.assertEqual(self.repo.curr_snapshot.path, snapshot_path) + self.assertIsInstance(self.repo.curr_snapshot, Snapshot) + + def tearDown(self): + self.patched_isdir.stop() + self.patched_glob.stop() diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py new file mode 100644 index 0000000..7687c13 --- /dev/null +++ b/tests/test_snapshot.py @@ -0,0 +1,53 @@ +""" +.. author:: Eric Torres + +Unit tests for the Snapshot class. +""" +import doctest +import unittest + +from rbackup.hierarchy.snapshot import Snapshot +from unittest.mock import patch + +# ========== Constants ========== +TESTING_MODULE = "rbackup.hierarchy.repository" + +# ========== Functions ========== +def load_tests(loader, tests, ignore): + tests.addTests(doctest.DocTestSuite(TESTING_MODULE)) + return tests + + +# ========== Test Cases ========== +class TestSnapshot(unittest.TestCase): + def setUp(self): + self.patched_isdir = patch("rbackup.hierarchy.snapshot.os.path.isdir") + self.mocked_isdir = self.patched_isdir.start() + + self.mocked_isdir.return_value = True + + self.snapshot_fullpath = "backup/data/snapshot-new" + self.snapshot_name = "snapshot-new" + + self.snapshot = Snapshot(self.snapshot_fullpath) + + def test_path(self): + self.assertEqual(self.snapshot.path, self.snapshot_fullpath) + + def test_name(self): + self.assertEqual(self.snapshot.name, self.snapshot_name) + + def test_boot_dir(self): + self.assertEqual(self.snapshot.boot_dir, f"{self.snapshot_fullpath}/boot") + + def test_etc_dir(self): + self.assertEqual(self.snapshot.etc_dir, f"{self.snapshot_fullpath}/etc") + + def test_home_dir(self): + self.assertEqual(self.snapshot.home_dir, f"{self.snapshot_fullpath}/home") + + def test_root_home_dir(self): + self.assertEqual(self.snapshot.root_home_dir, f"{self.snapshot_fullpath}/root") + + def tearDown(self): + self.patched_isdir.stop()