Skip to content

Commit 39834ed

Browse files
committed
chore: fix review comments
1 parent b84aa9f commit 39834ed

File tree

5 files changed

+44
-61
lines changed

5 files changed

+44
-61
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ __pycache__/
1212
.Python
1313
env/
1414
.venv/
15+
venv/
1516
build/
1617
develop-eggs/
1718
dist/

graphene_sqlalchemy/tests/models.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
String,
1717
Table,
1818
func,
19-
select,
2019
)
2120
from sqlalchemy.ext.declarative import declarative_base
2221
from sqlalchemy.ext.hybrid import hybrid_property
2322
from sqlalchemy.orm import backref, column_property, composite, mapper, relationship
2423

24+
from graphene_sqlalchemy.tests.utils import wrap_select_func
2525
from graphene_sqlalchemy.utils import SQL_VERSION_HIGHER_EQUAL_THAN_1_4
2626

2727
PetKind = Enum("cat", "dog", name="pet_kind")
@@ -118,15 +118,9 @@ def hybrid_prop_bool(self) -> bool:
118118
def hybrid_prop_list(self) -> List[int]:
119119
return [1, 2, 3]
120120

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-
)
121+
column_prop = column_property(
122+
wrap_select_func(func.cast(func.count(id), Integer)), doc="Column property"
123+
)
130124

131125
composite_prop = composite(
132126
CompositeFullName, first_name, last_name, doc="Composite"

graphene_sqlalchemy/tests/models_batching.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
String,
1212
Table,
1313
func,
14-
select,
1514
)
1615
from sqlalchemy.ext.declarative import declarative_base
1716
from sqlalchemy.orm import column_property, relationship
1817

19-
from graphene_sqlalchemy.utils import SQL_VERSION_HIGHER_EQUAL_THAN_1_4
18+
from graphene_sqlalchemy.tests.utils import wrap_select_func
2019

2120
PetKind = Enum("cat", "dog", name="pet_kind")
2221

@@ -62,14 +61,9 @@ class Reporter(Base):
6261
articles = relationship("Article", backref="reporter")
6362
favorite_article = relationship("Article", uselist=False)
6463

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-
)
64+
column_prop = column_property(
65+
wrap_select_func(func.cast(func.count(id), Integer)), doc="Column property"
66+
)
7367

7468

7569
class Article(Base):

graphene_sqlalchemy/tests/test_converter.py

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@
22
import sys
33
from typing import Dict, Union
44

5+
import graphene
56
import pytest
67
import sqlalchemy_utils as sqa_utils
7-
from sqlalchemy import Column, func, select, types
8+
from graphene.relay import Node
9+
from graphene.types.structures import Structure
10+
from sqlalchemy import Column, func, types
811
from sqlalchemy.dialects import postgresql
912
from sqlalchemy.ext.declarative import declarative_base
1013
from sqlalchemy.ext.hybrid import hybrid_property
1114
from sqlalchemy.inspection import inspect
1215
from sqlalchemy.orm import column_property, composite
1316

14-
import graphene
15-
from graphene.relay import Node
16-
from graphene.types.structures import Structure
17-
17+
from .models import (
18+
Article,
19+
CompositeFullName,
20+
Pet,
21+
Reporter,
22+
ShoppingCart,
23+
ShoppingCartItem,
24+
)
1825
from ..converter import (
1926
convert_sqlalchemy_column,
2027
convert_sqlalchemy_composite,
@@ -24,15 +31,7 @@
2431
from ..fields import UnsortedSQLAlchemyConnectionField, default_connection_field_factory
2532
from ..registry import Registry, get_global_registry
2633
from ..types import ORMField, SQLAlchemyObjectType
27-
from ..utils import SQL_VERSION_HIGHER_EQUAL_THAN_1_4, is_sqlalchemy_version_less_than
28-
from .models import (
29-
Article,
30-
CompositeFullName,
31-
Pet,
32-
Reporter,
33-
ShoppingCart,
34-
ShoppingCartItem,
35-
)
34+
from ..utils import is_sqlalchemy_version_less_than
3635

3736

3837
def mock_resolver():
@@ -88,9 +87,9 @@ def prop_method() -> int | str:
8887
return "not allowed in gql schema"
8988

9089
with pytest.raises(
91-
ValueError,
92-
match=r"Cannot convert hybrid_property Union to "
93-
r"graphene.Union: the Union contains scalars. \.*",
90+
ValueError,
91+
match=r"Cannot convert hybrid_property Union to "
92+
r"graphene.Union: the Union contains scalars. \.*",
9493
):
9594
get_hybrid_property_type(prop_method)
9695

@@ -337,25 +336,9 @@ class TestEnum(enum.IntEnum):
337336
assert graphene_type._meta.enum.__members__["two"].value == 2
338337

339338

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-
)
356339
def test_should_columproperty_convert():
357340
field = get_field_from_column(
358-
column_property(select([func.sum(func.cast(id, types.Integer))]).where(id == 1))
341+
column_property(wrap_select_func(func.sum(func.cast(id, types.Integer))).where(id == 1))
359342
)
360343

361344
assert field.type == graphene.Int
@@ -654,8 +637,8 @@ class Meta:
654637
)
655638

656639
for (
657-
hybrid_prop_name,
658-
hybrid_prop_expected_return_type,
640+
hybrid_prop_name,
641+
hybrid_prop_expected_return_type,
659642
) in shopping_cart_item_expected_types.items():
660643
hybrid_prop_field = ShoppingCartItemType._meta.fields[hybrid_prop_name]
661644

@@ -666,7 +649,7 @@ class Meta:
666649
str(hybrid_prop_expected_return_type),
667650
)
668651
assert (
669-
hybrid_prop_field.description is None
652+
hybrid_prop_field.description is None
670653
) # "doc" is ignored by hybrid property
671654

672655
###################################################
@@ -714,8 +697,8 @@ class Meta:
714697
)
715698

716699
for (
717-
hybrid_prop_name,
718-
hybrid_prop_expected_return_type,
700+
hybrid_prop_name,
701+
hybrid_prop_expected_return_type,
719702
) in shopping_cart_expected_types.items():
720703
hybrid_prop_field = ShoppingCartType._meta.fields[hybrid_prop_name]
721704

@@ -726,5 +709,5 @@ class Meta:
726709
str(hybrid_prop_expected_return_type),
727710
)
728711
assert (
729-
hybrid_prop_field.description is None
712+
hybrid_prop_field.description is None
730713
) # "doc" is ignored by hybrid property

graphene_sqlalchemy/tests/utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import inspect
22
import re
33

4+
from sqlalchemy import select
5+
6+
from graphene_sqlalchemy.utils import SQL_VERSION_HIGHER_EQUAL_THAN_1_4
7+
48

59
def to_std_dicts(value):
610
"""Convert nested ordered dicts to normal dicts for better comparison."""
@@ -18,8 +22,15 @@ def remove_cache_miss_stat(message):
1822
return re.sub(r"\[generated in \d+.?\d*s\]\s", "", message)
1923

2024

21-
async def eventually_await_session(session, func, *args):
25+
def wrap_select_func(query):
26+
# TODO remove this when we drop support for sqa < 2.0
27+
if SQL_VERSION_HIGHER_EQUAL_THAN_1_4:
28+
return select(query)
29+
else:
30+
return select([query])
31+
2232

33+
async def eventually_await_session(session, func, *args):
2334
if inspect.iscoroutinefunction(getattr(session, func)):
2435
await getattr(session, func)(*args)
2536
else:

0 commit comments

Comments
 (0)