|
| 1 | +import pytest |
| 2 | +import sqlalchemy |
| 3 | + |
| 4 | +from gino import UninitializedError, create_engine, InitializedError |
| 5 | +from gino.bakery import Bakery, BakedQuery |
| 6 | +from .models import db, User, MYSQL_URL |
| 7 | + |
| 8 | +pytestmark = pytest.mark.asyncio |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize( |
| 12 | + "query", |
| 13 | + [ |
| 14 | + User.query.where(User.id == db.bindparam("uid")), |
| 15 | + sqlalchemy.text("SELECT * FROM gino_users WHERE id = :uid"), |
| 16 | + "SELECT * FROM gino_users WHERE id = :uid", |
| 17 | + lambda: User.query.where(User.id == db.bindparam("uid")), |
| 18 | + lambda: sqlalchemy.text("SELECT * FROM gino_users WHERE id = :uid"), |
| 19 | + lambda: "SELECT * FROM gino_users WHERE id = :uid", |
| 20 | + ], |
| 21 | +) |
| 22 | +@pytest.mark.parametrize("options", [dict(return_model=False), dict(loader=User)]) |
| 23 | +@pytest.mark.parametrize("api", [True, False]) |
| 24 | +@pytest.mark.parametrize("timeout", [None, 1]) |
| 25 | +async def test(query, options, sa_engine, api, timeout): |
| 26 | + uid = sa_engine.execute(User.insert()).lastrowid |
| 27 | + if timeout: |
| 28 | + options["timeout"] = timeout |
| 29 | + |
| 30 | + if api: |
| 31 | + b = db._bakery |
| 32 | + qs = [db.bake(query, **options)] |
| 33 | + if callable(query): |
| 34 | + qs.append(db.bake(**options)(query)) |
| 35 | + else: |
| 36 | + b = Bakery() |
| 37 | + qs = [b.bake(query, **options)] |
| 38 | + if callable(query): |
| 39 | + qs.append(b.bake(**options)(query)) |
| 40 | + |
| 41 | + for q in qs: |
| 42 | + assert isinstance(q, BakedQuery) |
| 43 | + assert q in list(b) |
| 44 | + assert q.sql is None |
| 45 | + assert q.compiled_sql is None |
| 46 | + |
| 47 | + with pytest.raises(UninitializedError): |
| 48 | + q.bind.first() |
| 49 | + with pytest.raises(UninitializedError): |
| 50 | + await q.first() |
| 51 | + |
| 52 | + for k, v in options.items(): |
| 53 | + assert q.query.get_execution_options()[k] == v |
| 54 | + |
| 55 | + if api: |
| 56 | + e = await db.set_bind(MYSQL_URL, minsize=1) |
| 57 | + else: |
| 58 | + e = await create_engine(MYSQL_URL, bakery=b, minsize=1) |
| 59 | + |
| 60 | + with pytest.raises(InitializedError): |
| 61 | + b.bake("SELECT now()") |
| 62 | + |
| 63 | + with pytest.raises(InitializedError): |
| 64 | + await create_engine(MYSQL_URL, bakery=b, minsize=0) |
| 65 | + |
| 66 | + try: |
| 67 | + for q in qs: |
| 68 | + assert q.sql is not None |
| 69 | + assert q.compiled_sql is not None |
| 70 | + |
| 71 | + if api: |
| 72 | + assert q.bind is e |
| 73 | + else: |
| 74 | + with pytest.raises(UninitializedError): |
| 75 | + q.bind.first() |
| 76 | + with pytest.raises(UninitializedError): |
| 77 | + await q.first() |
| 78 | + |
| 79 | + if api: |
| 80 | + rv = await q.first(uid=uid) |
| 81 | + else: |
| 82 | + rv = await e.first(q, uid=uid) |
| 83 | + |
| 84 | + if options.get("return_model", True): |
| 85 | + assert isinstance(rv, User) |
| 86 | + assert rv.id == uid |
| 87 | + else: |
| 88 | + assert rv[0] == rv[User.id] == rv["id"] == uid |
| 89 | + |
| 90 | + eq = q.execution_options(return_model=True, loader=User) |
| 91 | + assert eq is not q |
| 92 | + assert isinstance(eq, BakedQuery) |
| 93 | + assert type(eq) is not BakedQuery |
| 94 | + assert eq in list(b) |
| 95 | + assert eq.sql == q.sql |
| 96 | + assert eq.compiled_sql is not q.compiled_sql |
| 97 | + |
| 98 | + if api: |
| 99 | + assert q.bind is e |
| 100 | + else: |
| 101 | + with pytest.raises(UninitializedError): |
| 102 | + eq.bind.first() |
| 103 | + with pytest.raises(UninitializedError): |
| 104 | + await eq.first() |
| 105 | + |
| 106 | + assert eq.query.get_execution_options()["return_model"] |
| 107 | + assert eq.query.get_execution_options()["loader"] is User |
| 108 | + |
| 109 | + if api: |
| 110 | + rv = await eq.first(uid=uid) |
| 111 | + non = await eq.first(uid=uid + 1) |
| 112 | + rvl = await eq.all(uid=uid) |
| 113 | + else: |
| 114 | + rv = await e.first(eq, uid=uid) |
| 115 | + non = await e.first(eq, uid=uid + 1) |
| 116 | + rvl = await e.all(eq, uid=uid) |
| 117 | + |
| 118 | + assert isinstance(rv, User) |
| 119 | + assert rv.id == uid |
| 120 | + |
| 121 | + assert non is None |
| 122 | + |
| 123 | + assert len(rvl) == 1 |
| 124 | + assert rvl[0].id == uid |
| 125 | + |
| 126 | + # original query is not affected |
| 127 | + if api: |
| 128 | + rv = await q.first(uid=uid) |
| 129 | + else: |
| 130 | + rv = await e.first(q, uid=uid) |
| 131 | + |
| 132 | + if options.get("return_model", True): |
| 133 | + assert isinstance(rv, User) |
| 134 | + assert rv.id == uid |
| 135 | + else: |
| 136 | + assert rv[0] == rv[User.id] == rv["id"] == uid |
| 137 | + |
| 138 | + finally: |
| 139 | + if api: |
| 140 | + await db.pop_bind().close() |
| 141 | + else: |
| 142 | + await e.close() |
| 143 | + |
| 144 | + |
| 145 | +async def test_class_level_bake(): |
| 146 | + class BakeOnClass(db.Model): |
| 147 | + __tablename__ = "bake_on_class_test" |
| 148 | + |
| 149 | + name = db.Column(db.String(255), primary_key=True) |
| 150 | + |
| 151 | + @db.bake |
| 152 | + def getter(cls): |
| 153 | + return cls.query.where(cls.name == db.bindparam("name")) |
| 154 | + |
| 155 | + async with db.with_bind(MYSQL_URL, prebake=False, autocommit=True): |
| 156 | + await db.gino.create_all() |
| 157 | + try: |
| 158 | + await BakeOnClass.create(name="exist") |
| 159 | + assert (await BakeOnClass.getter.one(name="exist")).name == "exist" |
| 160 | + assert (await BakeOnClass.getter.one_or_none(name="nonexist")) is None |
| 161 | + finally: |
| 162 | + await db.gino.drop_all() |
0 commit comments