| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
"""Tests for the fake (in-memory only) store.""" |
| 4 |
|
| 5 |
import unittest |
| 6 |
|
| 7 |
from acstore import fake_store |
| 8 |
|
| 9 |
from tests import test_lib |
| 10 |
|
| 11 |
|
| 12 |
class FakeAttributeContainerStoreTest(test_lib.BaseTestCase): |
| 13 |
"""Tests for the fake (in-memory only) store.""" |
| 14 |
|
| 15 |
# pylint: disable=protected-access |
| 16 |
|
| 17 |
def testRaiseIfNotReadable(self): |
| 18 |
"""Tests the _RaiseIfNotReadable function.""" |
| 19 |
test_store = fake_store.FakeAttributeContainerStore() |
| 20 |
|
| 21 |
with self.assertRaises(IOError): |
| 22 |
test_store._RaiseIfNotReadable() |
| 23 |
|
| 24 |
def testRaiseIfNotWritable(self): |
| 25 |
"""Tests the _RaiseIfNotWritable function.""" |
| 26 |
test_store = fake_store.FakeAttributeContainerStore() |
| 27 |
|
| 28 |
with self.assertRaises(IOError): |
| 29 |
test_store._RaiseIfNotWritable() |
| 30 |
|
| 31 |
def testWriteExistingAttributeContainer(self): |
| 32 |
"""Tests the _WriteExistingAttributeContainer function.""" |
| 33 |
attribute_container = test_lib.TestAttributeContainer() |
| 34 |
|
| 35 |
test_store = fake_store.FakeAttributeContainerStore() |
| 36 |
test_store.Open() |
| 37 |
|
| 38 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 39 |
attribute_container.CONTAINER_TYPE) |
| 40 |
self.assertEqual(number_of_containers, 0) |
| 41 |
|
| 42 |
with self.assertRaises(IOError): |
| 43 |
test_store._WriteExistingAttributeContainer(attribute_container) |
| 44 |
|
| 45 |
test_store._WriteNewAttributeContainer(attribute_container) |
| 46 |
|
| 47 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 48 |
attribute_container.CONTAINER_TYPE) |
| 49 |
self.assertEqual(number_of_containers, 1) |
| 50 |
|
| 51 |
test_store._WriteExistingAttributeContainer(attribute_container) |
| 52 |
|
| 53 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 54 |
attribute_container.CONTAINER_TYPE) |
| 55 |
self.assertEqual(number_of_containers, 1) |
| 56 |
|
| 57 |
test_store.Close() |
| 58 |
|
| 59 |
def testWriteNewAttributeContainer(self): |
| 60 |
"""Tests the _WriteNewAttributeContainer function.""" |
| 61 |
attribute_container = test_lib.TestAttributeContainer() |
| 62 |
|
| 63 |
test_store = fake_store.FakeAttributeContainerStore() |
| 64 |
test_store.Open() |
| 65 |
|
| 66 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 67 |
attribute_container.CONTAINER_TYPE) |
| 68 |
self.assertEqual(number_of_containers, 0) |
| 69 |
|
| 70 |
test_store._WriteNewAttributeContainer(attribute_container) |
| 71 |
|
| 72 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 73 |
attribute_container.CONTAINER_TYPE) |
| 74 |
self.assertEqual(number_of_containers, 1) |
| 75 |
|
| 76 |
test_store.Close() |
| 77 |
|
| 78 |
def testAddAttributeContainer(self): |
| 79 |
"""Tests the AddAttributeContainer function.""" |
| 80 |
attribute_container = test_lib.TestAttributeContainer() |
| 81 |
|
| 82 |
test_store = fake_store.FakeAttributeContainerStore() |
| 83 |
test_store.Open() |
| 84 |
|
| 85 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 86 |
attribute_container.CONTAINER_TYPE) |
| 87 |
self.assertEqual(number_of_containers, 0) |
| 88 |
|
| 89 |
test_store.AddAttributeContainer(attribute_container) |
| 90 |
|
| 91 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 92 |
attribute_container.CONTAINER_TYPE) |
| 93 |
self.assertEqual(number_of_containers, 1) |
| 94 |
|
| 95 |
test_store.Close() |
| 96 |
|
| 97 |
with self.assertRaises(IOError): |
| 98 |
test_store.AddAttributeContainer(attribute_container) |
| 99 |
|
| 100 |
def testGetAttributeContainerByIdentifier(self): |
| 101 |
"""Tests the GetAttributeContainerByIdentifier function.""" |
| 102 |
attribute_container = test_lib.TestAttributeContainer() |
| 103 |
|
| 104 |
test_store = fake_store.FakeAttributeContainerStore() |
| 105 |
test_store.Open() |
| 106 |
|
| 107 |
test_store.AddAttributeContainer(attribute_container) |
| 108 |
identifier = attribute_container.GetIdentifier() |
| 109 |
|
| 110 |
container = test_store.GetAttributeContainerByIdentifier( |
| 111 |
attribute_container.CONTAINER_TYPE, identifier) |
| 112 |
self.assertIsNotNone(container) |
| 113 |
|
| 114 |
identifier.sequence_number = 99 |
| 115 |
|
| 116 |
container = test_store.GetAttributeContainerByIdentifier( |
| 117 |
attribute_container.CONTAINER_TYPE, identifier) |
| 118 |
self.assertIsNone(container) |
| 119 |
|
| 120 |
test_store.Close() |
| 121 |
|
| 122 |
def testGetAttributeContainerByIndex(self): |
| 123 |
"""Tests the GetAttributeContainerByIndex function.""" |
| 124 |
attribute_container = test_lib.TestAttributeContainer() |
| 125 |
|
| 126 |
test_store = fake_store.FakeAttributeContainerStore() |
| 127 |
test_store.Open() |
| 128 |
|
| 129 |
container = test_store.GetAttributeContainerByIndex( |
| 130 |
attribute_container.CONTAINER_TYPE, 0) |
| 131 |
self.assertIsNone(container) |
| 132 |
|
| 133 |
test_store.AddAttributeContainer(attribute_container) |
| 134 |
|
| 135 |
container = test_store.GetAttributeContainerByIndex( |
| 136 |
attribute_container.CONTAINER_TYPE, 0) |
| 137 |
self.assertIsNotNone(container) |
| 138 |
|
| 139 |
test_store.Close() |
| 140 |
|
| 141 |
def testGetAttributeContainers(self): |
| 142 |
"""Tests the GetAttributeContainers function.""" |
| 143 |
attribute_container = test_lib.TestAttributeContainer() |
| 144 |
attribute_container.attribute = '8f0bf95a7959baad9666b21a7feed79d' |
| 145 |
|
| 146 |
test_store = fake_store.FakeAttributeContainerStore() |
| 147 |
test_store.Open() |
| 148 |
|
| 149 |
containers = list(test_store.GetAttributeContainers( |
| 150 |
attribute_container.CONTAINER_TYPE)) |
| 151 |
self.assertEqual(len(containers), 0) |
| 152 |
|
| 153 |
test_store.AddAttributeContainer(attribute_container) |
| 154 |
|
| 155 |
containers = list(test_store.GetAttributeContainers( |
| 156 |
attribute_container.CONTAINER_TYPE)) |
| 157 |
self.assertEqual(len(containers), 1) |
| 158 |
|
| 159 |
filter_expression = 'attribute == "8f0bf95a7959baad9666b21a7feed79d"' |
| 160 |
containers = list(test_store.GetAttributeContainers( |
| 161 |
attribute_container.CONTAINER_TYPE, |
| 162 |
filter_expression=filter_expression)) |
| 163 |
self.assertEqual(len(containers), 1) |
| 164 |
|
| 165 |
filter_expression = 'attribute != "8f0bf95a7959baad9666b21a7feed79d"' |
| 166 |
containers = list(test_store.GetAttributeContainers( |
| 167 |
attribute_container.CONTAINER_TYPE, |
| 168 |
filter_expression=filter_expression)) |
| 169 |
self.assertEqual(len(containers), 0) |
| 170 |
|
| 171 |
test_store.Close() |
| 172 |
|
| 173 |
def testGetNumberOfAttributeContainers(self): |
| 174 |
"""Tests the GetNumberOfAttributeContainers function.""" |
| 175 |
attribute_container = test_lib.TestAttributeContainer() |
| 176 |
|
| 177 |
test_store = fake_store.FakeAttributeContainerStore() |
| 178 |
test_store.Open() |
| 179 |
|
| 180 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 181 |
attribute_container.CONTAINER_TYPE) |
| 182 |
self.assertEqual(number_of_containers, 0) |
| 183 |
|
| 184 |
test_store.AddAttributeContainer(attribute_container) |
| 185 |
|
| 186 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 187 |
attribute_container.CONTAINER_TYPE) |
| 188 |
self.assertEqual(number_of_containers, 1) |
| 189 |
|
| 190 |
test_store.Close() |
| 191 |
|
| 192 |
def testHasAttributeContainers(self): |
| 193 |
"""Tests the HasAttributeContainers function.""" |
| 194 |
attribute_container = test_lib.TestAttributeContainer() |
| 195 |
|
| 196 |
test_store = fake_store.FakeAttributeContainerStore() |
| 197 |
test_store.Open() |
| 198 |
|
| 199 |
result = test_store.HasAttributeContainers( |
| 200 |
attribute_container.CONTAINER_TYPE) |
| 201 |
self.assertFalse(result) |
| 202 |
|
| 203 |
test_store.AddAttributeContainer(attribute_container) |
| 204 |
|
| 205 |
result = test_store.HasAttributeContainers( |
| 206 |
attribute_container.CONTAINER_TYPE) |
| 207 |
self.assertTrue(result) |
| 208 |
|
| 209 |
test_store.Close() |
| 210 |
|
| 211 |
def testOpenClose(self): |
| 212 |
"""Tests the Open and Close functions.""" |
| 213 |
test_store = fake_store.FakeAttributeContainerStore() |
| 214 |
test_store.Open() |
| 215 |
test_store.Close() |
| 216 |
|
| 217 |
test_store.Open() |
| 218 |
test_store.Close() |
| 219 |
|
| 220 |
test_store = fake_store.FakeAttributeContainerStore() |
| 221 |
test_store.Open() |
| 222 |
test_store.Close() |
| 223 |
|
| 224 |
test_store.Open() |
| 225 |
|
| 226 |
with self.assertRaises(IOError): |
| 227 |
test_store.Open() |
| 228 |
|
| 229 |
test_store.Close() |
| 230 |
|
| 231 |
with self.assertRaises(IOError): |
| 232 |
test_store.Close() |
| 233 |
|
| 234 |
def testUpdateAttributeContainer(self): |
| 235 |
"""Tests the UpdateAttributeContainer function.""" |
| 236 |
attribute_container = test_lib.TestAttributeContainer() |
| 237 |
|
| 238 |
test_store = fake_store.FakeAttributeContainerStore() |
| 239 |
test_store.Open() |
| 240 |
|
| 241 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 242 |
attribute_container.CONTAINER_TYPE) |
| 243 |
self.assertEqual(number_of_containers, 0) |
| 244 |
|
| 245 |
with self.assertRaises(IOError): |
| 246 |
test_store.UpdateAttributeContainer(attribute_container) |
| 247 |
|
| 248 |
test_store.AddAttributeContainer(attribute_container) |
| 249 |
|
| 250 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 251 |
attribute_container.CONTAINER_TYPE) |
| 252 |
self.assertEqual(number_of_containers, 1) |
| 253 |
|
| 254 |
test_store.UpdateAttributeContainer(attribute_container) |
| 255 |
|
| 256 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 257 |
attribute_container.CONTAINER_TYPE) |
| 258 |
self.assertEqual(number_of_containers, 1) |
| 259 |
|
| 260 |
test_store.Close() |
| 261 |
|
| 262 |
|
| 263 |
if __name__ == '__main__': |
| 264 |
unittest.main() |