Skip to content

Commit

Permalink
make tests a bit less patch-happy, ruff and pyright fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rerpha committed Dec 12, 2024
1 parent c2faea3 commit a9fb432
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 48 deletions.
4 changes: 2 additions & 2 deletions BlockServerToKafka/block_server_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def __init__(self, address: str, pvprefix: str, producer: ProducerWrapper) -> No

# Create the CA monitor callback
self.channel.add_masked_array_event(
ca.dbf_type_to_DBR_STS(self.channel.field_type()),
ca.dbf_type_to_DBR_STS(self.channel.field_type()), # pyright: ignore
0,
ca.DBE_VALUE,
ca.DBE_VALUE, # pyright: ignore
self.update,
None,
)
Expand Down
16 changes: 11 additions & 5 deletions BlockServerToKafka/forwarder_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,27 @@ class ForwarderConfig:
"""

def __init__(
self, topic: str, epics_protocol: Protocol = Protocol.CA, schema: str = "f144"
self,
topic: str,
epics_protocol: Protocol = Protocol.CA, # pyright: ignore
schema: str = "f144",
) -> None:
self.schema = schema
self.topic = topic
self.epics_protocol = epics_protocol

def _create_streams(self, pvs: List[str]) -> List[StreamInfo]:
return [StreamInfo(pv, self.schema, self.topic, self.epics_protocol, 0) for pv in pvs]
return [
StreamInfo(pv, self.schema, self.topic, self.epics_protocol, 0)
for pv in pvs
] # pyright: ignore

def create_forwarder_configuration(self, pvs: List[str]) -> bytes:
return serialise_fc00(UpdateType.ADD, self._create_streams(pvs))
return serialise_fc00(UpdateType.ADD, self._create_streams(pvs)) # pyright: ignore

def remove_forwarder_configuration(self, pvs: List[str]) -> bytes:
return serialise_fc00(UpdateType.REMOVE, self._create_streams(pvs))
return serialise_fc00(UpdateType.REMOVE, self._create_streams(pvs)) # pyright: ignore

@staticmethod
def remove_all_forwarder_configuration() -> bytes:
return serialise_fc00(UpdateType.REMOVEALL, [])
return serialise_fc00(UpdateType.REMOVEALL, []) # pyright: ignore
4 changes: 2 additions & 2 deletions BlockServerToKafka/kafka_producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(
server: str,
config_topic: str,
data_topic: str,
epics_protocol: Protocol = Protocol.CA,
epics_protocol: Protocol = Protocol.CA, # pyright: ignore
) -> None:
self.topic = config_topic
self.converter = ForwarderConfig(data_topic, epics_protocol)
Expand All @@ -51,7 +51,7 @@ def _set_up_producer(self, server: str) -> None:
)
except errors.NoBrokersAvailable:
print_and_log(f"No brokers found on server: {server[0]}")
except errors.ConnectionError:
except errors.KafkaConnectionError:
print_and_log("No server found, connection error")
except errors.InvalidConfigurationError:
print_and_log("Invalid configuration")
Expand Down
Empty file.
63 changes: 34 additions & 29 deletions BlockServerToKafka/test_modules/test_block_server_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,70 +21,75 @@
from BlockServerToKafka.block_server_monitor import BlockServerMonitor


@patch("CaChannel.CaChannel")
class TestBlockServerMonitor(unittest.TestCase):
test_address = "TEST_ADDRESS"
test_prefix = "TEST_PREFIX"

@patch("CaChannel.CaChannel")
@patch("CaChannel.CaChannel.searchw")
@patch("CaChannel.CaChannel.add_masked_array_event")
@patch("CaChannel.CaChannel.field_type")
@patch("CaChannel.CaChannel.pend_event")
def setUp(
self,
mock_ca_channel,
mock_search,
mock_add_array,
mock_field_type,
mock_pend_event,
):
def setUp(self):
self.mock_producer = MagicMock()
self.bs_monitor = BlockServerMonitor(
self.test_address, self.test_prefix, self.mock_producer
)

def test_WHEN_convert_one_char_to_string_THEN_returns_character(self):
def test_WHEN_convert_one_char_to_string_THEN_returns_character(
self,
mock_ca_channel,
):
c = "a"
arr = [ord(c)]
self.assertEqual(c, self.bs_monitor.convert_to_string(arr))
self.assertEqual(c, self.bs_monitor.convert_to_string(bytearray(arr)))

def test_WHEN_convert_many_chars_to_string_THEN_returns_characters(self):
def test_WHEN_convert_many_chars_to_string_THEN_returns_characters(
self, mock_ca_channel
):
chars = "hello world"
arr = [ord(c) for c in chars]
self.assertEqual(chars, self.bs_monitor.convert_to_string(arr))
self.assertEqual(chars, self.bs_monitor.convert_to_string(bytearray(arr)))

def test_WHEN_convert_chars_with_null_at_end_THEN_nulls_removed(self):
def test_WHEN_convert_chars_with_null_at_end_THEN_nulls_removed(
self,
mock_ca_channel,
):
chars = "hello world"
arr = [ord(c) for c in chars]
for i in range(3):
arr.append(0)
self.assertEqual(chars, self.bs_monitor.convert_to_string(arr))
self.assertEqual(chars, self.bs_monitor.convert_to_string(bytearray(arr)))

def test_WHEN_convert_chars_with_null_at_start_THEN_nulls_removed(self):
def test_WHEN_convert_chars_with_null_at_start_THEN_nulls_removed(
self,
mock_ca_channel,
):
chars = "hello world"
arr = [ord(c) for c in chars]
for i in range(3):
arr.insert(0, 0)
self.assertEqual(chars, self.bs_monitor.convert_to_string(arr))
self.assertEqual(chars, self.bs_monitor.convert_to_string(bytearray(arr)))

def test_WHEN_convert_chars_with_nulls_in_centre_THEN_nulls_removed(self):
def test_WHEN_convert_chars_with_nulls_in_centre_THEN_nulls_removed(
self, mock_ca_channel
):
chars = "hello world"
arr = [ord(c) for c in chars]
arr.insert(4, 0)
self.assertEqual(chars, self.bs_monitor.convert_to_string(arr))
self.assertEqual(chars, self.bs_monitor.convert_to_string(bytearray(arr)))

def test_WHEN_convert_nulls_THEN_empty_string_returned(self):
def test_WHEN_convert_nulls_THEN_empty_string_returned(
self,
mock_ca_channel,
):
arr = [0] * 10
self.assertEqual("", self.bs_monitor.convert_to_string(arr))
self.assertEqual("", self.bs_monitor.convert_to_string(bytearray(arr)))

def test_GIVEN_no_previous_pvs_WHEN_update_config_called_THEN_producer_is_called(
self,
self, mock_ca_channel
):
self.bs_monitor.update_config(["BLOCK"])
self.mock_producer.add_config.assert_called_once()

def test_GIVEN_no_previous_pvs_WHEN_update_config_called_THEN_producer_is_called_containing_block_name(
self,
self, mock_ca_channel
):
block = "BLOCK"
self.bs_monitor.update_config([block])
Expand All @@ -93,15 +98,15 @@ def test_GIVEN_no_previous_pvs_WHEN_update_config_called_THEN_producer_is_called
)

def test_GIVEN_previous_pvs_WHEN_update_config_called_with_same_pvs_THEN_producer_is_not_called(
self,
self, mock_ca_channel
):
block = "BLOCK"
self.bs_monitor.update_config([block])
self.bs_monitor.update_config([block])
self.mock_producer.add_config.assert_called_once()

def test_GIVEN_previous_pvs_WHEN_update_config_called_with_different_pvs_THEN_producer_is_called(
self,
self, mock_ca_channel
):
self.bs_monitor.update_config(["OLD_BLOCK"])
self.mock_producer.reset_mock()
Expand Down
41 changes: 31 additions & 10 deletions BlockServerToKafka/test_modules/test_forwarder_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,24 @@ def setUp(self):
self.config_with_two_blocks = [self.test_block_1, self.test_block_2]

def test_WHEN_new_forwarder_config_created_THEN_returns_valid_flatbuffers(self):
output = self.kafka_forwarder.create_forwarder_configuration(self.config_with_one_block)
output = self.kafka_forwarder.create_forwarder_configuration(
self.config_with_one_block
)
self.assertTrue(self.is_flatbuffers(output))

def test_WHEN_new_forwarder_config_created_THEN_returns_configuration_update_containing_add_command(
self,
):
raw_output = self.kafka_forwarder.create_forwarder_configuration(self.config_with_one_block)
raw_output = self.kafka_forwarder.create_forwarder_configuration(
self.config_with_one_block
)
output = deserialise_fc00(raw_output)
self.assertEqual(output.config_change, UpdateType.ADD)

def test_WHEN_forwarder_config_removed_THEN_output_has_correct_command_type(self):
raw_output = self.kafka_forwarder.remove_forwarder_configuration(self.config_with_one_block)
raw_output = self.kafka_forwarder.remove_forwarder_configuration(
self.config_with_one_block
)
output = deserialise_fc00(raw_output)
self.assertEqual(output.config_change, UpdateType.REMOVE)

Expand All @@ -69,7 +75,9 @@ def test_WHEN_all_pvs_removed_THEN_output_has_correct_command_type(self):
def test_WHEN_new_forwarder_config_created_THEN_returns_flatbuffer_containing_streams_with_channels_and_converters(
self,
):
raw_output = self.kafka_forwarder.create_forwarder_configuration(self.config_with_one_block)
raw_output = self.kafka_forwarder.create_forwarder_configuration(
self.config_with_one_block
)
output = deserialise_fc00(raw_output)
self.assertNotEqual(0, len(output[1]))
for stream in output[1]:
Expand All @@ -80,8 +88,13 @@ def test_WHEN_new_forwarder_config_created_THEN_returns_flatbuffer_containing_st
def test_GIVEN_using_version_4_WHEN_new_forwarder_config_created_THEN_returns_JSON_containing_streams_with_pva_channel_type(
self,
):
kafka_version_4 = ForwarderConfig(epics_protocol=Protocol.PVA, topic=self.test_topic)
raw_output = kafka_version_4.create_forwarder_configuration(self.config_with_one_block)
kafka_version_4 = ForwarderConfig(
epics_protocol=Protocol.PVA, # pyright: ignore noqa
topic=self.test_topic,
)
raw_output = kafka_version_4.create_forwarder_configuration(
self.config_with_one_block
)
output = deserialise_fc00(raw_output)
self.assertNotEqual(0, len(output[1]))
for stream in output[1]:
Expand All @@ -90,7 +103,9 @@ def test_GIVEN_using_version_4_WHEN_new_forwarder_config_created_THEN_returns_JS
def test_GIVEN_configuration_with_one_block_WHEN_new_forwarder_config_created_THEN_returns_JSON_containing_one_stream(
self,
):
raw_output = self.kafka_forwarder.create_forwarder_configuration(self.config_with_one_block)
raw_output = self.kafka_forwarder.create_forwarder_configuration(
self.config_with_one_block
)
output = deserialise_fc00(raw_output)
self.assertEqual(1, len(output[1]))

Expand All @@ -106,7 +121,9 @@ def test_GIVEN_configuration_with_two_block_WHEN_new_forwarder_config_created_TH
def test_GIVEN_configuration_with_one_block_WHEN_new_forwarder_config_created_THEN_returns_block_pv_string(
self,
):
raw_output = self.kafka_forwarder.create_forwarder_configuration(self.config_with_one_block)
raw_output = self.kafka_forwarder.create_forwarder_configuration(
self.config_with_one_block
)
output = deserialise_fc00(raw_output)
stream = output[1][0]
self.assertEqual(self.test_block_1, stream.channel)
Expand All @@ -122,13 +139,17 @@ def test_GIVEN_configuration_with_two_blocks_WHEN_new_forwarder_config_created_T
self.assertTrue(blk in [stream.channel for stream in output[1]])

def test_WHEN_removed_old_forwarder_THEN_JSON_returns_valid(self):
output = self.kafka_forwarder.remove_forwarder_configuration(self.config_with_one_block)
output = self.kafka_forwarder.remove_forwarder_configuration(
self.config_with_one_block
)
self.assertTrue(self.is_flatbuffers(output))

def test_GIVEN_configuration_with_one_block_WHEN_removed_old_forwarder_THEN_returns_JSON_containing_block_pv_string(
self,
):
raw_output = self.kafka_forwarder.remove_forwarder_configuration(self.config_with_one_block)
raw_output = self.kafka_forwarder.remove_forwarder_configuration(
self.config_with_one_block
)
output = deserialise_fc00(raw_output)
self.assertEqual(self.test_block_1, output[1][0].channel)

Expand Down

0 comments on commit a9fb432

Please sign in to comment.