Split test_hierarchy into 3 modules and make tests a package

This commit is contained in:
Eric Torres 2019-03-16 07:46:03 -07:00
parent 50f042b2b0
commit 41c194faad
4 changed files with 124 additions and 106 deletions

0
tests/__init__.py Normal file
View File

View File

@ -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

65
tests/test_repository.py Normal file
View File

@ -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()

53
tests/test_snapshot.py Normal file
View File

@ -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()