Update tests for the new metadata scheme

This commit is contained in:
Eric Torres 2019-03-29 15:38:37 -07:00
parent bf7416a42f
commit 874f94c5fc

View File

@ -1,12 +1,12 @@
import doctest # import doctest
import unittest import unittest
from hypothesis import given from hypothesis import given
from hypothesis.strategies import builds, lists, text from hypothesis.strategies import builds, lists, text
from pathlib import Path, PosixPath from pathlib import Path
from rbackup.hierarchy.repository import Repository from rbackup.hierarchy.repository import Repository
from rbackup.hierarchy.snapshot import Snapshot from rbackup.hierarchy.snapshot import Snapshot
from unittest.mock import patch, MagicMock, PropertyMock from unittest.mock import PropertyMock, patch
# ========== Constants ========== # ========== Constants ==========
TESTING_PACKAGE = "rbackup.hierarchy" TESTING_PACKAGE = "rbackup.hierarchy"
@ -15,31 +15,36 @@ SS_MODULE = f"{TESTING_PACKAGE}.snapshot"
# ========== Functions ========== # ========== Functions ==========
def load_tests(loader, tests, ignore): # @unittest.skip("Repositories create files, this should be mocked out")
tests.addTests(doctest.DocTestSuite(REPO_MODULE)) # def load_tests(loader, tests, ignore):
return tests # tests.addTests(doctest.DocTestSuite(REPO_MODULE))
# return tests
# ========== Integration Tests ========== # ========== Integration Tests ==========
class TestRepositoryPreCreate(unittest.TestCase): class TestRepositoryPreCreate(unittest.TestCase):
"""Test properties of the Repository before running create_snapshot().""" """Test properties of the Repository before running create_snapshot()."""
def setUp(self): def setUp(self):
self.patched_path = patch.object(
Repository, "metadata_path", new_callable=PropertyMock
)
self.patched_snapshot = patch( self.patched_snapshot = patch(
f"{TESTING_PACKAGE}.repository.Snapshot", spec_set=Snapshot f"{TESTING_PACKAGE}.repository.Snapshot", spec_set=Snapshot
) )
self.patched_snapshots = patch.object(
Repository, "snapshots", new_callable=PropertyMock
)
self.patched_pickle = patch(f"{TESTING_PACKAGE}.repository.pickle") self.patched_pickle = patch(f"{TESTING_PACKAGE}.repository.pickle")
self.repo_snapshots = self.patched_snapshots.start() self.mocked_path = self.patched_path.start()
self.mocked_snapshot = self.patched_snapshot.start() self.mocked_snapshot = self.patched_snapshot.start()
self.mocked_pickle = self.patched_pickle.start() self.mocked_pickle = self.patched_pickle.start()
@given(lists(builds(Snapshot, text()), unique=True)) @given(lists(builds(Snapshot, text()), unique=True))
def test_empty(self, l): def test_empty(self, l):
repo = Repository('backup') self.mocked_pickle.load.return_value = {
self.repo_snapshots.return_value = l "snapshots": l.copy(),
"current_snapshot": l[-1] if l else None,
}
repo = Repository("backup")
if l == []: if l == []:
self.assertTrue(repo.empty) self.assertTrue(repo.empty)
@ -48,15 +53,27 @@ class TestRepositoryPreCreate(unittest.TestCase):
@given(lists(builds(Snapshot, text()), unique=True)) @given(lists(builds(Snapshot, text()), unique=True))
def test_len(self, l): def test_len(self, l):
repo = Repository('backup') self.mocked_pickle.load.return_value = {
self.repo_snapshots.return_value = l "snapshots": l.copy(),
"current_snapshot": l[-1] if l else None,
}
repo = Repository("backup")
self.assertEqual(len(repo.snapshots), len(l)) self.assertEqual(len(repo.snapshots), len(l))
@given(lists(builds(Snapshot, text()), unique=True)) @given(lists(builds(Snapshot, text()), unique=True))
def test_current_snapshot(self, l): def test_current_snapshot(self, l):
self.repo_snapshots.return_value = l self.mocked_pickle.load.return_value = {
repo = Repository('backup') "snapshots": l.copy(),
"current_snapshot": l[-1] if l else None,
}
if l == []:
self.mocked_pickle.load.return_value["current_snapshot"] = None
else:
self.mocked_pickle.load.return_value["current_snapshot"] = l[-1]
repo = Repository("backup")
if l == []: if l == []:
self.assertIsNone(repo.current_snapshot) self.assertIsNone(repo.current_snapshot)
@ -65,30 +82,39 @@ class TestRepositoryPreCreate(unittest.TestCase):
self.assertIsInstance(repo.current_snapshot, Snapshot) self.assertIsInstance(repo.current_snapshot, Snapshot)
def tearDown(self): def tearDown(self):
self.patched_snapshots.stop() self.patched_path.stop()
self.patched_snapshot.stop() self.patched_snapshot.stop()
self.patched_pickle.stop() self.patched_pickle.stop()
class TestRepositoryPostCreate(unittest.TestCase): class TestRepositoryPostCreate(unittest.TestCase):
"""Test properties of the Repository before running create_snapshot().""" """Test properties of the Repository before running create_snapshot()."""
def setUp(self): def setUp(self):
self.patched_path = patch.object(
Repository, "metadata_path", new_callable=PropertyMock
)
self.patched_snapshot = patch( self.patched_snapshot = patch(
f"{TESTING_PACKAGE}.repository.Snapshot", spec_set=Snapshot f"{TESTING_PACKAGE}.repository.Snapshot", spec_set=Snapshot
) )
self.patched_snapshots = patch.object(
Repository, "snapshots", new_callable=PropertyMock
)
self.patched_pickle = patch(f"{TESTING_PACKAGE}.repository.pickle") self.patched_pickle = patch(f"{TESTING_PACKAGE}.repository.pickle")
self.repo_snapshots = self.patched_snapshots.start() self.mocked_path = self.patched_path.start()
self.mocked_snapshot = self.patched_snapshot.start() self.mocked_snapshot = self.patched_snapshot.start()
self.mocked_pickle = self.patched_pickle.start() self.mocked_pickle = self.patched_pickle.start()
@given(lists(builds(Snapshot, text()), unique=True)) @given(lists(builds(Snapshot, text()), unique=True))
def test_empty(self, l): def test_empty(self, l):
self.repo_snapshots.return_value = l.copy() self.mocked_pickle.load.return_value = {
repo = Repository('backup') "snapshots": l.copy(),
"current_snapshot": l[-1] if l else None,
}
if l == []:
self.mocked_pickle.load.return_value["current_snapshot"] = None
else:
self.mocked_pickle.load.return_value["current_snapshot"] = l[-1]
repo = Repository("backup")
repo.create_snapshot() repo.create_snapshot()
@ -96,17 +122,24 @@ class TestRepositoryPostCreate(unittest.TestCase):
@given(lists(builds(Snapshot, text()), unique=True)) @given(lists(builds(Snapshot, text()), unique=True))
def test_len(self, l): def test_len(self, l):
self.repo_snapshots.return_value = l.copy() self.mocked_pickle.load.return_value = {
repo = Repository('backup') "snapshots": l.copy(),
"current_snapshot": l[-1] if l else None,
}
repo = Repository("backup")
repo.create_snapshot() repo.create_snapshot()
self.assertEqual(len(repo), len(l) + 1)
self.assertEqual(len(repo.snapshots), len(l) + 1) self.assertEqual(len(repo.snapshots), len(l) + 1)
@given(lists(builds(Snapshot, text()), unique=True)) @given(lists(builds(Snapshot, text()), unique=True))
def test_current_snapshot(self, l): def test_current_snapshot(self, l):
self.repo_snapshots.return_value = l.copy() self.mocked_pickle.load.return_value = {
repo = Repository('backup') "snapshots": l.copy(),
"current_snapshot": l[-1] if l else None,
}
repo = Repository("backup")
new_snapshot = repo.create_snapshot() new_snapshot = repo.create_snapshot()
@ -114,6 +147,6 @@ class TestRepositoryPostCreate(unittest.TestCase):
self.assertIsInstance(new_snapshot, Snapshot) self.assertIsInstance(new_snapshot, Snapshot)
def tearDown(self): def tearDown(self):
self.patched_snapshots.stop() self.patched_path.stop()
self.patched_snapshot.stop() self.patched_snapshot.stop()
self.patched_pickle.stop() self.patched_pickle.stop()