| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
"""Tests for the SQLite-based attribute container store.""" |
| 4 |
|
| 5 |
import os |
| 6 |
import unittest |
| 7 |
|
| 8 |
from acstore import sqlite_store |
| 9 |
from acstore.containers import manager as containers_manager |
| 10 |
|
| 11 |
from tests import test_lib |
| 12 |
|
| 13 |
|
| 14 |
class _TestSQLiteAttributeContainerStoreV20220716( |
| 15 |
sqlite_store.SQLiteAttributeContainerStore): |
| 16 |
"""Test class for testing format compatibility checks.""" |
| 17 |
|
| 18 |
_FORMAT_VERSION = 20220716 |
| 19 |
_APPEND_COMPATIBLE_FORMAT_VERSION = 20211121 |
| 20 |
_UPGRADE_COMPATIBLE_FORMAT_VERSION = 20211121 |
| 21 |
_READ_COMPATIBLE_FORMAT_VERSION = 20211121 |
| 22 |
|
| 23 |
|
| 24 |
class _TestSQLiteAttributeContainerStoreV20221023( |
| 25 |
sqlite_store.SQLiteAttributeContainerStore): |
| 26 |
"""Test class for testing format compatibility checks.""" |
| 27 |
|
| 28 |
_FORMAT_VERSION = 20221023 |
| 29 |
_APPEND_COMPATIBLE_FORMAT_VERSION = 20221023 |
| 30 |
_UPGRADE_COMPATIBLE_FORMAT_VERSION = 20221023 |
| 31 |
_READ_COMPATIBLE_FORMAT_VERSION = 20211121 |
| 32 |
|
| 33 |
|
| 34 |
# TODO add tests for PythonAST2SQL. |
| 35 |
|
| 36 |
|
| 37 |
class SQLiteSchemaHelperTest(test_lib.BaseTestCase): |
| 38 |
"""Tests for the SQLite schema helper.""" |
| 39 |
|
| 40 |
# pylint: disable=protected-access |
| 41 |
|
| 42 |
def testGetStorageDataType(self): |
| 43 |
"""Tests the GetStorageDataType function.""" |
| 44 |
schema_helper = sqlite_store.SQLiteSchemaHelper() |
| 45 |
|
| 46 |
data_type = schema_helper.GetStorageDataType('bool') |
| 47 |
self.assertEqual(data_type, 'INTEGER') |
| 48 |
|
| 49 |
data_type = schema_helper.GetStorageDataType('int') |
| 50 |
self.assertEqual(data_type, 'INTEGER') |
| 51 |
|
| 52 |
data_type = schema_helper.GetStorageDataType('str') |
| 53 |
self.assertEqual(data_type, 'TEXT') |
| 54 |
|
| 55 |
data_type = schema_helper.GetStorageDataType('timestamp') |
| 56 |
self.assertEqual(data_type, 'BIGINT') |
| 57 |
|
| 58 |
data_type = schema_helper.GetStorageDataType('AttributeContainerIdentifier') |
| 59 |
self.assertEqual(data_type, 'TEXT') |
| 60 |
|
| 61 |
def testDeserializeValue(self): |
| 62 |
"""Tests the DeserializeValue function.""" |
| 63 |
schema_helper = sqlite_store.SQLiteSchemaHelper() |
| 64 |
|
| 65 |
value = schema_helper.DeserializeValue('bool', 0) |
| 66 |
self.assertFalse(value) |
| 67 |
|
| 68 |
value = schema_helper.DeserializeValue('bool', 1) |
| 69 |
self.assertTrue(value) |
| 70 |
|
| 71 |
value = schema_helper.DeserializeValue('int', 1) |
| 72 |
self.assertEqual(value, 1) |
| 73 |
|
| 74 |
value = schema_helper.DeserializeValue('str', 'one') |
| 75 |
self.assertEqual(value, 'one') |
| 76 |
|
| 77 |
value = schema_helper.DeserializeValue('timestamp', 1) |
| 78 |
self.assertEqual(value, 1) |
| 79 |
|
| 80 |
# TODO: add test for AttributeContainerIdentifier |
| 81 |
|
| 82 |
def testSerializeValue(self): |
| 83 |
"""Tests the SerializeValue function.""" |
| 84 |
schema_helper = sqlite_store.SQLiteSchemaHelper() |
| 85 |
|
| 86 |
value = schema_helper.SerializeValue('bool', False) |
| 87 |
self.assertEqual(value, 0) |
| 88 |
|
| 89 |
value = schema_helper.SerializeValue('bool', True) |
| 90 |
self.assertEqual(value, 1) |
| 91 |
|
| 92 |
value = schema_helper.SerializeValue('int', 1) |
| 93 |
self.assertEqual(value, 1) |
| 94 |
|
| 95 |
value = schema_helper.SerializeValue('str', 'one') |
| 96 |
self.assertEqual(value, 'one') |
| 97 |
|
| 98 |
value = schema_helper.SerializeValue('timestamp', 1) |
| 99 |
self.assertEqual(value, 1) |
| 100 |
|
| 101 |
# TODO: add test for AttributeContainerIdentifier |
| 102 |
|
| 103 |
|
| 104 |
class SQLiteAttributeContainerStoreTest(test_lib.BaseTestCase): |
| 105 |
"""Tests for the SQLite-based storage file object.""" |
| 106 |
|
| 107 |
# pylint: disable=protected-access |
| 108 |
|
| 109 |
def setUp(self): |
| 110 |
"""Sets up the needed objects used throughout the test.""" |
| 111 |
containers_manager.AttributeContainersManager.RegisterAttributeContainer( |
| 112 |
test_lib.TestAttributeContainer) |
| 113 |
|
| 114 |
def tearDown(self): |
| 115 |
"""Cleans up the needed objects used throughout the test.""" |
| 116 |
containers_manager.AttributeContainersManager.DeregisterAttributeContainer( |
| 117 |
test_lib.TestAttributeContainer) |
| 118 |
|
| 119 |
def testCheckStorageMetadata(self): |
| 120 |
"""Tests the _CheckStorageMetadata function.""" |
| 121 |
with test_lib.TempDirectory(): |
| 122 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 123 |
|
| 124 |
metadata_values = { |
| 125 |
'format_version': f'{test_store._FORMAT_VERSION:d}', |
| 126 |
'serialization_format': 'json'} |
| 127 |
test_store._CheckStorageMetadata(metadata_values) |
| 128 |
|
| 129 |
metadata_values['format_version'] = 'bogus' |
| 130 |
with self.assertRaises(IOError): |
| 131 |
test_store._CheckStorageMetadata(metadata_values) |
| 132 |
|
| 133 |
metadata_values['format_version'] = '1' |
| 134 |
with self.assertRaises(IOError): |
| 135 |
test_store._CheckStorageMetadata(metadata_values) |
| 136 |
|
| 137 |
metadata_values['format_version'] = f'{test_store._FORMAT_VERSION:d}' |
| 138 |
metadata_values['serialization_format'] = 'bogus' |
| 139 |
with self.assertRaises(IOError): |
| 140 |
test_store._CheckStorageMetadata(metadata_values) |
| 141 |
|
| 142 |
metadata_values['serialization_format'] = 'json' |
| 143 |
|
| 144 |
def testCreateAttributeContainerTable(self): |
| 145 |
"""Tests the _CreateAttributeContainerTable function.""" |
| 146 |
attribute_container = test_lib.TestAttributeContainer() |
| 147 |
|
| 148 |
with test_lib.TempDirectory() as temp_directory: |
| 149 |
test_path = os.path.join(temp_directory, 'acstore.sqlite') |
| 150 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 151 |
test_store.Open(path=test_path, read_only=False) |
| 152 |
|
| 153 |
try: |
| 154 |
test_store._CreateAttributeContainerTable( |
| 155 |
attribute_container.CONTAINER_TYPE) |
| 156 |
|
| 157 |
with self.assertRaises(IOError): |
| 158 |
test_store._CreateAttributeContainerTable( |
| 159 |
attribute_container.CONTAINER_TYPE) |
| 160 |
|
| 161 |
finally: |
| 162 |
test_store.Close() |
| 163 |
|
| 164 |
# TODO: add tests for _CreateAttributeContainerFromRow |
| 165 |
# TODO: add tests for _Flush |
| 166 |
# TODO: add tests for _FlushWriteCache |
| 167 |
|
| 168 |
def testGetAttributeContainersWithFilter(self): |
| 169 |
"""Tests the _GetAttributeContainersWithFilter function.""" |
| 170 |
attribute_container = test_lib.TestAttributeContainer() |
| 171 |
attribute_container.attribute = '8f0bf95a7959baad9666b21a7feed79d' |
| 172 |
|
| 173 |
column_names = ['attribute'] |
| 174 |
|
| 175 |
with test_lib.TempDirectory() as temp_directory: |
| 176 |
test_path = os.path.join(temp_directory, 'acstore.sqlite') |
| 177 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 178 |
test_store.Open(path=test_path, read_only=False) |
| 179 |
|
| 180 |
try: |
| 181 |
containers = list(test_store._GetAttributeContainersWithFilter( |
| 182 |
attribute_container.CONTAINER_TYPE, column_names=column_names)) |
| 183 |
self.assertEqual(len(containers), 0) |
| 184 |
|
| 185 |
test_store.AddAttributeContainer(attribute_container) |
| 186 |
|
| 187 |
containers = list(test_store._GetAttributeContainersWithFilter( |
| 188 |
attribute_container.CONTAINER_TYPE, column_names=column_names)) |
| 189 |
self.assertEqual(len(containers), 1) |
| 190 |
|
| 191 |
filter_expression = 'attribute == "8f0bf95a7959baad9666b21a7feed79d"' |
| 192 |
containers = list(test_store._GetAttributeContainersWithFilter( |
| 193 |
attribute_container.CONTAINER_TYPE, column_names=column_names, |
| 194 |
filter_expression=filter_expression)) |
| 195 |
self.assertEqual(len(containers), 1) |
| 196 |
|
| 197 |
filter_expression = 'attribute != "8f0bf95a7959baad9666b21a7feed79d"' |
| 198 |
containers = list(test_store._GetAttributeContainersWithFilter( |
| 199 |
attribute_container.CONTAINER_TYPE, column_names=column_names, |
| 200 |
filter_expression=filter_expression)) |
| 201 |
self.assertEqual(len(containers), 0) |
| 202 |
|
| 203 |
containers = list(test_store._GetAttributeContainersWithFilter( |
| 204 |
'bogus', column_names=column_names)) |
| 205 |
self.assertEqual(len(containers), 0) |
| 206 |
|
| 207 |
finally: |
| 208 |
test_store.Close() |
| 209 |
|
| 210 |
def testGetNumberOfAttributeContainerRows(self): |
| 211 |
"""Tests the _GetNumberOfAttributeContainerRows function.""" |
| 212 |
attribute_container = test_lib.TestAttributeContainer() |
| 213 |
|
| 214 |
with test_lib.TempDirectory() as temp_directory: |
| 215 |
test_path = os.path.join(temp_directory, 'acstore.sqlite') |
| 216 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 217 |
test_store.Open(path=test_path, read_only=False) |
| 218 |
|
| 219 |
try: |
| 220 |
number_of_containers = test_store._GetNumberOfAttributeContainerRows( |
| 221 |
attribute_container.CONTAINER_TYPE) |
| 222 |
self.assertEqual(number_of_containers, 0) |
| 223 |
|
| 224 |
test_store.AddAttributeContainer(attribute_container) |
| 225 |
|
| 226 |
number_of_containers = test_store._GetNumberOfAttributeContainerRows( |
| 227 |
attribute_container.CONTAINER_TYPE) |
| 228 |
self.assertEqual(number_of_containers, 1) |
| 229 |
|
| 230 |
# Test for a supported container type that does not have a table |
| 231 |
# present in the storage file. |
| 232 |
query = f'DROP TABLE {attribute_container.CONTAINER_TYPE:s}' |
| 233 |
test_store._cursor.execute(query) |
| 234 |
number_of_containers = test_store._GetNumberOfAttributeContainerRows( |
| 235 |
attribute_container.CONTAINER_TYPE) |
| 236 |
self.assertEqual(number_of_containers, 0) |
| 237 |
|
| 238 |
finally: |
| 239 |
test_store.Close() |
| 240 |
|
| 241 |
def testHasTable(self): |
| 242 |
"""Tests the _HasTable function.""" |
| 243 |
with test_lib.TempDirectory() as temp_directory: |
| 244 |
test_path = os.path.join(temp_directory, 'acstore.sqlite') |
| 245 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 246 |
test_store.Open(path=test_path, read_only=False) |
| 247 |
|
| 248 |
try: |
| 249 |
test_store._CreateAttributeContainerTable('test_container') |
| 250 |
|
| 251 |
result = test_store._HasTable('test_container') |
| 252 |
self.assertTrue(result) |
| 253 |
|
| 254 |
result = test_store._HasTable('bogus') |
| 255 |
self.assertFalse(result) |
| 256 |
|
| 257 |
finally: |
| 258 |
test_store.Close() |
| 259 |
|
| 260 |
def testRaiseIfNotReadable(self): |
| 261 |
"""Tests the _RaiseIfNotReadable function.""" |
| 262 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 263 |
|
| 264 |
with self.assertRaises(IOError): |
| 265 |
test_store._RaiseIfNotReadable() |
| 266 |
|
| 267 |
def testRaiseIfNotWritable(self): |
| 268 |
"""Tests the _RaiseIfNotWritable function.""" |
| 269 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 270 |
|
| 271 |
with self.assertRaises(IOError): |
| 272 |
test_store._RaiseIfNotWritable() |
| 273 |
|
| 274 |
# TODO: add tests for _ReadAndCheckStorageMetadata |
| 275 |
# TODO: add tests for _ReadMetadata |
| 276 |
# TODO: add tests for _UpdateStorageMetadataFormatVersion |
| 277 |
|
| 278 |
def testWriteExistingAttributeContainer(self): |
| 279 |
"""Tests the _WriteExistingAttributeContainer function.""" |
| 280 |
attribute_container = test_lib.TestAttributeContainer() |
| 281 |
|
| 282 |
with test_lib.TempDirectory() as temp_directory: |
| 283 |
test_path = os.path.join(temp_directory, 'acstore.sqlite') |
| 284 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 285 |
test_store.Open(path=test_path, read_only=False) |
| 286 |
|
| 287 |
try: |
| 288 |
number_of_containers = test_store._GetNumberOfAttributeContainerRows( |
| 289 |
attribute_container.CONTAINER_TYPE) |
| 290 |
self.assertEqual(number_of_containers, 0) |
| 291 |
|
| 292 |
test_store._WriteNewAttributeContainer(attribute_container) |
| 293 |
|
| 294 |
number_of_containers = test_store._GetNumberOfAttributeContainerRows( |
| 295 |
attribute_container.CONTAINER_TYPE) |
| 296 |
self.assertEqual(number_of_containers, 1) |
| 297 |
|
| 298 |
test_store._WriteExistingAttributeContainer(attribute_container) |
| 299 |
|
| 300 |
number_of_containers = test_store._GetNumberOfAttributeContainerRows( |
| 301 |
attribute_container.CONTAINER_TYPE) |
| 302 |
self.assertEqual(number_of_containers, 1) |
| 303 |
|
| 304 |
finally: |
| 305 |
test_store.Close() |
| 306 |
|
| 307 |
# TODO: add tests for _WriteMetadata |
| 308 |
# TODO: add tests for _WriteMetadataValue |
| 309 |
|
| 310 |
def testWriteNewAttributeContainer(self): |
| 311 |
"""Tests the _WriteNewAttributeContainer function.""" |
| 312 |
attribute_container = test_lib.TestAttributeContainer() |
| 313 |
|
| 314 |
with test_lib.TempDirectory() as temp_directory: |
| 315 |
test_path = os.path.join(temp_directory, 'acstore.sqlite') |
| 316 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 317 |
test_store.Open(path=test_path, read_only=False) |
| 318 |
|
| 319 |
try: |
| 320 |
number_of_containers = test_store._GetNumberOfAttributeContainerRows( |
| 321 |
attribute_container.CONTAINER_TYPE) |
| 322 |
self.assertEqual(number_of_containers, 0) |
| 323 |
|
| 324 |
test_store._WriteNewAttributeContainer(attribute_container) |
| 325 |
|
| 326 |
number_of_containers = test_store._GetNumberOfAttributeContainerRows( |
| 327 |
attribute_container.CONTAINER_TYPE) |
| 328 |
self.assertEqual(number_of_containers, 1) |
| 329 |
|
| 330 |
finally: |
| 331 |
test_store.Close() |
| 332 |
|
| 333 |
def testAddAttributeContainer(self): |
| 334 |
"""Tests the AddAttributeContainer function.""" |
| 335 |
attribute_container = test_lib.TestAttributeContainer() |
| 336 |
|
| 337 |
with test_lib.TempDirectory() as temp_directory: |
| 338 |
test_path = os.path.join(temp_directory, 'acstore.sqlite') |
| 339 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 340 |
test_store.Open(path=test_path, read_only=False) |
| 341 |
|
| 342 |
try: |
| 343 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 344 |
attribute_container.CONTAINER_TYPE) |
| 345 |
self.assertEqual(number_of_containers, 0) |
| 346 |
|
| 347 |
test_store.AddAttributeContainer(attribute_container) |
| 348 |
|
| 349 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 350 |
attribute_container.CONTAINER_TYPE) |
| 351 |
self.assertEqual(number_of_containers, 1) |
| 352 |
|
| 353 |
finally: |
| 354 |
test_store.Close() |
| 355 |
|
| 356 |
with self.assertRaises(IOError): |
| 357 |
test_store.AddAttributeContainer(attribute_container) |
| 358 |
|
| 359 |
# TODO: add tests for CheckSupportedFormat |
| 360 |
|
| 361 |
def testGetAttributeContainerByIdentifier(self): |
| 362 |
"""Tests the GetAttributeContainerByIdentifier function.""" |
| 363 |
attribute_container = test_lib.TestAttributeContainer() |
| 364 |
|
| 365 |
with test_lib.TempDirectory() as temp_directory: |
| 366 |
test_path = os.path.join(temp_directory, 'acstore.sqlite') |
| 367 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 368 |
test_store.Open(path=test_path, read_only=False) |
| 369 |
|
| 370 |
try: |
| 371 |
test_store.AddAttributeContainer(attribute_container) |
| 372 |
|
| 373 |
identifier = attribute_container.GetIdentifier() |
| 374 |
|
| 375 |
container = test_store.GetAttributeContainerByIdentifier( |
| 376 |
attribute_container.CONTAINER_TYPE, identifier) |
| 377 |
self.assertIsNotNone(container) |
| 378 |
|
| 379 |
identifier.sequence_number = 99 |
| 380 |
|
| 381 |
container = test_store.GetAttributeContainerByIdentifier( |
| 382 |
attribute_container.CONTAINER_TYPE, identifier) |
| 383 |
self.assertIsNone(container) |
| 384 |
|
| 385 |
finally: |
| 386 |
test_store.Close() |
| 387 |
|
| 388 |
def testGetAttributeContainerByIndex(self): |
| 389 |
"""Tests the GetAttributeContainerByIndex function.""" |
| 390 |
attribute_container = test_lib.TestAttributeContainer() |
| 391 |
|
| 392 |
with test_lib.TempDirectory() as temp_directory: |
| 393 |
test_path = os.path.join(temp_directory, 'acstore.sqlite') |
| 394 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 395 |
test_store.Open(path=test_path, read_only=False) |
| 396 |
|
| 397 |
try: |
| 398 |
container = test_store.GetAttributeContainerByIndex( |
| 399 |
attribute_container.CONTAINER_TYPE, 0) |
| 400 |
self.assertIsNone(container) |
| 401 |
|
| 402 |
test_store.AddAttributeContainer(attribute_container) |
| 403 |
|
| 404 |
container = test_store.GetAttributeContainerByIndex( |
| 405 |
attribute_container.CONTAINER_TYPE, 0) |
| 406 |
self.assertIsNotNone(container) |
| 407 |
|
| 408 |
container = test_store.GetAttributeContainerByIndex('bogus', 0) |
| 409 |
self.assertIsNone(container) |
| 410 |
|
| 411 |
finally: |
| 412 |
test_store.Close() |
| 413 |
|
| 414 |
def testGetAttributeContainers(self): |
| 415 |
"""Tests the GetAttributeContainers function.""" |
| 416 |
attribute_container = test_lib.TestAttributeContainer() |
| 417 |
attribute_container.attribute = '8f0bf95a7959baad9666b21a7feed79d' |
| 418 |
|
| 419 |
with test_lib.TempDirectory() as temp_directory: |
| 420 |
test_path = os.path.join(temp_directory, 'acstore.sqlite') |
| 421 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 422 |
test_store.Open(path=test_path, read_only=False) |
| 423 |
|
| 424 |
try: |
| 425 |
containers = list(test_store.GetAttributeContainers( |
| 426 |
attribute_container.CONTAINER_TYPE)) |
| 427 |
self.assertEqual(len(containers), 0) |
| 428 |
|
| 429 |
test_store.AddAttributeContainer(attribute_container) |
| 430 |
|
| 431 |
containers = list(test_store.GetAttributeContainers( |
| 432 |
attribute_container.CONTAINER_TYPE)) |
| 433 |
self.assertEqual(len(containers), 1) |
| 434 |
|
| 435 |
filter_expression = 'attribute == "8f0bf95a7959baad9666b21a7feed79d"' |
| 436 |
containers = list(test_store.GetAttributeContainers( |
| 437 |
attribute_container.CONTAINER_TYPE, |
| 438 |
filter_expression=filter_expression)) |
| 439 |
self.assertEqual(len(containers), 1) |
| 440 |
|
| 441 |
filter_expression = 'attribute != "8f0bf95a7959baad9666b21a7feed79d"' |
| 442 |
containers = list(test_store.GetAttributeContainers( |
| 443 |
attribute_container.CONTAINER_TYPE, |
| 444 |
filter_expression=filter_expression)) |
| 445 |
self.assertEqual(len(containers), 0) |
| 446 |
|
| 447 |
with self.assertRaises(IOError): |
| 448 |
list(test_store.GetAttributeContainers('bogus')) |
| 449 |
|
| 450 |
finally: |
| 451 |
test_store.Close() |
| 452 |
|
| 453 |
def testGetNumberOfAttributeContainers(self): |
| 454 |
"""Tests the GetNumberOfAttributeContainers function.""" |
| 455 |
attribute_container = test_lib.TestAttributeContainer() |
| 456 |
|
| 457 |
with test_lib.TempDirectory() as temp_directory: |
| 458 |
test_path = os.path.join(temp_directory, 'acstore.sqlite') |
| 459 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 460 |
test_store.Open(path=test_path, read_only=False) |
| 461 |
|
| 462 |
try: |
| 463 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 464 |
attribute_container.CONTAINER_TYPE) |
| 465 |
self.assertEqual(number_of_containers, 0) |
| 466 |
|
| 467 |
test_store.AddAttributeContainer(attribute_container) |
| 468 |
|
| 469 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 470 |
attribute_container.CONTAINER_TYPE) |
| 471 |
self.assertEqual(number_of_containers, 1) |
| 472 |
|
| 473 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 474 |
'bogus') |
| 475 |
self.assertEqual(number_of_containers, 0) |
| 476 |
|
| 477 |
finally: |
| 478 |
test_store.Close() |
| 479 |
|
| 480 |
def testHasAttributeContainers(self): |
| 481 |
"""Tests the HasAttributeContainers function.""" |
| 482 |
attribute_container = test_lib.TestAttributeContainer() |
| 483 |
|
| 484 |
with test_lib.TempDirectory() as temp_directory: |
| 485 |
test_path = os.path.join(temp_directory, 'acstore.sqlite') |
| 486 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 487 |
test_store.Open(path=test_path, read_only=False) |
| 488 |
|
| 489 |
try: |
| 490 |
result = test_store.HasAttributeContainers( |
| 491 |
attribute_container.CONTAINER_TYPE) |
| 492 |
self.assertFalse(result) |
| 493 |
|
| 494 |
test_store.AddAttributeContainer(attribute_container) |
| 495 |
|
| 496 |
result = test_store.HasAttributeContainers( |
| 497 |
attribute_container.CONTAINER_TYPE) |
| 498 |
self.assertTrue(result) |
| 499 |
|
| 500 |
result = test_store.HasAttributeContainers('bogus') |
| 501 |
self.assertFalse(result) |
| 502 |
|
| 503 |
finally: |
| 504 |
test_store.Close() |
| 505 |
|
| 506 |
# TODO: add tests for Open and Close |
| 507 |
|
| 508 |
def testUpdateAttributeContainer(self): |
| 509 |
"""Tests the UpdateAttributeContainer function.""" |
| 510 |
attribute_container = test_lib.TestAttributeContainer() |
| 511 |
|
| 512 |
with test_lib.TempDirectory() as temp_directory: |
| 513 |
test_path = os.path.join(temp_directory, 'acstore.sqlite') |
| 514 |
test_store = sqlite_store.SQLiteAttributeContainerStore() |
| 515 |
test_store.Open(path=test_path, read_only=False) |
| 516 |
|
| 517 |
try: |
| 518 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 519 |
attribute_container.CONTAINER_TYPE) |
| 520 |
self.assertEqual(number_of_containers, 0) |
| 521 |
|
| 522 |
test_store.AddAttributeContainer(attribute_container) |
| 523 |
|
| 524 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 525 |
attribute_container.CONTAINER_TYPE) |
| 526 |
self.assertEqual(number_of_containers, 1) |
| 527 |
|
| 528 |
test_store.UpdateAttributeContainer(attribute_container) |
| 529 |
|
| 530 |
number_of_containers = test_store.GetNumberOfAttributeContainers( |
| 531 |
attribute_container.CONTAINER_TYPE) |
| 532 |
self.assertEqual(number_of_containers, 1) |
| 533 |
|
| 534 |
finally: |
| 535 |
test_store.Close() |
| 536 |
|
| 537 |
def testVersionCompatibility(self): |
| 538 |
"""Tests the version compatibility methods.""" |
| 539 |
with test_lib.TempDirectory() as temp_directory: |
| 540 |
v1_storage_path = os.path.join(temp_directory, 'v20220716.sqlite') |
| 541 |
v1_test_store = _TestSQLiteAttributeContainerStoreV20220716() |
| 542 |
v1_test_store.Open(path=v1_storage_path, read_only=False) |
| 543 |
v1_test_store.Close() |
| 544 |
|
| 545 |
v2_test_store_rw = _TestSQLiteAttributeContainerStoreV20221023() |
| 546 |
|
| 547 |
with self.assertRaises((IOError, OSError)): |
| 548 |
v2_test_store_rw.Open(path=v1_storage_path, read_only=False) |
| 549 |
|
| 550 |
v2_test_store_ro = _TestSQLiteAttributeContainerStoreV20221023() |
| 551 |
v2_test_store_ro.Open(path=v1_storage_path, read_only=True) |
| 552 |
v2_test_store_ro.Close() |
| 553 |
|
| 554 |
|
| 555 |
if __name__ == '__main__': |
| 556 |
unittest.main() |