Debian Codegraph @

tests/helpers/json_serializer.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """Tests for the attribute container JSON serializer."""
4
5 import unittest
6
7 from acstore.containers import manager
8 from acstore.helpers import json_serializer
9
10 from tests import test_lib as shared_test_lib
11
12
13 class AttributeContainerJSONSerializerTest(shared_test_lib.BaseTestCase):
14 """Tests for the attribute container JSON serializer."""
15
16 _TEST_MANAGER = manager.AttributeContainersManager
17 _TEST_SERIALIZER = json_serializer.AttributeContainerJSONSerializer
18
19 def testConvertAttributeContainerToJSON(self):
20 """Tests the ConvertAttributeContainerToJSON function."""
21 attribute_container = shared_test_lib.TestAttributeContainer()
22 attribute_container.attribute = 'MyAttribute'
23
24 expected_json_dict = {
25 '__container_type__': 'test_container',
26 '__type__': 'AttributeContainer',
27 'attribute': 'MyAttribute'}
28
29 json_dict = self._TEST_SERIALIZER.ConvertAttributeContainerToJSON(
30 attribute_container)
31 self.assertEqual(json_dict, expected_json_dict)
32
33 def testConvertJSONToAttributeContainer(self):
34 """Tests the ConvertJSONToAttributeContainer function."""
35 json_dict = {
36 '__container_type__': 'test_container',
37 '__type__': 'AttributeContainer',
38 'attribute': 'MyAttribute'}
39
40 self._TEST_MANAGER.RegisterAttributeContainer(
41 shared_test_lib.TestAttributeContainer)
42
43 try:
44 attribute_container = (
45 self._TEST_SERIALIZER.ConvertJSONToAttributeContainer(json_dict))
46
47 finally:
48 self._TEST_MANAGER.DeregisterAttributeContainer(
49 shared_test_lib.TestAttributeContainer)
50
51 self.assertIsNotNone(attribute_container)
52 self.assertEqual(attribute_container.CONTAINER_TYPE, 'test_container')
53 self.assertEqual(attribute_container.attribute, 'MyAttribute')
54
55
56 if __name__ == '__main__':
57 unittest.main()