Beispiel #1
0
async def test_prepare(loop, postgres_url):
    app = BaseApplication(BaseConfig())
    app.add(
        'db',
        Postgres(
            PostgresConfig(url=postgres_url, log_result=True, log_query=True)),
    )
    await app.start()
    db: Postgres = app.get('db')  # type: ignore

    async with db.connection() as conn:
        st = await conn.prepare('SELECT $1::int as a', query_name='db4')
        row = await st.query_one(10)
        assert row['a'] == 10

        rows = await st.query_all(20)
        assert len(rows) == 1
        assert rows[0]['a'] == 20

        st2 = await conn.prepare('SELECT $1::text as a')
        row = await st2.query_one('30')
        assert row['a'] == '30'

        rows = await st2.query_all('30')
        assert len(rows) == 1
        assert rows[0]['a'] == '30'
Beispiel #2
0
async def create_lock(url: Optional[str]) -> Lock:
    app = BaseApplication(BaseConfig())
    app.add(
        'lock',
        Lock(LockConfig(url=url)),
    )
    await app.start()
    lock: Lock = app.get('lock')  # type: ignore
    return lock
Beispiel #3
0
async def test_sftp(loop) -> None:
    app = BaseApplication(BaseConfig())
    app.add(
        "sftp",
        SftpClient(
            SftpClientConfig(
                url="sftp://*****:*****@localhost:2222/upload")),
    )
    await app.start()

    sftp: SftpClient = app.get("sftp")  # type: ignore
    test_data = "Test data"

    base_remote_path = await sftp.getcwd()
    assert base_remote_path == "/upload"

    remote_path = os.path.join(base_remote_path, str(uuid.uuid4()))
    await sftp.mkdir(remote_path)
    await sftp.chdir(remote_path)

    with TemporaryDirectory() as local_path:
        local_file_out = os.path.join(local_path, f"{uuid.uuid4()}_out.txt")
        local_file_ren = os.path.join(local_path, f"{uuid.uuid4()}_ren.txt")
        local_file_in = os.path.join(local_path, f"{uuid.uuid4()}_in.txt")
        with open(local_file_out, "w") as f:
            f.write(test_data)

        remote_file_out = os.path.join(remote_path,
                                       os.path.basename(local_file_out))

        remote_file_ren = os.path.join(remote_path,
                                       os.path.basename(local_file_ren))

        await sftp.put(local_file_out, remote_file_out)
        assert await sftp.exists(remote_file_out) is True
        assert os.path.basename(local_file_out) in await sftp.listdir(
            remote_path)

        await sftp.get(remote_file_out, local_file_in)
        with open(local_file_in) as f:
            assert f.read() == test_data

        await sftp.rename(remote_file_out, remote_file_ren)
        assert await sftp.exists(remote_file_ren) is True
        assert os.path.basename(remote_file_ren) in [
            path.filename for path in await sftp.readdir(remote_path)
        ]

        await sftp.remove(remote_file_ren)
        assert await sftp.exists(remote_file_ren) is False
        assert os.path.basename(remote_file_ren) not in await sftp.listdir(
            remote_path)

    await app.stop()
Beispiel #4
0
async def startup() -> [BaseApplication, Oracle]:
    app = BaseApplication(BaseConfig())
    app.add(
        'db',
        Oracle(
            OracleConfig(
                user=oracle_user,
                password=oracle_pwd,
                dsn=oracle_host,
            )),
    )
    await app.start()
    db: Oracle = app.get('db')  # type: ignore
    return app, db
Beispiel #5
0
async def test_statement_cache_size(loop, postgres_url):
    app = BaseApplication(BaseConfig())
    app.add(
        'db',
        Postgres(
            PostgresConfig(
                url=postgres_url,
                log_result=True,
                log_query=True,
                statement_cache_size=0,
            )),
    )
    await app.start()
    db: Postgres = app.get('db')  # type: ignore
    async with db.connection() as conn:
        assert conn._conn._stmt_cache._max_size == 0
    await app.stop()
Beispiel #6
0
async def test_pika(loop, rabbitmq_url):

    messages: List[Tuple[bytes]] = []

    class TestPubChg(PikaChannel):
        name = 'pub'

    class TestCnsChg(PikaChannel):
        name = 'sub'

        async def prepare(self) -> None:
            await self.exchange_declare('myexchange', durable=False)
            await self.queue_declare('myqueue', durable=False)
            await self.queue_bind('myqueue', 'myexchange', '')
            await self.qos(prefetch_count=1)

        async def start(self) -> None:
            await self.consume('myqueue', self.message)

        async def message(
            self, body: bytes, deliver: Deliver, proprties: Properties
        ) -> None:
            await self.ack(delivery_tag=deliver.delivery_tag)
            messages.append((body,))

    app = BaseApplication(BaseConfig())
    app.add(
        'mq',
        Pika(
            PikaConfig(url=rabbitmq_url),
            [
                lambda: TestPubChg(PikaChannelConfig()),
                lambda: TestCnsChg(PikaChannelConfig()),
            ],
        ),
    )
    await app.start()
    mq: Pika = app.get('mq')  # type: ignore

    await mq.channel('pub').publish('myexchange', '', 'testmsg')

    await wait_for(lambda: len(messages) > 0)
    assert messages == [(b'testmsg',)]

    await app.stop()
Beispiel #7
0
async def test_base(loop, postgres_url):
    app = BaseApplication(BaseConfig())
    app.add(
        'db',
        Postgres(
            PostgresConfig(url=postgres_url, log_result=True, log_query=True)),
    )
    await app.start()
    db: Postgres = app.get('db')  # type: ignore

    res = await db.execute('SELECT $1::int as a', 10, query_name='db1')
    assert 'SELECT 1' == res

    res = await db.query_one('SELECT $1::int as a', 10, query_name='db2')
    assert isinstance(res, asyncpg.Record)
    assert res['a'] == 10

    # json
    res = await db.query_one('SELECT $1::json as a', {'a': 1})
    assert isinstance(res, asyncpg.Record)
    assert res['a'] == {'a': 1}

    # jsonb
    res = await db.query_one('SELECT $1::jsonb as a', {'a': 1})
    assert isinstance(res, asyncpg.Record)
    assert res['a'] == {'a': 1}

    res = await db.query_one('SELECT 1 as a WHERE FALSE')
    assert res is None

    res = await db.query_all('SELECT $1::int as a', 10, query_name='db3')
    assert isinstance(res, list)
    assert len(res) == 1
    assert isinstance(res[0], asyncpg.Record)
    assert res[0]['a'] == 10

    res = await db.query_all('SELECT 1 as a WHERE FALSE')
    assert isinstance(res, list)
    assert len(res) == 0

    await app.stop()
Beispiel #8
0
async def test_xact(loop, postgres_url):
    app = BaseApplication(BaseConfig())
    app.add(
        'db',
        Postgres(
            PostgresConfig(url=postgres_url, log_result=True, log_query=True)),
    )
    await app.start()
    db: Postgres = app.get('db')  # type: ignore

    async with db.connection() as conn:
        await conn.execute('CREATE TEMPORARY TABLE _test_xact(id int)')
        try:
            async with conn.xact():
                await conn.execute('INSERT INTO _test_xact(id) VALUES (12)')
                r = await conn.query_one('SELECT COUNT(*) c FROM _test_xact')
                assert r['c'] == 1
                raise Exception('rollback')
        except Exception as err:
            assert str(err) == 'rollback', err

        r = await conn.query_one('SELECT COUNT(*) c FROM _test_xact')
        assert r['c'] == 0
Beispiel #9
0
async def test_dead_letter_exchange(loop, rabbitmq_url):
    messages: List[Tuple[bytes]] = []
    queue: str

    class TestPubChg(PikaChannel):
        name = 'pub'

        async def prepare(self) -> None:
            await self.exchange_declare('myexchange1', durable=False)
            await self.queue_declare('myqueue1', durable=False)
            await self.queue_bind('myqueue1', 'myexchange1', '')

            q = await self.queue_declare(
                '',
                exclusive=True,
                arguments={
                    'x-dead-letter-exchange': '',
                    'x-dead-letter-routing-key': 'myqueue1',
                },
            )
            self.queue = q.method.queue

        async def send(self, body: bytes, expiration: str):
            await self.publish(
                '', self.queue, body, BasicProperties(expiration=expiration)
            )

    class TestCnsChg(PikaChannel):
        name = 'sub'

        async def prepare(self) -> None:
            await self.qos(prefetch_count=1)

        async def start(self) -> None:
            await self.consume('myqueue1', self.message)

        async def message(
            self, body: bytes, deliver: Deliver, proprties: Properties
        ) -> None:
            await self.ack(delivery_tag=deliver.delivery_tag)
            messages.append((body, proprties))

    app = BaseApplication(BaseConfig())
    app.add(
        'mq',
        Pika(
            PikaConfig(url=rabbitmq_url),
            [
                lambda: TestPubChg(PikaChannelConfig()),
                lambda: TestCnsChg(PikaChannelConfig()),
            ],
        ),
    )
    await app.start()
    mq: Pika = app.get('mq')  # type: ignore

    await mq.channel('pub').send(b'testmsg', '1')

    await wait_for(lambda: len(messages) > 0)

    assert messages[0][1].headers['x-death'][0]['count'] == 1
    assert repr(messages[0][1].headers['x-death'][0]['count']) == '1L'

    await app.stop()