1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
# Copyright 2015-2016 Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=
import unittest
import obnamlib
class BlobStoreTests(unittest.TestCase):
def test_returns_None_for_missing_blob(self):
bag_store = DummyBagStore()
blob_store = obnamlib.BlobStore()
blob_store.set_bag_store(bag_store)
blob_id = obnamlib.make_object_id(123, 456)
self.assertEqual(blob_store.get_blob(blob_id), None)
def test_returns_existing_blob(self):
bag_store = DummyBagStore()
blob = 'blobby blob blob blob'
blob_store = obnamlib.BlobStore()
blob_store.set_bag_store(bag_store)
blob_id = blob_store.put_blob(blob)
retrieved = blob_store.get_blob(blob_id)
self.assertEqual(blob, retrieved)
def test_stores_blob_persistently(self):
bag_store = DummyBagStore()
blob = 'this is a blob, yes it is'
blob_store = obnamlib.BlobStore()
blob_store.set_bag_store(bag_store)
blob_id = blob_store.put_blob(blob)
blob_store.flush()
blob_store_2 = obnamlib.BlobStore()
blob_store_2.set_bag_store(bag_store)
retrieved = blob_store_2.get_blob(blob_id)
self.assertEqual(blob, retrieved)
def test_gets_persistent_blob_twice(self):
bag_store = DummyBagStore()
blob = 'this is a blob, yes it is'
blob_store = obnamlib.BlobStore()
blob_store.set_bag_store(bag_store)
blob_id = blob_store.put_blob(blob)
blob_store.flush()
blob_store_2 = obnamlib.BlobStore()
blob_store_2.set_bag_store(bag_store)
retrieved_1 = blob_store_2.get_blob(blob_id)
retrieved_2 = blob_store_2.get_blob(blob_id)
self.assertEqual(retrieved_1, retrieved_2)
def test_obeys_max_bag_size(self):
bag_store = DummyBagStore()
blob = 'this is a blob, yes it is'
blob_store = obnamlib.BlobStore()
blob_store.set_bag_store(bag_store)
blob_store.set_max_bag_size(len(blob) + 1)
blob_store.put_blob(blob)
self.assertTrue(bag_store.is_empty())
blob_store.set_max_bag_size(1)
blob_store.put_blob(blob)
self.assertFalse(bag_store.is_empty())
def test_finds_unflushed_blob(self):
bag_store = DummyBagStore()
blob = 'this is a blob, yes it is'
blob_store = obnamlib.BlobStore()
blob_store.set_bag_store(bag_store)
blob_store.set_max_bag_size(len(blob) + 1)
blob_id = blob_store.put_blob(blob)
self.assertTrue(bag_store.is_empty())
retrieved = blob_store.get_blob(blob_id)
self.assertEqual(blob, retrieved)
def test_returns_None_if_well_known_blog_does_not_exist(self):
bag_store = DummyBagStore()
well_known_id = 'bobby'
blob_store = obnamlib.BlobStore()
blob_store.set_bag_store(bag_store)
retrieved = blob_store.get_well_known_blob(well_known_id)
self.assertEqual(retrieved, None)
def test_puts_well_known_blob(self):
bag_store = DummyBagStore()
well_known_blob = 'Bob'
well_known_id = 'bobby'
blob_store = obnamlib.BlobStore()
blob_store.set_bag_store(bag_store)
blob_store.set_max_bag_size(len(well_known_blob) + 1)
returned_id = blob_store.put_well_known_blob(
well_known_id, well_known_blob)
self.assertEqual(returned_id, None)
retrieved = blob_store.get_well_known_blob(well_known_id)
self.assertEqual(well_known_blob, retrieved)
class DummyBagStore(object):
def __init__(self):
self._bags = {}
self._prev_id = 0
def is_empty(self):
return len(self._bags) == 0
def reserve_bag_id(self):
self._prev_id += 1
return self._prev_id
def put_bag(self, bag):
self._bags[bag.get_id()] = bag
def has_bag(self, bag_id):
return bag_id in self._bags
def get_bag(self, bag_id):
return self._bags[bag_id]
|