aboutsummaryrefslogtreecommitdiff
path: root/test_pytest
diff options
context:
space:
mode:
authorbozo.kopic <bozo@kopic.xyz>2021-07-28 01:43:55 +0200
committerbozo.kopic <bozo@kopic.xyz>2021-07-29 00:01:57 +0200
commit1e874e790c12839695761a654b44fb427149a353 (patch)
tree6942441ac511ec1417b2434b111101fa8d7f7e68 /test_pytest
init
Diffstat (limited to 'test_pytest')
-rw-r--r--test_pytest/.coveragerc6
-rw-r--r--test_pytest/conftest.py16
-rw-r--r--test_pytest/test_backend.py115
-rw-r--r--test_pytest/test_main.py33
-rw-r--r--test_pytest/test_server.py50
5 files changed, 220 insertions, 0 deletions
diff --git a/test_pytest/.coveragerc b/test_pytest/.coveragerc
new file mode 100644
index 0000000..17ad177
--- /dev/null
+++ b/test_pytest/.coveragerc
@@ -0,0 +1,6 @@
+[report]
+show_missing = True
+
+[run]
+include =
+ ../src_py/*
diff --git a/test_pytest/conftest.py b/test_pytest/conftest.py
new file mode 100644
index 0000000..39370af
--- /dev/null
+++ b/test_pytest/conftest.py
@@ -0,0 +1,16 @@
+import asyncio
+
+import pytest
+
+from hat import aio
+
+
+@pytest.fixture(scope='session')
+def event_loop():
+ loop = asyncio.get_event_loop()
+ yield loop
+ loop.close()
+
+
+def pytest_configure(config):
+ aio.init_asyncio()
diff --git a/test_pytest/test_backend.py b/test_pytest/test_backend.py
new file mode 100644
index 0000000..539f5ce
--- /dev/null
+++ b/test_pytest/test_backend.py
@@ -0,0 +1,115 @@
+import pytest
+
+import restlog.backend
+
+
+pytestmark = pytest.mark.asyncio
+
+
+@pytest.fixture
+def db_path(tmp_path):
+ return tmp_path / 'restlog.db'
+
+
+async def test_create(db_path):
+ assert not db_path.exists()
+
+ backend = await restlog.backend.create(db_path, 100)
+ assert db_path.exists()
+
+ await backend.async_close()
+ assert db_path.exists()
+
+
+async def test_register(db_path):
+ backend = await restlog.backend.create(db_path, 100)
+
+ result = await backend.get_entries()
+ assert result == {'entries': [],
+ 'more': False}
+
+ entry = await backend.register(timestamp=123,
+ address='address',
+ source='source',
+ type='type',
+ data={'abc': 123})
+ assert entry['entry_id'] is not None
+ assert entry['timestamp'] == 123
+ assert entry['address'] == 'address'
+ assert entry['source'] == 'source'
+ assert entry['type'] == 'type'
+ assert entry['data'] == {'abc': 123}
+
+ result = await backend.get_entry(entry['entry_id'])
+ assert entry == result
+
+ result = await backend.get_entries()
+ assert result == {'entries': [entry],
+ 'more': False}
+
+ await backend.async_close()
+
+ backend = await restlog.backend.create(db_path, 100)
+ result = await backend.get_entry(entry['entry_id'])
+ assert entry == result
+ await backend.async_close()
+
+
+@pytest.mark.parametrize('entries_count', [1, 2, 10])
+async def test_get_entry(db_path, entries_count):
+ backend = await restlog.backend.create(db_path, 100)
+
+ result = await backend.get_entry(123)
+ assert result is None
+
+ entry_ids = []
+ for _ in range(entries_count):
+ entry = await backend.register(timestamp=123,
+ address='address',
+ source='source',
+ type='type',
+ data={'abc': 123})
+ entry_ids.append(entry['entry_id'])
+
+ for entry_id in entry_ids:
+ result = await backend.get_entry(entry_id)
+ assert result['entry_id'] == entry_id
+
+ await backend.async_close()
+
+
+@pytest.mark.parametrize('entries_count', [1, 2, 10])
+async def test_get_entries(db_path, entries_count):
+ backend = await restlog.backend.create(db_path, 100)
+
+ result = await backend.get_entry(123)
+ assert result is None
+
+ entry_ids = []
+ for i in range(entries_count):
+ entry = await backend.register(timestamp=123,
+ address='address',
+ source=f'source{i}',
+ type=f'type{i}',
+ data={'abc': 123})
+ entry_ids.append(entry['entry_id'])
+
+ assert entry_ids == sorted(entry_ids)
+
+ result = await backend.get_entries()
+ assert ([i['entry_id'] for i in result['entries']] ==
+ sorted(entry_ids, reverse=True))
+
+ for i in range(entries_count):
+ result = await backend.get_entries(source=f'source{i}')
+ assert len(result['entries']) == 1
+ assert result['entries'][0]['entry_id'] == entry_ids[i]
+
+ result = await backend.get_entries(type=f'type{i}')
+ assert len(result['entries']) == 1
+ assert result['entries'][0]['entry_id'] == entry_ids[i]
+
+ result = await backend.get_entries(last_entry_id=entry_ids[i])
+ assert len(result['entries']) == 1 + i
+
+ await backend.async_close()
diff --git a/test_pytest/test_main.py b/test_pytest/test_main.py
new file mode 100644
index 0000000..dd61c5f
--- /dev/null
+++ b/test_pytest/test_main.py
@@ -0,0 +1,33 @@
+import asyncio
+import pytest
+
+from hat import aio
+from hat import util
+
+import restlog.main
+
+
+pytestmark = pytest.mark.asyncio
+
+
+@pytest.fixture
+def conf(tmp_path):
+ return {'log': {'version': 1},
+ 'host': '127.0.0.1',
+ 'port': util.get_unused_tcp_port(),
+ 'db_path': str((tmp_path / 'restlog.db').resolve()),
+ 'max_results': 100}
+
+
+@pytest.fixture
+async def main(conf):
+ async_group = aio.Group()
+ try:
+ async_group.spawn(restlog.main.async_main, conf)
+ yield
+ finally:
+ await async_group.async_close()
+
+
+async def test_run(main):
+ await asyncio.sleep(0.01)
diff --git a/test_pytest/test_server.py b/test_pytest/test_server.py
new file mode 100644
index 0000000..2287f57
--- /dev/null
+++ b/test_pytest/test_server.py
@@ -0,0 +1,50 @@
+import itertools
+
+import pytest
+
+from hat import aio
+from hat import util
+
+import restlog.server
+
+
+pytestmark = pytest.mark.asyncio
+
+
+@pytest.fixture
+def port():
+ return util.get_unused_tcp_port()
+
+
+class Backend(aio.Resource):
+
+ def __init__(self):
+ self._async_group = aio.Group()
+ self._next_entry_ids = itertools.count(1)
+
+ @property
+ def async_group(self):
+ return self._async_group
+
+ async def register(self, timestamp, address, source, type, data):
+ return {'entry_id': next(self._next_entry_ids),
+ 'timestamp': timestamp,
+ 'address': address,
+ 'source': source,
+ 'type': type,
+ 'data': data}
+
+ async def get_entries(self, source=None, type=None, last_entry_id=None,
+ max_results=None):
+ return {'entries': [],
+ 'more': False}
+
+ async def get_entry(self, entry_id):
+ return
+
+
+async def test_create(port):
+ backend = Backend()
+ server = await restlog.server.create('127.0.0.1', port, backend)
+ assert server.is_open
+ await server.async_close()