Debian Codegraph @

tests/interface.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """Tests for the attribute container store interface."""
4
5 import unittest
6
7 from acstore import interface
8 from acstore.containers import manager
9
10 from tests import test_lib
11
12
13 class AttributeContainerStoreTest(test_lib.BaseTestCase):
14 """Tests for the attribute container store interface."""
15
16 # pylint: disable=protected-access
17
18 def testGetAttributeContainerNextSequenceNumber(self):
19 """Tests the _GetAttributeContainerNextSequenceNumber function."""
20 attribute_container = test_lib.TestAttributeContainer()
21
22 test_store = interface.AttributeContainerStore()
23
24 sequence_number = test_store._GetAttributeContainerNextSequenceNumber(
25 attribute_container.CONTAINER_TYPE)
26 self.assertEqual(sequence_number, 1)
27
28 sequence_number = test_store._GetAttributeContainerNextSequenceNumber(
29 attribute_container.CONTAINER_TYPE)
30 self.assertEqual(sequence_number, 2)
31
32 def testGetAttributeContainerSchema(self):
33 """Tests the _GetAttributeContainerSchema function."""
34 attribute_container = test_lib.TestAttributeContainer()
35
36 test_store = interface.AttributeContainerStore()
37
38 schema = test_store._GetAttributeContainerSchema(
39 attribute_container.CONTAINER_TYPE)
40 self.assertEqual(schema, {})
41
42 manager.AttributeContainersManager.RegisterAttributeContainer(
43 test_lib.TestAttributeContainer)
44
45 try:
46 schema = test_store._GetAttributeContainerSchema(
47 attribute_container.CONTAINER_TYPE)
48 self.assertEqual(schema, test_lib.TestAttributeContainer.SCHEMA)
49
50 finally:
51 manager.AttributeContainersManager.DeregisterAttributeContainer(
52 test_lib.TestAttributeContainer)
53
54 # TODO: add tests for _SetAttributeContainerNextSequenceNumber
55
56 def testSetStorageProfiler(self):
57 """Tests the SetStorageProfiler function."""
58 test_store = interface.AttributeContainerStore()
59 test_store.SetStorageProfiler(None)
60
61
62 class AttributeContainerStoreWithReadCacheTest(test_lib.BaseTestCase):
63 """Tests for the attribute container store with read cache."""
64
65 # pylint: disable=protected-access
66
67 def testCacheAttributeContainerByIndex(self):
68 """Tests the _CacheAttributeContainerByIndex function."""
69 attribute_container = test_lib.TestAttributeContainer()
70
71 with test_lib.TempDirectory():
72 test_store = interface.AttributeContainerStoreWithReadCache()
73
74 self.assertEqual(len(test_store._attribute_container_cache), 0)
75
76 test_store._CacheAttributeContainerByIndex(attribute_container, 0)
77 self.assertEqual(len(test_store._attribute_container_cache), 1)
78
79 def testGetCachedAttributeContainer(self):
80 """Tests the _GetCachedAttributeContainer function."""
81 attribute_container = test_lib.TestAttributeContainer()
82
83 with test_lib.TempDirectory():
84 test_store = interface.AttributeContainerStoreWithReadCache()
85
86 cached_container = test_store._GetCachedAttributeContainer(
87 attribute_container.CONTAINER_TYPE, 1)
88 self.assertIsNone(cached_container)
89
90 test_store._CacheAttributeContainerByIndex(attribute_container, 1)
91
92 cached_container = test_store._GetCachedAttributeContainer(
93 attribute_container.CONTAINER_TYPE, 1)
94 self.assertIsNotNone(cached_container)
95
96
97 if __name__ == '__main__':
98 unittest.main()