Add gen_snapshot_path() and add appropriate test

This commit is contained in:
Eric Torres 2019-03-30 14:14:41 -07:00
parent 5cca10a12b
commit 41599058fe
2 changed files with 35 additions and 6 deletions

View File

@ -85,6 +85,7 @@ class Repository(Hierarchy):
self._data = self.read_metadata() self._data = self.read_metadata()
def _init_new_repository(self): def _init_new_repository(self):
"""Perform the setup steps for a new repository."""
self.metadata_path.parent.mkdir(mode=DIRMODE, exist_ok=True) self.metadata_path.parent.mkdir(mode=DIRMODE, exist_ok=True)
self.metadata_path.touch(mode=FILEMODE) self.metadata_path.touch(mode=FILEMODE)
@ -125,14 +126,29 @@ class Repository(Hierarchy):
Example Example
------- -------
>>> repo = Repository('/tmp') >>> repo = Repository('backup')
>>> repo.snapshot_dir # doctest: +ELLIPSIS >>> repo.snapshot_dir # doctest: +ELLIPSIS
PosixPath('/tmp/data') PosixPath('backup/data')
:rtype: path-like object :rtype: path-like object
""" """
return self.path / "data" return self.path / "data"
def gen_snapshot_path(self, name):
"""Generate a path for a Snapshot by name.
Example
-------
>>> repo = Repository('backup')
>>> repo.gen_snapshot_path('new-snapshot') # doctest: +ELLIPSIS
PosixPath('backup/data/new-snapshot')
:param name: name of the Snapshot
:type name: str or path-like object
:rtype: path-like object
"""
return self.snapshot_dir / name
@property @property
def snapshots(self): def snapshots(self):
"""Return a list of snapshots stored in this Repository. """Return a list of snapshots stored in this Repository.
@ -188,6 +204,7 @@ class Repository(Hierarchy):
:param name: the name of the snapshot :param name: the name of the snapshot
:type name: str :type name: str
:return: a new Snapshot object :return: a new Snapshot object
:raises: FileExistsError if snapshot directory already exists
""" """
syslog.debug("Creating snapshot") syslog.debug("Creating snapshot")
@ -196,9 +213,7 @@ class Repository(Hierarchy):
if name is not None if name is not None
else datetime.datetime.utcnow().isoformat().replace(":", "_") else datetime.datetime.utcnow().isoformat().replace(":", "_")
) )
path = self.snapshot_dir / snapshot_name self._data["current_snapshot"] = Snapshot(self.gen_snapshot_path(snapshot_name))
self._data["current_snapshot"] = Snapshot(path)
self._data["snapshots"].append(self._data["current_snapshot"]) self._data["snapshots"].append(self._data["current_snapshot"])
self.write_metadata() self.write_metadata()

View File

@ -39,7 +39,21 @@ class TestRepositoryPreCreate(unittest.TestCase):
self.mocked_w_metadata = self.patched_w_metadata.start() self.mocked_w_metadata = self.patched_w_metadata.start()
self.mocked_path = self.patched_path.start() self.mocked_path = self.patched_path.start()
self.mocked_snapshot = self.patched_snapshot.start() self.mocked_snapshot = self.patched_snapshot.start()
self.mocked_pickle = self.patched_pickle.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")
snapshot_path = repo.gen_snapshot_path(name)
self.assertEqual(snapshot_path, Path(f"backup/data/{name}"))
self.assertIsInstance(snapshot_path, Path)
@given(lists(builds(Snapshot, text()), unique=True)) @given(lists(builds(Snapshot, text()), unique=True))
def test_empty(self, l): def test_empty(self, l):