Add ctime attribute and test
This commit is contained in:
parent
f55b5ba8af
commit
4d81405400
@ -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
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user