async def test_on_receive_handler(self): app = MockApp("mock_app") await app.on_receive(generic_message_factory((35, "a"))) await app.on_receive(generic_message_factory((35, "b"))) # Wait for tasks to complete tasks = asyncio.all_tasks() await asyncio.wait(tasks, timeout=0.1) assert app.counter["a"] == 1 assert app.counter["b"] == 0 assert app.counter["unhandled"] == 1
async def test_on_receive_handler_raises_exception_if_message_not_returned( self): with pytest.raises(MessageProcessingError): app = MockApp("mock_app") await app.on_receive(generic_message_factory((35, "z"))) # Wait for separate tasks to complete tasks = asyncio.all_tasks() await asyncio.wait(tasks, timeout=0.1)
def test_regression_for_getting_fields_for_message_with_repeating_groups( self): """ Regression where repeating group fields for market data request messages (type V) were not extracted correctly """ mdr_message = generic_message_factory( ( connection.protocol.Tag.MsgType, connection.protocol.MsgType.MarketDataRequest, ), (connection.protocol.Tag.MDReqID, uuid.uuid4().hex), ( connection.protocol.Tag.SubscriptionRequestType, "h", ), # Historical request (connection.protocol.Tag.MarketDepth, 0), ) mdr_message[connection.protocol.Tag.NoRelatedSym] = Group( (connection.protocol.Tag.NoRelatedSym, 1), (connection.protocol.Tag.SecurityID, "test123"), template=[connection.protocol.Tag.SecurityID], ) mdr_message[connection.protocol.Tag.NoMDEntryTypes] = Group( (connection.protocol.Tag.NoMDEntryTypes, 1), (connection.protocol.Tag.MDEntryType, "h"), template=[connection.protocol.Tag.MDEntryType], ) mdr_message[9956] = 1 mdr_message[9957] = 3 mdr_message[9958] = int(datetime.utcnow().timestamp()) mdr_message[9959] = int(datetime.utcnow().timestamp()) mdr_message[9960] = 1 assert len(mdr_message) == 13 assert len(list(mdr_message.values())) == 13 assert all(field in mdr_message for field in [ 35, 262, 263, 264, 146, 48, 267, 269, 9956, 9957, 9958, 9959, 9960, ])
async def on_receive(self, message: RawMessage) -> FIXMessage: fields = ( message.BeginString, message.BodyLength, message.MsgType, message.MsgSeqNum, *Field.fields_frombytes(message.encoded_body), message.CheckSum, ) message = generic_message_factory(*fields, group_templates=self.group_templates) return message
def test_encode_message_nested_group(self, encoder_app, nested_parties_group): m = generic_message_factory((35, "a"), (34, 1), (2, "bb")) m[nested_parties_group.tag] = nested_parties_group m.SendingTime = datetime.utcnow().strftime( settings.DATETIME_FORMAT)[:-3] # Compare just the group-related bytes. assert encoder_app.encode_message(m)[82:-7] == ( b"539=2\x01" # Header + b"524=a\x01525=aa\x01538=aaa\x01" # Group identifier + b"804=2\x01545=c\x01805=cc\x01545=d\x01805=dd\x01" # First group + b"524=b\x01525=bb\x01538=bbb\x01" # First nested group + b"804=2\x01545=e\x01805=ee\x01545=f\x01805=ff\x01" # Second group ) # Second nested group
def test_decode_message_raises_exception_if_no_beginstring( self, encoder_app, decoder_app): with pytest.raises(ParsingError): m = generic_message_factory( ( connection.protocol.Tag.MsgType, connection.protocol.MsgType.TestRequest, ), (connection.protocol.Tag.MsgSeqNum, 1), (connection.protocol.Tag.TestReqID, "a"), ( connection.protocol.Tag.SendingTime, datetime.utcnow().strftime(settings.DATETIME_FORMAT)[:-3], ), ) data = encoder_app.encode_message(m).replace( b"8=" + utils.encode(settings.BEGIN_STRING), b"") decoder_app.decode_message(data)
def email_message(): faker = Faker() return generic_message_factory( (connection.protocol.Tag.MsgType, connection.protocol.MsgType.Email), (connection.protocol.Tag.MsgSeqNum, 1), ( connection.protocol.Tag.SenderCompID, settings.CONNECTIONS[connection.name]["SENDER"], ), (connection.protocol.Tag.SendingTime, "20181206-10:24:27.018"), ( connection.protocol.Tag.TargetCompID, settings.CONNECTIONS[connection.name]["TARGET"], ), (connection.protocol.Tag.NoLinesOfText, 1), (connection.protocol.Tag.Text, "abc"), (connection.protocol.Tag.EmailType, 0), (connection.protocol.Tag.Subject, "Test message"), (connection.protocol.Tag.EmailThreadID, faker.pyint()), )
def test_encode_message_invalid(self, encoder_app): with pytest.raises(ValidationError): encoder_app.encode_message( generic_message_factory((1, "a"), (2, "b")))