From ab049682215464d06789a3a77f5bb9a15f3daf0e Mon Sep 17 00:00:00 2001 From: stedif <stefano.difrischia@inaf.it> Date: Fri, 28 Oct 2022 15:55:57 +0200 Subject: [PATCH] L2SS-1030: create unittest for configuration --- .../test/common/test_configuration.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tangostationcontrol/tangostationcontrol/test/common/test_configuration.py diff --git a/tangostationcontrol/tangostationcontrol/test/common/test_configuration.py b/tangostationcontrol/tangostationcontrol/test/common/test_configuration.py new file mode 100644 index 000000000..b5a5016f7 --- /dev/null +++ b/tangostationcontrol/tangostationcontrol/test/common/test_configuration.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +# +# This file is part of the LOFAR 2.0 Station Software +# +# +# +# Distributed under the terms of the APACHE license. +# See LICENSE.txt for more info. + +from tangostationcontrol.common.configuration import query_to_tuples, add_to_devices_dict, add_to_attrs_dict, add_to_server_dict + +from tangostationcontrol.test import base + +class TestDbDataConversion(base.TestCase): + def test_query_to_tuples(self): + """ Test whether Tango DB data are correctly converted into tuples """ + raw_result = ['device1', 'property_name1', 'value1', 'device1', 'property_name2', 'value2'] + num_col = 3 + record1 = ('device1', 'property_name1', 'value1') + record2 = ('device1', 'property_name2', 'value2') + expected_result = [record1, record2] + self.assertEqual(query_to_tuples(raw_result, num_col), expected_result) + + def test_add_to_devices_dict(self): + """ Test whether data retrieved from DB are correctly inserted into devices dictionary """ + data = [('device1', 'property_name1', 'value1'), ('device1', 'property_name2', 'value2')] + expected_result = {'device1': {'properties': { 'property_name1': ['value1'], + 'property_name2': ['value2']}}} + self.assertEqual(add_to_devices_dict({}, data), expected_result) + + def test_add_to_attrs_dict(self): + """ Test whether data retrieved from DB are correctly inserted into attributes dictionary """ + # Two attributes + data_2attrs = [('device1', 'attribute1', 'attr_property_name1', 'value1'), + ('device1', 'attribute2', 'attr_property_name1', 'value2')] + expected_result = {'device1': {'attribute_properties': {'attribute1': {'attr_property_name1': ['value1']}, + 'attribute2': {'attr_property_name1': ['value2']}}}} + self.assertEqual(add_to_attrs_dict({}, data_2attrs), expected_result) + # One attribute, two property values + data_1attr = [('device1', 'attribute1', 'attr_property_name1', 'value1'), + ('device1', 'attribute1', 'attr_property_name1', 'value2')] + expected_result = {'device1': {'attribute_properties': {'attribute1': + {'attr_property_name1': ['value1','value2']}}}} + self.assertEqual(add_to_attrs_dict({}, data_1attr), expected_result) + + def test_add_to_server_dict(self): + """ Test whether data retrieved from DB are correctly inserted into server dictionary """ + data = [('server_name/server_instance', 'server_class', 'device1')] + devices_dict = {'device1': {'properties': { 'property_name1': ['value1'], + 'property_name2': ['value2']}}} + expected_result = {'server_name': {'server_instance': {'server_class': + {'device1': {'properties': {'property_name1': ['value1'], + 'property_name2': ['value2']}}}}}} + self.assertEqual(add_to_server_dict({}, devices_dict, data), expected_result) -- GitLab