Skip to content
Snippets Groups Projects
test_extract_data.py 6.74 KiB
Newer Older
import unittest

from sohstationviewer.database.extractData import (
    getChanPlotInfo,
    getWFPlotInfo,
    getChanLabel,
    signatureChannels,
    getColorDef,
    getColorRanges,
)


class TestExtractData(unittest.TestCase):
    def test_get_chan_plot_info_good_channel_and_data_type(self):
        """
        Test basic functionality of getChanPlotInfo - 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(getChanPlotInfo('SOH/Data Def', 'RT130'),
                             expected_result)

    def test_get_chan_plot_info_data_type_is_unknown(self):
        """
        Test basic functionality of getChanPlotInfo - 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(getChanPlotInfo('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(getChanPlotInfo('LCE', 'Unknown'),
                             expected_result)

    def test_get_chan_plot_info_bad_channel_or_data_type(self):
        """
        Test basic functionality of getChanPlotInfo - 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.detectDataType.
        expected_result['label'] = 'DEFAULT-SOH/Data Def'
        self.assertDictEqual(getChanPlotInfo('SOH/Data Def', None),
                             expected_result)

        # Channel and data type are empty strings
        expected_result['label'] = 'DEFAULT-'
        self.assertDictEqual(getChanPlotInfo('', ''), expected_result)

        # Channel exists in database but data type does not
        expected_result['label'] = 'DEFAULT-SOH/Data Def'
        self.assertDictEqual(getChanPlotInfo('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(getChanPlotInfo('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(getChanPlotInfo('SOH/Data Def', 'Q330'),
                             expected_result)

    def test_get_wf_plot_info(self):
        """
        Test basic functionality of getWFPlotInfo - ensures returned dictionary
        contains all the needed key. Bad channel IDs cases are handled in tests
        for getChanLabel.
        """
        result = getWFPlotInfo('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 getChanLabel - 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(getChanLabel('CH1'), 'CH1-NS')
        self.assertEqual(getChanLabel('CH2'), 'CH2-EW')

        # Channel ID starts with 'DS'
        self.assertEqual(getChanLabel('DS-TEST-CHANNEL'), 'DS-TEST-CHANNEL')

    def test_get_chan_label_bad_channel_id(self):
        """
        Test basic functionality of getChanLabel - channel ID does not end in
        one of the keys in conf.dbSettings.dbConf['seisLabel'] or is the empty
        string.
        """
        self.assertRaises(KeyError, getChanLabel, 'CHG')
        self.assertRaises(IndexError, getChanLabel, '')

    def test_signature_channels(self):
        """Test basic functionality of signatureChannels"""
        self.assertIsInstance(signatureChannels(), dict)

    def test_get_color_def(self):
        """Test basic functionality of getColorDef"""
        colors = getColorDef()
        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 getColorDef"""
        names, all_counts, all_display_strings = getColorRanges()
        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)