Add unit tests with 68% coverage, refactor a bit

This commit is contained in:
Vladan Popovic 2020-09-06 00:23:34 +02:00
parent 7967b1d024
commit 2b9435dddd
9 changed files with 213 additions and 39 deletions

0
tests/__init__.py Normal file
View file

49
tests/conftest.py Normal file
View file

@ -0,0 +1,49 @@
import asyncio
import pytest
from chweb.cmd import create_config
@pytest.fixture()
def config():
config_dict = {
'kafka': {
'servers': ["localhost:9992"],
'topic': "sample",
},
'postgres': {
'dbhost': "localhost",
'dbport': 5432,
'dbname': "chweb",
'dbuser': "vladan",
'dbpass': "",
},
'sites': [{
'url': "https://example.com",
'regex': "aaaaaaaaaaaaa",
'check_interval': 8,
},
]
}
return create_config(config_dict)
@pytest.fixture
def config_invalid():
config_dict = {
'kafka': {
'servers': ["localhost:9992"],
'topic': "sample",
},
'postgres': {
'dbhost': "localhost",
'dbport': 5432,
'dbname': "chweb",
'dbuser': "vladan",
'dbpass': "",
},
'sites': [{
'url': "https://dsadakjhkjsahkjh.com",
'regex': "domain",
'check_interval': 5,
},
]
}
return create_config(config_dict)

67
tests/test_checker.py Normal file
View file

@ -0,0 +1,67 @@
"""
All tests fot the ``chweb.checker`` module.
"""
import asyncio
from mock import Mock
import pytest
import requests
from chweb.collector import Collector
@pytest.mark.asyncio
async def test_valid_site_200(config, event_loop):
queue = asyncio.Queue()
coll = Collector(config, Mock(), event_loop, queue)
check = await coll.check('https://example.com', None)
assert check.domain == 'example.com'
assert check.regex_matches is None
assert check.status == 200
assert check.response_time > 0
@pytest.mark.asyncio
async def test_valid_site_404(config, event_loop):
queue = asyncio.Queue()
coll = Collector(config, Mock(), event_loop, queue)
check = await coll.check('https://example.com/404', None)
assert check.domain == 'example.com'
assert check.regex_matches is None
assert check.status == 404
assert check.response_time > 0
@pytest.mark.asyncio
async def test_invalid_site(config, event_loop):
queue = asyncio.Queue()
coll = Collector(config, Mock(), event_loop, queue)
with pytest.raises(requests.exceptions.ConnectionError):
_ = await coll.check('https://non.existant.domain.noooo', None)
@pytest.mark.asyncio
async def test_check_forever_valid(config, event_loop):
"""
The :meth:`chweb.collector.Collector.check_forever` method runs an infinite
loop, so we'll test if it's running for 2s and assume it's ok.
"""
queue = asyncio.Queue()
coll = Collector(config, Mock(), event_loop, queue)
task = event_loop.create_task(coll.check_forever(config.sites[0]))
await asyncio.sleep(2)
assert not task.done()
task.cancel()
@pytest.mark.asyncio
async def test_check_forever_invalid(config_invalid, event_loop):
"""
The :meth:`chweb.collector.Collector.check_forever` method cancels the Task
on error, so if we get an invalid site, the task should be done.
"""
queue = asyncio.Queue()
coll = Collector(config_invalid, Mock(), event_loop, queue)
task = event_loop.create_task(coll.check_forever(config_invalid.sites[0]))
await asyncio.sleep(1)
assert task.done()

47
tests/test_producer.py Normal file
View file

@ -0,0 +1,47 @@
import asyncio
import aiokafka
from mock import Mock
import pytest
from chweb.collector import Producer
from chweb.models import Check
@pytest.mark.asyncio
async def test_producer_called(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()
task = event_loop.create_task(producer.produce())
await asyncio.sleep(0)
producer.producer.send_and_wait.assert_called_with(
config.kafka.topic, bytes(check.json().encode('utf-8')))
task.cancel()
@pytest.mark.asyncio
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()
task = event_loop.create_task(producer.produce())
await asyncio.sleep(0)
producer.logger.error.assert_called()
assert task.done()