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)
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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."""

Maeva Pourpoint
committed
time_stamp = '2021 111 222 1 1 1'
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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'])