Skip to content

Commit

Permalink
chore: use pytest-aiohttp instead of tornado in tests
Browse files Browse the repository at this point in the history
RHINENG-14625
  • Loading branch information
psegedy authored and jdobes committed Dec 2, 2024
1 parent aca7e48 commit 48372d7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 24 deletions.
7 changes: 3 additions & 4 deletions tests/notificator_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import pytest
from aiohttp import test_utils
from aiohttp import web
from tornado.httpclient import AsyncHTTPClient

from notificator.app import CacheHandler
from notificator.app import create_notificator_app
Expand Down Expand Up @@ -36,14 +35,14 @@ async def notificator_server():
site = web.TCPSite(runner, "0.0.0.0", port)
await site.start()

yield f"http://localhost:{port}"
yield app

await runner.cleanup()


@pytest.fixture()
def http_client():
async def http_client(notificator_server, aiohttp_client):
"""Http client fixture"""
client = AsyncHTTPClient()
client = await aiohttp_client(notificator_server)
yield client
client.close()
9 changes: 3 additions & 6 deletions tests/notificator_tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import asyncio

import pytest

from notificator.notificator import NOTIFICATOR_PERIOD
from notificator.notificator import NotificationType
from notificator.notificator import NotificatorConditions
Expand All @@ -25,17 +23,16 @@ async def _init_notificator(self, pool):
await queue.init()
return conditions, queue

@pytest.mark.asyncio
async def test_cache_handler(self, asyncpg_pool, notificator_server, http_client):
async def test_cache_handler(self, asyncpg_pool, http_client):
"""Test cache refresh handler"""
_, queue = await self._init_notificator(asyncpg_pool)

# these notified accounts are not in db
queue.notified_accounts[(1337, 1)] = (NotificationType.CVSS_NOTIFICATION, NotificationType.EXPLOITS_NOTIFICATION)
queue.notified_accounts[(333, 1)] = NotificationType.CVSS_NOTIFICATION

resp = await http_client.fetch(f"{notificator_server}/api/v1/cache", method="PUT", body="")
assert resp.code == 200
resp = await http_client.put("/api/v1/cache", data="")
assert resp.status == 200

assert (1337, 1) not in queue.notified_accounts
assert (333, 1) not in queue.notified_accounts
Expand Down
7 changes: 3 additions & 4 deletions tests/taskomatic_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pytest
from aiohttp import test_utils
from aiohttp import web
from tornado.httpclient import AsyncHTTPClient

from common import database_handler
from taskomatic import taskomatic
Expand Down Expand Up @@ -37,13 +36,13 @@ async def taskomatic_server():
site = web.TCPSite(runner, "0.0.0.0", port)
await site.start()

yield f"http://localhost:{port}"
yield app.app

await runner.cleanup()


@pytest.fixture()
def http_client():
client = AsyncHTTPClient()
async def http_client(taskomatic_server, aiohttp_client):
client = await aiohttp_client(taskomatic_server)
yield client
client.close()
16 changes: 6 additions & 10 deletions tests/taskomatic_tests/test_taskomatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"""
import asyncio

import pytest
from psycopg2.extensions import connection
from tornado.httpclient import HTTPClientError

import taskomatic.jobs.common as tjc
from taskomatic import taskomatic
Expand Down Expand Up @@ -92,14 +90,12 @@ def run_forever(self, *_, **__):
class TestTaskomaticApp:
"""Test taskomatic App handlers"""

@pytest.mark.asyncio
async def test_run_job(self, taskomatic_server, http_client):
async def test_run_job(self, http_client):
"""Test run job handler"""
response = await http_client.fetch("%s%s" % (taskomatic_server, "/api/v1/run/stale_systems"), method="PUT", body="{}")
assert response.code == 200
response = await http_client.put("/api/v1/run/stale_systems", data={})
assert response.status == 200

@pytest.mark.asyncio
async def test_run_job_wrong(self, taskomatic_server, http_client):
async def test_run_job_wrong(self, http_client):
"""Test run job handler with wrong job"""
with pytest.raises(HTTPClientError):
await http_client.fetch("%s%s" % (taskomatic_server, "/api/v1/run/nonexisting_job"), method="PUT", body="{}")
response = await http_client.put("/api/v1/run/nonexisting_job", data={})
assert response.status == 400

0 comments on commit 48372d7

Please sign in to comment.