Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

LocalStructuredProperty support #11

Merged
merged 4 commits into from
Jun 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion graphene_gae/ndb/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import inflect
from google.appengine.ext import ndb

from graphene import LazyType
from graphene.core.types import Field
from graphene.core.types.definitions import List
from graphene.core.types.scalars import String, Boolean, Int, Float
from graphene.core.types.custom_scalars import JSONString, DateTime
from graphene_gae.ndb.fields import NdbKeyField
Expand Down Expand Up @@ -65,6 +68,20 @@ def convert_ndb_key_propety(ndb_key_prop, meta):
return ConversionResult(name=name, field=field)


def convert_local_structured_property(ndb_structured_prop, meta):
is_required = ndb_structured_prop._required
is_repeated = ndb_structured_prop._repeated
model = ndb_structured_prop._modelclass
name = ndb_structured_prop._code_name

t = LazyType(model.__name__ + 'Type')
if is_repeated:
l = List(t)
return ConversionResult(name=name, field=l.NonNull if is_required else l)

return ConversionResult(name=name, field=Field(t))


converters = {
ndb.StringProperty: convert_ndb_string_property,
ndb.TextProperty: convert_ndb_string_property,
Expand All @@ -74,7 +91,8 @@ def convert_ndb_key_propety(ndb_key_prop, meta):
ndb.JsonProperty: convert_ndb_json_property,
ndb.DateProperty: convert_ndb_datetime_property,
ndb.DateTimeProperty: convert_ndb_datetime_property,
ndb.KeyProperty: convert_ndb_key_propety
ndb.KeyProperty: convert_ndb_key_propety,
ndb.LocalStructuredProperty: convert_local_structured_property
}


Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pip
setuptools

wheel==0.23.0
tox
coverage
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def get_version(package_name):
'PyYAML==3.11',
'webapp2==2.5.2',
'webob==1.2.3',
'WebTest==2.0.11'
'WebTest==2.0.11',
'mock'
]


Expand Down
12 changes: 12 additions & 0 deletions tests/_ndb/test_converter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mock
from tests.base_test import BaseTest

from google.appengine.ext import ndb
Expand Down Expand Up @@ -29,6 +30,17 @@ def testUnknownProperty_raisesException(self):

self.assertTrue("Don't know how to convert" in context.exception.message, msg=context.exception.message)

@mock.patch('graphene_gae.ndb.converter.converters')
def testNoneResult_raisesException(self, patch_convert):
from graphene_gae.ndb.converter import convert_ndb_property
patch_convert.get.return_value = lambda _, __: None
with self.assertRaises(Exception) as context:
prop = ndb.StringProperty()
prop._code_name = "my_prop"
convert_ndb_property(prop)

self.assertTrue("Failed to convert NDB field my_prop" in context.exception.message, msg=context.exception.message)

def testStringProperty_shouldConvertToString(self):
self.__assert_conversion(ndb.StringProperty, graphene.String)

Expand Down
83 changes: 82 additions & 1 deletion tests/_ndb/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,31 @@
import graphene

from graphene_gae import NdbObjectType
from tests.models import Tag, Comment, Article
from tests.models import Tag, Comment, Article, Address, Author, PhoneNumber

__author__ = 'ekampf'

schema = graphene.Schema()


@schema.register
class AddressType(NdbObjectType):
class Meta:
model = Address


@schema.register
class PhoneNumberType(NdbObjectType):
class Meta:
model = PhoneNumber


@schema.register
class AuthorType(NdbObjectType):
class Meta:
model = Author


@schema.register
class TagType(NdbObjectType):
class Meta:
Expand Down Expand Up @@ -169,3 +187,66 @@ def testQuery_repeatedProperty(self):
self.assertEqual(article["createdAt"], str(a.get().created_at.isoformat()))
self.assertEqual(article["headline"], "Test1")
self.assertListEqual(article["keywords"], keywords)

def testQuery_structuredProperty(self):
mobile = PhoneNumber(area="650", number="12345678")
author_key = Author(name="John Dow", email="john@dow.com", mobile=mobile).put()
Article(headline="Test1", author_key=author_key).put()

result = schema.execute("""
query Articles {
articles {
headline,
author {
name
email
mobile { area, number }
}
}
}
""")
self.assertEmpty(result.errors, msg=str(result.errors))

article = result.data['articles'][0]
self.assertEqual(article["headline"], "Test1")

author = article['author']
self.assertEqual(author["name"], "John Dow")
self.assertEqual(author["email"], "john@dow.com")
self.assertDictEqual(dict(area="650", number="12345678"), dict(author["mobile"]))

def testQuery_structuredProperty_repeated(self):
address1 = Address(address1="address1", address2="apt 1", city="Mountain View")
address2 = Address(address1="address2", address2="apt 2", city="Mountain View")
author_key = Author(name="John Dow", email="john@dow.com", addresses=[address1, address2]).put()
Article(headline="Test1", author_key=author_key).put()

result = schema.execute("""
query Articles {
articles {
headline,
author {
name
email
addresses {
address1
address2
city
}
}
}
}
""")
self.assertEmpty(result.errors)

article = result.data['articles'][0]
self.assertEqual(article["headline"], "Test1")

author = article['author']
self.assertEqual(author["name"], "John Dow")
self.assertEqual(author["email"], "john@dow.com")
self.assertLength(author["addresses"], 2)

addresses = [dict(d) for d in author["addresses"]]
self.assertIn(address1.to_dict(), addresses)
self.assertIn(address2.to_dict(), addresses)
14 changes: 13 additions & 1 deletion tests/_ndb/test_types_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@
import graphene
from graphene_gae import NdbNode, NdbConnectionField

from tests.models import Tag, Comment, Article, Author
from tests.models import Tag, Comment, Article, Author, Address, PhoneNumber

__author__ = 'ekampf'


schema = graphene.Schema()


@schema.register
class AddressType(NdbNode):
class Meta:
model = Address


@schema.register
class PhoneNumberType(NdbNode):
class Meta:
model = PhoneNumber


@schema.register
class AuthorType(NdbNode):
class Meta:
Expand Down
13 changes: 13 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
__author__ = 'ekampf'


class Address(ndb.Model):
address1 = ndb.StringProperty()
address2 = ndb.StringProperty()
city = ndb.StringProperty()


class PhoneNumber(ndb.Model):
area = ndb.StringProperty()
number = ndb.StringProperty()


class Author(ndb.Model):
name = ndb.StringProperty()
email = ndb.StringProperty()
addresses = ndb.LocalStructuredProperty(Address, repeated=True)
mobile = ndb.LocalStructuredProperty(PhoneNumber)


class Tag(ndb.Model):
Expand Down