|
| 1 | +from graphql.core import graphql |
| 2 | +from epoxy import TypeRegistry |
| 3 | + |
| 4 | + |
| 5 | +def test_can_be(): |
| 6 | + R = TypeRegistry() |
| 7 | + |
| 8 | + @R.Cat.CanBe |
| 9 | + class MyCat(object): |
| 10 | + def __init__(self, name, meow): |
| 11 | + self.name = name |
| 12 | + self.meow = meow |
| 13 | + |
| 14 | + class Pet(R.Interface): |
| 15 | + name = R.String |
| 16 | + |
| 17 | + class Dog(R.Implements.Pet): |
| 18 | + bark = R.String |
| 19 | + |
| 20 | + class Cat(R.Implements.Pet): |
| 21 | + meow = R.String |
| 22 | + |
| 23 | + class Bird(R.Implements.Pet): |
| 24 | + tweet = R.String |
| 25 | + |
| 26 | + class Query(R.ObjectType): |
| 27 | + pets = R.Pet.List |
| 28 | + |
| 29 | + @R.Dog.CanBe |
| 30 | + class MyDog(object): |
| 31 | + def __init__(self, name, bark): |
| 32 | + self.name = name |
| 33 | + self.bark = bark |
| 34 | + |
| 35 | + schema = R.Schema(Query) |
| 36 | + |
| 37 | + @R.Bird.CanBe |
| 38 | + class MyBird(object): |
| 39 | + def __init__(self, name, tweet): |
| 40 | + self.name = name |
| 41 | + self.tweet = tweet |
| 42 | + |
| 43 | + data = Query(pets=[ |
| 44 | + MyDog(name='Clifford', bark='Really big bark, because it\'s a really big dog.'), |
| 45 | + MyCat(name='Garfield', meow='Lasagna'), |
| 46 | + MyBird(name='Tweetie', tweet='#yolo'), |
| 47 | + |
| 48 | + Dog(name='OTClifford', bark='Really big bark, because it\'s a really big dog.'), |
| 49 | + Cat(name='OTGarfield', meow='Lasagna'), |
| 50 | + Bird(name='OTTweetie', tweet='#yolo'), |
| 51 | + ]) |
| 52 | + |
| 53 | + result = graphql(schema, ''' |
| 54 | + { |
| 55 | + pets { |
| 56 | + name |
| 57 | + __typename |
| 58 | + ... on Dog { |
| 59 | + bark |
| 60 | + } |
| 61 | +
|
| 62 | + ... on Cat { |
| 63 | + meow |
| 64 | + } |
| 65 | +
|
| 66 | + ... on Bird { |
| 67 | + tweet |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +
|
| 72 | + ''', data) |
| 73 | + assert not result.errors |
| 74 | + assert result.data == { |
| 75 | + 'pets': [ |
| 76 | + {'__typename': 'Dog', 'bark': "Really big bark, because it's a really big dog.", 'name': 'Clifford'}, |
| 77 | + {'__typename': 'Cat', 'meow': 'Lasagna', 'name': 'Garfield'}, |
| 78 | + {'__typename': 'Bird', 'tweet': '#yolo', 'name': 'Tweetie'}, |
| 79 | + {'__typename': 'Dog', 'bark': "Really big bark, because it's a really big dog.", 'name': 'OTClifford'}, |
| 80 | + {'__typename': 'Cat', 'meow': 'Lasagna', 'name': 'OTGarfield'}, |
| 81 | + {'__typename': 'Bird', 'tweet': '#yolo', 'name': 'OTTweetie'}, |
| 82 | + ] |
| 83 | + } |
0 commit comments