Update tests for struct package

This commit is contained in:
Eric Torres 2019-04-10 18:17:21 -07:00
parent 35167f28c6
commit 94c70b7b2e
3 changed files with 119 additions and 105 deletions

View File

@ -20,7 +20,36 @@ class TestHierarchyPaths(unittest.TestCase):
def test_returns_correct_path(self, p):
self.assertEqual(Path(p), Hierarchy(p).path)
@given(one_of(iterables(elements=none()), booleans()))
def test_raises_value_error(self, p):
with self.assertRaises(TypeError):
Hierarchy(p)
def test_raises_notimplemented_error(self):
h = Hierarchy("backup")
with self.assertRaises(NotImplementedError):
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()

View File

@ -43,27 +43,6 @@ class TestCreatePackageManager(unittest.TestCase):
def test_create_with_valid_values(self, 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):
self.patched_path.stop()
self.patched_subprocess.stop()

View File

@ -34,8 +34,12 @@ class TestRepositoryPreCreate(unittest.TestCase):
self.patched_path = patch.object(
Repository, "metadata_path", new_callable=PropertyMock
)
self.patched_r_metadata = patch.object(Repository, "read_metadata")
self.patched_w_metadata = patch.object(Repository, "write_metadata")
self.patched_r_metadata = patch.object(
Repository, "read_metadata", spec_set=list
)
self.patched_w_metadata = patch.object(
Repository, "write_metadata", spec_set=list
)
self.patched_snapshot = patch(
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
@given(text())
def test_gen_snapshot_path(self, name):
self.mocked_r_metadata.return_value = {
"snapshots": [],
"current_snapshot": None,
}
@given(
lists(
text(
alphabet=characters(blacklist_characters=UNWANTED_SNAPSHOT_CHARS),
min_size=1,
),
unique=True,
)
)
def test_empty(self, snapshots):
self.mocked_r_metadata.return_value = snapshots.copy()
repo = Repository("backup")
if "/" not in name:
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 == []:
if not snapshots:
self.assertTrue(repo.empty)
else:
self.assertFalse(repo.empty)
@given(lists(builds(Snapshot, text()), unique=True))
def test_len(self, l):
self.mocked_r_metadata.return_value = {
"snapshots": l.copy(),
"current_snapshot": l[-1] if l else None,
}
@given(
lists(
text(
alphabet=characters(blacklist_characters=UNWANTED_SNAPSHOT_CHARS),
min_size=1,
),
unique=True,
)
)
def test_len(self, snapshots):
self.mocked_r_metadata.return_value = snapshots.copy()
repo = Repository("backup")
self.assertEqual(len(repo.snapshots), len(l))
self.assertEqual(len(repo.snapshots), len(snapshots))
@given(lists(builds(Snapshot, text()), unique=True))
def test_current_snapshot(self, l):
self.mocked_r_metadata.return_value = {
"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]
@given(text(min_size=1))
def test_contains(self, name):
self.mocked_r_metadata = []
repo = Repository("backup")
if l == []:
self.assertIsNone(repo.current_snapshot)
self.assertFalse(name in repo)
@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:
self.assertIsNotNone(repo.current_snapshot)
self.assertIsInstance(repo.current_snapshot, Snapshot)
self.assertTrue(Repository.is_valid_snapshot_name(name))
def test_snapshots_returns_empty_list(self):
r = Repository("backup")
self.assertListEqual(r.snapshots, [])
def tearDown(self):
self.patched_path.stop()
@ -132,8 +129,12 @@ class TestRepositoryPostCreate(unittest.TestCase):
self.patched_path = patch.object(
Repository, "metadata_path", new_callable=PropertyMock
)
self.patched_r_metadata = patch.object(Repository, "read_metadata")
self.patched_w_metadata = patch.object(Repository, "write_metadata")
self.patched_r_metadata = patch.object(
Repository, "read_metadata", spec_set=list
)
self.patched_w_metadata = patch.object(
Repository, "write_metadata", spec_set=list
)
self.patched_snapshot = patch(
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_snapshot = self.patched_snapshot.start()
@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,
}
if l == []:
self.mocked_r_metadata.return_value["current_snapshot"] = None
else:
self.mocked_r_metadata.return_value["current_snapshot"] = l[-1]
@given(
lists(
text(
alphabet=characters(blacklist_characters=UNWANTED_SNAPSHOT_CHARS),
min_size=1,
),
unique=True,
)
)
def test_empty(self, snapshots):
self.mocked_r_metadata.return_value = snapshots.copy()
repo = Repository("backup")
repo.create_snapshot()
self.assertFalse(repo.empty)
@given(lists(builds(Snapshot, text()), unique=True))
def test_len(self, l):
self.mocked_r_metadata.return_value = {
"snapshots": l.copy(),
"current_snapshot": l[-1] if l else None,
}
@given(
lists(
text(
alphabet=characters(blacklist_characters=UNWANTED_SNAPSHOT_CHARS),
min_size=1,
),
unique=True,
)
)
def test_len(self, snapshots):
self.mocked_r_metadata.return_value = snapshots.copy()
repo = Repository("backup")
repo.create_snapshot()
self.assertEqual(len(repo), len(l) + 1)
self.assertEqual(len(repo.snapshots), len(l) + 1)
self.assertEqual(len(repo), len(snapshots) + 1)
self.assertEqual(len(repo.snapshots), len(snapshots) + 1)
@given(lists(builds(Snapshot, text()), unique=True))
def test_current_snapshot(self, l):
self.mocked_r_metadata.return_value = {
"snapshots": l.copy(),
"current_snapshot": l[-1] if l else None,
}
@given(
text(
alphabet=characters(blacklist_characters=UNWANTED_SNAPSHOT_CHARS),
min_size=1,
)
)
def test_contains(self, name):
self.mocked_path.return_value.exists.return_value = False
repo = Repository("backup")
new_snapshot = repo.create_snapshot()
self.assertIs(new_snapshot, repo.current_snapshot)
self.assertIsInstance(new_snapshot, Snapshot)
repo.create_snapshot(name)
self.assertTrue(name in repo)
def tearDown(self):
self.patched_path.stop()