Skip to content

Commit

Permalink
Fix createConnectionField deprecation warnings (#229)
Browse files Browse the repository at this point in the history
* Fix deprecation warnings
* Release `2.2.1`
* Add unrelated test to increase test coverage and unblock PR
  • Loading branch information
jnak committed Jun 18, 2019
1 parent 9a0f740 commit c89cf80
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
2 changes: 1 addition & 1 deletion graphene_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .fields import SQLAlchemyConnectionField
from .utils import get_query, get_session

__version__ = "2.2.0"
__version__ = "2.2.1"

__all__ = [
"__version__",
Expand Down
21 changes: 11 additions & 10 deletions graphene_sqlalchemy/fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
import warnings
from functools import partial

from promise import Promise, is_thenable
Expand All @@ -10,8 +10,6 @@

from .utils import get_query

log = logging.getLogger()


class UnsortedSQLAlchemyConnectionField(ConnectionField):
@property
Expand Down Expand Up @@ -100,34 +98,37 @@ def __init__(self, type, *args, **kwargs):
def default_connection_field_factory(relationship, registry, **field_kwargs):
model = relationship.mapper.entity
model_type = registry.get_type_for_model(model)
return createConnectionField(model_type, **field_kwargs)
return __connectionFactory(model_type, **field_kwargs)


# TODO Remove in next major version
__connectionFactory = UnsortedSQLAlchemyConnectionField


def createConnectionField(_type, **field_kwargs):
log.warning(
warnings.warn(
'createConnectionField is deprecated and will be removed in the next '
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.'
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
DeprecationWarning,
)
return __connectionFactory(_type, **field_kwargs)


def registerConnectionFieldFactory(factoryMethod):
log.warning(
warnings.warn(
'registerConnectionFieldFactory is deprecated and will be removed in the next '
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.'
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
DeprecationWarning,
)
global __connectionFactory
__connectionFactory = factoryMethod


def unregisterConnectionFieldFactory():
log.warning(
warnings.warn(
'registerConnectionFieldFactory is deprecated and will be removed in the next '
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.'
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
DeprecationWarning,
)
global __connectionFactory
__connectionFactory = UnsortedSQLAlchemyConnectionField
64 changes: 42 additions & 22 deletions graphene_sqlalchemy/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from ..converter import convert_sqlalchemy_composite
from ..fields import (SQLAlchemyConnectionField,
UnsortedSQLAlchemyConnectionField,
UnsortedSQLAlchemyConnectionField, createConnectionField,
registerConnectionFieldFactory,
unregisterConnectionFieldFactory)
from ..types import ORMField, SQLAlchemyObjectType, SQLAlchemyObjectTypeOptions
Expand Down Expand Up @@ -224,6 +224,19 @@ class Meta:
assert pets_field.type().description == 'Overridden'


def test_invalid_model_attr():
err_msg = (
"Cannot map ORMField to a model attribute.\n"
"Field: 'ReporterType.first_name'"
)
with pytest.raises(ValueError, match=err_msg):
class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter

first_name = ORMField(model_attr='does_not_exist')


def test_only_fields():
class ReporterType(SQLAlchemyObjectType):
class Meta:
Expand Down Expand Up @@ -364,33 +377,40 @@ class Meta:


def test_deprecated_registerConnectionFieldFactory():
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)
with pytest.warns(DeprecationWarning):
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)

class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter
interfaces = (Node,)

class ArticleType(SQLAlchemyObjectType):
class Meta:
model = Article
interfaces = (Node,)
class ArticleType(SQLAlchemyObjectType):
class Meta:
model = Article
interfaces = (Node,)

assert isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)
assert isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)


def test_deprecated_unregisterConnectionFieldFactory():
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)
unregisterConnectionFieldFactory()
with pytest.warns(DeprecationWarning):
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)
unregisterConnectionFieldFactory()

class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter
interfaces = (Node,)

class ArticleType(SQLAlchemyObjectType):
class Meta:
model = Article
interfaces = (Node,)

assert not isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)

class ArticleType(SQLAlchemyObjectType):
class Meta:
model = Article
interfaces = (Node,)

assert not isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)
def test_deprecated_createConnectionField():
with pytest.warns(DeprecationWarning):
createConnectionField(None)
5 changes: 4 additions & 1 deletion graphene_sqlalchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ def construct_fields(
for orm_field_name, orm_field in custom_orm_fields_items:
attr_name = orm_field.kwargs.get('model_attr', orm_field_name)
if attr_name not in all_model_attrs:
raise Exception('Cannot map ORMField "{}" to SQLAlchemy model property'.format(orm_field_name))
raise ValueError((
"Cannot map ORMField to a model attribute.\n"
"Field: '{}.{}'"
).format(obj_type.__name__, orm_field_name,))
orm_field.kwargs['model_attr'] = attr_name

# Merge automatic fields with custom ORM fields
Expand Down

0 comments on commit c89cf80

Please sign in to comment.