-
-
Notifications
You must be signed in to change notification settings - Fork 571
/
Copy pathconftest.py
49 lines (37 loc) · 1.22 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Shared pytest fixtures.
"""
import pytest
import records
@pytest.fixture(
params=[
# request: (sql_url_id, sql_url_template)
("sqlite_memory", "sqlite:///:memory:"),
# ('sqlite_file', 'sqlite:///{dbfile}'),
# ('psql', 'postgresql://records:records@localhost/records')
],
ids=lambda r: r[0],
)
def db(request, tmpdir):
"""Instance of `records.Database(dburl)`
Ensure, it gets closed after being used in a test or fixture.
Parametrized with (sql_url_id, sql_url_template) tuple.
If `sql_url_template` contains `{dbfile}` it is replaced with path to a
temporary file.
Feel free to parametrize for other databases and experiment with them.
"""
id, url = request.param
# replace {dbfile} in url with temporary db file path
url = url.format(dbfile=str(tmpdir / "db.sqlite"))
db = records.Database(url)
yield db # providing fixture value for a test case
# tear_down
db.close()
@pytest.fixture
def foo_table(db):
"""Database with table `foo` created
tear_down drops the table.
Typically applied by `@pytest.mark.usefixtures('foo_table')`
"""
db.query("CREATE TABLE foo (a integer)")
yield
db.query("DROP TABLE foo")