async def test_insert_delete(self, proxy, db_pool: asyncpg.pool.Pool): proxy_obj = Proxy.create_from_url(url=proxy) proxy_db = ProxyDb(db_connect=db_pool, table_proxy=proxy_table) await proxy_db.insert_proxy(**proxy_obj.as_dict()) proxy_2_obj = Proxy.create_from_url(url=proxy) proxy_2_obj.login = '******' proxy_2_obj.password = '******' await proxy_db.update_proxy_pm(**proxy_2_obj.as_dict()) res = await proxy_db.select_proxy_pm(host=proxy_2_obj.host, port=proxy_2_obj.port) assert res['password'] == proxy_2_obj.password and res['login'] == proxy_2_obj.login await proxy_db.delete_proxy_pm(host=proxy_2_obj.host, port=proxy_2_obj.port) res = await proxy_db.select_proxy_pm(host=proxy_2_obj.host, port=proxy_2_obj.port) assert res is None
async def test_api(self, proxy, aiohttp_session): proxy = Proxy.create_from_url(proxy) api_location = ApiLocation(http_session=aiohttp_session) location = await api_location.find_location(proxy=proxy) assert isinstance(location, Location) for loc in location.keys: assert loc in location.as_dict()
async def test_select(self, proxy, db_pool: asyncpg.pool.Pool): proxy = Proxy.create_from_url(url=proxy) proxy_db = ProxyDb(db_connect=db_pool, table_proxy=proxy_table) await proxy_db.insert_proxy(**proxy.as_dict()) res = await proxy_db.select_proxy_pm(host=proxy.host, port=proxy.port) assert res['port'] == proxy.port and str(res['host']) == proxy.host async with db_pool.acquire() as conn: query = sqlalchemy.text('delete from proxy where (host = $1 and port = $2)') res = await conn.execute(query, proxy.host, proxy.port)
async def test_processing(self, proxy): queue_in = asyncio.Queue(2) queue_out = asyncio.Queue(2) proxy = Proxy.create_from_url(proxy) handler = TaskProxyCheckHandler(incoming_queue=queue_in, outgoing_queue=queue_out) await handler.processing_task(proxy) proxy_out = await asyncio.wait_for(queue_out.get(), 1) queue_out.task_done() assert isinstance(proxy_out, Proxy)
async def test_check(self, proxy): proxy = Proxy.create_from_url(proxy) check_proxy = await ProxyChecker.check(proxy=proxy) assert check_proxy.is_alive is True assert isinstance(check_proxy.latency, float) assert isinstance(check_proxy.as_dict(), dict) keys = ['host', 'port', 'scheme', 'login', 'password'] for k in keys: assert k in check_proxy.as_dict()
async def test_select_and_set_proxy_to_process(self, proxy, db_pool): proxy_obj = Proxy.create_from_url(url=proxy) proxy_db = ProxyDb(db_connect=db_pool, table_proxy=proxy_table) await proxy_db.insert_proxy(**proxy_obj.as_dict()) res = await proxy_db.select_and_set_proxy_to_process() for k, v in proxy_obj.as_dict().items(): assert str(res[k]) == str(v) assert res['in_process'] is False res = await proxy_db.select_proxy_pm(host=proxy_obj.host, port=proxy_obj.port) assert res['in_process'] is True await proxy_db.delete_proxy_pm(host=proxy_obj.host, port=proxy_obj.port)
async def test_start(self, proxy, db_pool: asyncpg.pool.Pool, caplog): import logging caplog.set_level(logging.DEBUG) queue_in = asyncio.Queue(2) proxy = Proxy.create_from_url(proxy) psql_db = ProxyDb(db_connect=db_pool, table_proxy=proxy_table) handler = TaskHandlerToDB(incoming_queue=queue_in, proxy_db=psql_db) await handler.start() assert handler.is_running() is True clear_test_data = self.clear_test_data(db_pool, proxy) await clear_test_data.__anext__() await queue_in.put(proxy) await asyncio.sleep(1) query = sqlalchemy.text(f"select * from proxy where host= $1 and port = $2") async with db_pool.acquire() as conn: res = await conn.fetchrow(query, proxy.host, proxy.port) assert str(res['host']) == proxy.host and res['port'] == proxy.port await clear_test_data.__anext__() handler.stop()
async def test_proxy_client(self, proxy): proxy = Proxy.create_from_url(proxy) async with ProxyClient(proxy=proxy) as sess: assert isinstance(sess._session, aiohttp.ClientSession) assert sess.closed is False assert sess.closed is True
def test_create_from_url(self, proxy): prx = Proxy.create_from_url(proxy) assert prx.url == proxy
async def test_get(self, proxy): proxy = Proxy.create_from_url(proxy) async with ProxyClient(proxy=proxy) as sess: answ = await sess.get() assert answ['status_response'] == 200 assert 'latency' in answ.keys()