Utilize pathlib over os.path for internal path handling
This commit is contained in:
@ -1,15 +1,13 @@
|
||||
import doctest
|
||||
import unittest
|
||||
|
||||
from pathlib import PosixPath
|
||||
from rbackup.hierarchy.repository import Repository
|
||||
from rbackup.hierarchy.snapshot import Snapshot
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import patch, PropertyMock
|
||||
|
||||
# ========== 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 ==========
|
||||
@ -19,47 +17,67 @@ def load_tests(loader, tests, ignore):
|
||||
|
||||
|
||||
# ========== Integration Tests ==========
|
||||
class TestRepository(unittest.TestCase):
|
||||
class TestPopulatedRepository(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",
|
||||
Snapshot("backup/data/snapshot-first"),
|
||||
Snapshot("backup/data/snapshot-second"),
|
||||
Snapshot("backup/data/snapshot-third"),
|
||||
]
|
||||
|
||||
self.mocked_glob.return_value = self.snapshots
|
||||
self.patched_snapshots = patch(
|
||||
f"{TESTING_MODULE}.Repository.snapshots", new_callable=PropertyMock
|
||||
)
|
||||
self.mocked_snapshots = self.patched_snapshots.start()
|
||||
self.mocked_snapshots.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]
|
||||
found_snapshots = [s 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}"
|
||||
last_snapshot = Snapshot(f"backup/data/snapshot-{snapshot_name}")
|
||||
|
||||
self.assertEqual(self.repo.curr_snapshot.path, snapshot_path)
|
||||
self.assertEqual(self.repo.curr_snapshot.path, last_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}"
|
||||
snapshot_path = PosixPath(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()
|
||||
self.patched_snapshots.stop()
|
||||
|
||||
|
||||
class TestEmptyRepository(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.patched_snapshots = patch(
|
||||
f"{TESTING_MODULE}.Repository.snapshots", new_callable=PropertyMock
|
||||
)
|
||||
self.mocked_snapshots = self.patched_snapshots.start()
|
||||
self.mocked_snapshots.return_value = []
|
||||
|
||||
self.repo_basepath = "backup"
|
||||
self.repo = Repository(self.repo_basepath)
|
||||
|
||||
def test_curr_snapshot_pre_create(self):
|
||||
self.assertIsNone(self.repo.curr_snapshot)
|
||||
|
||||
def test_curr_snapshot_post_create(self):
|
||||
snapshot_name = "new"
|
||||
new_snapshot = Snapshot(f"backup/data/snapshot-{snapshot_name}")
|
||||
|
||||
self.repo.create_snapshot(snapshot_name)
|
||||
self.assertEqual(self.repo.curr_snapshot.path, new_snapshot.path)
|
||||
self.assertIsInstance(self.repo.curr_snapshot, Snapshot)
|
||||
|
||||
def tearDown(self):
|
||||
self.patched_snapshots.stop()
|
||||
|
@ -6,12 +6,13 @@ Unit tests for the Snapshot class.
|
||||
import doctest
|
||||
import unittest
|
||||
|
||||
from pathlib import Path
|
||||
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))
|
||||
@ -21,33 +22,31 @@ def load_tests(loader, tests, ignore):
|
||||
# ========== 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.snapshot_fullpath = Path("backup/data/snapshot-new")
|
||||
self.test_snapshot = Snapshot(self.snapshot_fullpath)
|
||||
|
||||
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_fullpath(self):
|
||||
self.assertEqual(self.test_snapshot.path, self.snapshot_fullpath)
|
||||
|
||||
def test_name(self):
|
||||
self.assertEqual(self.snapshot.name, self.snapshot_name)
|
||||
self.assertEqual(self.test_snapshot.name, "snapshot-new")
|
||||
|
||||
def test_boot_dir(self):
|
||||
self.assertEqual(self.snapshot.boot_dir, f"{self.snapshot_fullpath}/boot")
|
||||
self.assertEqual(
|
||||
self.test_snapshot.boot_dir, self.snapshot_fullpath / "boot"
|
||||
)
|
||||
|
||||
def test_etc_dir(self):
|
||||
self.assertEqual(self.snapshot.etc_dir, f"{self.snapshot_fullpath}/etc")
|
||||
self.assertEqual(
|
||||
self.test_snapshot.etc_dir, self.snapshot_fullpath / "etc"
|
||||
)
|
||||
|
||||
def test_home_dir(self):
|
||||
self.assertEqual(self.snapshot.home_dir, f"{self.snapshot_fullpath}/home")
|
||||
self.assertEqual(
|
||||
self.test_snapshot.home_dir, 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()
|
||||
self.assertEqual(
|
||||
self.test_snapshot.root_home_dir, self.snapshot_fullpath / "root"
|
||||
)
|
||||
|
Reference in New Issue
Block a user