| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
"""Tests for the attribute container interface.""" |
| 4 |
|
| 5 |
import unittest |
| 6 |
|
| 7 |
from acstore.containers import interface |
| 8 |
|
| 9 |
from tests import test_lib |
| 10 |
|
| 11 |
|
| 12 |
class AttributeContainerIdentifierTest(test_lib.BaseTestCase): |
| 13 |
"""Tests for the attribute container identifier.""" |
| 14 |
|
| 15 |
def testCopyToString(self): |
| 16 |
"""Tests the CopyToString function.""" |
| 17 |
sequence_number = id(self) |
| 18 |
|
| 19 |
identifier = interface.AttributeContainerIdentifier( |
| 20 |
name='test_container', sequence_number=sequence_number) |
| 21 |
|
| 22 |
identifier_string = identifier.CopyToString() |
| 23 |
self.assertEqual( |
| 24 |
identifier_string, f'test_container.{sequence_number:d}') |
| 25 |
|
| 26 |
|
| 27 |
class AttributeContainerTest(test_lib.BaseTestCase): |
| 28 |
"""Tests for the attribute container interface.""" |
| 29 |
|
| 30 |
# pylint: disable=protected-access |
| 31 |
|
| 32 |
def testCopyToDict(self): |
| 33 |
"""Tests the CopyToDict function.""" |
| 34 |
attribute_container = interface.AttributeContainer() |
| 35 |
attribute_container.attribute_name = 'MyName' |
| 36 |
attribute_container.attribute_value = 'MyValue' |
| 37 |
|
| 38 |
expected_dict = { |
| 39 |
'attribute_name': 'MyName', |
| 40 |
'attribute_value': 'MyValue'} |
| 41 |
|
| 42 |
test_dict = attribute_container.CopyToDict() |
| 43 |
|
| 44 |
self.assertEqual(test_dict, expected_dict) |
| 45 |
|
| 46 |
def testGetAttributeNames(self): |
| 47 |
"""Tests the GetAttributeNames function.""" |
| 48 |
attribute_container = interface.AttributeContainer() |
| 49 |
attribute_container._protected_attribute = 'protected' |
| 50 |
attribute_container.attribute_name = 'MyName' |
| 51 |
attribute_container.attribute_value = 'MyValue' |
| 52 |
|
| 53 |
expected_attribute_names = ['attribute_name', 'attribute_value'] |
| 54 |
|
| 55 |
attribute_names = sorted(attribute_container.GetAttributeNames()) |
| 56 |
|
| 57 |
self.assertEqual(attribute_names, expected_attribute_names) |
| 58 |
|
| 59 |
setattr(attribute_container, '_SERIALIZABLE_PROTECTED_ATTRIBUTES', [ |
| 60 |
'_protected_attribute']) |
| 61 |
|
| 62 |
expected_attribute_names = [ |
| 63 |
'_protected_attribute', 'attribute_name', 'attribute_value'] |
| 64 |
|
| 65 |
attribute_names = sorted(attribute_container.GetAttributeNames()) |
| 66 |
|
| 67 |
self.assertEqual(attribute_names, expected_attribute_names) |
| 68 |
|
| 69 |
def testGetAttributes(self): |
| 70 |
"""Tests the GetAttributes function.""" |
| 71 |
attribute_container = interface.AttributeContainer() |
| 72 |
attribute_container._protected_attribute = 'protected' |
| 73 |
attribute_container.attribute_name = 'MyName' |
| 74 |
attribute_container.attribute_value = 'MyValue' |
| 75 |
|
| 76 |
expected_attributes = [ |
| 77 |
('attribute_name', 'MyName'), |
| 78 |
('attribute_value', 'MyValue')] |
| 79 |
|
| 80 |
attributes = sorted(attribute_container.GetAttributes()) |
| 81 |
|
| 82 |
self.assertEqual(attributes, expected_attributes) |
| 83 |
|
| 84 |
setattr(attribute_container, '_SERIALIZABLE_PROTECTED_ATTRIBUTES', [ |
| 85 |
'_protected_attribute']) |
| 86 |
|
| 87 |
expected_attributes = [ |
| 88 |
('_protected_attribute', 'protected'), |
| 89 |
('attribute_name', 'MyName'), |
| 90 |
('attribute_value', 'MyValue')] |
| 91 |
|
| 92 |
attributes = sorted(attribute_container.GetAttributes()) |
| 93 |
|
| 94 |
self.assertEqual(attributes, expected_attributes) |
| 95 |
|
| 96 |
def testGetAttributeValueHash(self): |
| 97 |
"""Tests the GetAttributeValuesHash function.""" |
| 98 |
attribute_container = interface.AttributeContainer() |
| 99 |
attribute_container._protected_attribute = 'protected' |
| 100 |
attribute_container.attribute_name = 'MyName' |
| 101 |
attribute_container.attribute_value = 'MyValue' |
| 102 |
|
| 103 |
attribute_values_hash1 = attribute_container.GetAttributeValuesHash() |
| 104 |
|
| 105 |
attribute_container.attribute_value = 'changes' |
| 106 |
|
| 107 |
attribute_values_hash2 = attribute_container.GetAttributeValuesHash() |
| 108 |
|
| 109 |
self.assertNotEqual(attribute_values_hash1, attribute_values_hash2) |
| 110 |
|
| 111 |
attribute_container.attribute_value = 'MyValue' |
| 112 |
|
| 113 |
setattr(attribute_container, '_SERIALIZABLE_PROTECTED_ATTRIBUTES', [ |
| 114 |
'_protected_attribute']) |
| 115 |
|
| 116 |
attribute_values_hash2 = attribute_container.GetAttributeValuesHash() |
| 117 |
|
| 118 |
self.assertNotEqual(attribute_values_hash1, attribute_values_hash2) |
| 119 |
|
| 120 |
def testGetAttributeValuesString(self): |
| 121 |
"""Tests the GetAttributeValuesString function.""" |
| 122 |
attribute_container = interface.AttributeContainer() |
| 123 |
attribute_container._protected_attribute = 'protected' |
| 124 |
attribute_container.attribute_name = 'MyName' |
| 125 |
attribute_container.attribute_value = 'MyValue' |
| 126 |
|
| 127 |
attribute_values_string1 = attribute_container.GetAttributeValuesString() |
| 128 |
|
| 129 |
attribute_container.attribute_value = 'changes' |
| 130 |
|
| 131 |
attribute_values_string2 = attribute_container.GetAttributeValuesString() |
| 132 |
|
| 133 |
self.assertNotEqual(attribute_values_string1, attribute_values_string2) |
| 134 |
|
| 135 |
attribute_container.attribute_value = 'MyValue' |
| 136 |
|
| 137 |
setattr(attribute_container, '_SERIALIZABLE_PROTECTED_ATTRIBUTES', [ |
| 138 |
'_protected_attribute']) |
| 139 |
|
| 140 |
attribute_values_string2 = attribute_container.GetAttributeValuesString() |
| 141 |
|
| 142 |
self.assertNotEqual(attribute_values_string1, attribute_values_string2) |
| 143 |
|
| 144 |
def testGetIdentifier(self): |
| 145 |
"""Tests the GetIdentifier function.""" |
| 146 |
attribute_container = interface.AttributeContainer() |
| 147 |
|
| 148 |
identifier = attribute_container.GetIdentifier() |
| 149 |
|
| 150 |
self.assertIsNotNone(identifier) |
| 151 |
|
| 152 |
def testMatchesExpression(self): |
| 153 |
"""Tests the MatchesExpression function.""" |
| 154 |
attribute_container = interface.AttributeContainer() |
| 155 |
attribute_container.name = 'value' |
| 156 |
|
| 157 |
result = attribute_container.MatchesExpression('name == "value"') |
| 158 |
self.assertTrue(result) |
| 159 |
|
| 160 |
result = attribute_container.MatchesExpression('name == "bogus"') |
| 161 |
self.assertFalse(result) |
| 162 |
|
| 163 |
result = attribute_container.MatchesExpression('bogus') |
| 164 |
self.assertFalse(result) |
| 165 |
|
| 166 |
def testSetIdentifier(self): |
| 167 |
"""Tests the SetIdentifier function.""" |
| 168 |
attribute_container = interface.AttributeContainer() |
| 169 |
|
| 170 |
attribute_container.SetIdentifier(None) |
| 171 |
|
| 172 |
|
| 173 |
if __name__ == '__main__': |
| 174 |
unittest.main() |