Exemplo n.º 1
0
async def test_datastore_setup(loop, empty_db, dsn):
    async with create_engine(dsn, loop=loop, timeout=5) as engine:
        ds = PostgresDataStore()
        ds.engine = engine
        controller = Controller(ds)
        async with ds.connection() as conn:
            action = Action("*****@*****.**", None, Verbs.ADD)
            conv_id = await controller.act(action, subject="the subject")
            cds = ds.new_conv_ds(conv_id, conn)
            props = await cds.get_core_properties()
            assert props["subject"] == "the subject"
Exemplo n.º 2
0
async def test_datastore_rollback(loop, empty_db, dsn, timestamp):
    async with create_engine(dsn, loop=loop, timeout=5) as engine:
        ds = PostgresDataStore()
        ds.engine = engine
        controller = Controller(ds)
        line = 0
        with pytest.raises(ConversationNotFound):
            async with controller.ds.connection() as conn:
                conversation = dict(conv_id="x", creator="x", subject="x", ref="x", timestamp=timestamp, status="draft")
                await conn.execute(sa_conversations.insert().values(**conversation))
                con_count = await conn.scalar(sa_conversations.count())
                assert con_count == 1
                cds = ds.new_conv_ds("123", conn)
                line = 1
                await cds.get_core_properties()
                line = 2

        assert line == 1  # check the above snippet gets to the right place
        # connection above should rollback on ConversationNotFound so there should now be no conversations
        async with controller.ds.connection() as conn:
            con_count = await conn.scalar(sa_conversations.count())
            assert con_count == 0
Exemplo n.º 3
0
def test_prepare_database(pg_conn):
    settings, cur = pg_conn
    cur.execute('SELECT EXISTS (SELECT datname FROM pg_catalog.pg_database WHERE datname=%s)', (settings.PG_DATABASE,))
    assert cur.fetchone()[0] is False

    loop = asyncio.new_event_loop()

    def count_convs(_ctrl):
        retrieval = Retrieval('*****@*****.**', verb=RVerbs.LIST)
        return len(loop.run_until_complete(_ctrl.retrieve(retrieval)))

    assert prepare_database(settings, skip_existing=True) is False

    cur.execute('SELECT EXISTS (SELECT datname FROM pg_catalog.pg_database WHERE datname=%s)', (settings.PG_DATABASE,))
    assert cur.fetchone()[0] is True

    ds = PostgresDataStore(settings, loop)
    loop.run_until_complete(ds.create_engine())
    ctrl = Controller(ds)
    loop.run_until_complete(ctrl.act(Action('*****@*****.**', None, Verbs.ADD), subject='first conversation'))
    assert count_convs(ctrl) == 1
    loop.run_until_complete(ds.finish())

    assert prepare_database(settings, skip_existing=True) is True

    # check conversation still exists as we haven't recreated the database
    ds = PostgresDataStore(settings, loop)
    loop.run_until_complete(ds.create_engine())
    assert count_convs(Controller(ds)) == 1
    loop.run_until_complete(ds.finish())

    assert prepare_database(settings, skip_existing=False) is None

    # check conversation doesn't exists as we have recreated the database
    ds = PostgresDataStore(settings, loop)
    loop.run_until_complete(ds.create_engine())
    assert count_convs(Controller(ds)) == 0
    loop.run_until_complete(ds.finish())