def test_transport_registry_get_event_transports(redis_default_config): registry = TransportRegistry().load_config(redis_default_config) # See comment in test_transport_registry_get_rpc_transports registry.set_event_transport("redis1", RedisEventTransport, RedisEventTransport.Config(batch_size=99), Config.default()) registry.set_event_transport("redis2", RedisEventTransport, RedisEventTransport.Config(batch_size=99), Config.default()) registry.set_event_transport("debug1", DebugEventTransport, DebugEventTransport.Config(), Config.default()) registry.set_event_transport("debug2", DebugEventTransport, DebugEventTransport.Config(), Config.default()) transports = registry.get_event_transports( ["default", "foo", "bar", "redis1", "redis2", "debug1", "debug2"]) default_transport = registry.get_event_transport("default") redis_transport = registry.get_event_transport("redis1") debug_transport = registry.get_event_transport("debug1") assert set(transports[default_transport]) == {"default", "foo", "bar"} assert set(transports[debug_transport]) == {"debug1", "debug2"} assert set(transports[redis_transport]) == {"redis1", "redis2"}
def test_equal(dummy_pool): """Test the __eq__ method""" assert dummy_pool == TransportPool( transport_class=DebugEventTransport, transport_config=DebugEventTransport.Config(), config=Config.default(), )
def transaction_transport(aiopg_connection, aiopg_cursor, loop): transport = TransactionalEventTransport(DebugEventTransport()) block(transport.set_connection(aiopg_connection, aiopg_cursor), loop=loop, timeout=1) block(transport.database.migrate(), loop=loop, timeout=1) return transport
def dummy_bus(loop, redis_server_url): # fmt: off dummy_bus = lightbus.creation.create( config=RootConfig( apis={ 'default': ApiConfig( rpc_transport=RpcTransportSelector( debug=DebugRpcTransport.Config()), result_transport=ResultTransportSelector( debug=DebugResultTransport.Config()), event_transport=EventTransportSelector( debug=DebugEventTransport.Config()), ) }, bus=BusConfig( schema=SchemaConfig(transport=SchemaTransportSelector( redis=RedisSchemaTransport.Config( url=redis_server_url)), ))), plugins=[], ) # fmt: on yield dummy_bus try: dummy_bus.client.close() except BusAlreadyClosed: pass
async def dummy_pool(): pool = TransportPool( transport_class=DebugEventTransport, transport_config=DebugEventTransport.Config(), config=Config.default(), ) yield pool await pool.close()
def test_hash_equal(dummy_pool): """Test the __hash__ method""" assert hash(dummy_pool) == hash( TransportPool( transport_class=DebugEventTransport, transport_config=DebugEventTransport.Config(), config=Config.default(), ))
def test_init_multiple_transports(dummy_bus: BusPath, aiopg_connection): registry = dummy_bus.client.transport_registry registry.set_event_transport( "another_api", TransactionalEventTransport(DebugEventTransport())) with pytest.raises(ApisMustUseSameTransport): lightbus_set_database(dummy_bus, aiopg_connection, apis=["some_api", "another_api"])
def test_get_all_transports(redis_default_config): registry = TransportRegistry().load_config(redis_default_config) # Is equal to the default transport, so will not be included # in the get_all_transports() return value registry.set_event_transport("another", RedisEventTransport, RedisEventTransport.Config(), Config.default()) registry.set_event_transport("foo", DebugEventTransport, DebugEventTransport.Config(), Config.default()) # redis rpc + redis result + redis event + foo debug transport (above) = 4 assert len(registry.get_all_transports()) == 4
async def make_it(event_messages): async def dummy_consume_method(*args, **kwargs): for event_message in event_messages: yield event_message yield True transport = TransactionalEventTransport(DebugEventTransport()) # start_transaction=False, as we start a transaction below (using BEGIN) await transport.set_connection(aiopg_connection, aiopg_cursor) await aiopg_cursor.execute( "BEGIN -- transaction_transport_with_consumer") await transport.database.migrate() # Commit the migrations await aiopg_cursor.execute( "COMMIT -- transaction_transport_with_consumer") transport.child_transport.consume = dummy_consume_method return transport
def test_transport_registry_get_event_transports(redis_default_config): registry = TransportRegistry().load_config(redis_default_config) debug_transport = DebugEventTransport() redis_transport = RedisEventTransport(service_name="foo", consumer_name="bar") registry.set_event_transport("redis1", redis_transport) registry.set_event_transport("redis2", redis_transport) registry.set_event_transport("debug1", debug_transport) registry.set_event_transport("debug2", debug_transport) transports = registry.get_event_transports( ["default", "foo", "bar", "redis1", "redis2", "debug1", "debug2"] ) default_redis_transport = registry.get_event_transport("default") transports = dict(transports) assert set(transports[default_redis_transport]) == {"default", "foo", "bar"} assert set(transports[debug_transport]) == {"debug1", "debug2"} assert set(transports[redis_transport]) == {"redis1", "redis2"}
def test_get_all_transports(redis_default_config): registry = TransportRegistry().load_config(redis_default_config) registry.set_event_transport("another", registry.get_event_transport("default")) registry.set_event_transport("foo", DebugEventTransport()) assert len(registry.get_all_transports()) == 4
async def test_set_connection_already_set(aiopg_connection, aiopg_cursor): transport = TransactionalEventTransport(DebugEventTransport()) await transport.set_connection(aiopg_connection, aiopg_cursor) with pytest.raises(ConnectionAlreadySet): await transport.set_connection(aiopg_connection, aiopg_cursor)
async def test_set_connection_ok(aiopg_connection, aiopg_cursor): transport = TransactionalEventTransport(DebugEventTransport()) await transport.set_connection(aiopg_connection, aiopg_cursor) assert transport.connection == aiopg_connection assert transport.cursor == aiopg_cursor assert await active_transactions() == 0
async def transaction_transport(aiopg_connection, aiopg_cursor, loop): transport = TransactionalEventTransport(DebugEventTransport()) await transport.set_connection(aiopg_connection, aiopg_cursor) await transport.database.migrate() return transport
async def test_commit_and_finish_no_database(): transport = TransactionalEventTransport(DebugEventTransport()) with pytest.raises(DatabaseNotSet): await transport.commit_and_finish()