Fix async mocks and add test for consumer
This commit is contained in:
parent
db45628b2c
commit
524e27eafe
4 changed files with 42 additions and 21 deletions
|
@ -19,6 +19,7 @@ class Consumer(Service):
|
||||||
they can be serialized in a :class:`chweb.models.Check` object, then they
|
they can be serialized in a :class:`chweb.models.Check` object, then they
|
||||||
are saved in the database.
|
are saved in the database.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, config: Config,
|
def __init__(self, config: Config,
|
||||||
logger: logging.Logger,
|
logger: logging.Logger,
|
||||||
event_loop: asyncio.AbstractEventLoop,
|
event_loop: asyncio.AbstractEventLoop,
|
||||||
|
@ -110,12 +111,12 @@ class Db:
|
||||||
"""
|
"""
|
||||||
if self.conn is not None:
|
if self.conn is not None:
|
||||||
try:
|
try:
|
||||||
await self.conn.execute('''
|
await self.conn.execute(
|
||||||
INSERT INTO statuses (domain, regex, regex_matches,
|
'''INSERT INTO statuses (domain, regex, regex_matches,
|
||||||
request_time, response_time,
|
request_time, response_time,
|
||||||
status, url)
|
status, url)
|
||||||
VALUES($1, $2, $3, $4, $5, $6, $7)
|
VALUES($1, $2, $3, $4, $5, $6, $7)''',
|
||||||
''', data.domain, data.regex, data.regex_matches,
|
data.domain, data.regex, data.regex_matches,
|
||||||
data.request_time, data.response_time, data.status,
|
data.request_time, data.response_time, data.status,
|
||||||
data.url)
|
data.url)
|
||||||
except asyncpg.PostgresError as exc:
|
except asyncpg.PostgresError as exc:
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import pytest
|
import pytest
|
||||||
from chweb.cmd import create_config
|
from chweb.cmd import create_config
|
||||||
|
from chweb.models import Check
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def config():
|
def config():
|
||||||
|
@ -47,3 +48,12 @@ def config_invalid():
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
return create_config(config_dict)
|
return create_config(config_dict)
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def check():
|
||||||
|
return Check(
|
||||||
|
domain="example.com",
|
||||||
|
response_time=3265,
|
||||||
|
status=200,
|
||||||
|
url="https://example.com",
|
||||||
|
)
|
||||||
|
|
21
tests/test_consumer.py
Normal file
21
tests/test_consumer.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
import aiokafka
|
||||||
|
from mock import Mock, AsyncMock
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from chweb.consumer import Consumer
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_consumer_called(check, config, event_loop):
|
||||||
|
consumer = Consumer(config, Mock(), event_loop, Mock())
|
||||||
|
|
||||||
|
consumer.consumer = AsyncMock()
|
||||||
|
consumer.db = AsyncMock()
|
||||||
|
|
||||||
|
task = event_loop.create_task(consumer())
|
||||||
|
await asyncio.sleep(0)
|
||||||
|
consumer.db.setup.assert_called()
|
||||||
|
consumer.consumer.start.assert_called()
|
||||||
|
task.cancel()
|
|
@ -1,25 +1,19 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
import aiokafka
|
import aiokafka
|
||||||
from mock import Mock
|
from mock import Mock, AsyncMock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from chweb.collector import Producer
|
from chweb.collector import Producer
|
||||||
from chweb.models import Check
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_producer_called(config, event_loop):
|
async def test_producer_called(check, config, event_loop):
|
||||||
queue = asyncio.Queue()
|
queue = asyncio.Queue()
|
||||||
producer = Producer(config, Mock(), event_loop, queue)
|
producer = Producer(config, Mock(), event_loop, queue)
|
||||||
check = Check()
|
|
||||||
await queue.put(check)
|
await queue.put(check)
|
||||||
|
|
||||||
async def async_patch():
|
producer.producer = AsyncMock()
|
||||||
pass
|
|
||||||
Mock.__await__ = lambda x: async_patch().__await__()
|
|
||||||
|
|
||||||
producer.producer = Mock()
|
|
||||||
|
|
||||||
task = event_loop.create_task(producer())
|
task = event_loop.create_task(producer())
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
|
@ -32,14 +26,9 @@ async def test_producer_called(config, event_loop):
|
||||||
async def test_producer_called_invalid(config, event_loop):
|
async def test_producer_called_invalid(config, event_loop):
|
||||||
queue = asyncio.Queue()
|
queue = asyncio.Queue()
|
||||||
producer = Producer(config, Mock(), event_loop, queue)
|
producer = Producer(config, Mock(), event_loop, queue)
|
||||||
check = Check()
|
|
||||||
await queue.put('')
|
await queue.put('')
|
||||||
|
|
||||||
async def async_patch():
|
producer.producer = AsyncMock()
|
||||||
pass
|
|
||||||
Mock.__await__ = lambda x: async_patch().__await__()
|
|
||||||
|
|
||||||
producer.producer = Mock()
|
|
||||||
|
|
||||||
task = event_loop.create_task(producer())
|
task = event_loop.create_task(producer())
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
|
|
Loading…
Reference in a new issue