Module redvox.tests.common.test_api_reader
tests for api X reader
Expand source code
"""
tests for api X reader
"""
from datetime import timedelta
import unittest
import os
import redvox.tests as tests
from redvox.common import date_time_utils as dtu
from redvox.common import api_reader
from redvox.common.io import ReadFilter
class ApiReaderTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.input_dir = tests.APIX_READER_TEST_DATA_DIR
cls.api_900_extensions = {".rdvxz"}
cls.api_1000_extensions = {".rdvxm"}
def test_read_all_start_time(self):
reader = api_reader.ApiReader(
self.input_dir,
True,
ReadFilter(start_dt_buf=timedelta(seconds=30),
start_dt=dtu.datetime_from_epoch_seconds_utc(1611696200)),
)
result = reader.index_summary.total_packets()
self.assertEqual(result, 2)
result_by_id = reader.read_files_by_id("1000001000")
self.assertEqual(len(result_by_id), 1)
self.assertEqual(
result_by_id[0].station_information.id, "1000001000"
)
def test_read_all_end_time(self):
reader = api_reader.ApiReader(
self.input_dir,
True,
ReadFilter(end_dt_buf=timedelta(seconds=30),
end_dt=dtu.datetime_from_epoch_seconds_utc(1611696100)),
)
result = reader.index_summary.total_packets()
self.assertEqual(result, 2)
result_by_id = reader.read_files_by_id("1000000900")
self.assertEqual(len(result_by_id), 1)
self.assertEqual(
result_by_id[0].station_information.id, "1000000900"
)
def test_read_all_start_time_no_match(self):
reader = api_reader.ApiReader(
self.input_dir,
True,
ReadFilter(start_dt=dtu.datetime_from_epoch_seconds_utc(1700000000)),
)
result = reader.index_summary.total_packets()
self.assertEqual(result, 0)
def test_read_all_station_ids_no_match(self):
api1000_dir = os.path.join(self.input_dir, "api1000")
reader = api_reader.ApiReader(api1000_dir, True, ReadFilter(station_ids={"1000000900"}))
result = reader.index_summary.total_packets()
self.assertEqual(result, 0)
result_by_id = reader.read_files_by_id("1000000900")
self.assertIsNone(result_by_id)
def test_read_all_station_ids(self):
reader = api_reader.ApiReader(
self.input_dir, True, ReadFilter(station_ids={"1000001000", "2000001000"})
)
result = reader.index_summary.total_packets()
self.assertEqual(result, 2)
result_by_id = reader.read_files_by_id("1000001000")
self.assertEqual(len(result_by_id), 2)
self.assertEqual(
result_by_id[0].station_information.id, "1000001000"
)
def test_read_all_api900_in_unstructured_dir(self):
reader = api_reader.ApiReader(
self.input_dir, read_filter=ReadFilter(extensions=self.api_900_extensions)
)
result = reader.index_summary.total_packets()
self.assertEqual(result, 1)
result_by_id = reader.read_files_by_id("2000000900")
self.assertEqual(len(result_by_id), 1)
self.assertEqual(
result_by_id[0].station_information.id, "2000000900"
)
def test_read_all_api900_in_structured_dir(self):
reader = api_reader.ApiReader(
self.input_dir, True, ReadFilter(extensions=self.api_900_extensions)
)
result = reader.index_summary.total_packets()
self.assertEqual(result, 2)
result_by_id = reader.read_files_by_id("1000000900")
self.assertEqual(len(result_by_id), 2)
self.assertEqual(
result_by_id[0].station_information.id, "1000000900"
)
def test_read_all_api1000_in_unstructured_dir(self):
reader = api_reader.ApiReader(
self.input_dir, read_filter=ReadFilter(extensions=self.api_1000_extensions)
)
result = reader.index_summary.total_packets()
self.assertEqual(result, 1)
result_by_id = reader.read_files_by_id("2000001000")
self.assertEqual(len(result_by_id), 1)
self.assertEqual(
result_by_id[0].station_information.id, "2000001000"
)
def test_read_all_api1000_in_structured_dir(self):
reader = api_reader.ApiReader(
self.input_dir, True, ReadFilter(extensions=self.api_1000_extensions)
)
result = reader.index_summary.total_packets()
self.assertEqual(result, 2)
result_by_id = reader.read_files_by_id("1000001000")
self.assertEqual(len(result_by_id), 2)
self.assertEqual(
result_by_id[0].station_information.id, "1000001000"
)
def test_read_all_in_unstructured_dir(self):
reader = api_reader.ApiReader(self.input_dir)
result = reader.index_summary.total_packets()
self.assertEqual(result, 2)
result_by_id = reader.read_files_by_id("2000001000")
self.assertEqual(len(result_by_id), 1)
self.assertEqual(
result_by_id[0].station_information.id, "2000001000"
)
result_by_id = reader.read_files_by_id("2000000900")
self.assertEqual(len(result_by_id), 1)
self.assertEqual(
result_by_id[0].station_information.id, "2000000900"
)
def test_read_all_in_structured_dir(self):
reader = api_reader.ApiReader(self.input_dir, True)
result = reader.index_summary.total_packets()
self.assertEqual(result, 4)
result_by_id = reader.read_files_by_id("1000001000")
self.assertEqual(len(result_by_id), 2)
self.assertEqual(
result_by_id[0].station_information.id, "1000001000"
)
result_by_id = reader.read_files_by_id("1000000900")
self.assertEqual(len(result_by_id), 2)
self.assertEqual(
result_by_id[0].station_information.id, "1000000900"
)
def test_filter_loop(self):
filter_ids = ["1000000900", "1000001000", "2000000900"]
final_result = 0
for f_id in filter_ids:
reader = api_reader.ApiReader(self.input_dir, True, ReadFilter(station_ids={f_id}))
result = reader.index_summary.total_packets()
if result == 0:
self.assertTrue("2000000900" in reader.filter.station_ids)
continue # skip failed filter result
self.assertEqual(result, 2)
final_result += result
self.assertEqual(final_result, 4)
def test_file_size(self):
reader = api_reader.ApiReader(self.input_dir)
for i in reader.index_summary.station_summaries.values():
for s in i.values():
self.assertTrue(s.single_packet_decompressed_size_bytes > 0)
Classes
class ApiReaderTest (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 ApiReaderTest(unittest.TestCase): @classmethod def setUpClass(cls) -> None: cls.input_dir = tests.APIX_READER_TEST_DATA_DIR cls.api_900_extensions = {".rdvxz"} cls.api_1000_extensions = {".rdvxm"} def test_read_all_start_time(self): reader = api_reader.ApiReader( self.input_dir, True, ReadFilter(start_dt_buf=timedelta(seconds=30), start_dt=dtu.datetime_from_epoch_seconds_utc(1611696200)), ) result = reader.index_summary.total_packets() self.assertEqual(result, 2) result_by_id = reader.read_files_by_id("1000001000") self.assertEqual(len(result_by_id), 1) self.assertEqual( result_by_id[0].station_information.id, "1000001000" ) def test_read_all_end_time(self): reader = api_reader.ApiReader( self.input_dir, True, ReadFilter(end_dt_buf=timedelta(seconds=30), end_dt=dtu.datetime_from_epoch_seconds_utc(1611696100)), ) result = reader.index_summary.total_packets() self.assertEqual(result, 2) result_by_id = reader.read_files_by_id("1000000900") self.assertEqual(len(result_by_id), 1) self.assertEqual( result_by_id[0].station_information.id, "1000000900" ) def test_read_all_start_time_no_match(self): reader = api_reader.ApiReader( self.input_dir, True, ReadFilter(start_dt=dtu.datetime_from_epoch_seconds_utc(1700000000)), ) result = reader.index_summary.total_packets() self.assertEqual(result, 0) def test_read_all_station_ids_no_match(self): api1000_dir = os.path.join(self.input_dir, "api1000") reader = api_reader.ApiReader(api1000_dir, True, ReadFilter(station_ids={"1000000900"})) result = reader.index_summary.total_packets() self.assertEqual(result, 0) result_by_id = reader.read_files_by_id("1000000900") self.assertIsNone(result_by_id) def test_read_all_station_ids(self): reader = api_reader.ApiReader( self.input_dir, True, ReadFilter(station_ids={"1000001000", "2000001000"}) ) result = reader.index_summary.total_packets() self.assertEqual(result, 2) result_by_id = reader.read_files_by_id("1000001000") self.assertEqual(len(result_by_id), 2) self.assertEqual( result_by_id[0].station_information.id, "1000001000" ) def test_read_all_api900_in_unstructured_dir(self): reader = api_reader.ApiReader( self.input_dir, read_filter=ReadFilter(extensions=self.api_900_extensions) ) result = reader.index_summary.total_packets() self.assertEqual(result, 1) result_by_id = reader.read_files_by_id("2000000900") self.assertEqual(len(result_by_id), 1) self.assertEqual( result_by_id[0].station_information.id, "2000000900" ) def test_read_all_api900_in_structured_dir(self): reader = api_reader.ApiReader( self.input_dir, True, ReadFilter(extensions=self.api_900_extensions) ) result = reader.index_summary.total_packets() self.assertEqual(result, 2) result_by_id = reader.read_files_by_id("1000000900") self.assertEqual(len(result_by_id), 2) self.assertEqual( result_by_id[0].station_information.id, "1000000900" ) def test_read_all_api1000_in_unstructured_dir(self): reader = api_reader.ApiReader( self.input_dir, read_filter=ReadFilter(extensions=self.api_1000_extensions) ) result = reader.index_summary.total_packets() self.assertEqual(result, 1) result_by_id = reader.read_files_by_id("2000001000") self.assertEqual(len(result_by_id), 1) self.assertEqual( result_by_id[0].station_information.id, "2000001000" ) def test_read_all_api1000_in_structured_dir(self): reader = api_reader.ApiReader( self.input_dir, True, ReadFilter(extensions=self.api_1000_extensions) ) result = reader.index_summary.total_packets() self.assertEqual(result, 2) result_by_id = reader.read_files_by_id("1000001000") self.assertEqual(len(result_by_id), 2) self.assertEqual( result_by_id[0].station_information.id, "1000001000" ) def test_read_all_in_unstructured_dir(self): reader = api_reader.ApiReader(self.input_dir) result = reader.index_summary.total_packets() self.assertEqual(result, 2) result_by_id = reader.read_files_by_id("2000001000") self.assertEqual(len(result_by_id), 1) self.assertEqual( result_by_id[0].station_information.id, "2000001000" ) result_by_id = reader.read_files_by_id("2000000900") self.assertEqual(len(result_by_id), 1) self.assertEqual( result_by_id[0].station_information.id, "2000000900" ) def test_read_all_in_structured_dir(self): reader = api_reader.ApiReader(self.input_dir, True) result = reader.index_summary.total_packets() self.assertEqual(result, 4) result_by_id = reader.read_files_by_id("1000001000") self.assertEqual(len(result_by_id), 2) self.assertEqual( result_by_id[0].station_information.id, "1000001000" ) result_by_id = reader.read_files_by_id("1000000900") self.assertEqual(len(result_by_id), 2) self.assertEqual( result_by_id[0].station_information.id, "1000000900" ) def test_filter_loop(self): filter_ids = ["1000000900", "1000001000", "2000000900"] final_result = 0 for f_id in filter_ids: reader = api_reader.ApiReader(self.input_dir, True, ReadFilter(station_ids={f_id})) result = reader.index_summary.total_packets() if result == 0: self.assertTrue("2000000900" in reader.filter.station_ids) continue # skip failed filter result self.assertEqual(result, 2) final_result += result self.assertEqual(final_result, 4) def test_file_size(self): reader = api_reader.ApiReader(self.input_dir) for i in reader.index_summary.station_summaries.values(): for s in i.values(): self.assertTrue(s.single_packet_decompressed_size_bytes > 0)
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.APIX_READER_TEST_DATA_DIR cls.api_900_extensions = {".rdvxz"} cls.api_1000_extensions = {".rdvxm"}
Methods
def test_file_size(self)
-
Expand source code
def test_file_size(self): reader = api_reader.ApiReader(self.input_dir) for i in reader.index_summary.station_summaries.values(): for s in i.values(): self.assertTrue(s.single_packet_decompressed_size_bytes > 0)
def test_filter_loop(self)
-
Expand source code
def test_filter_loop(self): filter_ids = ["1000000900", "1000001000", "2000000900"] final_result = 0 for f_id in filter_ids: reader = api_reader.ApiReader(self.input_dir, True, ReadFilter(station_ids={f_id})) result = reader.index_summary.total_packets() if result == 0: self.assertTrue("2000000900" in reader.filter.station_ids) continue # skip failed filter result self.assertEqual(result, 2) final_result += result self.assertEqual(final_result, 4)
def test_read_all_api1000_in_structured_dir(self)
-
Expand source code
def test_read_all_api1000_in_structured_dir(self): reader = api_reader.ApiReader( self.input_dir, True, ReadFilter(extensions=self.api_1000_extensions) ) result = reader.index_summary.total_packets() self.assertEqual(result, 2) result_by_id = reader.read_files_by_id("1000001000") self.assertEqual(len(result_by_id), 2) self.assertEqual( result_by_id[0].station_information.id, "1000001000" )
def test_read_all_api1000_in_unstructured_dir(self)
-
Expand source code
def test_read_all_api1000_in_unstructured_dir(self): reader = api_reader.ApiReader( self.input_dir, read_filter=ReadFilter(extensions=self.api_1000_extensions) ) result = reader.index_summary.total_packets() self.assertEqual(result, 1) result_by_id = reader.read_files_by_id("2000001000") self.assertEqual(len(result_by_id), 1) self.assertEqual( result_by_id[0].station_information.id, "2000001000" )
def test_read_all_api900_in_structured_dir(self)
-
Expand source code
def test_read_all_api900_in_structured_dir(self): reader = api_reader.ApiReader( self.input_dir, True, ReadFilter(extensions=self.api_900_extensions) ) result = reader.index_summary.total_packets() self.assertEqual(result, 2) result_by_id = reader.read_files_by_id("1000000900") self.assertEqual(len(result_by_id), 2) self.assertEqual( result_by_id[0].station_information.id, "1000000900" )
def test_read_all_api900_in_unstructured_dir(self)
-
Expand source code
def test_read_all_api900_in_unstructured_dir(self): reader = api_reader.ApiReader( self.input_dir, read_filter=ReadFilter(extensions=self.api_900_extensions) ) result = reader.index_summary.total_packets() self.assertEqual(result, 1) result_by_id = reader.read_files_by_id("2000000900") self.assertEqual(len(result_by_id), 1) self.assertEqual( result_by_id[0].station_information.id, "2000000900" )
def test_read_all_end_time(self)
-
Expand source code
def test_read_all_end_time(self): reader = api_reader.ApiReader( self.input_dir, True, ReadFilter(end_dt_buf=timedelta(seconds=30), end_dt=dtu.datetime_from_epoch_seconds_utc(1611696100)), ) result = reader.index_summary.total_packets() self.assertEqual(result, 2) result_by_id = reader.read_files_by_id("1000000900") self.assertEqual(len(result_by_id), 1) self.assertEqual( result_by_id[0].station_information.id, "1000000900" )
def test_read_all_in_structured_dir(self)
-
Expand source code
def test_read_all_in_structured_dir(self): reader = api_reader.ApiReader(self.input_dir, True) result = reader.index_summary.total_packets() self.assertEqual(result, 4) result_by_id = reader.read_files_by_id("1000001000") self.assertEqual(len(result_by_id), 2) self.assertEqual( result_by_id[0].station_information.id, "1000001000" ) result_by_id = reader.read_files_by_id("1000000900") self.assertEqual(len(result_by_id), 2) self.assertEqual( result_by_id[0].station_information.id, "1000000900" )
def test_read_all_in_unstructured_dir(self)
-
Expand source code
def test_read_all_in_unstructured_dir(self): reader = api_reader.ApiReader(self.input_dir) result = reader.index_summary.total_packets() self.assertEqual(result, 2) result_by_id = reader.read_files_by_id("2000001000") self.assertEqual(len(result_by_id), 1) self.assertEqual( result_by_id[0].station_information.id, "2000001000" ) result_by_id = reader.read_files_by_id("2000000900") self.assertEqual(len(result_by_id), 1) self.assertEqual( result_by_id[0].station_information.id, "2000000900" )
def test_read_all_start_time(self)
-
Expand source code
def test_read_all_start_time(self): reader = api_reader.ApiReader( self.input_dir, True, ReadFilter(start_dt_buf=timedelta(seconds=30), start_dt=dtu.datetime_from_epoch_seconds_utc(1611696200)), ) result = reader.index_summary.total_packets() self.assertEqual(result, 2) result_by_id = reader.read_files_by_id("1000001000") self.assertEqual(len(result_by_id), 1) self.assertEqual( result_by_id[0].station_information.id, "1000001000" )
def test_read_all_start_time_no_match(self)
-
Expand source code
def test_read_all_start_time_no_match(self): reader = api_reader.ApiReader( self.input_dir, True, ReadFilter(start_dt=dtu.datetime_from_epoch_seconds_utc(1700000000)), ) result = reader.index_summary.total_packets() self.assertEqual(result, 0)
def test_read_all_station_ids(self)
-
Expand source code
def test_read_all_station_ids(self): reader = api_reader.ApiReader( self.input_dir, True, ReadFilter(station_ids={"1000001000", "2000001000"}) ) result = reader.index_summary.total_packets() self.assertEqual(result, 2) result_by_id = reader.read_files_by_id("1000001000") self.assertEqual(len(result_by_id), 2) self.assertEqual( result_by_id[0].station_information.id, "1000001000" )
def test_read_all_station_ids_no_match(self)
-
Expand source code
def test_read_all_station_ids_no_match(self): api1000_dir = os.path.join(self.input_dir, "api1000") reader = api_reader.ApiReader(api1000_dir, True, ReadFilter(station_ids={"1000000900"})) result = reader.index_summary.total_packets() self.assertEqual(result, 0) result_by_id = reader.read_files_by_id("1000000900") self.assertIsNone(result_by_id)