Update tests for struct package
This commit is contained in:
parent
35167f28c6
commit
94c70b7b2e
@ -20,7 +20,36 @@ class TestHierarchyPaths(unittest.TestCase):
|
|||||||
def test_returns_correct_path(self, p):
|
def test_returns_correct_path(self, p):
|
||||||
self.assertEqual(Path(p), Hierarchy(p).path)
|
self.assertEqual(Path(p), Hierarchy(p).path)
|
||||||
|
|
||||||
@given(one_of(iterables(elements=none()), booleans()))
|
def test_raises_notimplemented_error(self):
|
||||||
def test_raises_value_error(self, p):
|
h = Hierarchy("backup")
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(NotImplementedError):
|
||||||
Hierarchy(p)
|
h.gen_metadata()
|
||||||
|
|
||||||
|
|
||||||
|
class TestHierarchyMetadata(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.patched_json = patch(f"{TESTING_MODULE}.json")
|
||||||
|
self.patched_path = patch.object(
|
||||||
|
Hierarchy, "metadata_path", new_callable=PropertyMock, spec_set=Path
|
||||||
|
)
|
||||||
|
|
||||||
|
self.mocked_path = self.patched_path.start()
|
||||||
|
self.mocked_json = self.patched_json.start()
|
||||||
|
|
||||||
|
@unittest.skip("Figure out how to mock file objects")
|
||||||
|
@given(text())
|
||||||
|
def test_write_metadata(self, data):
|
||||||
|
file_obj = StringIO()
|
||||||
|
self.mocked_path.return_value.open.return_value = file_obj
|
||||||
|
|
||||||
|
self.mocked_json.load.return_value = file_obj.getvalue()
|
||||||
|
|
||||||
|
h = Hierarchy("backup")
|
||||||
|
h.write_metadata(data)
|
||||||
|
read_data = h.read_metadata()
|
||||||
|
|
||||||
|
self.assertEqual(data, read_data)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.patched_json.stop()
|
||||||
|
self.patched_path.stop()
|
||||||
|
@ -43,27 +43,6 @@ class TestCreatePackageManager(unittest.TestCase):
|
|||||||
def test_create_with_valid_values(self, l):
|
def test_create_with_valid_values(self, l):
|
||||||
PackageManager("nothing", "nothing", l)
|
PackageManager("nothing", "nothing", l)
|
||||||
|
|
||||||
@given(one_of(none(), booleans(), integers(), dictionaries(text(), text())))
|
|
||||||
def test_incorrect_cmd_type(self, cmd):
|
|
||||||
with self.assertRaises(TypeError):
|
|
||||||
PackageManager("nothing", "nothing", cmd)
|
|
||||||
|
|
||||||
def test_empty_cmd(self):
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
PackageManager("nothing", "nothing", [])
|
|
||||||
PackageManager("nothing", "nothing", set())
|
|
||||||
PackageManager("nothing", "nothing", "")
|
|
||||||
|
|
||||||
@given(iterables(one_of(none(), booleans(), integers()), min_size=1))
|
|
||||||
def test_wrong_iterable_element_type(self, cmd):
|
|
||||||
with self.assertRaises(TypeError):
|
|
||||||
PackageManager("nothing", "nothing", cmd)
|
|
||||||
|
|
||||||
def test_empty_str_in_iterable(self):
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
PackageManager("nothing", "nothing", [""])
|
|
||||||
PackageManager("nothing", "nothing", ["pacman", ""])
|
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
self.patched_path.stop()
|
self.patched_path.stop()
|
||||||
self.patched_subprocess.stop()
|
self.patched_subprocess.stop()
|
||||||
|
@ -34,8 +34,12 @@ class TestRepositoryPreCreate(unittest.TestCase):
|
|||||||
self.patched_path = patch.object(
|
self.patched_path = patch.object(
|
||||||
Repository, "metadata_path", new_callable=PropertyMock
|
Repository, "metadata_path", new_callable=PropertyMock
|
||||||
)
|
)
|
||||||
self.patched_r_metadata = patch.object(Repository, "read_metadata")
|
self.patched_r_metadata = patch.object(
|
||||||
self.patched_w_metadata = patch.object(Repository, "write_metadata")
|
Repository, "read_metadata", spec_set=list
|
||||||
|
)
|
||||||
|
self.patched_w_metadata = patch.object(
|
||||||
|
Repository, "write_metadata", spec_set=list
|
||||||
|
)
|
||||||
self.patched_snapshot = patch(
|
self.patched_snapshot = patch(
|
||||||
f"{TESTING_PACKAGE}.repository.Snapshot", spec_set=Snapshot
|
f"{TESTING_PACKAGE}.repository.Snapshot", spec_set=Snapshot
|
||||||
)
|
)
|
||||||
@ -47,65 +51,58 @@ class TestRepositoryPreCreate(unittest.TestCase):
|
|||||||
|
|
||||||
self.mocked_path.return_value.exists.return_value = True
|
self.mocked_path.return_value.exists.return_value = True
|
||||||
|
|
||||||
@given(text())
|
@given(
|
||||||
def test_gen_snapshot_path(self, name):
|
lists(
|
||||||
self.mocked_r_metadata.return_value = {
|
text(
|
||||||
"snapshots": [],
|
alphabet=characters(blacklist_characters=UNWANTED_SNAPSHOT_CHARS),
|
||||||
"current_snapshot": None,
|
min_size=1,
|
||||||
}
|
),
|
||||||
|
unique=True,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
def test_empty(self, snapshots):
|
||||||
|
self.mocked_r_metadata.return_value = snapshots.copy()
|
||||||
repo = Repository("backup")
|
repo = Repository("backup")
|
||||||
|
|
||||||
if "/" not in name:
|
if not snapshots:
|
||||||
snapshot_path = repo.gen_snapshot_path(name)
|
|
||||||
self.assertEqual(snapshot_path, Path(f"backup/data/{name}"))
|
|
||||||
self.assertIsInstance(snapshot_path, Path)
|
|
||||||
else:
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
snapshot_path = repo.gen_snapshot_path(name)
|
|
||||||
|
|
||||||
@given(lists(builds(Snapshot, text()), unique=True))
|
|
||||||
def test_empty(self, l):
|
|
||||||
self.mocked_r_metadata.return_value = {
|
|
||||||
"snapshots": l.copy(),
|
|
||||||
"current_snapshot": l[-1] if l else None,
|
|
||||||
}
|
|
||||||
repo = Repository("backup")
|
|
||||||
|
|
||||||
if l == []:
|
|
||||||
self.assertTrue(repo.empty)
|
self.assertTrue(repo.empty)
|
||||||
else:
|
else:
|
||||||
self.assertFalse(repo.empty)
|
self.assertFalse(repo.empty)
|
||||||
|
|
||||||
@given(lists(builds(Snapshot, text()), unique=True))
|
@given(
|
||||||
def test_len(self, l):
|
lists(
|
||||||
self.mocked_r_metadata.return_value = {
|
text(
|
||||||
"snapshots": l.copy(),
|
alphabet=characters(blacklist_characters=UNWANTED_SNAPSHOT_CHARS),
|
||||||
"current_snapshot": l[-1] if l else None,
|
min_size=1,
|
||||||
}
|
),
|
||||||
|
unique=True,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
def test_len(self, snapshots):
|
||||||
|
self.mocked_r_metadata.return_value = snapshots.copy()
|
||||||
repo = Repository("backup")
|
repo = Repository("backup")
|
||||||
|
|
||||||
self.assertEqual(len(repo.snapshots), len(l))
|
self.assertEqual(len(repo.snapshots), len(snapshots))
|
||||||
|
|
||||||
@given(lists(builds(Snapshot, text()), unique=True))
|
@given(text(min_size=1))
|
||||||
def test_current_snapshot(self, l):
|
def test_contains(self, name):
|
||||||
self.mocked_r_metadata.return_value = {
|
self.mocked_r_metadata = []
|
||||||
"snapshots": l.copy(),
|
|
||||||
"current_snapshot": l[-1] if l else None,
|
|
||||||
}
|
|
||||||
|
|
||||||
if l == []:
|
|
||||||
self.mocked_r_metadata.return_value["current_snapshot"] = None
|
|
||||||
else:
|
|
||||||
self.mocked_r_metadata.return_value["current_snapshot"] = l[-1]
|
|
||||||
repo = Repository("backup")
|
repo = Repository("backup")
|
||||||
|
|
||||||
if l == []:
|
self.assertFalse(name in repo)
|
||||||
self.assertIsNone(repo.current_snapshot)
|
|
||||||
|
@given(text())
|
||||||
|
def test_valid_name(self, name):
|
||||||
|
self.mocked_r_metadata.return_value = []
|
||||||
|
|
||||||
|
if not name or "/" in name:
|
||||||
|
self.assertFalse(Repository.is_valid_snapshot_name(name))
|
||||||
else:
|
else:
|
||||||
self.assertIsNotNone(repo.current_snapshot)
|
self.assertTrue(Repository.is_valid_snapshot_name(name))
|
||||||
self.assertIsInstance(repo.current_snapshot, Snapshot)
|
|
||||||
|
def test_snapshots_returns_empty_list(self):
|
||||||
|
r = Repository("backup")
|
||||||
|
self.assertListEqual(r.snapshots, [])
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
self.patched_path.stop()
|
self.patched_path.stop()
|
||||||
@ -132,8 +129,12 @@ class TestRepositoryPostCreate(unittest.TestCase):
|
|||||||
self.patched_path = patch.object(
|
self.patched_path = patch.object(
|
||||||
Repository, "metadata_path", new_callable=PropertyMock
|
Repository, "metadata_path", new_callable=PropertyMock
|
||||||
)
|
)
|
||||||
self.patched_r_metadata = patch.object(Repository, "read_metadata")
|
self.patched_r_metadata = patch.object(
|
||||||
self.patched_w_metadata = patch.object(Repository, "write_metadata")
|
Repository, "read_metadata", spec_set=list
|
||||||
|
)
|
||||||
|
self.patched_w_metadata = patch.object(
|
||||||
|
Repository, "write_metadata", spec_set=list
|
||||||
|
)
|
||||||
self.patched_snapshot = patch(
|
self.patched_snapshot = patch(
|
||||||
f"{TESTING_PACKAGE}.repository.Snapshot", spec_set=Snapshot
|
f"{TESTING_PACKAGE}.repository.Snapshot", spec_set=Snapshot
|
||||||
)
|
)
|
||||||
@ -143,48 +144,53 @@ class TestRepositoryPostCreate(unittest.TestCase):
|
|||||||
self.mocked_w_metadata = self.patched_w_metadata.start()
|
self.mocked_w_metadata = self.patched_w_metadata.start()
|
||||||
self.mocked_snapshot = self.patched_snapshot.start()
|
self.mocked_snapshot = self.patched_snapshot.start()
|
||||||
|
|
||||||
@given(lists(builds(Snapshot, text()), unique=True))
|
@given(
|
||||||
def test_empty(self, l):
|
lists(
|
||||||
self.mocked_r_metadata.return_value = {
|
text(
|
||||||
"snapshots": l.copy(),
|
alphabet=characters(blacklist_characters=UNWANTED_SNAPSHOT_CHARS),
|
||||||
"current_snapshot": l[-1] if l else None,
|
min_size=1,
|
||||||
}
|
),
|
||||||
|
unique=True,
|
||||||
if l == []:
|
)
|
||||||
self.mocked_r_metadata.return_value["current_snapshot"] = None
|
)
|
||||||
else:
|
def test_empty(self, snapshots):
|
||||||
self.mocked_r_metadata.return_value["current_snapshot"] = l[-1]
|
self.mocked_r_metadata.return_value = snapshots.copy()
|
||||||
repo = Repository("backup")
|
repo = Repository("backup")
|
||||||
|
|
||||||
repo.create_snapshot()
|
repo.create_snapshot()
|
||||||
|
|
||||||
self.assertFalse(repo.empty)
|
self.assertFalse(repo.empty)
|
||||||
|
|
||||||
@given(lists(builds(Snapshot, text()), unique=True))
|
@given(
|
||||||
def test_len(self, l):
|
lists(
|
||||||
self.mocked_r_metadata.return_value = {
|
text(
|
||||||
"snapshots": l.copy(),
|
alphabet=characters(blacklist_characters=UNWANTED_SNAPSHOT_CHARS),
|
||||||
"current_snapshot": l[-1] if l else None,
|
min_size=1,
|
||||||
}
|
),
|
||||||
|
unique=True,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
def test_len(self, snapshots):
|
||||||
|
self.mocked_r_metadata.return_value = snapshots.copy()
|
||||||
repo = Repository("backup")
|
repo = Repository("backup")
|
||||||
|
|
||||||
repo.create_snapshot()
|
repo.create_snapshot()
|
||||||
|
|
||||||
self.assertEqual(len(repo), len(l) + 1)
|
self.assertEqual(len(repo), len(snapshots) + 1)
|
||||||
self.assertEqual(len(repo.snapshots), len(l) + 1)
|
self.assertEqual(len(repo.snapshots), len(snapshots) + 1)
|
||||||
|
|
||||||
@given(lists(builds(Snapshot, text()), unique=True))
|
@given(
|
||||||
def test_current_snapshot(self, l):
|
text(
|
||||||
self.mocked_r_metadata.return_value = {
|
alphabet=characters(blacklist_characters=UNWANTED_SNAPSHOT_CHARS),
|
||||||
"snapshots": l.copy(),
|
min_size=1,
|
||||||
"current_snapshot": l[-1] if l else None,
|
)
|
||||||
}
|
)
|
||||||
|
def test_contains(self, name):
|
||||||
|
self.mocked_path.return_value.exists.return_value = False
|
||||||
repo = Repository("backup")
|
repo = Repository("backup")
|
||||||
|
|
||||||
new_snapshot = repo.create_snapshot()
|
repo.create_snapshot(name)
|
||||||
|
self.assertTrue(name in repo)
|
||||||
self.assertIs(new_snapshot, repo.current_snapshot)
|
|
||||||
self.assertIsInstance(new_snapshot, Snapshot)
|
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
self.patched_path.stop()
|
self.patched_path.stop()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user