mirror of
https://github.com/kennethreitz/records.git
synced 2026-06-05 06:46:17 +00:00
tests/test_transactions.py refactored
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
"""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')
|
||||
+45
-22
@@ -1,19 +1,43 @@
|
||||
"""Test manipulating database table in various transaction scenarios.
|
||||
|
||||
Varying conditions:
|
||||
|
||||
- db for different database backends (see `db` fixture)
|
||||
- query run via
|
||||
|
||||
- `db=records.Database(); db.query()
|
||||
- `conn=db.get_connection(); conn.query()`
|
||||
|
||||
- transaction
|
||||
- not used at all
|
||||
- used and created in different ways
|
||||
- transaction succeeds
|
||||
- transaction fails or raise
|
||||
"""
|
||||
import pytest
|
||||
|
||||
import records
|
||||
|
||||
db = records.Database('sqlite:///:memory:')
|
||||
@pytest.mark.usefixtures('foo_table')
|
||||
def test_plain_db(db):
|
||||
"""Manipulate database by `db.query` without transactions.
|
||||
"""
|
||||
db.query('INSERT INTO foo VALUES (42)')
|
||||
db.query('INSERT INTO foo VALUES (43)')
|
||||
assert db.query('SELECT count(*) AS n FROM foo')[0].n == 2
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def table_setup(request):
|
||||
db.query('CREATE TABLE foo (a integer)')
|
||||
def drop_table():
|
||||
db.query('DROP TABLE foo')
|
||||
request.addfinalizer(drop_table)
|
||||
@pytest.mark.usefixtures('foo_table')
|
||||
def test_plain_conn(db):
|
||||
"""Manipulate database by `conn.query` without transactions.
|
||||
"""
|
||||
conn = db.get_connection()
|
||||
conn.query('INSERT INTO foo VALUES (42)')
|
||||
conn.query('INSERT INTO foo VALUES (43)')
|
||||
assert conn.query('SELECT count(*) AS n FROM foo')[0].n == 2
|
||||
|
||||
|
||||
def test_failing_transaction_self_managed(table_setup):
|
||||
@pytest.mark.usefixtures('foo_table')
|
||||
def test_failing_transaction_self_managed(db):
|
||||
conn = db.get_connection()
|
||||
tx = conn.transaction()
|
||||
try:
|
||||
@@ -22,14 +46,15 @@ def test_failing_transaction_self_managed(table_setup):
|
||||
raise ValueError()
|
||||
tx.commit()
|
||||
conn.query('INSERT INTO foo VALUES (44)')
|
||||
except:
|
||||
except ValueError:
|
||||
tx.rollback()
|
||||
finally:
|
||||
conn.close()
|
||||
assert db.query('SELECT count(*) AS n FROM foo')[0].n == 0
|
||||
|
||||
|
||||
def test_failing_transaction(table_setup):
|
||||
@pytest.mark.usefixtures('foo_table')
|
||||
def test_failing_transaction(db):
|
||||
with db.transaction() as conn:
|
||||
conn.query('INSERT INTO foo VALUES (42)')
|
||||
conn.query('INSERT INTO foo VALUES (43)')
|
||||
@@ -38,21 +63,19 @@ def test_failing_transaction(table_setup):
|
||||
assert db.query('SELECT count(*) AS n FROM foo')[0].n == 0
|
||||
|
||||
|
||||
def test_passing_transaction_self_managed(table_setup):
|
||||
@pytest.mark.usefixtures('foo_table')
|
||||
def test_passing_transaction_self_managed(db):
|
||||
conn = db.get_connection()
|
||||
tx = conn.transaction()
|
||||
try:
|
||||
conn.query('INSERT INTO foo VALUES (42)')
|
||||
conn.query('INSERT INTO foo VALUES (43)')
|
||||
tx.commit()
|
||||
except:
|
||||
tx.rollback()
|
||||
finally:
|
||||
conn.close()
|
||||
assert db.query('SELECT count(*) AS n FROM foo')[0].n == 2
|
||||
conn.query('INSERT INTO foo VALUES (42)')
|
||||
conn.query('INSERT INTO foo VALUES (43)')
|
||||
tx.commit()
|
||||
conn.close()
|
||||
assert db.query('SELECT count(*) AS n FROM foo')[0].n == 2
|
||||
|
||||
|
||||
def test_passing_transaction(table_setup):
|
||||
@pytest.mark.usefixtures('foo_table')
|
||||
def test_passing_transaction(db):
|
||||
with db.transaction() as conn:
|
||||
conn.query('INSERT INTO foo VALUES (42)')
|
||||
conn.query('INSERT INTO foo VALUES (43)')
|
||||
|
||||
Reference in New Issue
Block a user