Skip to content
Snippets Groups Projects
test_utils.py 10.1 KiB
Newer Older
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Tests for `utils` module."""

import logging
import unittest

from obspy import UTCDateTime

from lemi2seed.utils import (convert_coordinate, convert_time, str2list,
                             check_email_formatting, check_instrument_specs,
                             check_serial_number, is_empty, get_e_loc,
                             get_e_ids, get_run_list, log_file_path)

SCR_DIR = "lemi2seed.utils"
logging.config.fileConfig(log_file_path)
logger = logging.getLogger(SCR_DIR)


class TestCheckEmailFormatting(unittest.TestCase):
    """Test suite for check_email_formatting."""

    def test_check_email_formatting_valid_email(self):
        """Test basic functionality of check_email_formatting."""
        email = 'm@passcal.nmt.edu'
        self.assertTrue(check_email_formatting(email))

    def test_check_email_formatting_erroneous_type(self):
        """Test basic functionality of check_email_formatting."""
        email = 12
        valid = check_email_formatting(email)
        self.assertRaises(TypeError)
        self.assertFalse(valid)

    def test_check_email_formatting_erroneous_email(self):
        """Test basic functionality of check_email_formatting."""
        email = 'm_passcal.nmt.edu'
        with self.assertLogs(logger, level='ERROR') as cmd:
            check_email_formatting(email)
        msg = ("Invalid email. The provided email '{}' does not meet minimum "
               "formatting requirements: account@domain.".format(email))
        self.assertEqual(cmd.output, [":".join(['ERROR', SCR_DIR, msg])])


class TestCheckInstrumentSpecs(unittest.TestCase):
    """Test suite for check_instrument_specs."""

    def test_check_instrument_specs_default_value(self):
        """Test basic functionality of check_instrument_specs."""
        specs = 'LEMI-039'
        self.assertTrue(check_instrument_specs(specs, 'fluxgate', ''))

    def test_check_instrument_specs_valid_user_input(self):
        """Test basic functionality of check_instrument_specs."""
        specs = 'Manufacturer: a - Model: b - Type: c'
        self.assertTrue(check_instrument_specs(specs, 'fluxgate', ''))

    def test_check_instrument_specs_no_type(self):
        """Test basic functionality of check_instrument_specs."""
        specs = 'Manufacturer: a - Model: b - Type: '
        self.assertTrue(check_instrument_specs(specs, 'fluxgate', ''))

    def test_check_instrument_specs_no_manufacturer(self):
        """Test basic functionality of check_instrument_specs."""
        specs = 'Manufacturer:  - Model: b - Type: c'
        with self.assertLogs(logger, level='ERROR') as cmd:
            check_instrument_specs(specs, 'fluxgate', '')
        msg = "Please provide fluxgate manufacturer. Required metadata field."
        self.assertEqual(cmd.output, [":".join(['ERROR', SCR_DIR, msg])])

    def test_check_instrument_specs_no_model(self):
        """Test basic functionality of check_instrument_specs."""
        specs = 'Manufacturer: a - Model:  - Type: c'
        with self.assertLogs(logger, level='ERROR') as cmd:
            check_instrument_specs(specs, 'fluxgate', '')
        msg = "Please provide fluxgate model. Required metadata field."
        self.assertEqual(cmd.output, [":".join(['ERROR', SCR_DIR, msg])])

    def test_check_instrument_specs_erroneous_specs(self):
        """Test basic functionality of check_instrument_specs."""
        specs = 'Other'
        with self.assertLogs(logger, level='ERROR') as cmd:
            check_instrument_specs(specs, 'fluxgate', '')
        msg = "Please provide fluxgate specs for. Required metadata field."
        self.assertEqual(cmd.output, [":".join(['ERROR', SCR_DIR, msg])])


class TestCheckSerialNumber(unittest.TestCase):
    """Test suite for check_serial_number."""

    def test_check_serial_number_no_input(self):
        """Test basic functionality of check_serial_number."""
        serial_number = None
        with self.assertLogs(logger, level='ERROR') as cmd:
            check_serial_number(serial_number, 'data logger')
        msg = "Please provide a serial number for the data logger!"
        self.assertEqual(cmd.output, [":".join(['ERROR', SCR_DIR, msg])])

    def test_check_serial_number_invalid_input_data_logger(self):
        """Test basic functionality of check_serial_number."""
        serial_number = 1234
        with self.assertLogs(logger, level='ERROR') as cmd:
            check_serial_number(serial_number, 'data logger')
        msg = "The serial number of the data logger should be 3 digits long."
        self.assertEqual(cmd.output, [":".join(['ERROR', SCR_DIR, msg])])

    def test_check_serial_number_valid_input_data_logger(self):
        """Test basic functionality of check_serial_number."""
        serial_number = 110
        self.assertTrue(check_serial_number(serial_number, "data logger"))

    def test_check_serial_number_invalid_input_electrode(self):
        """Test basic functionality of check_serial_number."""
        serial_number = '1234a'
        with self.assertLogs(logger, level='ERROR') as cmd:
            check_serial_number(serial_number, 'electrode')
        msg = "The serial number of the electrode should be x digits long."
        self.assertEqual(cmd.output, [":".join(['ERROR', SCR_DIR, msg])])

    def test_check_serial_number_valid_input_electrode(self):
        """Test basic functionality of check_serial_number."""
        serial_number = 1103455
        self.assertTrue(check_serial_number(serial_number, "electrode"))


class TestConvertCoordinate(unittest.TestCase):
    """Test suite for convert_coordinate."""

    def test_convert_coordinate_positive_lat(self):
        """Test basic functionality of convert_coordinate."""
        coordinate = 3404.83926
        hemisphere = 'N'
        self.assertEqual(convert_coordinate(coordinate, hemisphere),
                         34.0483926)

    def test_convert_coordinate_negative_lat(self):
        """Test basic functionality of convert_coordinate."""
        coordinate = 2456.43250
        hemisphere = 'S'
        self.assertEqual(convert_coordinate(coordinate, hemisphere),
                         -24.564325)

    def test_convert_coordinate_positive_lon(self):
        """Test basic functionality of convert_coordinate."""
        coordinate = 4303.74563
        hemisphere = 'E'
        self.assertEqual(convert_coordinate(coordinate, hemisphere),
                         43.0374563)

    def test_convert_coordinate_negative_lon(self):
        """Test basic functionality of convert_coordinate."""
        coordinate = 1071.84450
        hemisphere = 'W'
        self.assertEqual(convert_coordinate(coordinate, hemisphere),
                         -10.718445)

    def test_convert_coordinate_erroneous_hemisphere(self):
        """Test basic functionality of convert_coordinate."""
        coordinate = 1071.84450
        hemisphere = 'B'
        self.assertEqual(convert_coordinate(coordinate, hemisphere), None)

    def test_convert_coordinate_erroneous_type(self):
        """Test basic functionality of convert_coordinate."""
        coordinate = '1071.84450'
        hemisphere = 'B'
        self.assertRaises(ValueError)
        self.assertEqual(convert_coordinate(coordinate, hemisphere), None)


class TestConvertTime(unittest.TestCase):
    """Test suite for convert_time."""

    def test_convert_time(self):
        """Test basic functionality of convert_time."""
        time_stamp = '2020 09 30 20 54 00'
        self.assertEqual(convert_time(time_stamp),
                         UTCDateTime('2020-09-30T20:54:00.000000Z'))

    def test_convert_time_erroneous_time(self):
        """Test basic functionality of convert_time."""
        time_stamp = '2020 09 30 20 74 00'
        self.assertRaises(ValueError)
        self.assertEqual(convert_time(time_stamp), None)

    def test_convert_time_erroneous_type(self):
        """Test basic functionality of convert_time."""
        self.assertEqual(convert_time(time_stamp), None)


class TestGetELoc(unittest.TestCase):
    """Test suite for get_e_loc."""

    def test_get_e_loc_two_electrode_pairs(self):
        """Test basic functionality of get_e_loc."""
        e_info = {'E1': 'Ex', 'E2': 'Ey'}
        self.assertDictEqual(get_e_loc(e_info), {})

    def test_get_e_loc_more_than_two_electrode_pairs(self):
        """Test basic functionality of get_e_loc."""
        e_info = {'E1': 'Ex', 'E2': 'Ey', 'E3': 'Ex', 'E4': 'Ey'}
        e_loc = {'E1': '00', 'E3': '01', 'E2': '00', 'E4': '01'}
        self.assertDictEqual(get_e_loc(e_info), e_loc)


class TestGetRunListGetEIds(unittest.TestCase):
    """Test suite for get_run_list and get_e_ids."""

    def test_get_run_list(self):
        """Test basic functionality of get_run_list."""
        nbr_runs = 3
        self.assertListEqual(get_run_list(nbr_runs), ['a', 'b', 'c'])

    def test_get_e_ids(self):
        """Test basic functionality of get_e_ids."""
        nbr_runs = 2
        e_ids = [('a', 1), ('a', 2), ('a', 3), ('a', 4), ('b', 1), ('b', 2),
                 ('b', 3), ('b', 4)]
        self.assertListEqual(get_e_ids(nbr_runs), e_ids)


class TestIsEmpty(unittest.TestCase):
    """Test suite for is_empty."""

    def test_is_empty_list(self):
        """Test basic functionality of is_empty."""
        val = []
        self.assertTrue(is_empty(val))

    def test_is_empty_dict_set(self):
        """Test basic functionality of is_empty."""
        val = {}
        self.assertTrue(is_empty(val))

    def test_is_empty_none(self):
        """Test basic functionality of is_empty."""
        val = None
        self.assertTrue(is_empty(val))

    def test_is_empty_str(self):
        """Test basic functionality of is_empty."""
        val = ''
        self.assertFalse(is_empty(val))


class TestStr2list(unittest.TestCase):
    """Test suite for str2list."""

    def test_str2list_with_space(self):
        """Test basic functionality of str2list."""
        str_input = 'a, b, c, d, e'
        self.assertEqual(str2list(str_input), ['a', 'b', 'c', 'd', 'e'])

    def test_str2list_no_space(self):
        """Test basic functionality of str2list."""
        str_input = 'a,b,c,d,e'
        self.assertEqual(str2list(str_input), ['a', 'b', 'c', 'd', 'e'])