def test_one_message(self): ## assuming only one message will be sent by the obd parsed_message = parse_message(self.message) print(parsed_message) self.assertIsInstance(parsed_message, dict) self.assertEqual(parsed_message['protocol'], '01') self.assertEqual(parsed_message['device_id'], '861585040983700') self.assertEqual(parsed_message['timestamp'], '201910130120')
def test_parse_message_with_missing_argument(): with pytest.raises(MissingArgumentException) as excinfo: command = 'PUT' key = 'TEST' value = 1 bad_data = '{};{};{}'.format(command, key, value) p_command, p_key, p_value = parse_message(bad_data) assert 'Wrong number of arguments!', str(excinfo.value)
def test_invalid_message(self): # message without header parsed_message = parse_message(self.message[1:]) self.assertIsInstance(parsed_message, dict) self.assertIn('error', parsed_message) self.assertEqual(parsed_message['error'], "invalid message") # message without tail parsed_message = parse_message(self.message[:-1]) self.assertIsInstance(parsed_message, dict) self.assertIn('error', parsed_message) self.assertEqual(parsed_message['error'], "invalid message") #random string parsed_message = parse_message("Czk9cQyp2hiQSe1wsO0m") self.assertIsInstance(parsed_message, dict) self.assertIn('error', parsed_message) self.assertEqual(parsed_message['error'], "invalid message")
def test_parser(line): parsed = parser.parse_message(line) assert parsed is not None, f'"{line}" was not parsed properly' for attr in [ 'text', 'name', 'modechar', 'month', 'day', 'hour', 'minute', 'second' ]: assert parsed.group( attr) is not None, f'{attr} was not properly parsed (is None)'
def test_parse_message_success(): command = 'PUT' key = 'TEST' value = 1 value_type = 'INT' good_data = '{};{};{};{}'.format(command, key, value, value_type) p_command, p_key, p_value = parse_message(good_data) assert command, p_command assert key, p_key assert value, p_value
async def on_message(message): if message.author == discord_client.user: return if not validate_message(message.content): return response = parse_message(message.content) await message.channel.send(response)
def data_receive_callback(message, sender_addr): if not sender_addr in known_modules: print("Unknown sender", sender_addr, flush=True) return parsed_data = parse_message(message) write_data_to_file(message, parsed_data, database_name) if parsed_data == None: return print("Received data:", parsed_data, flush=True) ifdb_write(parsed_data)
def test_parse_message_gps(self): time_ms = 12345 sensor_id = 7 lat, lng = 457811111, -1085038888 bstrm = struct.pack('<IBii', time_ms, sensor_id, lat, lng) self.assertEqual(parser.parse_message(bstrm), { 'measurement': 'gps1', 'time': 12345, 'fields': { 'coords': '45°46\'52.00"N, 108°30\'14.00"W' } })
def main(): SOCKET.bind((HOST, PORT)) SOCKET.listen(1) while True: try: connection, address = SOCKET.accept() except KeyboardInterrupt: exit_gracefully() logging.info('New connection from [{}]'.format(address)) try: data = connection.recv(4096).decode() command, key, value = parse_message(data) if command == 'STATS': response = COMMAND_HANDLERS['STATS']() elif command in ( 'GET', 'GETLIST', 'INCREMENT', 'DELETE' ): response = COMMAND_HANDLERS[command](key) elif command in ( 'PUT', 'PUTLIST', 'APPEND', ): response = COMMAND_HANDLERS[command](key, value) else: response = (False, 'Unknown command type [{}]'.format(command)) COMMAND_HANDLERS['UPDATE_STATS'](command, response[0]) except MissingArgumentException as e: connection.sendall('False;{}'.format(e)) except UnicodeDecodeError: logging.info("Client disconneted") finally: if 'response' in locals(): connection.sendall('{};{}'.format(response[0], response[1])) connection.close()