rbackup/tests/test_hierarchy.py

61 lines
1.6 KiB
Python
Raw Normal View History

2019-04-10 19:23:17 -07:00
"""
2019-04-14 14:56:09 -07:00
.. moduleauthor:: Eric Torres
2019-04-10 19:23:17 -07:00
Tests for the rbackup.struct.hierarchy module.
"""
import shutil
import unittest
from pathlib import Path
from unittest.mock import DEFAULT, patch
from hypothesis import given
from hypothesis.strategies import from_regex
2019-04-10 18:03:58 -07:00
from rbackup.struct.hierarchy import Hierarchy
# ========== Constants ==========
2019-04-10 18:00:47 -07:00
TESTING_PACKAGE = "rbackup.struct"
TESTING_MODULE = f"{TESTING_PACKAGE}.hierarchy"
# ========== Tests ==========
class TestHierarchyPaths(unittest.TestCase):
def setUp(self):
self.patched_path = patch.multiple(
Path, exists=DEFAULT, mkdir=DEFAULT, symlink_to=DEFAULT, touch=DEFAULT
)
self.mocked_path = self.patched_path.start()
2019-04-14 12:40:10 -07:00
def test_retrieves_correct_metadata_filename(self):
self.assertEqual(Hierarchy("/tmp/backup").metadata_path.name, ".metadata")
2019-04-14 12:40:10 -07:00
2019-04-12 11:12:19 -07:00
@given(from_regex(r"[\w/._-]+", fullmatch=True))
def test_returns_absolute_path(self, dest):
try:
self.assertTrue(Hierarchy(dest).path.is_absolute())
except PermissionError:
pass
2019-04-10 18:17:21 -07:00
def test_raises_notimplemented_error(self):
h = Hierarchy("/tmp/backup")
2019-04-10 18:17:21 -07:00
with self.assertRaises(NotImplementedError):
h.gen_metadata()
def tearDown(self):
self.patched_path.stop()
2019-04-10 18:17:21 -07:00
class TestHierarchyMetadata(unittest.TestCase):
"""Only meant to check that data written is the same data that is read."""
2019-04-14 12:40:10 -07:00
def test_write_metadata(self):
data = ["test", "data"]
h = Hierarchy("/tmp/backup")
h.metadata_path.touch()
2019-04-10 18:17:21 -07:00
h.write_metadata(data)
self.assertEqual(data, h.read_metadata())
2019-04-10 18:17:21 -07:00
shutil.rmtree(h)