Skip to content

Commit

Permalink
Merge pull request #229 from simonsobs/verify-data-type-bool-patch
Browse files Browse the repository at this point in the history
Update feed message data check to handle bools
  • Loading branch information
BrianJKoopman authored Oct 8, 2021
2 parents e937839 + 88c3c80 commit e7c9351
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ocs/ocs_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,19 @@ def verify_message_data_type(value):
"""
valid_types = (float, int, str)

# separate bool checks since bool is a subclass of int
# multi-sample check
if isinstance(value, list):
if not all(isinstance(x, valid_types) for x in value):
if (any(isinstance(x, bool) for x in value)
or not all(isinstance(x, valid_types) for x in value)):
type_set = set([type(x) for x in value])
invalid_types = type_set.difference(valid_types)
raise TypeError("message 'data' block contains invalid data" +
f"types: {invalid_types}")

# single sample check
else:
if not isinstance(value, valid_types):
if isinstance(value, bool) or not isinstance(value, valid_types):
invalid_type = type(value)
raise TypeError("message 'data' block contains invalid " +
f"data type: {invalid_type}")
Expand Down
30 changes: 30 additions & 0 deletions tests/test_ocs_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,36 @@ def test_str_single_sample_input(self):

test_feed.publish_message(test_message)

def test_bool_single_sample_input(self):
mock_agent = MagicMock()
test_feed = ocs_feed.Feed(mock_agent, 'test_feed', record=True)

test_message = {
'block_name': 'test',
'timestamp': time.time(),
'data': {
'key1': True,
}
}

with pytest.raises(TypeError):
test_feed.publish_message(test_message)

def test_bool_multi_sample_input(self):
mock_agent = MagicMock()
test_feed = ocs_feed.Feed(mock_agent, 'test_feed', record=True)

test_message = {
'block_name': 'test',
'timestamps': [time.time(), time.time()+1, time.time()+2],
'data': {
'key1': [True, False, True],
}
}

with pytest.raises(TypeError):
test_feed.publish_message(test_message)

def test_str_multi_sample_input(self):
"""Passing multiple points, including invalid datatypes,
should cause a TypeError upon publishing.
Expand Down

0 comments on commit e7c9351

Please sign in to comment.