Cleanup tests and use class decorators in tests.test_repository

This commit is contained in:
Eric Torres 2019-03-20 00:23:25 -07:00
parent 6fbf6a34de
commit 46aa2c44eb

View File

@ -17,115 +17,137 @@ def load_tests(loader, tests, ignore):
# ========== Integration Tests ========== # ========== Integration Tests ==========
@patch.object(Repository, "snapshots", new_callable=PropertyMock)
class TestEmptyRepository(unittest.TestCase): class TestEmptyRepository(unittest.TestCase):
def setUp(self): """Test a repository that has no 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 = list()
def setUp(self):
self.repo_basepath = "backup"
self.repo = Repository(self.repo_basepath)
self.new_snapshot_path = self.repo.snapshot_dir / "snapshot-new"
self.created_snapshot = Snapshot(self.new_snapshot_path)
def test_len_pre_create(self, repo_snapshots):
repo_snapshots.return_value = []
self.assertEqual(len(self.repo), 0)
self.assertEqual(len(self.repo), len(repo_snapshots.return_value))
def test_iteration_pre_create(self, repo_snapshots):
repo_snapshots.return_value = []
with self.assertRaises(StopIteration):
self.repo.__next__()
def test_subscript_pre_create(self, repo_snapshots):
repo_snapshots.return_value = []
with self.assertRaises(IndexError):
self.repo[0]
def test_curr_snapshot_pre_create(self, repo_snapshots):
repo_snapshots.return_value = []
self.assertIsNone(self.repo.curr_snapshot)
def test_curr_snapshot_post_create(self, repo_snapshots):
repo_snapshots.return_value = []
snapshot_name = "new"
self.repo.create_snapshot(snapshot_name)
self.assertEqual(self.repo.curr_snapshot.path, self.created_snapshot.path)
def test_len_post_create(self, repo_snapshots):
repo_snapshots.return_value = [self.created_snapshot.path]
self.assertEqual(len(self.repo), len(repo_snapshots.return_value))
def test_iteration_post_create(self, repo_snapshots):
repo_snapshots.return_value = [self.created_snapshot]
result = []
for snapshot in self.repo:
result.append(snapshot)
self.assertListEqual(result, [self.created_snapshot])
def test_subscript_post_create(self, repo_snapshots):
repo_snapshots.return_value = [self.created_snapshot]
self.assertEqual(self.repo[0].path, self.new_snapshot_path)
@patch.object(Repository, "snapshots", new_callable=PropertyMock)
class TestPopulatedRepository(unittest.TestCase):
"""Test a repository that has no snapshots."""
def setUp(self):
self.repo_basepath = "backup" self.repo_basepath = "backup"
self.repo = Repository(self.repo_basepath) self.repo = Repository(self.repo_basepath)
def test_curr_snapshot_pre_create(self): self.new_snapshot_path_1 = self.repo.snapshot_dir / "snapshot-one"
self.assertIsNone(self.repo.curr_snapshot) self.new_snapshot_path_2 = self.repo.snapshot_dir / "snapshot-two"
self.assertEqual(len(self.repo), 0)
def test_iteration_pre_create(self): self.existing_snapshots = [
result = list() Snapshot(self.new_snapshot_path_1),
for snapshot in self.repo: Snapshot(self.new_snapshot_path_2),
result.append(snapshot.path)
self.assertListEqual(result, [])
def test_subscript_pre_create(self):
with self.assertRaises(IndexError):
self.assertRaises(IndexError, self.repo[0])
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.assertEqual(len(self.repo), 1)
self.assertIsInstance(self.repo.curr_snapshot, Snapshot)
result = list()
for snapshot in self.repo:
result.append(snapshot.path)
self.assertListEqual(result, [PosixPath("backup/data/snapshot-new")])
self.assertEqual(self.repo[0], self.repo.curr_snapshot)
self.assertEqual(self.repo[-1], self.repo.curr_snapshot)
def tearDown(self):
self.patched_snapshots.stop()
class TestPopulatedRepository(unittest.TestCase):
def setUp(self):
self.snapshots = [
Snapshot("backup/data/snapshot-first"),
Snapshot("backup/data/snapshot-second"),
Snapshot("backup/data/snapshot-third"),
] ]
self.patched_snapshots = patch( def test_len_pre_create(self, repo_snapshots):
f"{TESTING_MODULE}.Repository.snapshots", new_callable=PropertyMock repo_snapshots.return_value = self.existing_snapshots
) self.assertEqual(len(self.repo), len(repo_snapshots.return_value))
self.mocked_snapshots = self.patched_snapshots.start()
self.mocked_snapshots.return_value = list(self.snapshots)
self.repo_basepath = "backup" def test_iteration_pre_create(self, repo_snapshots):
self.repo = Repository(self.repo_basepath) repo_snapshots.return_value = self.existing_snapshots
def test_snapshots(self): # Exhaust the iterator first
found_snapshots = [s for s in self.repo.snapshots] for iteration in range(0, len(self.existing_snapshots)):
self.assertListEqual(found_snapshots, self.snapshots) self.repo.__next__()
self.assertEqual(len(self.repo), len(self.snapshots))
def test_curr_snapshot_pre_create(self): with self.assertRaises(StopIteration):
snapshot_name = "third" self.repo.__next__()
last_snapshot = Snapshot(f"backup/data/snapshot-{snapshot_name}")
self.assertEqual(self.repo.curr_snapshot.path, last_snapshot.path) def test_subscript_pre_create(self, repo_snapshots):
self.assertEqual(len(self.repo), len(self.snapshots)) repo_snapshots.return_value = self.existing_snapshots
self.assertIsInstance(self.repo.curr_snapshot, Snapshot)
def test_iteration_pre_create(self): with self.assertRaises(IndexError):
result = list() self.repo[len(self.repo) + 1]
for snapshot in self.repo:
result.append(snapshot)
self.assertListEqual(result, self.snapshots) with self.assertRaises(IndexError):
self.repo[-1 * len(self.repo) - 1]
def test_subscript_pre_create(self): def test_curr_snapshot_pre_create(self, repo_snapshots):
self.assertEqual(self.repo[0], self.snapshots[0]) repo_snapshots.return_value = self.existing_snapshots
self.assertEqual(self.repo[-1], self.snapshots[-1])
self.assertEqual(self.repo[len(self.repo) - 1], self.snapshots[-1]) self.assertListEqual(self.repo.snapshots, self.existing_snapshots)
def test_curr_snapshot_post_create(self, repo_snapshots):
"""We want to combine all of the tests before the snapshot
creation into one snapshot so as to not repeat the creation
of a new snapshot for each test."""
repo_snapshots.return_value = self.existing_snapshots
def test_curr_snapshot_post_create(self):
snapshot_name = "new" snapshot_name = "new"
snapshot_path = PosixPath(f"backup/data/snapshot-{snapshot_name}")
self.repo.create_snapshot(snapshot_name) self.repo.create_snapshot(snapshot_name)
self.assertEqual(self.repo.curr_snapshot.path, snapshot_path)
self.assertEqual(len(self.repo), len(self.snapshots) + 1)
self.assertIsInstance(self.repo.curr_snapshot, Snapshot)
def test_iteration_post_create(self): self.new_snapshot_path_3 = self.repo.snapshot_dir / f"snapshot-{snapshot_name}"
result = list()
self.assertEqual(self.repo.curr_snapshot.path, self.new_snapshot_path_3)
# Test that len works correctly
self.assertEqual(len(self.repo), len(self.existing_snapshots))
# Test that iteration works correctly
result = []
for snapshot in self.repo: for snapshot in self.repo:
result.append(snapshot) result.append(snapshot)
self.assertListEqual(result, self.snapshots) self.assertListEqual(result, self.existing_snapshots)
def test_subscript_post_create(self): repo_snapshots.return_value = self.existing_snapshots
self.assertEqual(self.repo[0], self.snapshots[0])
self.assertEqual(self.repo[-1], self.repo.curr_snapshot)
def tearDown(self): # Test that subscripts work correctly
self.patched_snapshots.stop() self.assertEqual(self.repo[0].path, self.existing_snapshots[0].path)
self.assertEqual(self.repo[-1].path, self.existing_snapshots[-1].path)