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
|
@ -1,6 +1,7 @@
|
|||
import asyncio
|
||||
import pytest
|
||||
from chweb.cmd import create_config
|
||||
from chweb.models import Check
|
||||
|
||||
@pytest.fixture()
|
||||
def config():
|
||||
|
@ -47,3 +48,12 @@ def config_invalid():
|
|||
]
|
||||
}
|
||||
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 aiokafka
|
||||
from mock import Mock
|
||||
from mock import Mock, AsyncMock
|
||||
import pytest
|
||||
|
||||
from chweb.collector import Producer
|
||||
from chweb.models import Check
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_producer_called(config, event_loop):
|
||||
async def test_producer_called(check, config, event_loop):
|
||||
queue = asyncio.Queue()
|
||||
producer = Producer(config, Mock(), event_loop, queue)
|
||||
check = Check()
|
||||
await queue.put(check)
|
||||
|
||||
async def async_patch():
|
||||
pass
|
||||
Mock.__await__ = lambda x: async_patch().__await__()
|
||||
|
||||
producer.producer = Mock()
|
||||
producer.producer = AsyncMock()
|
||||
|
||||
task = event_loop.create_task(producer())
|
||||
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):
|
||||
queue = asyncio.Queue()
|
||||
producer = Producer(config, Mock(), event_loop, queue)
|
||||
check = Check()
|
||||
await queue.put('')
|
||||
|
||||
async def async_patch():
|
||||
pass
|
||||
Mock.__await__ = lambda x: async_patch().__await__()
|
||||
|
||||
producer.producer = Mock()
|
||||
producer.producer = AsyncMock()
|
||||
|
||||
task = event_loop.create_task(producer())
|
||||
await asyncio.sleep(0)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue