Skip to content

Commit

Permalink
Fix unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Diamond committed Oct 4, 2023
1 parent a5d26d1 commit 7a068ed
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 51 deletions.
26 changes: 1 addition & 25 deletions python/tests/test_data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,10 @@ def test_read_all(self, data_path):
expected_result = message_list_to_dict(expected_messages)

# Construct a reader. This will attempt to set t0 immediately by scanning the data file. If an index file
# exists, the reader will use the index file to find t0 quickly. If not, it'll read the file directly, but will
# _not_ attempt to generate an index (which requires reading the entire data file).
# exists, the reader will use the index file to find t0 quickly.
reader = DataLoader(path=str(data_path))
assert reader.t0 is not None
assert reader.system_t0 is not None
assert not reader.reader.have_index()

# Now read the data itself. This _will_ generate an index file.
result = reader.read()
Expand Down Expand Up @@ -224,34 +222,12 @@ def test_read_pose_mixed_binary(self, data_path):
assert len(reader.reader._original_index) == len(messages)
assert len(reader.reader.index) == len(expected_messages)

def test_read_no_generate_index(self, data_path):
expected_result = generate_data(data_path=str(data_path), include_binary=False)

# Construct a reader with index generation disabled. This never generates an index, but the read() call below
# would if we did not set this.
reader = DataLoader(path=str(data_path), generate_index=False)
assert reader.t0 is not None
assert reader.system_t0 is not None
assert not reader.reader.have_index()

# Now read the data itself. This will _not_ generate an index file.
result = reader.read()
self._check_results(result, expected_result)
assert not reader.reader.have_index()

# Do the same but this time using the disable argument to read().
reader = DataLoader(path=str(data_path))
result = reader.read(disable_index_generation=True)
self._check_results(result, expected_result)
assert not reader.reader.have_index()

def test_read_in_order(self, data_path):
messages = generate_data(data_path=str(data_path), include_binary=False, return_dict=False)
expected_messages = [m for m in messages if m.get_type() in (MessageType.POSE, MessageType.EVENT_NOTIFICATION)]
expected_result = message_list_to_messagedata(expected_messages)

reader = DataLoader(path=str(data_path))
assert not reader.reader.have_index()

result = reader.read(message_types=[PoseMessage, EventNotificationMessage], return_in_order=True)
self._check_results(result, expected_result)
Expand Down
32 changes: 6 additions & 26 deletions python/tests/test_mixed_log_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ def test_read_all(self, data_path):
messages = self._generate_mixed_data(data_path)

reader = MixedLogReader(str(data_path))
assert reader.index is None
self._check_results(reader, messages)

# Verify that we successfully generated an index file.
Expand All @@ -145,7 +144,6 @@ def test_read_pose(self, data_path):
expected_messages = [m for m in messages if isinstance(m, PoseMessage)]

reader = MixedLogReader(str(data_path))
assert reader.index is None
reader.filter_in_place((PoseMessage,))
self._check_results(reader, expected_messages)

Expand Down Expand Up @@ -175,7 +173,6 @@ def test_read_pose_mixed_binary(self, data_path):
expected_messages = [m for m in messages if isinstance(m, PoseMessage)]

reader = MixedLogReader(str(data_path))
assert reader.index is None
reader.filter_in_place((PoseMessage,))
self._check_results(reader, expected_messages)

Expand Down Expand Up @@ -220,10 +217,8 @@ def test_read_no_generate_index(self, data_path):
expected_messages = [m for m in messages if isinstance(m, PoseMessage)]

reader = MixedLogReader(str(data_path), generate_index=False)
assert reader.index is None
reader.filter_in_place((PoseMessage,))
self._check_results(reader, expected_messages)
assert reader.index is None

def test_read_with_index(self, data_path):
messages = self._generate_mixed_data(data_path)
Expand Down Expand Up @@ -256,16 +251,11 @@ def test_read_overwrite_index(self, data_path):
MixedLogReader.generate_index_file(str(data_path))
reader = MixedLogReader(str(data_path), ignore_index=True)

# By default, we will be generating an index file, overwriting the existing one, so the reader should have
# deleted the existing one.
assert reader.index is None
assert not os.path.exists(reader.index_path)

reader.filter_in_place((PoseMessage,))
self._check_results(reader, expected_messages)

# Check that we generated a new index.
assert os.path.exists(reader.index_path)
assert os.path.exists(FileIndex.get_path(data_path))
assert reader.index is not None and len(reader.index) == len(expected_messages)
assert len(reader._original_index) == len(messages)

Expand All @@ -275,17 +265,13 @@ def test_read_ignore_index(self, data_path):

MixedLogReader.generate_index_file(str(data_path))
reader = MixedLogReader(str(data_path), ignore_index=True, generate_index=False)
assert reader.index is None

# This time, we are not generating an index so we do _not_ delete the existing file and leave it intact.
assert reader.index is None
assert os.path.exists(reader.index_path)
assert os.path.exists(FileIndex.get_path(data_path))

reader.filter_in_place((PoseMessage,))
self._check_results(reader, expected_messages)

assert reader.index is None

# Note: TimeRange objects keep internal state, so we can't use them here since the state will remain across multiple
# calls for different use_index values. Instead we store range strings and parse them on each call.
@pytest.mark.parametrize("time_range", [
Expand Down Expand Up @@ -319,10 +305,8 @@ def _test_rewind(self, data_path, use_index):
_, message = next(reader)
self._check_message(message, expected_messages[i])

if use_index:
assert reader.index is not None
else:
assert reader.index is None

assert reader.index is not None

reader.rewind()
self._check_results(reader, expected_messages)
Expand Down Expand Up @@ -425,12 +409,8 @@ def _test_seek_to_eof(self, data_path, use_index):
_, message = next(reader)
self._check_message(message, expected_messages[i])

# Now jump to EOF. If we're generating an index file, this is illegal.
if use_index:
reader.seek_to_eof()
else:
with pytest.raises(ValueError):
reader.seek_to_eof()
# Now jump to EOF.
reader.seek_to_eof()

def test_seek_to_eof_no_index(self, data_path):
self._test_seek_to_eof(data_path, use_index=False)
Expand Down

0 comments on commit 7a068ed

Please sign in to comment.