1
# Copyright 2015 Lars Wirzenius
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
# =*= License: GPL-3+ =*=
26
class BagStoreTests(unittest.TestCase):
29
self.tempdir = tempfile.mkdtemp()
30
self.fs = obnamlib.LocalFS(self.tempdir)
31
self.store = obnamlib.BagStore()
32
self.store.set_location(self.fs, '.')
33
self.bag = obnamlib.Bag()
34
bag_id = self.store.reserve_bag_id()
35
self.bag.set_id(bag_id)
38
shutil.rmtree(self.tempdir)
40
def assertEqualBags(self, a, b):
41
self.assertEqual(a.get_id(), b.get_id())
42
self.assertEqual(len(a), len(b))
43
for i in range(len(a)):
44
self.assertEqual(a[i], b[i])
46
def test_stores_and_retrieves_an_empty_bag(self):
47
self.store.put_bag(self.bag)
48
new_bag = self.store.get_bag(self.bag.get_id())
49
self.assertEqualBags(new_bag, self.bag)
51
def test_stores_and_retrieves_a_full_bag(self):
52
self.bag.append('foo')
53
self.store.put_bag(self.bag)
54
new_bag = self.store.get_bag(self.bag.get_id())
55
self.assertEqualBags(new_bag, self.bag)
57
def test_has_no_bags_initially(self):
58
store = obnamlib.BagStore()
59
store.set_location(self.fs, 'empty')
60
self.assertEqual(list(store.get_bag_ids()), [])
62
def test_has_a_put_bag(self):
63
self.store.put_bag(self.bag)
64
self.assertTrue(self.store.has_bag(self.bag.get_id()))
66
def test_does_not_have_a_removed_bag(self):
67
self.store.put_bag(self.bag)
68
self.store.remove_bag(self.bag.get_id())
69
self.assertFalse(self.store.has_bag(self.bag.get_id()))
71
def test_lists_bag_that_has_been_put(self):
72
self.store.put_bag(self.bag)
73
self.assertEqual(list(self.store.get_bag_ids()), [self.bag.get_id()])
75
def test_removes_bag(self):
76
self.store.put_bag(self.bag)
77
self.store.remove_bag(self.bag.get_id())
78
self.assertEqual(list(self.store.get_bag_ids()), [])