async def test_atomic_tx(dbpool): set_default_pool(dbpool) tbl = table('testuser') @atomic(isolation='repeatable_read') async def f1(): r = await tbl.filter(name='mike').update(info='info 02') await f2() rm = await tbl.filter(name='marry').get_one() # isolation is repeatable_read, but it's the same transaction assert rm['info'] == 'info 12' raise AbortTransaction() @atomic(isolation='repeatable_read') async def f2(): r = await tbl.filter(name='marry').update(info='info 12') assert r['info'] == 'info 12' try: await tbl.insert(name='mike', gender='male', info='info 01') await tbl.insert(name='marry', gender='female', info='info 11') try: await f1() except AbortTransaction: pass r = await tbl.filter(name='marry').get_one() assert r['info'] == 'info 11' r = await tbl.filter(name='mike').get_one() assert r['info'] == 'info 01' finally: await tbl.delete()
async def test_cor_tx(dbpool): set_default_pool(dbpool) tbl = table('testuser') try: await tbl.insert(name='mike', gender='male', info='info 01') await tbl.insert(name='marry', gender='female', info='info 11') try: async with local_transaction(isolation='repeatable_read') as c1: r = await tbl.filter(name='mike').update(info='info 02') rm = await tbl.filter(name='marry').get_one() assert rm['info'] == 'info 11' # isolation is repeatable_read t1 = asyncio.create_task(update_marry_info(c1)) await asyncio.sleep(2) rm = await tbl.filter(name='marry').get_one() assert rm['info'] == 'info 11' raise AbortTransaction() except AbortTransaction: pass r = await tbl.filter(name='marry').get_one() assert r['info'] == 'info 12' r = await tbl.filter(name='mike').get_one() assert r['info'] == 'info 01' finally: await tbl.delete()
async def test_select(dbpool): set_default_pool(dbpool) tbl = table('testuser') await tbl.using(None).insert(name='mike', gender='male') try: r = await tbl.filter(name='mike').get_one() assert r['gender'] == 'male' finally: await tbl.delete()
async def update_marry_info(c1): try: tbl = table('testuser') async with local_transaction(isolation='repeatable_read') as c2: assert c1 is not c2 r = await tbl.filter(name='marry').update(info='info 12') assert r['info'] == 'info 12' except: import traceback traceback.print_exc() raise
async def test_update(dbpool): set_default_pool(dbpool) tbl = table('testuser') await tbl.insert(name='jake', gender='male', info='a good man') try: r = await tbl.filter(name='jake').update(info='a bad man') assert r['info'] == 'a bad man' finally: await tbl.delete()
async def test_rollback(dbpool): set_default_pool(dbpool) tbl = table('testuser') try: await tbl.insert(name='mike', gender='male') try: async with local_transaction() as _conn: r = await tbl.filter(name='mike').update(gender='female') assert r['gender'] == 'female' # abort the transation, rollback the gender changing raise AbortTransaction() except AbortTransaction: pass r = await tbl.filter(name='mike').get_one() assert r['gender'] == 'male' finally: await tbl.delete()
async def test_get_iter(dbpool): set_default_pool(dbpool) tbl = table('testuser') await tbl.using(None).insert(name='kitty', gender='female') await tbl.using(None).insert(name='hello', gender='male') await tbl.using(None).insert(name='cosmos', gender='female') try: async with local_transaction(): rqs = tbl.filter().order_by('id').get_iter() async for r in rqs: if r['name'] == 'kitty': assert r['gender'] == 'female' elif r['name'] == 'cosmos': assert r['gender'] == 'female' qs = tbl.filter().order_by('id').get_iter_as(TUser) async for r in qs: if r.name == 'kitty': assert r['gender'] == 'female' elif r.name == 'cosmos': assert r.gender == 'female' finally: await tbl.delete()