sqlite-utils icon indicating copy to clipboard operation
sqlite-utils copied to clipboard

`with db:` for transactions

Open simonw opened this issue 2 years ago • 3 comments

This can be a documented wrapper around with db.conn:.

simonw avatar Feb 24 '22 19:02 simonw

This test fails and I don't understand why:

from sqlite_utils import Database


def test_transaction():
    db1 = Database(memory_name="transaction_test", tracer=print)
    db2 = Database(memory_name="transaction_test", tracer=print)
    with db1.conn:
        db1["t"].insert({"foo": 1})
        assert list(db2["t"].rows) == []
    assert list(db2["t"].rows) == [{"foo": 1}]

simonw avatar Mar 01 '22 22:03 simonw

pytest xklb/check.py --pdb

xklb/check.py:11: in test_transaction
    assert list(db2["t"].rows) == []
E   AssertionError: assert [{'foo': 1}] == []
E    +  where [{'foo': 1}] = list(<generator object Queryable.rows_where at 0x7f2d84d1f0d0>)
E    +    where <generator object Queryable.rows_where at 0x7f2d84d1f0d0> = <Table t (foo)>.rows
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PDB post_mortem (IO-capturing turned off) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> /home/xk/github/xk/lb/xklb/check.py(11)test_transaction()
      9     with db1.conn:
     10         db1["t"].insert({"foo": 1})
---> 11         assert list(db2["t"].rows) == []
     12     assert list(db2["t"].rows) == [{"foo": 1}]

It fails because it is already inserted.

btw if you put these two lines in you pyproject.toml you can get ipdb in pytest

[tool.pytest.ini_options]
addopts = "--pdbcls=IPython.terminal.debugger:TerminalPdb --ignore=tests/data --capture=tee-sys --log-cli-level=ERROR"

chapmanjacobd avatar Oct 01 '22 03:10 chapmanjacobd

oh weird. it inserts into db2

chapmanjacobd avatar Oct 01 '22 03:10 chapmanjacobd