2019-04-10 10:23:56 -07:00
|
|
|
import json
|
2019-03-28 12:09:09 -07:00
|
|
|
import unittest
|
2019-04-10 18:03:58 -07:00
|
|
|
from io import StringIO
|
|
|
|
from pathlib import Path
|
|
|
|
from unittest.mock import PropertyMock, patch
|
2019-03-28 12:09:09 -07:00
|
|
|
|
|
|
|
from hypothesis import given
|
2019-04-10 18:03:58 -07:00
|
|
|
from hypothesis.strategies import characters, one_of, lists, text
|
|
|
|
|
2019-04-09 17:45:38 -07:00
|
|
|
from rbackup.struct.hierarchy import Hierarchy
|
2019-03-13 03:13:32 -07:00
|
|
|
|
2019-03-13 20:38:22 -07:00
|
|
|
# ========== Constants ==========
|
2019-04-10 18:00:47 -07:00
|
|
|
TESTING_PACKAGE = "rbackup.struct"
|
|
|
|
TESTING_MODULE = f"{TESTING_PACKAGE}.hierarchy"
|
2019-03-28 12:09:09 -07:00
|
|
|
|
|
|
|
|
|
|
|
# ========== Tests ==========
|
|
|
|
class TestHierarchyPaths(unittest.TestCase):
|
|
|
|
@given(one_of(text(), characters()))
|
|
|
|
def test_returns_correct_path(self, p):
|
|
|
|
self.assertEqual(Path(p), Hierarchy(p).path)
|
|
|
|
|
2019-04-10 18:17:21 -07:00
|
|
|
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()
|