Here are the examples of the python api sqlalchemy.orm.Bundle taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
51 Examples
3
Source : large_resultsets.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_orm_bundles(n):
"""Load lightweight "bundle" objects using the ORM."""
sess = Session(engine)
bundle = Bundle(
"customer", Customer.id, Customer.name, Customer.description
)
for row in sess.query(bundle).yield_per(10000).limit(n):
pass
@Profiler.profile
3
Source : test_orm.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_bundle_wo_annotation(self):
A = self.classes.A
a = self.tables.a
s = Session()
q = s.query(Bundle("ASdf", a.c.data), A).select_from(A)
@profiling.function_call_count(warmup=1)
def go():
for i in range(100):
q.all()
go()
def test_bundle_w_annotation(self):
3
Source : test_orm.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_bundle_w_annotation(self):
A = self.classes.A
s = Session()
q = s.query(Bundle("ASdf", A.data), A).select_from(A)
@profiling.function_call_count(warmup=1)
def go():
for i in range(100):
q.all()
go()
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_same_named_col_clauselist(self):
Data, Other = self.classes("Data", "Other")
bundle = Bundle("pk", Data.id, Other.id)
self.assert_compile(ClauseList(Data.id, Other.id), "data.id, other.id")
self.assert_compile(bundle.__clause_element__(), "data.id, other.id")
def test_same_named_col_in_orderby(self):
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_same_named_col_in_orderby(self):
Data, Other = self.classes("Data", "Other")
bundle = Bundle("pk", Data.id, Other.id)
sess = Session()
self.assert_compile(
sess.query(Data, Other).order_by(bundle),
"SELECT data.id AS data_id, data.d1 AS data_d1, "
"data.d2 AS data_d2, data.d3 AS data_d3, "
"other.id AS other_id, other.data_id AS other_data_id, "
"other.o1 AS other_o1 "
"FROM data, other ORDER BY data.id, other.id",
)
def test_same_named_col_in_fetch(self):
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_same_named_col_in_fetch(self):
Data, Other = self.classes("Data", "Other")
bundle = Bundle("pk", Data.id, Other.id)
sess = Session()
eq_(
sess.query(bundle)
.filter(Data.id == Other.id)
.filter(Data.id < 3)
.all(),
[((1, 1),), ((2, 2),)],
)
def test_c_attr(self):
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_c_attr(self):
Data = self.classes.Data
b1 = Bundle("b1", Data.d1, Data.d2)
self.assert_compile(
select([b1.c.d1, b1.c.d2]), "SELECT data.d1, data.d2 FROM data"
)
def test_result(self):
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_result(self):
Data = self.classes.Data
sess = Session()
b1 = Bundle("b1", Data.d1, Data.d2)
eq_(
sess.query(b1).filter(b1.c.d1.between("d3d1", "d5d1")).all(),
[(("d3d1", "d3d2"),), (("d4d1", "d4d2"),), (("d5d1", "d5d2"),)],
)
def test_subclass(self):
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_single_entity(self):
Data = self.classes.Data
sess = Session()
b1 = Bundle("b1", Data.d1, Data.d2, single_entity=True)
eq_(
sess.query(b1).filter(b1.c.d1.between("d3d1", "d5d1")).all(),
[("d3d1", "d3d2"), ("d4d1", "d4d2"), ("d5d1", "d5d2")],
)
def test_single_entity_flag_but_multi_entities(self):
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_single_entity_flag_but_multi_entities(self):
Data = self.classes.Data
sess = Session()
b1 = Bundle("b1", Data.d1, Data.d2, single_entity=True)
b2 = Bundle("b1", Data.d3, single_entity=True)
eq_(
sess.query(b1, b2).filter(b1.c.d1.between("d3d1", "d5d1")).all(),
[
(("d3d1", "d3d2"), ("d3d3",)),
(("d4d1", "d4d2"), ("d4d3",)),
(("d5d1", "d5d2"), ("d5d3",)),
],
)
def test_bundle_nesting(self):
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_bundle_nesting(self):
Data = self.classes.Data
sess = Session()
b1 = Bundle("b1", Data.d1, Bundle("b2", Data.d2, Data.d3))
eq_(
sess.query(b1)
.filter(b1.c.d1.between("d3d1", "d7d1"))
.filter(b1.c.b2.c.d2.between("d4d2", "d6d2"))
.all(),
[
(("d4d1", ("d4d2", "d4d3")),),
(("d5d1", ("d5d2", "d5d3")),),
(("d6d1", ("d6d2", "d6d3")),),
],
)
def test_bundle_nesting_unions(self):
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_query_count(self):
Data = self.classes.Data
b1 = Bundle("b1", Data.d1, Data.d2)
eq_(Session().query(b1).count(), 10)
def test_join_relationship(self):
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_join_relationship(self):
Data = self.classes.Data
sess = Session()
b1 = Bundle("b1", Data.d1, Data.d2)
q = sess.query(b1).join(Data.others)
self.assert_compile(
q,
"SELECT data.d1 AS data_d1, data.d2 "
"AS data_d2 FROM data "
"JOIN other ON data.id = other.data_id",
)
def test_join_selectable(self):
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_join_selectable(self):
Data = self.classes.Data
Other = self.classes.Other
sess = Session()
b1 = Bundle("b1", Data.d1, Data.d2)
q = sess.query(b1).join(Other)
self.assert_compile(
q,
"SELECT data.d1 AS data_d1, data.d2 AS data_d2 "
"FROM data "
"JOIN other ON data.id = other.data_id",
)
def test_joins_from_adapted_entities(self):
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_filter_by(self):
Data = self.classes.Data
b1 = Bundle("b1", Data.id, Data.d1, Data.d2)
sess = Session()
self.assert_compile(
sess.query(b1).filter_by(d1="d1"),
"SELECT data.id AS data_id, data.d1 AS data_d1, "
"data.d2 AS data_d2 FROM data WHERE data.d1 = :d1_1",
)
def test_clause_expansion(self):
3
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_clause_expansion(self):
Data = self.classes.Data
b1 = Bundle("b1", Data.id, Data.d1, Data.d2)
sess = Session()
self.assert_compile(
sess.query(Data).order_by(b1),
"SELECT data.id AS data_id, data.d1 AS data_d1, "
"data.d2 AS data_d2, data.d3 AS data_d3 FROM data "
"ORDER BY data.id, data.d1, data.d2",
)
self.assert_compile(
sess.query(func.row_number().over(order_by=b1)),
"SELECT row_number() OVER (ORDER BY data.id, data.d1, data.d2) "
"AS anon_1 FROM data",
)
3
Source : test_orm.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_bundle_wo_annotation(self):
A = self.classes.A
a = self.tables.a
s = fixture_session()
q = s.query(Bundle("ASdf", a.c.data), A).select_from(A)
@profiling.function_call_count(warmup=1)
def go():
for i in range(100):
# test counts assume objects remain in the session
# from previous run
r = q.all() # noqa F841
go()
def test_bundle_w_annotation(self):
3
Source : test_orm.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_bundle_w_annotation(self):
A = self.classes.A
s = fixture_session()
q = s.query(Bundle("ASdf", A.data), A).select_from(A)
@profiling.function_call_count(warmup=1)
def go():
for i in range(100):
# test counts assume objects remain in the session
# from previous run
r = q.all() # noqa F841
go()
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_same_named_col_in_orderby(self):
Data, Other = self.classes("Data", "Other")
bundle = Bundle("pk", Data.id, Other.id)
sess = fixture_session()
self.assert_compile(
sess.query(Data, Other).order_by(bundle),
"SELECT data.id AS data_id, data.d1 AS data_d1, "
"data.d2 AS data_d2, data.d3 AS data_d3, "
"other.id AS other_id, other.data_id AS other_data_id, "
"other.o1 AS other_o1 "
"FROM data, other ORDER BY data.id, other.id",
)
def test_same_named_col_in_fetch(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_same_named_col_in_fetch(self):
Data, Other = self.classes("Data", "Other")
bundle = Bundle("pk", Data.id, Other.id)
sess = fixture_session()
eq_(
sess.query(bundle)
.filter(Data.id == Other.id)
.filter(Data.id < 3)
.all(),
[((1, 1),), ((2, 2),)],
)
def test_c_attr(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_c_attr(self):
Data = self.classes.Data
b1 = Bundle("b1", Data.d1, Data.d2)
self.assert_compile(
select(b1.c.d1, b1.c.d2), "SELECT data.d1, data.d2 FROM data"
)
def test_result(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_result(self):
Data = self.classes.Data
sess = fixture_session()
b1 = Bundle("b1", Data.d1, Data.d2)
eq_(
sess.query(b1).filter(b1.c.d1.between("d3d1", "d5d1")).all(),
[(("d3d1", "d3d2"),), (("d4d1", "d4d2"),), (("d5d1", "d5d2"),)],
)
def test_subclass(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_single_entity_legacy_query(self):
Data = self.classes.Data
sess = fixture_session()
b1 = Bundle("b1", Data.d1, Data.d2, single_entity=True)
eq_(
sess.query(b1).filter(b1.c.d1.between("d3d1", "d5d1")).all(),
[("d3d1", "d3d2"), ("d4d1", "d4d2"), ("d5d1", "d5d2")],
)
def test_labeled_cols_non_single_entity_legacy_query(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_labeled_cols_non_single_entity_legacy_query(self):
Data = self.classes.Data
sess = fixture_session()
b1 = Bundle("b1", Data.d1.label("x"), Data.d2.label("y"))
eq_(
sess.query(b1).filter(b1.c.x.between("d3d1", "d5d1")).all(),
[(("d3d1", "d3d2"),), (("d4d1", "d4d2"),), (("d5d1", "d5d2"),)],
)
def test_labeled_cols_single_entity_legacy_query(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_labeled_cols_single_entity_legacy_query(self):
Data = self.classes.Data
sess = fixture_session()
b1 = Bundle(
"b1", Data.d1.label("x"), Data.d2.label("y"), single_entity=True
)
eq_(
sess.query(b1).filter(b1.c.x.between("d3d1", "d5d1")).all(),
[("d3d1", "d3d2"), ("d4d1", "d4d2"), ("d5d1", "d5d2")],
)
def test_labeled_cols_as_rows_future(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_labeled_cols_as_rows_future(self):
Data = self.classes.Data
sess = fixture_session()
b1 = Bundle("b1", Data.d1.label("x"), Data.d2.label("y"))
stmt = select(b1).filter(b1.c.x.between("d3d1", "d5d1"))
eq_(
sess.execute(stmt).all(),
[(("d3d1", "d3d2"),), (("d4d1", "d4d2"),), (("d5d1", "d5d2"),)],
)
def test_labeled_cols_as_scalars_future(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_labeled_cols_as_scalars_future(self):
Data = self.classes.Data
sess = fixture_session()
b1 = Bundle("b1", Data.d1.label("x"), Data.d2.label("y"))
stmt = select(b1).filter(b1.c.x.between("d3d1", "d5d1"))
eq_(
sess.scalars(stmt).all(),
[("d3d1", "d3d2"), ("d4d1", "d4d2"), ("d5d1", "d5d2")],
)
def test_single_entity_flag_is_legacy_w_future(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_single_entity_flag_is_legacy_w_future(self):
Data = self.classes.Data
sess = Session(testing.db, future=True)
# flag has no effect
b1 = Bundle("b1", Data.d1, Data.d2, single_entity=True)
stmt = select(b1).filter(b1.c.d1.between("d3d1", "d5d1"))
rows = sess.execute(stmt).all()
eq_(
rows,
[(("d3d1", "d3d2"),), (("d4d1", "d4d2"),), (("d5d1", "d5d2"),)],
)
def test_as_scalars_future(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_as_scalars_future(self):
Data = self.classes.Data
sess = Session(testing.db)
b1 = Bundle("b1", Data.d1, Data.d2)
stmt = select(b1).filter(b1.c.d1.between("d3d1", "d5d1"))
eq_(
sess.scalars(stmt).all(),
[("d3d1", "d3d2"), ("d4d1", "d4d2"), ("d5d1", "d5d2")],
)
def test_single_entity_flag_but_multi_entities(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_single_entity_flag_but_multi_entities(self):
Data = self.classes.Data
sess = fixture_session()
b1 = Bundle("b1", Data.d1, Data.d2, single_entity=True)
b2 = Bundle("b1", Data.d3, single_entity=True)
eq_(
sess.query(b1, b2).filter(b1.c.d1.between("d3d1", "d5d1")).all(),
[
(("d3d1", "d3d2"), ("d3d3",)),
(("d4d1", "d4d2"), ("d4d3",)),
(("d5d1", "d5d2"), ("d5d3",)),
],
)
def test_bundle_nesting(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_bundle_nesting(self):
Data = self.classes.Data
sess = fixture_session()
b1 = Bundle("b1", Data.d1, Bundle("b2", Data.d2, Data.d3))
eq_(
sess.query(b1)
.filter(b1.c.d1.between("d3d1", "d7d1"))
.filter(b1.c.b2.c.d2.between("d4d2", "d6d2"))
.all(),
[
(("d4d1", ("d4d2", "d4d3")),),
(("d5d1", ("d5d2", "d5d3")),),
(("d6d1", ("d6d2", "d6d3")),),
],
)
def test_bundle_nesting_unions(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_query_count(self):
Data = self.classes.Data
b1 = Bundle("b1", Data.d1, Data.d2)
eq_(fixture_session().query(b1).count(), 10)
def test_join_relationship(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_join_relationship(self):
Data = self.classes.Data
sess = fixture_session()
b1 = Bundle("b1", Data.d1, Data.d2)
q = sess.query(b1).join(Data.others)
self.assert_compile(
q,
"SELECT data.d1 AS data_d1, data.d2 "
"AS data_d2 FROM data "
"JOIN other ON data.id = other.data_id",
)
def test_join_selectable(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_join_selectable(self):
Data = self.classes.Data
Other = self.classes.Other
sess = fixture_session()
b1 = Bundle("b1", Data.d1, Data.d2)
q = sess.query(b1).join(Other)
self.assert_compile(
q,
"SELECT data.d1 AS data_d1, data.d2 AS data_d2 "
"FROM data "
"JOIN other ON data.id = other.data_id",
)
def test_joins_from_adapted_entities(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_filter_by(self):
Data = self.classes.Data
b1 = Bundle("b1", Data.id, Data.d1, Data.d2)
sess = fixture_session()
self.assert_compile(
sess.query(b1).filter_by(d1="d1"),
"SELECT data.id AS data_id, data.d1 AS data_d1, "
"data.d2 AS data_d2 FROM data WHERE data.d1 = :d1_1",
)
def test_clause_expansion(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_non_mapped_columns_non_single_entity(self):
data_table = self.tables.data
b1 = Bundle("b1", data_table.c.d1, data_table.c.d2)
sess = fixture_session()
eq_(
sess.query(b1).filter(b1.c.d1.between("d3d1", "d5d1")).all(),
[(("d3d1", "d3d2"),), (("d4d1", "d4d2"),), (("d5d1", "d5d2"),)],
)
def test_non_mapped_columns_single_entity(self):
3
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_non_mapped_columns_single_entity(self):
data_table = self.tables.data
b1 = Bundle("b1", data_table.c.d1, data_table.c.d2, single_entity=True)
sess = fixture_session()
eq_(
sess.query(b1).filter(b1.c.d1.between("d3d1", "d5d1")).all(),
[("d3d1", "d3d2"), ("d4d1", "d4d2"), ("d5d1", "d5d2")],
)
3
Source : test_cache_key.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_bundles_in_annotations(self):
User = self.classes.User
self._run_cache_key_fixture(
lambda: (
Bundle("mybundle", User.id).__clause_element__(),
Bundle("myotherbundle", User.id).__clause_element__(),
Bundle("mybundle", User.name).__clause_element__(),
Bundle("mybundle", User.id, User.name).__clause_element__(),
),
compare_values=True,
)
def test_bundles_directly(self):
3
Source : test_cache_key.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_bundles_directly(self):
User = self.classes.User
self._run_cache_key_fixture(
lambda: (
Bundle("mybundle", User.id),
Bundle("mybundle", User.id).__clause_element__(),
Bundle("myotherbundle", User.id),
Bundle("mybundle", User.name),
Bundle("mybundle", User.id, User.name),
),
compare_values=True,
)
def test_query_expr(self):
0
Source : test_single.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_bundle_qualification(self):
Employee, JuniorEngineer, Manager, Engineer = (
self.classes.Employee,
self.classes.JuniorEngineer,
self.classes.Manager,
self.classes.Engineer,
)
session, m1, e1, e2 = self._fixture_one()
m1id, e1id, e2id = m1.employee_id, e1.employee_id, e2.employee_id
def scalar(q):
return [x[0] for x, in q]
eq_(
scalar(session.query(Bundle("name", Employee.employee_id))),
[m1id, e1id, e2id],
)
eq_(
scalar(session.query(Bundle("name", Engineer.employee_id))),
[e1id, e2id],
)
eq_(scalar(session.query(Bundle("name", Manager.employee_id))), [m1id])
# this currently emits "WHERE type IN (?, ?) AND type IN (?, ?)",
# so no result.
eq_(
session.query(
Bundle("name", Manager.employee_id, Engineer.employee_id)
).all(),
[],
)
eq_(
scalar(session.query(Bundle("name", JuniorEngineer.employee_id))),
[e2id],
)
def test_from_self(self):
0
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_multi_bundle(self):
Data = self.classes.Data
Other = self.classes.Other
d1 = aliased(Data)
b1 = Bundle("b1", d1.d1, d1.d2)
b2 = Bundle("b2", Data.d1, Other.o1)
sess = Session()
q = (
sess.query(b1, b2)
.join(Data.others)
.join(d1, d1.id == Data.id)
.filter(b1.c.d1 == "d3d1")
)
eq_(
q.all(),
[
(("d3d1", "d3d2"), ("d3d1", "d3o0")),
(("d3d1", "d3d2"), ("d3d1", "d3o1")),
(("d3d1", "d3d2"), ("d3d1", "d3o2")),
(("d3d1", "d3d2"), ("d3d1", "d3o3")),
(("d3d1", "d3d2"), ("d3d1", "d3o4")),
],
)
def test_single_entity(self):
0
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_bundle_nesting_unions(self):
Data = self.classes.Data
sess = Session()
b1 = Bundle("b1", Data.d1, Bundle("b2", Data.d2, Data.d3))
q1 = (
sess.query(b1)
.filter(b1.c.d1.between("d3d1", "d7d1"))
.filter(b1.c.b2.c.d2.between("d4d2", "d5d2"))
)
q2 = (
sess.query(b1)
.filter(b1.c.d1.between("d3d1", "d7d1"))
.filter(b1.c.b2.c.d2.between("d5d2", "d6d2"))
)
eq_(
q1.union(q2).all(),
[
(("d4d1", ("d4d2", "d4d3")),),
(("d5d1", ("d5d2", "d5d3")),),
(("d6d1", ("d6d2", "d6d3")),),
],
)
# naming structure is preserved
row = q1.union(q2).first()
eq_(row.b1.d1, "d4d1")
eq_(row.b1.b2.d2, "d4d2")
def test_query_count(self):
0
Source : test_bundle.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_joins_from_adapted_entities(self):
Data = self.classes.Data
# test for #1853 in terms of bundles
# specifically this exercises adapt_to_selectable()
b1 = Bundle("b1", Data.id, Data.d1, Data.d2)
session = Session()
first = session.query(b1)
second = session.query(b1)
unioned = first.union(second)
subquery = session.query(Data.id).subquery()
joined = unioned.outerjoin(subquery, subquery.c.id == Data.id)
joined = joined.order_by(Data.id, Data.d1, Data.d2)
self.assert_compile(
joined,
"SELECT anon_1.data_id AS anon_1_data_id, "
"anon_1.data_d1 AS anon_1_data_d1, "
"anon_1.data_d2 AS anon_1_data_d2 FROM "
"(SELECT data.id AS data_id, data.d1 AS data_d1, "
"data.d2 AS data_d2 FROM "
"data UNION SELECT data.id AS data_id, data.d1 AS data_d1, "
"data.d2 AS data_d2 FROM data) AS anon_1 "
"LEFT OUTER JOIN (SELECT data.id AS id FROM data) AS anon_2 "
"ON anon_2.id = anon_1.data_id "
"ORDER BY anon_1.data_id, anon_1.data_d1, anon_1.data_d2",
)
# tuple nesting still occurs
eq_(
joined.all(),
[
((1, "d0d1", "d0d2"),),
((2, "d1d1", "d1d2"),),
((3, "d2d1", "d2d2"),),
((4, "d3d1", "d3d2"),),
((5, "d4d1", "d4d2"),),
((6, "d5d1", "d5d2"),),
((7, "d6d1", "d6d2"),),
((8, "d7d1", "d7d2"),),
((9, "d8d1", "d8d2"),),
((10, "d9d1", "d9d2"),),
],
)
def test_filter_by(self):
0
Source : test_query.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_column_metadata(self, test_case):
users, Address, addresses, User = (
self.tables.users,
self.classes.Address,
self.tables.addresses,
self.classes.User,
)
mapper(User, users)
mapper(Address, addresses)
sess = create_session()
user_alias = aliased(User)
user_alias_id_label = user_alias.id.label("foo")
address_alias = aliased(Address, name="aalias")
fn = func.count(User.id)
name_label = User.name.label("uname")
bundle = Bundle("b1", User.id, User.name)
cte = sess.query(User.id).cte()
q, asserted = testing.resolve_lambda(test_case, **locals())
eq_(q.column_descriptions, asserted)
def test_unhashable_type(self):
0
Source : test_query.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_entity_or_mapper_zero(self):
User, Address = self.classes.User, self.classes.Address
s = create_session()
q = s.query(User, Address)
is_(q._mapper_zero(), inspect(User))
is_(q._entity_zero(), inspect(User))
u1 = aliased(User)
q = s.query(u1, Address)
is_(q._mapper_zero(), inspect(User))
is_(q._entity_zero(), inspect(u1))
q = s.query(User).select_from(Address)
is_(q._mapper_zero(), inspect(User))
is_(q._entity_zero(), inspect(Address))
q = s.query(User.name, Address)
is_(q._mapper_zero(), inspect(User))
is_(q._entity_zero(), inspect(User))
q = s.query(u1.name, Address)
is_(q._mapper_zero(), inspect(User))
is_(q._entity_zero(), inspect(u1))
q1 = s.query(User).exists()
q = s.query(q1)
is_(q._mapper_zero(), None)
is_(q._entity_zero(), None)
q1 = s.query(Bundle("b1", User.id, User.name))
is_(q1._mapper_zero(), inspect(User))
is_(q1._entity_zero(), inspect(User))
@testing.combinations(
0
Source : test_single.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_bundle_qualification(self):
Employee, JuniorEngineer, Manager, Engineer = (
self.classes.Employee,
self.classes.JuniorEngineer,
self.classes.Manager,
self.classes.Engineer,
)
session, m1, e1, e2 = self._fixture_one()
m1id, e1id, e2id = m1.employee_id, e1.employee_id, e2.employee_id
def scalar(q):
return [x[0] for x, in q]
eq_(
scalar(session.query(Bundle("name", Employee.employee_id))),
[m1id, e1id, e2id],
)
eq_(
scalar(session.query(Bundle("name", Engineer.employee_id))),
[e1id, e2id],
)
eq_(scalar(session.query(Bundle("name", Manager.employee_id))), [m1id])
# this currently emits "WHERE type IN (?, ?) AND type IN (?, ?)",
# so no result.
eq_(
session.query(
Bundle("name", Manager.employee_id, Engineer.employee_id)
).all(),
[],
)
eq_(
scalar(session.query(Bundle("name", JuniorEngineer.employee_id))),
[e2id],
)
def test_from_subq(self):
0
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_multi_bundle(self):
Data = self.classes.Data
Other = self.classes.Other
d1 = aliased(Data)
b1 = Bundle("b1", d1.d1, d1.d2)
b2 = Bundle("b2", Data.d1, Other.o1)
sess = fixture_session()
q = (
sess.query(b1, b2)
.join(Data.others)
.join(d1, d1.id == Data.id)
.filter(b1.c.d1 == "d3d1")
)
eq_(
q.all(),
[
(("d3d1", "d3d2"), ("d3d1", "d3o0")),
(("d3d1", "d3d2"), ("d3d1", "d3o1")),
(("d3d1", "d3d2"), ("d3d1", "d3o2")),
(("d3d1", "d3d2"), ("d3d1", "d3o3")),
(("d3d1", "d3d2"), ("d3d1", "d3o4")),
],
)
def test_multi_bundle_future(self):
0
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_multi_bundle_future(self):
Data = self.classes.Data
Other = self.classes.Other
d1 = aliased(Data)
b1 = Bundle("b1", d1.d1, d1.d2)
b2 = Bundle("b2", Data.d1, Other.o1)
sess = Session(testing.db, future=True)
stmt = (
select(b1, b2)
.join(Data.others)
.join(d1, d1.id == Data.id)
.filter(b1.c.d1 == "d3d1")
)
eq_(
sess.execute(stmt).all(),
[
(("d3d1", "d3d2"), ("d3d1", "d3o0")),
(("d3d1", "d3d2"), ("d3d1", "d3o1")),
(("d3d1", "d3d2"), ("d3d1", "d3o2")),
(("d3d1", "d3d2"), ("d3d1", "d3o3")),
(("d3d1", "d3d2"), ("d3d1", "d3o4")),
],
)
def test_single_entity_legacy_query(self):
0
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_bundle_nesting_unions(self):
Data = self.classes.Data
sess = fixture_session()
b1 = Bundle("b1", Data.d1, Bundle("b2", Data.d2, Data.d3))
q1 = (
sess.query(b1)
.filter(b1.c.d1.between("d3d1", "d7d1"))
.filter(b1.c.b2.c.d2.between("d4d2", "d5d2"))
)
q2 = (
sess.query(b1)
.filter(b1.c.d1.between("d3d1", "d7d1"))
.filter(b1.c.b2.c.d2.between("d5d2", "d6d2"))
)
eq_(
q1.union(q2).all(),
[
(("d4d1", ("d4d2", "d4d3")),),
(("d5d1", ("d5d2", "d5d3")),),
(("d6d1", ("d6d2", "d6d3")),),
],
)
# naming structure is preserved
row = q1.union(q2).first()
eq_(row.b1.d1, "d4d1")
eq_(row.b1.b2.d2, "d4d2")
def test_query_count(self):
0
Source : test_bundle.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_joins_from_adapted_entities(self):
Data = self.classes.Data
# test for #1853 in terms of bundles
# specifically this exercises adapt_to_selectable()
b1 = Bundle("b1", Data.id, Data.d1, Data.d2)
session = fixture_session()
first = session.query(b1)
second = session.query(b1)
unioned = first.union(second)
subquery = session.query(Data.id).subquery()
joined = unioned.outerjoin(subquery, subquery.c.id == Data.id)
joined = joined.order_by(Data.id, Data.d1, Data.d2)
self.assert_compile(
joined,
"SELECT anon_1.data_id AS anon_1_data_id, "
"anon_1.data_d1 AS anon_1_data_d1, "
"anon_1.data_d2 AS anon_1_data_d2 FROM "
"(SELECT data.id AS data_id, data.d1 AS data_d1, "
"data.d2 AS data_d2 FROM "
"data UNION SELECT data.id AS data_id, data.d1 AS data_d1, "
"data.d2 AS data_d2 FROM data) AS anon_1 "
"LEFT OUTER JOIN (SELECT data.id AS id FROM data) AS anon_2 "
"ON anon_2.id = anon_1.data_id "
"ORDER BY anon_1.data_id, anon_1.data_d1, anon_1.data_d2",
)
# tuple nesting still occurs
eq_(
joined.all(),
[
((1, "d0d1", "d0d2"),),
((2, "d1d1", "d1d2"),),
((3, "d2d1", "d2d2"),),
((4, "d3d1", "d3d2"),),
((5, "d4d1", "d4d2"),),
((6, "d5d1", "d5d2"),),
((7, "d6d1", "d6d2"),),
((8, "d7d1", "d7d2"),),
((9, "d8d1", "d8d2"),),
((10, "d9d1", "d9d2"),),
],
)
def test_filter_by(self):
See More Examples