Module redvox.tests.common.test_data_window

tests for data window objects

Expand source code
"""
tests for data window objects
"""
import unittest

import redvox.tests as tests
import redvox.common.date_time_utils as dt
from redvox.common import data_window as dw


class EventOriginTest(unittest.TestCase):
    def test_load_empty_origin(self):
        ev_orig: dw.EventOrigin = dw.EventOrigin.from_dict({})
        self.assertEqual(ev_orig.provider, "UNKNOWN")
        self.assertEqual(ev_orig.event_radius_m, 0.0)

    def test_load_origin(self):
        ev_orig: dw.EventOrigin = dw.EventOrigin.from_dict(
            {"provider": "TEST", "latitude": 19.75, "longitude": -156.05, "altitude": 23.48, "event_radius_m": 1.0}
        )
        self.assertEqual(ev_orig.provider, "TEST")
        self.assertEqual(ev_orig.latitude, 19.75)
        self.assertEqual(ev_orig.event_radius_m, 1.0)


class DataWindowConfigTest(unittest.TestCase):
    def test_load_empty_config(self):
        """
        specifically tests for failure to load from a dictionary.
        """
        try:
            _ = dw.DataWindowConfig.from_dict({})
            self.assertTrue(False)
        except KeyError:
            self.assertTrue(True)


class DataWindowTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.input_dir = tests.TEST_DATA_DIR

    def test_data_window_simple(self):
        datawindow = dw.DataWindow(config=dw.DataWindowConfig(input_dir=self.input_dir, structured_layout=False))
        self.assertEqual(len(datawindow.stations()), 3)
        self.assertEqual(len(datawindow.station_ids()), 3)
        self.assertTrue("1637680001" in datawindow.station_ids())
        self.assertEqual(len(datawindow.config().extensions), 2)
        self.assertEqual(len(datawindow.config().api_versions), 2)

    def test_data_window(self):
        datawindow = dw.DataWindow(
            config=dw.DataWindowConfig(
                input_dir=self.input_dir,
                structured_layout=False,
                station_ids={"1637650010", "0000000001"},
            )
        )
        self.assertEqual(len(datawindow.stations()), 2)
        test_station = datawindow.get_station("1637650010")[0]
        self.assertTrue(test_station.is_timestamps_updated)
        self.assertNotEqual(
            test_station.first_data_timestamp, test_station.audio_sensor().unaltered_data_timestamps()[0]
        )
        self.assertIsNotNone(datawindow.get_station("1637650010")[0].audio_sensor())
        test_sensor = datawindow.get_station("1637650010")[0].accelerometer_sensor()
        self.assertEqual(test_sensor.num_samples(), 643)
        self.assertEqual(test_sensor.first_data_timestamp(), test_station.audio_sensor().first_data_timestamp())
        test_sensor = datawindow.get_station("0000000001")[0].audio_sensor()
        self.assertIsNotNone(test_sensor)
        self.assertEqual(test_sensor.num_samples(), 720000)
        test_sensor = datawindow.get_station("0000000001")[0].location_sensor()
        self.assertIsNotNone(test_sensor)
        self.assertEqual(test_sensor.num_samples(), 4)

    def test_dw_with_start_end(self):
        dw_with_start_end = dw.DataWindow(
            config=dw.DataWindowConfig(
                input_dir=self.input_dir,
                station_ids={"1637650010", "0000000001"},
                start_datetime=dt.datetime_from_epoch_seconds_utc(1597189455),
                end_datetime=dt.datetime_from_epoch_seconds_utc(1597189465),
                structured_layout=False,
            )
        )
        self.assertEqual(len(dw_with_start_end.stations()), 1)
        audio_sensor = dw_with_start_end.get_station("0000000001")[0].audio_sensor()
        self.assertIsNotNone(audio_sensor)
        self.assertEqual(audio_sensor.num_samples(), 480000)
        loc_sensor = dw_with_start_end.get_station("0000000001")[0].location_sensor()
        self.assertIsNotNone(loc_sensor)
        self.assertEqual(loc_sensor.num_samples(), 4)

    def test_dw_invalid(self):
        dw_invalid = dw.DataWindow(
            config=dw.DataWindowConfig(
                input_dir=self.input_dir,
                station_ids={"does_not_exist"},
                structured_layout=False,
            )
        )
        self.assertIsNone(dw_invalid.get_station("does_not_exist"))
        self.assertIsNone(dw_invalid.first_station())

    def test_dw_first_station(self):
        dw_test = dw.DataWindow(
            config=dw.DataWindowConfig(input_dir=self.input_dir, structured_layout=False, station_ids=["1637650010"])
        )
        first_station = dw_test.first_station()
        self.assertEqual("1637650010", first_station.id())


# doesn't work with test module, but works on its own.
# class DataWindowConfigFileTest(unittest.TestCase):
#     def test_load(self):
#         dw_test = dw.DataWindow.from_config_file(f"{tests.TEST_DATA_DIR}/dw_test.config.toml")
#         self.assertEqual(len(dw_test.stations()), 2)

Classes

class DataWindowConfigTest (methodName='runTest')

A class whose instances are single test cases.

By default, the test code itself should be placed in a method named 'runTest'.

If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.

Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.

If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.

When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

Expand source code
class DataWindowConfigTest(unittest.TestCase):
    def test_load_empty_config(self):
        """
        specifically tests for failure to load from a dictionary.
        """
        try:
            _ = dw.DataWindowConfig.from_dict({})
            self.assertTrue(False)
        except KeyError:
            self.assertTrue(True)

Ancestors

  • unittest.case.TestCase

Methods

def test_load_empty_config(self)

specifically tests for failure to load from a dictionary.

Expand source code
def test_load_empty_config(self):
    """
    specifically tests for failure to load from a dictionary.
    """
    try:
        _ = dw.DataWindowConfig.from_dict({})
        self.assertTrue(False)
    except KeyError:
        self.assertTrue(True)
class DataWindowTest (methodName='runTest')

A class whose instances are single test cases.

By default, the test code itself should be placed in a method named 'runTest'.

If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.

Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.

If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.

When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

Expand source code
class DataWindowTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.input_dir = tests.TEST_DATA_DIR

    def test_data_window_simple(self):
        datawindow = dw.DataWindow(config=dw.DataWindowConfig(input_dir=self.input_dir, structured_layout=False))
        self.assertEqual(len(datawindow.stations()), 3)
        self.assertEqual(len(datawindow.station_ids()), 3)
        self.assertTrue("1637680001" in datawindow.station_ids())
        self.assertEqual(len(datawindow.config().extensions), 2)
        self.assertEqual(len(datawindow.config().api_versions), 2)

    def test_data_window(self):
        datawindow = dw.DataWindow(
            config=dw.DataWindowConfig(
                input_dir=self.input_dir,
                structured_layout=False,
                station_ids={"1637650010", "0000000001"},
            )
        )
        self.assertEqual(len(datawindow.stations()), 2)
        test_station = datawindow.get_station("1637650010")[0]
        self.assertTrue(test_station.is_timestamps_updated)
        self.assertNotEqual(
            test_station.first_data_timestamp, test_station.audio_sensor().unaltered_data_timestamps()[0]
        )
        self.assertIsNotNone(datawindow.get_station("1637650010")[0].audio_sensor())
        test_sensor = datawindow.get_station("1637650010")[0].accelerometer_sensor()
        self.assertEqual(test_sensor.num_samples(), 643)
        self.assertEqual(test_sensor.first_data_timestamp(), test_station.audio_sensor().first_data_timestamp())
        test_sensor = datawindow.get_station("0000000001")[0].audio_sensor()
        self.assertIsNotNone(test_sensor)
        self.assertEqual(test_sensor.num_samples(), 720000)
        test_sensor = datawindow.get_station("0000000001")[0].location_sensor()
        self.assertIsNotNone(test_sensor)
        self.assertEqual(test_sensor.num_samples(), 4)

    def test_dw_with_start_end(self):
        dw_with_start_end = dw.DataWindow(
            config=dw.DataWindowConfig(
                input_dir=self.input_dir,
                station_ids={"1637650010", "0000000001"},
                start_datetime=dt.datetime_from_epoch_seconds_utc(1597189455),
                end_datetime=dt.datetime_from_epoch_seconds_utc(1597189465),
                structured_layout=False,
            )
        )
        self.assertEqual(len(dw_with_start_end.stations()), 1)
        audio_sensor = dw_with_start_end.get_station("0000000001")[0].audio_sensor()
        self.assertIsNotNone(audio_sensor)
        self.assertEqual(audio_sensor.num_samples(), 480000)
        loc_sensor = dw_with_start_end.get_station("0000000001")[0].location_sensor()
        self.assertIsNotNone(loc_sensor)
        self.assertEqual(loc_sensor.num_samples(), 4)

    def test_dw_invalid(self):
        dw_invalid = dw.DataWindow(
            config=dw.DataWindowConfig(
                input_dir=self.input_dir,
                station_ids={"does_not_exist"},
                structured_layout=False,
            )
        )
        self.assertIsNone(dw_invalid.get_station("does_not_exist"))
        self.assertIsNone(dw_invalid.first_station())

    def test_dw_first_station(self):
        dw_test = dw.DataWindow(
            config=dw.DataWindowConfig(input_dir=self.input_dir, structured_layout=False, station_ids=["1637650010"])
        )
        first_station = dw_test.first_station()
        self.assertEqual("1637650010", first_station.id())

Ancestors

  • unittest.case.TestCase

Static methods

def setUpClass() ‑> None

Hook method for setting up class fixture before running tests in the class.

Expand source code
@classmethod
def setUpClass(cls) -> None:
    cls.input_dir = tests.TEST_DATA_DIR

Methods

def test_data_window(self)
Expand source code
def test_data_window(self):
    datawindow = dw.DataWindow(
        config=dw.DataWindowConfig(
            input_dir=self.input_dir,
            structured_layout=False,
            station_ids={"1637650010", "0000000001"},
        )
    )
    self.assertEqual(len(datawindow.stations()), 2)
    test_station = datawindow.get_station("1637650010")[0]
    self.assertTrue(test_station.is_timestamps_updated)
    self.assertNotEqual(
        test_station.first_data_timestamp, test_station.audio_sensor().unaltered_data_timestamps()[0]
    )
    self.assertIsNotNone(datawindow.get_station("1637650010")[0].audio_sensor())
    test_sensor = datawindow.get_station("1637650010")[0].accelerometer_sensor()
    self.assertEqual(test_sensor.num_samples(), 643)
    self.assertEqual(test_sensor.first_data_timestamp(), test_station.audio_sensor().first_data_timestamp())
    test_sensor = datawindow.get_station("0000000001")[0].audio_sensor()
    self.assertIsNotNone(test_sensor)
    self.assertEqual(test_sensor.num_samples(), 720000)
    test_sensor = datawindow.get_station("0000000001")[0].location_sensor()
    self.assertIsNotNone(test_sensor)
    self.assertEqual(test_sensor.num_samples(), 4)
def test_data_window_simple(self)
Expand source code
def test_data_window_simple(self):
    datawindow = dw.DataWindow(config=dw.DataWindowConfig(input_dir=self.input_dir, structured_layout=False))
    self.assertEqual(len(datawindow.stations()), 3)
    self.assertEqual(len(datawindow.station_ids()), 3)
    self.assertTrue("1637680001" in datawindow.station_ids())
    self.assertEqual(len(datawindow.config().extensions), 2)
    self.assertEqual(len(datawindow.config().api_versions), 2)
def test_dw_first_station(self)
Expand source code
def test_dw_first_station(self):
    dw_test = dw.DataWindow(
        config=dw.DataWindowConfig(input_dir=self.input_dir, structured_layout=False, station_ids=["1637650010"])
    )
    first_station = dw_test.first_station()
    self.assertEqual("1637650010", first_station.id())
def test_dw_invalid(self)
Expand source code
def test_dw_invalid(self):
    dw_invalid = dw.DataWindow(
        config=dw.DataWindowConfig(
            input_dir=self.input_dir,
            station_ids={"does_not_exist"},
            structured_layout=False,
        )
    )
    self.assertIsNone(dw_invalid.get_station("does_not_exist"))
    self.assertIsNone(dw_invalid.first_station())
def test_dw_with_start_end(self)
Expand source code
def test_dw_with_start_end(self):
    dw_with_start_end = dw.DataWindow(
        config=dw.DataWindowConfig(
            input_dir=self.input_dir,
            station_ids={"1637650010", "0000000001"},
            start_datetime=dt.datetime_from_epoch_seconds_utc(1597189455),
            end_datetime=dt.datetime_from_epoch_seconds_utc(1597189465),
            structured_layout=False,
        )
    )
    self.assertEqual(len(dw_with_start_end.stations()), 1)
    audio_sensor = dw_with_start_end.get_station("0000000001")[0].audio_sensor()
    self.assertIsNotNone(audio_sensor)
    self.assertEqual(audio_sensor.num_samples(), 480000)
    loc_sensor = dw_with_start_end.get_station("0000000001")[0].location_sensor()
    self.assertIsNotNone(loc_sensor)
    self.assertEqual(loc_sensor.num_samples(), 4)
class EventOriginTest (methodName='runTest')

A class whose instances are single test cases.

By default, the test code itself should be placed in a method named 'runTest'.

If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.

Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.

If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.

When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

Expand source code
class EventOriginTest(unittest.TestCase):
    def test_load_empty_origin(self):
        ev_orig: dw.EventOrigin = dw.EventOrigin.from_dict({})
        self.assertEqual(ev_orig.provider, "UNKNOWN")
        self.assertEqual(ev_orig.event_radius_m, 0.0)

    def test_load_origin(self):
        ev_orig: dw.EventOrigin = dw.EventOrigin.from_dict(
            {"provider": "TEST", "latitude": 19.75, "longitude": -156.05, "altitude": 23.48, "event_radius_m": 1.0}
        )
        self.assertEqual(ev_orig.provider, "TEST")
        self.assertEqual(ev_orig.latitude, 19.75)
        self.assertEqual(ev_orig.event_radius_m, 1.0)

Ancestors

  • unittest.case.TestCase

Methods

def test_load_empty_origin(self)
Expand source code
def test_load_empty_origin(self):
    ev_orig: dw.EventOrigin = dw.EventOrigin.from_dict({})
    self.assertEqual(ev_orig.provider, "UNKNOWN")
    self.assertEqual(ev_orig.event_radius_m, 0.0)
def test_load_origin(self)
Expand source code
def test_load_origin(self):
    ev_orig: dw.EventOrigin = dw.EventOrigin.from_dict(
        {"provider": "TEST", "latitude": 19.75, "longitude": -156.05, "altitude": 23.48, "event_radius_m": 1.0}
    )
    self.assertEqual(ev_orig.provider, "TEST")
    self.assertEqual(ev_orig.latitude, 19.75)
    self.assertEqual(ev_orig.event_radius_m, 1.0)