Do not ship test suite with actual project
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
28
tests/test_hierarchy.py
Normal file
28
tests/test_hierarchy.py
Normal file
@ -0,0 +1,28 @@
|
||||
import doctest
|
||||
import unittest
|
||||
|
||||
from hypothesis import given
|
||||
from hypothesis.strategies import booleans, characters, iterables, one_of, none, text
|
||||
from pathlib import Path
|
||||
from rbackup.hierarchy.hierarchy import Hierarchy
|
||||
|
||||
# ========== Constants ==========
|
||||
TESTING_MODULE = "rbackup.hierarchy.hierarchy"
|
||||
|
||||
|
||||
# ========== Functions ==========
|
||||
def load_tests(loader, tests, ignore):
|
||||
tests.addTests(doctest.DocTestSuite(TESTING_MODULE))
|
||||
return tests
|
||||
|
||||
|
||||
# ========== Tests ==========
|
||||
class TestHierarchyPaths(unittest.TestCase):
|
||||
@given(one_of(text(), characters()))
|
||||
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)
|
148
tests/test_packagemanager.py
Normal file
148
tests/test_packagemanager.py
Normal file
@ -0,0 +1,148 @@
|
||||
"""
|
||||
.. author:: Eric Torres
|
||||
:synopsis: Unit tests for the PackageManager module.
|
||||
"""
|
||||
import doctest
|
||||
import subprocess
|
||||
import unittest
|
||||
|
||||
from hypothesis import given
|
||||
from hypothesis.strategies import (
|
||||
booleans,
|
||||
dictionaries,
|
||||
from_regex,
|
||||
integers,
|
||||
iterables,
|
||||
one_of,
|
||||
none,
|
||||
text,
|
||||
)
|
||||
from pathlib import Path
|
||||
from rbackup.package_managers.packagemanager import PackageManager
|
||||
from unittest.mock import patch
|
||||
|
||||
# ========== Constants ==========
|
||||
TESTING_MODULE = "rbackup.package_managers.packagemanager"
|
||||
|
||||
|
||||
# ========== Functions ==========
|
||||
def load_tests(loader, tests, ignore):
|
||||
tests.addTests(doctest.DocTestSuite(TESTING_MODULE))
|
||||
return tests
|
||||
|
||||
|
||||
# ========== Test Cases ==========
|
||||
class TestCreatePackageManager(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.patched_path = patch(f"{TESTING_MODULE}.Path", autospec=Path)
|
||||
self.patched_subprocess = patch(f"{TESTING_MODULE}.subprocess.run")
|
||||
self.patched_tarfile = patch(f"{TESTING_MODULE}.tarfile.open")
|
||||
self.patched_tempfile = patch(f"{TESTING_MODULE}.NamedTemporaryFile")
|
||||
|
||||
self.mocked_path = self.patched_path.start()
|
||||
self.mocked_run = self.patched_subprocess.start()
|
||||
self.mocked_tarfile = self.patched_tarfile.start()
|
||||
self.mocked_tempfile = self.patched_tempfile.start()
|
||||
|
||||
self.cachedir = "/var/cache/pacman/"
|
||||
self.db_path = "/var/lib/pacman"
|
||||
self.pkglist_cmd = ["pacman", "-Qqe"]
|
||||
self.p = PackageManager(self.cachedir, self.db_path, self.pkglist_cmd)
|
||||
|
||||
@given(one_of(text(min_size=1), iterables(text(min_size=1), min_size=1)))
|
||||
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()
|
||||
self.patched_tarfile.stop()
|
||||
self.patched_tempfile.stop()
|
||||
|
||||
|
||||
class TestPackageManagerMethods(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.patched_path = patch(f"{TESTING_MODULE}.Path", autospec=Path)
|
||||
self.patched_subprocess = patch(f"{TESTING_MODULE}.subprocess.run")
|
||||
self.patched_tarfile = patch(f"{TESTING_MODULE}.tarfile.open")
|
||||
self.patched_tempfile = patch(f"{TESTING_MODULE}.NamedTemporaryFile")
|
||||
|
||||
self.mocked_path = self.patched_path.start()
|
||||
self.mocked_run = self.patched_subprocess.start()
|
||||
self.mocked_tarfile = self.patched_tarfile.start()
|
||||
self.mocked_tempfile = self.patched_tempfile.start()
|
||||
|
||||
self.cachedir = "/var/cache/pacman/"
|
||||
self.db_path = "/var/lib/pacman"
|
||||
self.pkglist_cmd = ["pacman", "-Qqe"]
|
||||
self.p = PackageManager(self.cachedir, self.db_path, self.pkglist_cmd)
|
||||
|
||||
def test_pkglist(self):
|
||||
self.mocked_run.return_value.stdout = "packages"
|
||||
self.mocked_tempfile.return_value.name = "tempfile"
|
||||
|
||||
pkglist = self.p.gen_pkglist()
|
||||
|
||||
self.mocked_tempfile.return_value.__enter__.return_value.write.assert_called_with(
|
||||
"packages"
|
||||
)
|
||||
self.assertIsInstance(pkglist, Path)
|
||||
|
||||
def test_pkglist_subprocess_error(self):
|
||||
self.mocked_run.side_effect = subprocess.CalledProcessError(1, self.pkglist_cmd)
|
||||
|
||||
self.p.gen_pkglist()
|
||||
self.mocked_tempfile.assert_not_called()
|
||||
|
||||
def test_db_archive(self):
|
||||
p = Path("tmpfile")
|
||||
self.mocked_path.return_value = p
|
||||
|
||||
archive = self.p.gen_db_archive()
|
||||
|
||||
self.assertIsInstance(archive, Path)
|
||||
self.mocked_tempfile.assert_called_with(delete=False, suffix=".tar")
|
||||
self.mocked_tarfile.assert_called_with(name=p, mode="w")
|
||||
|
||||
def test_db_archive_compress_mode(self):
|
||||
p = Path("tmpfile")
|
||||
compress = "xz"
|
||||
self.mocked_path.return_value = p
|
||||
|
||||
archive = self.p.gen_db_archive(compress)
|
||||
|
||||
self.assertIsInstance(archive, Path)
|
||||
self.mocked_tempfile.assert_called_with(delete=False, suffix=".tar.xz")
|
||||
self.mocked_tarfile.assert_called_with(name=p, mode="w:xz")
|
||||
|
||||
@given(from_regex(r"(?!gz|bz2|lzma|xz)"))
|
||||
def test_db_archive_invalid_compress_mode(self, invalid_mode):
|
||||
with self.assertRaises(ValueError):
|
||||
self.p.gen_db_archive(invalid_mode)
|
||||
|
||||
def tearDown(self):
|
||||
self.patched_path.stop()
|
||||
self.patched_subprocess.stop()
|
||||
self.patched_tarfile.stop()
|
||||
self.patched_tempfile.stop()
|
177
tests/test_repository.py
Normal file
177
tests/test_repository.py
Normal file
@ -0,0 +1,177 @@
|
||||
# import doctest
|
||||
import unittest
|
||||
|
||||
from hypothesis import given
|
||||
from hypothesis.strategies import builds, lists, text
|
||||
from pathlib import Path
|
||||
from rbackup.hierarchy.repository import Repository
|
||||
from rbackup.hierarchy.snapshot import Snapshot
|
||||
from unittest.mock import PropertyMock, patch
|
||||
|
||||
# ========== Constants ==========
|
||||
TESTING_PACKAGE = "rbackup.hierarchy"
|
||||
REPO_MODULE = f"{TESTING_PACKAGE}.repository"
|
||||
SS_MODULE = f"{TESTING_PACKAGE}.snapshot"
|
||||
|
||||
|
||||
# ========== Functions ==========
|
||||
# @unittest.skip("Repositories create files, this should be mocked out")
|
||||
# def load_tests(loader, tests, ignore):
|
||||
# tests.addTests(doctest.DocTestSuite(REPO_MODULE))
|
||||
# return tests
|
||||
|
||||
|
||||
# ========== Integration Tests ==========
|
||||
class TestRepositoryPreCreate(unittest.TestCase):
|
||||
"""Test properties of the Repository before running create_snapshot()."""
|
||||
|
||||
def setUp(self):
|
||||
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_snapshot = patch(
|
||||
f"{TESTING_PACKAGE}.repository.Snapshot", spec_set=Snapshot
|
||||
)
|
||||
|
||||
self.mocked_r_metadata = self.patched_r_metadata.start()
|
||||
self.mocked_w_metadata = self.patched_w_metadata.start()
|
||||
self.mocked_path = self.patched_path.start()
|
||||
self.mocked_snapshot = self.patched_snapshot.start()
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
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 == []:
|
||||
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,
|
||||
}
|
||||
|
||||
repo = Repository("backup")
|
||||
|
||||
self.assertEqual(len(repo.snapshots), len(l))
|
||||
|
||||
@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]
|
||||
repo = Repository("backup")
|
||||
|
||||
if l == []:
|
||||
self.assertIsNone(repo.current_snapshot)
|
||||
else:
|
||||
self.assertIsNotNone(repo.current_snapshot)
|
||||
self.assertIsInstance(repo.current_snapshot, Snapshot)
|
||||
|
||||
def tearDown(self):
|
||||
self.patched_path.stop()
|
||||
self.patched_r_metadata.stop()
|
||||
self.patched_w_metadata.stop()
|
||||
self.patched_snapshot.stop()
|
||||
|
||||
|
||||
class TestRepositoryPostCreate(unittest.TestCase):
|
||||
"""Test properties of the Repository before running create_snapshot()."""
|
||||
|
||||
def setUp(self):
|
||||
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_snapshot = patch(
|
||||
f"{TESTING_PACKAGE}.repository.Snapshot", spec_set=Snapshot
|
||||
)
|
||||
|
||||
self.mocked_path = self.patched_path.start()
|
||||
self.mocked_r_metadata = self.patched_r_metadata.start()
|
||||
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]
|
||||
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,
|
||||
}
|
||||
repo = Repository("backup")
|
||||
|
||||
repo.create_snapshot()
|
||||
|
||||
self.assertEqual(len(repo), len(l) + 1)
|
||||
self.assertEqual(len(repo.snapshots), len(l) + 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,
|
||||
}
|
||||
repo = Repository("backup")
|
||||
|
||||
new_snapshot = repo.create_snapshot()
|
||||
|
||||
self.assertIs(new_snapshot, repo.current_snapshot)
|
||||
self.assertIsInstance(new_snapshot, Snapshot)
|
||||
|
||||
def tearDown(self):
|
||||
self.patched_path.stop()
|
||||
self.patched_r_metadata.stop()
|
||||
self.patched_w_metadata.stop()
|
||||
self.patched_snapshot.stop()
|
21
tests/test_snapshot.py
Normal file
21
tests/test_snapshot.py
Normal file
@ -0,0 +1,21 @@
|
||||
"""
|
||||
.. author:: Eric Torres
|
||||
|
||||
Unit tests for the Snapshot class.
|
||||
"""
|
||||
import doctest
|
||||
import unittest
|
||||
|
||||
from rbackup.hierarchy.snapshot import Snapshot
|
||||
|
||||
# ========== Constants ==========
|
||||
TESTING_MODULE = "rbackup.hierarchy.snapshot"
|
||||
|
||||
|
||||
# ========== Functions ==========
|
||||
def load_tests(loader, tests, ignore):
|
||||
tests.addTests(doctest.DocTestSuite(TESTING_MODULE))
|
||||
return tests
|
||||
|
||||
|
||||
# ========== Unit Tests ==========
|
Reference in New Issue
Block a user