Skip to content

Commit

Permalink
Merge pull request #37 from Snawoot/improve_coverage
Browse files Browse the repository at this point in the history
Improve coverage
  • Loading branch information
Snawoot authored Jun 1, 2019
2 parents 0f7180a + 4ce9ff6 commit 7dd98ba
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 4 deletions.
2 changes: 1 addition & 1 deletion postfix_mta_sts_resolver/sqlite_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def stop(self):
pass

def borrow(self, timeout=None):
if not self._ready: # pragma: no cover
if not self._ready:
raise RuntimeError("Pool not prepared!")
class PoolBorrow:
# pylint: disable=no-self-argument
Expand Down
4 changes: 2 additions & 2 deletions tests/test_responder.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def responder(event_loop):
(b'test bad-cert1.loc', b'NOTFOUND '),
(b'test bad-cert2.loc', b'NOTFOUND '),
]
@pytest.mark.parametrize("params", itertools.product(reqresps, buf_sizes))
@pytest.mark.parametrize("params", tuple(itertools.product(reqresps, buf_sizes)))
@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_responder(responder, params):
Expand Down Expand Up @@ -130,7 +130,7 @@ async def answer():
finally:
writer.close()

@pytest.mark.parametrize("params", itertools.product(reqresps, buf_sizes))
@pytest.mark.parametrize("params", tuple(itertools.product(reqresps, buf_sizes)))
@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_responder_with_custom_socket(event_loop, responder, params):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_responder_strict.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def responder(event_loop):
(b'test bad-cert1.loc', b'NOTFOUND '),
(b'test bad-cert2.loc', b'NOTFOUND '),
]
@pytest.mark.parametrize("params", itertools.product(reqresps, buf_sizes))
@pytest.mark.parametrize("params", tuple(itertools.product(reqresps, buf_sizes)))
@pytest.mark.asyncio
@pytest.mark.timeout(5)
async def test_responder(responder, params):
Expand Down
118 changes: 118 additions & 0 deletions tests/test_sqliteconnpool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import asyncio
import tempfile

import pytest

from postfix_mta_sts_resolver.sqlite_cache import SqliteConnPool

CONN_INIT = [
"PRAGMA journal_mode=WAL",
"PRAGMA synchronous=NORMAL",
]

@pytest.fixture
def dbfile():
with tempfile.NamedTemporaryFile() as f:
yield f.name

@pytest.mark.asyncio
async def test_raises_re(dbfile):
pool = SqliteConnPool(1, (dbfile,), init_queries=CONN_INIT)
with pytest.raises(RuntimeError):
async with pool.borrow() as conn:
pass

@pytest.mark.asyncio
@pytest.mark.timeout(2)
async def test_basic(dbfile):
pool = SqliteConnPool(1, (dbfile,), init_queries=CONN_INIT)
await pool.prepare()
try:
async with pool.borrow() as conn:
await conn.execute("create table if not exists t (id int)")
async with pool.borrow() as conn:
for i in range(10):
await conn.execute("insert into t (id) values (?)", (i,))
await conn.commit()
async with pool.borrow() as conn:
async with conn.execute('select sum(id) from t') as cur:
res = await cur.fetchone()
assert res[0] == sum(range(10))
finally:
await pool.stop()

@pytest.mark.asyncio
@pytest.mark.timeout(2)
async def test_early_stop(dbfile):
pool = SqliteConnPool(5, (dbfile,), init_queries=CONN_INIT)
await pool.prepare()
async with pool.borrow() as conn:
await pool.stop()
await conn.execute("create table if not exists t (id int)")
for i in range(10):
await conn.execute("insert into t (id) values (?)", (i,))
await conn.commit()
async with conn.execute('select sum(id) from t') as cur:
res = await cur.fetchone()
assert res[0] == sum(range(10))

@pytest.mark.asyncio
@pytest.mark.timeout(2)
async def test_borrow_timeout(dbfile):
pool = SqliteConnPool(1, (dbfile,), init_queries=CONN_INIT)
await pool.prepare()
try:
async with pool.borrow(1) as conn1:
with pytest.raises(asyncio.TimeoutError):
async with pool.borrow(1) as conn2:
pass
finally:
await pool.stop()

@pytest.mark.asyncio
@pytest.mark.timeout(2)
async def test_conn_reuse(dbfile):
pool = SqliteConnPool(1, (dbfile,), init_queries=CONN_INIT)
await pool.prepare()
try:
async with pool.borrow() as conn:
first = conn
async with pool.borrow() as conn:
second = conn
assert first is second
finally:
await pool.stop()

@pytest.mark.asyncio
@pytest.mark.timeout(2)
async def test_conn_ressurection(dbfile):
class TestError(Exception):
pass
pool = SqliteConnPool(1, (dbfile,), init_queries=CONN_INIT)
await pool.prepare()
try:
with pytest.raises(TestError):
async with pool.borrow() as conn:
first = conn
async with conn.execute("SELECT 1") as cur:
result = await cur.fetchone()
assert result[0] == 1
raise TestError()
async with pool.borrow() as conn:
async with conn.execute("SELECT 1") as cur:
result = await cur.fetchone()
assert result[0] == 1
second = conn
assert first is not second
finally:
await pool.stop()

@pytest.mark.asyncio
@pytest.mark.timeout(2)
async def test_bad_init(dbfile):
pool = SqliteConnPool(1, (dbfile,), init_queries=['BOGUSQUERY'])
try:
with pytest.raises(Exception):
await pool.prepare()
finally:
await pool.stop()

0 comments on commit 7dd98ba

Please sign in to comment.