diff --git a/tests/test_model/test_handling_data.py b/tests/test_model/test_handling_data.py index 651855b42c3d072fb063ff4498f718bf4195f32e..af428f90b5d5607ea2bcb14e4ca66fd5524e187a 100644 --- a/tests/test_model/test_handling_data.py +++ b/tests/test_model/test_handling_data.py @@ -12,9 +12,11 @@ from copy import copy as shallow_copy import tracemalloc from unittest import TestCase, expectedFailure -from unittest.mock import patch, DEFAULT +from unittest.mock import patch -from obspy.core import Trace, read as read_ms +from obspy.core import Trace +from obspy.core import UTCDateTime +from obspy.core import read as read_ms import numpy as np from sohstationviewer.model.handling_data import ( @@ -34,17 +36,15 @@ from sohstationviewer.model.handling_data import ( squash_gaps, downsample, chunk_minmax, - trim_downsample_SOHChan + trim_downsample_SOHChan, + get_eachDay5MinList, ) from sohstationviewer.model.reftek.from_rt2ms.core import Reftek130 -from sohstationviewer.conf import constants as consts +from sohstationviewer.conf.constants import SEC_5M, SEC_DAY, NO_5M_DAY TEST_DATA_DIR = Path(__file__).parent.parent.joinpath('test_data') -# tempfile.tempdir = './tempdir' - - class TestHandlingData(TestCase): @classmethod def setUpClass(cls) -> None: @@ -980,3 +980,247 @@ class TestTrimDownsampleWfChan(TestCase): @expectedFailure def test(self): raise NotImplementedError(type(self)) + + +class TestGetEachDay5MinList(TestCase): + @staticmethod + def calc_next_day_start_time(time: float): + utc_time = UTCDateTime(time) + start_of_day_utc_time = utc_time.replace(second=0, minute=0, hour=0) + start_of_next_day_time = start_of_day_utc_time.timestamp + SEC_DAY + return start_of_next_day_time + + def test_one_five_minute_list_each_day_times_are_same_day(self): + num_of_days = 1 + with self.subTest('test_positive_time'): + start_time = UTCDateTime(1970, 1, 2, 4, 12, 52).timestamp + end_time = UTCDateTime(1970, 1, 2, 15, 24, 43).timestamp + five_min_lists = get_eachDay5MinList(start_time, end_time) + self.assertEqual(len(five_min_lists), num_of_days) + with self.subTest('test_negative_time'): + start_time = UTCDateTime(1969, 10, 21, 4, 12, 52).timestamp + end_time = UTCDateTime(1969, 10, 21, 15, 24, 43).timestamp + five_min_lists = get_eachDay5MinList(start_time, end_time) + self.assertEqual(len(five_min_lists), num_of_days) + + def test_one_five_minute_list_each_day_times_not_start_of_day(self): + num_of_days = 5 + test_names = ( + 'test_positive_start_time', + 'test_negative_start_time_and_positive_end_time', + 'test_negative_start_time_and_negative_end_time' + ) + test_inputs = ( + ( + UTCDateTime(1970, 1, 1, 0, 2, 31).timestamp, + UTCDateTime(1970, 1, 5, 4, 2, 52).timestamp + ), + ( + UTCDateTime(1969, 12, 30, 4, 2, 52).timestamp, + UTCDateTime(1970, 1, 3, 4, 2, 52).timestamp + ), + ( + UTCDateTime(1969, 12, 20, 4, 2, 52).timestamp, + UTCDateTime(1969, 12, 24, 4, 2, 52).timestamp + ), + ) + for test_name, time_pair in zip(test_names, test_inputs): + with self.subTest(test_name): + start_time, end_time = time_pair + five_min_lists = get_eachDay5MinList(start_time, end_time) + self.assertEqual(len(five_min_lists), num_of_days) + + def test_one_five_minute_list_each_day_start_time_is_start_of_day(self): + num_of_days = 5 + with self.subTest('test_zero_start_time'): + start_time = 0 + end_time = UTCDateTime(1970, 1, 5, 12, 12, 12).timestamp + five_min_lists = get_eachDay5MinList(start_time, end_time) + self.assertEqual(len(five_min_lists), num_of_days) + + with self.subTest('test_positive_start_time'): + start_time = UTCDateTime(1970, 1, 2, 0, 0, 0).timestamp + end_time = UTCDateTime(1970, 1, 6, 12, 12, 12).timestamp + five_min_lists = get_eachDay5MinList(start_time, end_time) + self.assertEqual(len(five_min_lists), num_of_days) + + with self.subTest('test_negative_start_time'): + start_time = UTCDateTime(1969, 12, 24, 0, 0, 0).timestamp + end_time = UTCDateTime(1969, 12, 28, 12, 12, 12).timestamp + five_min_lists = get_eachDay5MinList(start_time, end_time) + self.assertEqual(len(five_min_lists), num_of_days) + + def test_one_five_minute_list_each_day_end_time_is_start_of_day(self): + num_of_days = 4 + with self.subTest('test_zero_end_time'): + start_time = UTCDateTime(1969, 12, 28, 12, 12, 12).timestamp + end_time = 0 + five_min_lists = get_eachDay5MinList(start_time, end_time) + self.assertEqual(len(five_min_lists), num_of_days) + + with self.subTest('test_positive_end_time'): + start_time = UTCDateTime(1970, 1, 2, 12, 12, 12).timestamp + end_time = UTCDateTime(1970, 1, 6, 0, 0, 0).timestamp + five_min_lists = get_eachDay5MinList(start_time, end_time) + self.assertEqual(len(five_min_lists), num_of_days) + + with self.subTest('test_negative_end_time'): + start_time = UTCDateTime(1969, 12, 16, 12, 12, 12).timestamp + end_time = UTCDateTime(1969, 12, 20, 0, 0, 0).timestamp + five_min_lists = get_eachDay5MinList(start_time, end_time) + self.assertEqual(len(five_min_lists), num_of_days) + + def test_each_day_contains_288_five_minutes_interval(self): + test_names = ( + 'test_positive_start_time', + 'test_negative_start_time_and_positive_end_time', + 'test_negative_start_time_and_negative_end_time' + ) + test_inputs = ( + ( + UTCDateTime(1970, 1, 1, 0, 2, 31).timestamp, + UTCDateTime(1970, 1, 5, 4, 2, 52).timestamp + ), + ( + UTCDateTime(1969, 12, 30, 4, 2, 52).timestamp, + UTCDateTime(1970, 1, 3, 4, 2, 52).timestamp + ), + ( + UTCDateTime(1969, 12, 20, 4, 2, 52).timestamp, + UTCDateTime(1969, 12, 24, 4, 2, 52).timestamp + ), + ) + for test_name, time_pair in zip(test_names, test_inputs): + with self.subTest(test_name): + start_time, end_time = time_pair + five_min_lists = get_eachDay5MinList(start_time, end_time) + self.assertTrue(all(day_5_min_list.size == NO_5M_DAY + for day_5_min_list in five_min_lists)) + + def test_end_minutes_correct_start_day_equal_to_end_day(self): + test_names = ( + 'test_positive_time', + 'test_negative_time' + ) + test_inputs = ( + ( + UTCDateTime(1970, 1, 1, 5, 21, 25).timestamp, + UTCDateTime(1970, 1, 1, 6, 6, 12).timestamp + ), + ( + UTCDateTime(1969, 12, 20, 1, 12, 26).timestamp, + UTCDateTime(1969, 12, 20, 6, 7, 51).timestamp + ), + ) + for test_name, time_pair in zip(test_names, test_inputs): + with self.subTest(test_name): + start_time, end_time = time_pair + five_min_lists = get_eachDay5MinList(start_time, end_time) + self.assertEqual( + self.calc_next_day_start_time(start_time) - SEC_5M, + five_min_lists[0][-1] + ) + + def test_end_minutes_correct_start_day_and_end_day_differ_by_one_day(self): + test_names = ( + 'test_positive_start_time', + 'test_negative_start_time_and_positive_end_time', + 'test_negative_start_time_and_negative_end_time' + ) + test_inputs = ( + ( + UTCDateTime(1970, 1, 1, 5, 21, 25).timestamp, + UTCDateTime(1970, 1, 2, 4, 6, 12).timestamp + ), + ( + UTCDateTime(1969, 12, 31, 1, 12, 26).timestamp, + UTCDateTime(1970, 1, 1, 6, 7, 51).timestamp + ), + ( + UTCDateTime(1969, 12, 30, 11, 25, 9).timestamp, + UTCDateTime(1969, 12, 31, 21, 5, 51).timestamp + ), + ) + for test_name, time_pair in zip(test_names, test_inputs): + with self.subTest(test_name): + start_time, end_time = time_pair + five_min_lists = get_eachDay5MinList(start_time, end_time) + self.assertEqual( + self.calc_next_day_start_time(start_time) - SEC_5M, + five_min_lists[0][-1] + ) + self.assertEqual( + self.calc_next_day_start_time(end_time) - SEC_5M, + five_min_lists[1][-1] + ) + + def test_end_minutes_correct_start_day_and_end_day_differ_by_multiple_days(self): # noqa + test_names = ( + 'test_positive_start_time', + 'test_negative_start_time_and_positive_end_time', + 'test_negative_start_time_and_negative_end_time' + ) + test_inputs = ( + ( + UTCDateTime(1970, 1, 1, 5, 21, 25).timestamp, + UTCDateTime(1970, 1, 5, 4, 6, 12).timestamp + ), + ( + UTCDateTime(1969, 12, 31, 1, 12, 26).timestamp, + UTCDateTime(1970, 1, 4, 4, 7, 51).timestamp + ), + ( + UTCDateTime(1969, 12, 20, 11, 25, 9).timestamp, + UTCDateTime(1969, 12, 24, 21, 5, 51).timestamp + ), + ) + for test_name, time_pair in zip(test_names, test_inputs): + with self.subTest(test_name): + start_time, end_time = time_pair + five_min_lists = get_eachDay5MinList(start_time, end_time) + curr_day_time = start_time + day_after_last_start = self.calc_next_day_start_time(end_time) + day_from_start = 0 + while curr_day_time < day_after_last_start: + self.assertEqual( + self.calc_next_day_start_time(curr_day_time) - SEC_5M, + five_min_lists[day_from_start][-1] + ) + curr_day_time += SEC_DAY + day_from_start += 1 + + def test_each_interval_is_five_minutes_apart(self): + start_time = UTCDateTime(1969, 12, 25, 12, 12, 12).timestamp + end_time = UTCDateTime(1970, 1, 5, 12, 12, 12).timestamp + five_min_lists = get_eachDay5MinList(start_time, end_time) + a = np.apply_along_axis(lambda arr: np.absolute(arr[1:] - arr[:-1]), 1, + five_min_lists) + self.assertTrue(all(np.ravel(a) == SEC_5M)) + + def test_start_time_is_equal_to_end_time(self): + with self.subTest('test_zero_time'): + input_time = 0 + with self.assertRaises(Exception): + get_eachDay5MinList(input_time, input_time) + with self.subTest('test_positive_time'): + input_time = UTCDateTime(1970, 2, 1, 12, 25, 51).timestamp + five_min_lists = get_eachDay5MinList(input_time, input_time) + self.assertEqual(len(five_min_lists), 1) + self.assertEqual( + self.calc_next_day_start_time(input_time) - SEC_5M, + five_min_lists[0][-1] + ) + with self.subTest('test_negative_time'): + input_time = UTCDateTime(1969, 11, 7, 12, 25, 51).timestamp + five_min_lists = get_eachDay5MinList(input_time, input_time) + self.assertEqual(len(five_min_lists), 1) + self.assertEqual( + self.calc_next_day_start_time(input_time) - SEC_5M, + five_min_lists[0][-1] + ) + + def test_end_time_earlier_than_start_time(self): + start_time = UTCDateTime(9999, 12, 31, 23, 59, 59).timestamp + end_time = UTCDateTime(1, 1, 1, 0, 0).timestamp + with self.assertRaises(Exception): + get_eachDay5MinList(start_time, end_time)