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'
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
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()
async def s3() -> AsyncGenerator[S3, None]: s3 = S3( S3Config( endpoint_url='http://127.0.0.1:9000', aws_access_key_id='EXAMPLEACCESSKEY', aws_secret_access_key='EXAMPLESECRETKEY', )) app = BaseApplication(BaseConfig()) app.add('s3', s3) await app.start() yield s3 await app.stop()
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
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()
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()
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()
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
async def test_http(unused_tcp_port): class TestClient(Client): async def send(self, url: str) -> ClientResponse: return await self.request('GET', url) async def send_add_get(self, url: str) -> ClientResponse: return await self.request('GET', url) async def send_add_head(self, url: str) -> ClientResponse: return await self.request('HEAD', url) async def send_add_post(self, url: str) -> ClientResponse: return await self.request('POST', url) async def send_add_put(self, url: str) -> ClientResponse: return await self.request('PUT', url) async def send_add_patch(self, url: str) -> ClientResponse: return await self.request('PATCH', url) async def send_add_delete(self, url: str) -> ClientResponse: return await self.request('DELETE', url) class Handler(ServerHandler): async def prepare(self) -> None: self.server.add_route('GET', '/', self.home) self.server.add_head('/', self.test_head) self.server.add_get('/test_get', self.test_get) self.server.add_post('/test_post', self.test_post) self.server.add_patch('/test_patch', self.test_patch) self.server.add_put('/test_put', self.test_put) self.server.add_delete('/test_delete', self.test_delete) async def home(self, request: web.Request) -> web.Response: return web.Response(text='OK') async def test_get(self, request: web.Request) -> web.Response: body = request.method return web.Response(text=body) async def test_head(self, request: web.Request) -> web.Response: return web.Response() async def test_post(self, request: web.Request) -> web.Response: body = request.method return web.Response(text=body) async def test_patch(self, request: web.Request) -> web.Response: body = request.method return web.Response(text=body) async def test_put(self, request: web.Request) -> web.Response: body = request.method return web.Response(text=body) async def test_delete(self, request: web.Request) -> web.Response: body = request.method return web.Response(text=body) app = BaseApplication(BaseConfig()) app.add('srv', Server(ServerConfig(port=unused_tcp_port), Handler())) app.add('clt', TestClient()) url_test = 'http://127.0.0.1:%d' % unused_tcp_port await app.start() resp = await app.get('clt').send(url_test) assert resp.status == 200 assert await resp.text() == 'OK' resp_head = await app.get('clt').send_add_head(f'{url_test}/') assert resp_head.status == 200 resp_get = await app.get('clt').send_add_get(f'{url_test}/test_get') assert resp_get.status == 200 assert await resp_get.text() == 'GET' resp_post = await app.get('clt').send_add_post(f'{url_test}/test_post') assert resp_post.status == 200 assert await resp_post.text() == 'POST' resp_patch = await app.get('clt').send_add_patch(f'{url_test}/test_patch') assert resp_patch.status == 200 assert await resp_patch.text() == 'PATCH' resp_put = await app.get('clt').send_add_put(f'{url_test}/test_put') assert resp_put.status == 200 assert await resp_put.text() == 'PUT' resp_delete = await app.get('clt').send_add_delete( f'{url_test}/test_delete', ) assert resp_delete.status == 200 assert await resp_delete.text() == 'DELETE' await app.stop()
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()