summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2015-07-19 16:19:53 +0300
committerLars Wirzenius <liw@liw.fi>2015-07-19 16:20:52 +0300
commit28a6295fed6d59c74ef4db265713c2c9590baef2 (patch)
tree1a94757189cc80794acdefdeffe51353efa270d5
parentcc00d3ab4f5a96bab844f52896a04fea1cd8df52 (diff)
downloadobnam-28a6295fed6d59c74ef4db265713c2c9590baef2.tar.gz
Add Bag.get_bytes to get length in bytes
-rw-r--r--obnamlib/bag.py5
-rw-r--r--obnamlib/bag_tests.py10
2 files changed, 13 insertions, 2 deletions
diff --git a/obnamlib/bag.py b/obnamlib/bag.py
index 1a1e2648..cf176b1b 100644
--- a/obnamlib/bag.py
+++ b/obnamlib/bag.py
@@ -24,6 +24,7 @@ class Bag(object):
def __init__(self):
self._bag_id = None
self._blobs = []
+ self._blobs_bytes = 0
def get_id(self):
return self._bag_id
@@ -35,11 +36,15 @@ class Bag(object):
if self.get_id() is None:
raise BagIdNotSetError()
self._blobs.append(blob)
+ self._blobs_bytes += len(blob)
return obnamlib.make_object_id(self.get_id(), len(self) - 1)
def __len__(self):
return len(self._blobs)
+ def get_bytes(self):
+ return self._blobs_bytes
+
def __getitem__(self, index):
return self._blobs[index]
diff --git a/obnamlib/bag_tests.py b/obnamlib/bag_tests.py
index e0b5c4e3..1465fc24 100644
--- a/obnamlib/bag_tests.py
+++ b/obnamlib/bag_tests.py
@@ -36,6 +36,10 @@ class BagTests(unittest.TestCase):
bag = obnamlib.Bag()
self.assertEqual(len(bag), 0)
+ def test_has_no_bytes_initially(self):
+ bag = obnamlib.Bag()
+ self.assertEqual(bag.get_bytes(), 0)
+
def test_raises_error_if_appending_blob_without_id_being_set(self):
bag = obnamlib.Bag()
self.assertRaises(
@@ -43,11 +47,13 @@ class BagTests(unittest.TestCase):
bag.append, 'blob')
def test_appends_a_blob(self):
+ blob = 'foo'
bag = obnamlib.Bag()
bag.set_id(1)
- bag.append('foo')
+ bag.append(blob)
self.assertEqual(len(bag), 1)
- self.assertEqual(bag[0], 'foo')
+ self.assertEqual(bag.get_bytes(), len(blob))
+ self.assertEqual(bag[0], blob)
def test_appending_returns_object_id(self):
bag = obnamlib.Bag()