import unittest from sohstationviewer.database.extract_data import ( get_chan_plot_info, get_wf_plot_info, get_chan_label, get_signature_channels, get_color_def, get_color_ranges, ) class TestExtractData(unittest.TestCase): def test_get_chan_plot_info_good_channel_and_data_type(self): """ Test basic functionality of get_chan_plot_info - channel and data type combination exists in database table `Channels` """ expected_result = {'channel': 'SOH/Data Def', 'plotType': 'upDownDots', 'height': 2, 'unit': '', 'linkedChan': None, 'convertFactor': 1, 'label': 'SOH/Data Def', 'fixPoint': 0, 'valueColors': '0:W|1:C'} self.assertDictEqual(get_chan_plot_info('SOH/Data Def', 'RT130'), expected_result) def test_get_chan_plot_info_data_type_is_unknown(self): """ Test basic functionality of get_chan_plot_info - data type is the string 'Unknown'. """ # Channel does not exist in database expected_result = {'channel': 'DEFAULT', 'plotType': 'linesDots', 'height': 2, 'unit': '', 'linkedChan': None, 'convertFactor': 1, 'label': 'DEFAULT-Bad Channel ID', 'fixPoint': '0', 'valueColors': None} self.assertDictEqual(get_chan_plot_info('Bad Channel ID', 'Unknown'), expected_result) # Channel exist in database expected_result = {'channel': 'LCE', 'plotType': 'linesDots', 'height': 3, 'unit': 'us', 'linkedChan': None, 'convertFactor': 1, 'label': 'LCE-PhaseError', 'fixPoint': 0, 'valueColors': 'L:W|D:Y'} self.assertDictEqual(get_chan_plot_info('LCE', 'Unknown'), expected_result) self.assertDictEqual(get_chan_plot_info('LCE', 'Unknown'), expected_result) def test_get_chan_plot_info_bad_channel_or_data_type(self): """ Test basic functionality of get_chan_plot_info - channel and data type combination does not exist in database table Channels and data type is not the string 'Unknown'. """ # noinspection PyDictCreation expected_result = {'channel': 'DEFAULT', 'plotType': 'linesDots', 'height': 2, 'unit': '', 'linkedChan': None, 'convertFactor': 1, 'label': None, # Change for each test case 'fixPoint': '0', 'valueColors': None} # Data type has None value. None value comes from # controller.processing.detect_data_type. expected_result['label'] = 'DEFAULT-SOH/Data Def' self.assertDictEqual(get_chan_plot_info('SOH/Data Def', None), expected_result) # Channel and data type are empty strings expected_result['label'] = 'DEFAULT-' self.assertDictEqual(get_chan_plot_info('', ''), expected_result) # Channel exists in database but data type does not expected_result['label'] = 'DEFAULT-SOH/Data Def' self.assertDictEqual( get_chan_plot_info('SOH/Data Def', 'Bad Data Type'), expected_result ) # Data type exists in database but channel does not expected_result['label'] = 'DEFAULT-Bad Channel ID' self.assertDictEqual(get_chan_plot_info('Bad Channel ID', 'RT130'), expected_result) # Both channel and data type exists in database but not their # combination expected_result['label'] = 'DEFAULT-SOH/Data Def' self.assertDictEqual(get_chan_plot_info('SOH/Data Def', 'Q330'), expected_result) def test_get_wf_plot_info(self): """ Test basic functionality of get_wf_plot_info - ensures returned dictionary contains all the needed key. Bad channel IDs cases are handled in tests for get_chan_label. """ result = get_wf_plot_info('CH1') expected_keys = ('param', 'plotType', 'valueColors', 'height', 'label', 'unit', 'channel') self.assertTupleEqual(tuple(result.keys()), expected_keys) def test_get_chan_label_good_channel_id(self): """ Test basic functionality of get_chan_label - channel ID ends in one of the keys in conf.dbSettings.dbConf['seisLabel'] or starts with 'DS' """ # Channel ID does not start with 'DS' self.assertEqual(get_chan_label('CH1'), 'CH1-NS') self.assertEqual(get_chan_label('CH2'), 'CH2-EW') # Channel ID starts with 'DS' self.assertEqual(get_chan_label('DS-TEST-CHANNEL'), 'DS-TEST-CHANNEL') def test_get_chan_label_bad_channel_id(self): """ Test basic functionality of get_chan_label - channel ID does not end in one of the keys in conf.dbSettings.dbConf['seisLabel'] or is the empty string. """ self.assertRaises(KeyError, get_chan_label, 'CHG') self.assertRaises(IndexError, get_chan_label, '') def test_get_signature_channels(self): """Test basic functionality of get_signature_channels""" self.assertIsInstance(get_signature_channels(), dict) def test_get_color_def(self): """Test basic functionality of get_color_def""" colors = get_color_def() expected_colors = ['K', 'U', 'C', 'G', 'Y', 'R', 'M', 'E'] self.assertListEqual(colors, expected_colors) def test_get_color_ranges(self): """Test basic functionality of get_color_ranges""" names, all_counts, all_display_strings = get_color_ranges() num_color_def = 7 expected_names = ['antarctica', 'low', 'med', 'high'] self.assertEqual(names, expected_names) # Check that each name correspond to a list of counts and list of # of strings to display self.assertEqual(len(names), len(all_counts)) self.assertEqual(len(names), len(all_display_strings)) # Check that each list of counts have enough items for counts in all_counts: self.assertEqual(len(counts), num_color_def) # Check that each list of strings to display have enough items for display_strings in all_display_strings: self.assertEqual(len(display_strings), num_color_def + 1)