def test_query_union(): s = ("select * from users union select users.* from organisations " "left join users on users.organisation_id = organisations.id") q = (Query(User).select().union(Organisation).select( fn.wild(User)).left_join(User, on=[User.organisation_id, Organisation.id])) assertSQL(q, s)
def get_by_id(cls, id): return ( Query(cls) .select() .where(cls.id == id, fn.is_null(cls.tombstoned)) .execute() )
def get_by_email(cls, email): user = ( Query(cls) .get_or_none() .where(cls.email == email, op.is_null(cls.tombstoned)) .execute() ) return user
def test_has_one_get(): org = ( Query(Organisation) .get() .preload({Organisation.admin: [{Admin.user: [User.name]}]}) .execute() ) assert org.get("admin") is not None assert org["admin"].get("user") is not None
def test_has_many_select(): Query(Organisation).select().preload(Organisation.users).execute() assert True
def test_has_one_select(): Query(Organisation).select().preload(Organisation.admin).execute() assert True
def test_has_many_get(): org = Query(Organisation).get().preload(Organisation.users).execute() assert len(org.get("users")) > 0
def test_left_join(): s = ("select * from users left join organisations " "on users.organisation_id = organisations.id") q = (Query(User).select().left_join( Organisation, on=[User.organisation_id, Organisation.id])) assertSQL(q, s)
from estoult import Query, op from .base import assertSQL, User _query = Query(User).select(User.id) def test_or(): s = ("select users.id from users " "where ((users.name) = ('Astolfo')) or ((users.id) = (1))") q = _query.copy().where(op.or_(User.name == "Astolfo", User.id == 1)) assertSQL(q, s) def test_and(): s = ("select users.id from users " "where ((users.name) = ('Astolfo')) and ((users.id) = (1))") q = _query.copy().where(op.and_(User.name == "Astolfo", User.id == 1)) assertSQL(q, s) def test_in(): s = "select users.id from users where " "(users.id) in (select users.id from users)" q = _query.copy().where(op.in_(User.id, _query)) assertSQL(q, s) def test_like(): s = "select users.id from users where (users.name) like ('%stolf%')" q = _query.copy().where(op.like(User.name, "%stolf%")) assertSQL(q, s)
def test_wildcard_preload_pass(): Query(User).select().preload({User.data: [Data.value]}).execute()
def test_wildcard_preload_fail(): with pytest.raises(QueryError): Query(User).select().preload(User.data).execute()
def test_query(): s = "select * from users" q = Query(User).select() assertSQL(q, s)
def test_wildcard_pass(): Query(Data).select(Data.value).execute()
def test_wildcard_fail(): with pytest.raises(QueryError): Query(Data).select().execute()
def test_order_by(): s = "select * from users order by users.name desc, users.id limit 10 offset 2" q = Query(User).select().order_by({ User.name: "desc" }, User.id).limit(10, 2) assertSQL(q, s)
def test_query_update(): Query(User).update({ "name": "Astolfo" }).where(User.name == "Charlemagne").execute()
def get_by_name(cls, name): return Query(cls).get_or_none().where(cls.name == name).execute()
def test_query_delete(): Query(User).delete().where(User.name == "Charlemagne").execute()
def applied_at(self, id): return (Query(RiderMigration).get_or_none().where( RiderMigration.migration == id).execute() or {}).get("applied_at")
def test_query_specify(): s = "select users.name from users" q = Query(User).select(User.name) assertSQL(q, s)