2019-03-16 07:46:03 -07:00
|
|
|
import doctest
|
|
|
|
import unittest
|
|
|
|
|
2019-03-17 18:21:04 -07:00
|
|
|
from pathlib import PosixPath
|
2019-03-16 07:46:03 -07:00
|
|
|
from rbackup.hierarchy.repository import Repository
|
|
|
|
from rbackup.hierarchy.snapshot import Snapshot
|
2019-03-17 18:21:04 -07:00
|
|
|
from unittest.mock import patch, PropertyMock
|
2019-03-16 07:46:03 -07:00
|
|
|
|
|
|
|
# ========== Constants ==========
|
|
|
|
TESTING_MODULE = "rbackup.hierarchy.repository"
|
|
|
|
|
|
|
|
|
|
|
|
# ========== Functions ==========
|
|
|
|
def load_tests(loader, tests, ignore):
|
|
|
|
tests.addTests(doctest.DocTestSuite(TESTING_MODULE))
|
|
|
|
return tests
|
|
|
|
|
|
|
|
|
|
|
|
# ========== Integration Tests ==========
|
2019-03-20 00:23:25 -07:00
|
|
|
@patch.object(Repository, "snapshots", new_callable=PropertyMock)
|
2019-03-18 10:36:30 -07:00
|
|
|
class TestEmptyRepository(unittest.TestCase):
|
2019-03-20 00:23:25 -07:00
|
|
|
"""Test a repository that has no snapshots."""
|
2019-03-16 07:46:03 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
def setUp(self):
|
2019-03-16 07:46:03 -07:00
|
|
|
self.repo_basepath = "backup"
|
|
|
|
self.repo = Repository(self.repo_basepath)
|
2019-03-20 00:23:25 -07:00
|
|
|
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 = []
|
2019-03-16 07:46:03 -07:00
|
|
|
|
2019-03-18 10:36:30 -07:00
|
|
|
self.assertEqual(len(self.repo), 0)
|
2019-03-20 00:23:25 -07:00
|
|
|
self.assertEqual(len(self.repo), len(repo_snapshots.return_value))
|
2019-03-16 07:46:03 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
def test_iteration_pre_create(self, repo_snapshots):
|
|
|
|
repo_snapshots.return_value = []
|
|
|
|
|
|
|
|
with self.assertRaises(StopIteration):
|
|
|
|
self.repo.__next__()
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
def test_subscript_pre_create(self, repo_snapshots):
|
|
|
|
repo_snapshots.return_value = []
|
2019-03-18 10:36:30 -07:00
|
|
|
|
|
|
|
with self.assertRaises(IndexError):
|
2019-03-20 00:23:25 -07:00
|
|
|
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 = []
|
2019-03-16 07:46:03 -07:00
|
|
|
|
|
|
|
snapshot_name = "new"
|
|
|
|
|
|
|
|
self.repo.create_snapshot(snapshot_name)
|
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
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 = []
|
2019-03-18 10:36:30 -07:00
|
|
|
for snapshot in self.repo:
|
2019-03-20 00:23:25 -07:00
|
|
|
result.append(snapshot)
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
self.assertListEqual(result, [self.created_snapshot])
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
def test_subscript_post_create(self, repo_snapshots):
|
|
|
|
repo_snapshots.return_value = [self.created_snapshot]
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
self.assertEqual(self.repo[0].path, self.new_snapshot_path)
|
2019-03-17 18:21:04 -07:00
|
|
|
|
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
@patch.object(Repository, "snapshots", new_callable=PropertyMock)
|
2019-03-18 10:36:30 -07:00
|
|
|
class TestPopulatedRepository(unittest.TestCase):
|
2019-03-20 00:23:25 -07:00
|
|
|
"""Test a repository that has no snapshots."""
|
|
|
|
|
2019-03-17 18:21:04 -07:00
|
|
|
def setUp(self):
|
2019-03-20 00:23:25 -07:00
|
|
|
self.repo_basepath = "backup"
|
|
|
|
self.repo = Repository(self.repo_basepath)
|
|
|
|
|
|
|
|
self.new_snapshot_path_1 = self.repo.snapshot_dir / "snapshot-one"
|
|
|
|
self.new_snapshot_path_2 = self.repo.snapshot_dir / "snapshot-two"
|
|
|
|
|
|
|
|
self.existing_snapshots = [
|
|
|
|
Snapshot(self.new_snapshot_path_1),
|
|
|
|
Snapshot(self.new_snapshot_path_2),
|
2019-03-18 10:36:30 -07:00
|
|
|
]
|
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
def test_len_pre_create(self, repo_snapshots):
|
|
|
|
repo_snapshots.return_value = self.existing_snapshots
|
|
|
|
self.assertEqual(len(self.repo), len(repo_snapshots.return_value))
|
2019-03-17 18:21:04 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
def test_iteration_pre_create(self, repo_snapshots):
|
|
|
|
repo_snapshots.return_value = self.existing_snapshots
|
2019-03-17 18:21:04 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
# Exhaust the iterator first
|
|
|
|
for iteration in range(0, len(self.existing_snapshots)):
|
|
|
|
self.repo.__next__()
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
with self.assertRaises(StopIteration):
|
|
|
|
self.repo.__next__()
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
def test_subscript_pre_create(self, repo_snapshots):
|
|
|
|
repo_snapshots.return_value = self.existing_snapshots
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
self.repo[len(self.repo) + 1]
|
|
|
|
|
|
|
|
with self.assertRaises(IndexError):
|
|
|
|
self.repo[-1 * len(self.repo) - 1]
|
|
|
|
|
|
|
|
def test_curr_snapshot_pre_create(self, repo_snapshots):
|
|
|
|
repo_snapshots.return_value = self.existing_snapshots
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
self.assertListEqual(self.repo.snapshots, self.existing_snapshots)
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
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
|
2019-03-17 18:21:04 -07:00
|
|
|
|
|
|
|
snapshot_name = "new"
|
|
|
|
|
|
|
|
self.repo.create_snapshot(snapshot_name)
|
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
self.new_snapshot_path_3 = self.repo.snapshot_dir / f"snapshot-{snapshot_name}"
|
|
|
|
|
|
|
|
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 = []
|
2019-03-18 10:36:30 -07:00
|
|
|
for snapshot in self.repo:
|
|
|
|
result.append(snapshot)
|
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
self.assertListEqual(result, self.existing_snapshots)
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
repo_snapshots.return_value = self.existing_snapshots
|
2019-03-18 10:36:30 -07:00
|
|
|
|
2019-03-20 00:23:25 -07:00
|
|
|
# Test that subscripts work correctly
|
|
|
|
self.assertEqual(self.repo[0].path, self.existing_snapshots[0].path)
|
|
|
|
self.assertEqual(self.repo[-1].path, self.existing_snapshots[-1].path)
|