Skip to content

Commit 7288117

Browse files
committed
fix: all unit tests running
Signed-off-by: Erik Wrede <[email protected]>
1 parent 812b28b commit 7288117

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed

graphene_sqlalchemy/batching.py

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sqlalchemy
66
from sqlalchemy.orm import Session, strategies
77
from sqlalchemy.orm.query import QueryContext
8+
from sqlalchemy.util import immutabledict
89

910
from .utils import SQL_VERSION_HIGHER_EQUAL_THAN_1_4, is_graphene_version_less_than
1011

@@ -77,6 +78,17 @@ async def batch_load_fn(self, parents):
7778
else:
7879
query_context = QueryContext(session.query(parent_mapper.entity))
7980
if SQL_VERSION_HIGHER_EQUAL_THAN_1_4:
81+
self.selectin_loader._load_for_path(
82+
query_context,
83+
parent_mapper._path_registry,
84+
states,
85+
None,
86+
child_mapper,
87+
None,
88+
None, # recursion depth can be none
89+
immutabledict(), # default value for selectinload->lazyload
90+
)
91+
elif SQL_VERSION_HIGHER_EQUAL_THAN_1_4:
8092
self.selectin_loader._load_for_path(
8193
query_context,
8294
parent_mapper._path_registry,

graphene_sqlalchemy/tests/models.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from sqlalchemy.ext.hybrid import hybrid_property
2323
from sqlalchemy.orm import backref, column_property, composite, mapper, relationship
2424

25+
from graphene_sqlalchemy.utils import SQL_VERSION_HIGHER_EQUAL_THAN_1_4
26+
2527
PetKind = Enum("cat", "dog", name="pet_kind")
2628

2729

@@ -116,9 +118,15 @@ def hybrid_prop_bool(self) -> bool:
116118
def hybrid_prop_list(self) -> List[int]:
117119
return [1, 2, 3]
118120

119-
column_prop = column_property(
120-
select([func.cast(func.count(id), Integer)]), doc="Column property"
121-
)
121+
# TODO Remove when switching min sqlalchemy version to SQLAlchemy 1.4
122+
if SQL_VERSION_HIGHER_EQUAL_THAN_1_4:
123+
column_prop = column_property(
124+
select(func.cast(func.count(id), Integer)), doc="Column property"
125+
)
126+
else:
127+
column_prop = column_property(
128+
select([func.cast(func.count(id), Integer)]), doc="Column property"
129+
)
122130

123131
composite_prop = composite(
124132
CompositeFullName, first_name, last_name, doc="Composite"
@@ -161,7 +169,11 @@ def __subclasses__(cls):
161169

162170
editor_table = Table("editors", Base.metadata, autoload=True)
163171

164-
mapper(ReflectedEditor, editor_table)
172+
# TODO Remove when switching min sqlalchemy version to SQLAlchemy 1.4
173+
if SQL_VERSION_HIGHER_EQUAL_THAN_1_4:
174+
Base.registry.map_imperatively(ReflectedEditor, editor_table)
175+
else:
176+
mapper(ReflectedEditor, editor_table)
165177

166178

167179
############################################

graphene_sqlalchemy/tests/models_batching.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from sqlalchemy.ext.declarative import declarative_base
1717
from sqlalchemy.orm import column_property, relationship
1818

19+
from graphene_sqlalchemy.utils import SQL_VERSION_HIGHER_EQUAL_THAN_1_4
20+
1921
PetKind = Enum("cat", "dog", name="pet_kind")
2022

2123

@@ -60,9 +62,14 @@ class Reporter(Base):
6062
articles = relationship("Article", backref="reporter")
6163
favorite_article = relationship("Article", uselist=False)
6264

63-
column_prop = column_property(
64-
select([func.cast(func.count(id), Integer)]), doc="Column property"
65-
)
65+
if SQL_VERSION_HIGHER_EQUAL_THAN_1_4:
66+
column_prop = column_property(
67+
select(func.cast(func.count(id), Integer)), doc="Column property"
68+
)
69+
else:
70+
column_prop = column_property(
71+
select([func.cast(func.count(id), Integer)]), doc="Column property"
72+
)
6673

6774

6875
class Article(Base):

graphene_sqlalchemy/tests/test_converter.py

+25
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from ..fields import UnsortedSQLAlchemyConnectionField, default_connection_field_factory
2525
from ..registry import Registry, get_global_registry
2626
from ..types import ORMField, SQLAlchemyObjectType
27+
from ..utils import SQL_VERSION_HIGHER_EQUAL_THAN_1_4, is_sqlalchemy_version_less_than
2728
from .models import (
2829
Article,
2930
CompositeFullName,
@@ -336,6 +337,22 @@ class TestEnum(enum.IntEnum):
336337
assert graphene_type._meta.enum.__members__["two"].value == 2
337338

338339

340+
@pytest.mark.skipif(
341+
not SQL_VERSION_HIGHER_EQUAL_THAN_1_4,
342+
reason="SQLAlchemy <1.4 does not support this",
343+
)
344+
def test_should_columproperty_convert_sqa_20():
345+
field = get_field_from_column(
346+
column_property(select(func.sum(func.cast(id, types.Integer))).where(id == 1))
347+
)
348+
349+
assert field.type == graphene.Int
350+
351+
352+
@pytest.mark.skipif(
353+
not is_sqlalchemy_version_less_than("2.0.0b1"),
354+
reason="SQLAlchemy >=2.0 does not support this syntax, see convert_sqa_20",
355+
)
339356
def test_should_columproperty_convert():
340357
field = get_field_from_column(
341358
column_property(select([func.sum(func.cast(id, types.Integer))]).where(id == 1))
@@ -355,10 +372,18 @@ def test_should_jsontype_convert_jsonstring():
355372
assert get_field(types.JSON).type == graphene.JSONString
356373

357374

375+
@pytest.mark.skipif(
376+
(not is_sqlalchemy_version_less_than("2.0.0b1")),
377+
reason="SQLAlchemy >=2.0 does not support this: Variant is no longer used in SQLAlchemy",
378+
)
358379
def test_should_variant_int_convert_int():
359380
assert get_field(types.Variant(types.Integer(), {})).type == graphene.Int
360381

361382

383+
@pytest.mark.skipif(
384+
(not is_sqlalchemy_version_less_than("2.0.0b1")),
385+
reason="SQLAlchemy >=2.0 does not support this: Variant is no longer used in SQLAlchemy",
386+
)
362387
def test_should_variant_string_convert_string():
363388
assert get_field(types.Variant(types.String(), {})).type == graphene.String
364389

graphene_sqlalchemy/utils.py

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ def is_graphene_version_less_than(version_string): # pragma: no cover
3232
SQL_VERSION_HIGHER_EQUAL_THAN_1_4 = True
3333

3434

35+
SQL_VERSION_HIGHER_EQUAL_THAN_2 = False
36+
37+
if not is_sqlalchemy_version_less_than("2.0.0b1"):
38+
SQL_VERSION_HIGHER_EQUAL_THAN_2 = True
39+
40+
3541
def get_session(context):
3642
return context.get("session")
3743

0 commit comments

Comments
 (0)