diff --git a/rbackup/struct/snapshot.py b/rbackup/struct/snapshot.py index 1e5da0e..5127d79 100644 --- a/rbackup/struct/snapshot.py +++ b/rbackup/struct/snapshot.py @@ -3,6 +3,7 @@ .. module:: rbackup.struct.snapshot :synopsis: Class for creating the backup snapshot hierarchy. """ +import datetime import logging from rbackup.struct.hierarchy import Hierarchy @@ -26,12 +27,30 @@ class Snapshot(Hierarchy): """Default constructor for the Snapshot class.""" super().__init__(path) + self._gen_metadata() + self._pkg_dir = self.path / "pkg" def __repr__(self): """Return a string representation of this Snapshot.""" return f"{self.__class__.__name__}('{self.name}')" + def _gen_metadata(self): + """Generate this Snapshot's metadata.""" + if self.metadata_path.exists(): + self._ctime = self.read_metadata() + else: + self._ctime = datetime.datetime.utcnow().isoformat() + self.write_metadata(self._ctime) + + @property + def ctime(self): + """ + :returns: this Snapshot's creation time + :rtype: str + """ + return self._ctime + @property def pkg_dir(self): """ @@ -39,6 +58,3 @@ class Snapshot(Hierarchy): :rtype: path-like object """ return self._pkg_dir - - def gen_metadata(self): - pass diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index 32bac2b..d2c86e6 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -4,8 +4,35 @@ Unit tests for the rbackup.struct.snapshot module. """ +import unittest +from pathlib import Path +from unittest.mock import DEFAULT, patch + +from rbackup.struct.snapshot import Snapshot + # ========== Constants ========== TESTING_PACKAGE = "rbackup.struct" TESTING_MODULE = f"{TESTING_PACKAGE}.snapshot" -# ========== Unit Tests ========== + +# ========== Tests ========== +class TestSnapshotProperties(unittest.TestCase): + def setUp(self): + self.patched_path = patch.multiple( + Path, exists=DEFAULT, mkdir=DEFAULT, symlink_to=DEFAULT, touch=DEFAULT + ) + self.patched_metadata = patch.multiple( + Snapshot, read_metadata=DEFAULT, write_metadata=DEFAULT + ) + + self.mocked_path = self.patched_path.start() + self.mocked_metadata = self.patched_metadata.start() + + self.mocked_path["exists"].return_value = False + + def test_ctime_returns_str(self): + self.assertIsInstance(Snapshot("/tmp/backup/snapshot").ctime, str) + + def tearDown(self): + self.patched_path.stop() + self.patched_metadata.stop()