From 41599058fe8791c42fe6a345cb2a4a59d2972ee4 Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Sat, 30 Mar 2019 14:14:41 -0700 Subject: [PATCH] Add gen_snapshot_path() and add appropriate test --- rbackup/hierarchy/repository.py | 25 ++++++++++++++++++++----- rbackup/tests/test_repository.py | 16 +++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/rbackup/hierarchy/repository.py b/rbackup/hierarchy/repository.py index ca931b1..0178071 100644 --- a/rbackup/hierarchy/repository.py +++ b/rbackup/hierarchy/repository.py @@ -85,6 +85,7 @@ class Repository(Hierarchy): self._data = self.read_metadata() 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.touch(mode=FILEMODE) @@ -125,14 +126,29 @@ class Repository(Hierarchy): Example ------- - >>> repo = Repository('/tmp') + >>> repo = Repository('backup') >>> repo.snapshot_dir # doctest: +ELLIPSIS - PosixPath('/tmp/data') + PosixPath('backup/data') :rtype: path-like object """ 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 def snapshots(self): """Return a list of snapshots stored in this Repository. @@ -188,6 +204,7 @@ class Repository(Hierarchy): :param name: the name of the snapshot :type name: str :return: a new Snapshot object + :raises: FileExistsError if snapshot directory already exists """ syslog.debug("Creating snapshot") @@ -196,9 +213,7 @@ class Repository(Hierarchy): if name is not None else datetime.datetime.utcnow().isoformat().replace(":", "_") ) - path = self.snapshot_dir / snapshot_name - - self._data["current_snapshot"] = Snapshot(path) + self._data["current_snapshot"] = Snapshot(self.gen_snapshot_path(snapshot_name)) self._data["snapshots"].append(self._data["current_snapshot"]) self.write_metadata() diff --git a/rbackup/tests/test_repository.py b/rbackup/tests/test_repository.py index fceb7d2..11c5756 100644 --- a/rbackup/tests/test_repository.py +++ b/rbackup/tests/test_repository.py @@ -39,7 +39,21 @@ class TestRepositoryPreCreate(unittest.TestCase): 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_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)) def test_empty(self, l):