|
3 | 3 | import graphene |
4 | 4 |
|
5 | 5 | from graphene_gae import NdbObjectType |
6 | | -from tests.models import Tag, Comment, Article |
| 6 | +from tests.models import Tag, Comment, Article, Address, Author, PhoneNumber |
7 | 7 |
|
8 | 8 | __author__ = 'ekampf' |
9 | 9 |
|
10 | 10 | schema = graphene.Schema() |
11 | 11 |
|
12 | 12 |
|
| 13 | +@schema.register |
| 14 | +class AddressType(NdbObjectType): |
| 15 | + class Meta: |
| 16 | + model = Address |
| 17 | + |
| 18 | + |
| 19 | +@schema.register |
| 20 | +class PhoneNumberType(NdbObjectType): |
| 21 | + class Meta: |
| 22 | + model = PhoneNumber |
| 23 | + |
| 24 | + |
| 25 | +@schema.register |
| 26 | +class AuthorType(NdbObjectType): |
| 27 | + class Meta: |
| 28 | + model = Author |
| 29 | + |
| 30 | + |
13 | 31 | @schema.register |
14 | 32 | class TagType(NdbObjectType): |
15 | 33 | class Meta: |
@@ -169,3 +187,66 @@ def testQuery_repeatedProperty(self): |
169 | 187 | self.assertEqual(article["createdAt"], str(a.get().created_at.isoformat())) |
170 | 188 | self.assertEqual(article["headline"], "Test1") |
171 | 189 | self.assertListEqual(article["keywords"], keywords) |
| 190 | + |
| 191 | + def testQuery_structuredProperty(self): |
| 192 | + mobile = PhoneNumber(area="650", number="12345678") |
| 193 | + author_key = Author( name="John Dow", email="[email protected]", mobile=mobile). put() |
| 194 | + Article(headline="Test1", author_key=author_key).put() |
| 195 | + |
| 196 | + result = schema.execute(""" |
| 197 | + query Articles { |
| 198 | + articles { |
| 199 | + headline, |
| 200 | + author { |
| 201 | + name |
| 202 | + email |
| 203 | + mobile { area, number } |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + """) |
| 208 | + self.assertEmpty(result.errors, msg=str(result.errors)) |
| 209 | + |
| 210 | + article = result.data['articles'][0] |
| 211 | + self.assertEqual(article["headline"], "Test1") |
| 212 | + |
| 213 | + author = article['author'] |
| 214 | + self.assertEqual(author["name"], "John Dow") |
| 215 | + self. assertEqual( author[ "email"], "[email protected]") |
| 216 | + self.assertDictEqual(dict(area="650", number="12345678"), dict(author["mobile"])) |
| 217 | + |
| 218 | + def testQuery_structuredProperty_repeated(self): |
| 219 | + address1 = Address(address1="address1", address2="apt 1", city="Mountain View") |
| 220 | + address2 = Address(address1="address2", address2="apt 2", city="Mountain View") |
| 221 | + author_key = Author( name="John Dow", email="[email protected]", addresses=[ address1, address2]). put() |
| 222 | + Article(headline="Test1", author_key=author_key).put() |
| 223 | + |
| 224 | + result = schema.execute(""" |
| 225 | + query Articles { |
| 226 | + articles { |
| 227 | + headline, |
| 228 | + author { |
| 229 | + name |
| 230 | + email |
| 231 | + addresses { |
| 232 | + address1 |
| 233 | + address2 |
| 234 | + city |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + """) |
| 240 | + self.assertEmpty(result.errors) |
| 241 | + |
| 242 | + article = result.data['articles'][0] |
| 243 | + self.assertEqual(article["headline"], "Test1") |
| 244 | + |
| 245 | + author = article['author'] |
| 246 | + self.assertEqual(author["name"], "John Dow") |
| 247 | + self. assertEqual( author[ "email"], "[email protected]") |
| 248 | + self.assertLength(author["addresses"], 2) |
| 249 | + |
| 250 | + addresses = [dict(d) for d in author["addresses"]] |
| 251 | + self.assertIn(address1.to_dict(), addresses) |
| 252 | + self.assertIn(address2.to_dict(), addresses) |
0 commit comments