Exemplo n.º 1
0
    def test_author_node_query__returns_model_fields(self):
        AuthorFactory.create(
            id=3,
            first_name='Buddy',
            last_name='Holly',
            twitter_account='@buddy',
        )
        query_string = self.build_query_with_fields(
            'id',
            'firstName',
            'lastName',
            'twitterAccount',
        )
        variables = {'id': to_global_id(AuthorType, 3)}

        result = self.schema.execute(query_string,
                                     context=self.request,
                                     variables=variables)

        self.assertEqual(result.errors, None)
        self.assertDictEqual(
            result.data['author'], {
                'id': to_global_id(AuthorType, 3),
                'firstName': 'Buddy',
                'lastName': 'Holly',
                'twitterAccount': '@buddy',
            })
Exemplo n.º 2
0
    def test_story_node_query__returns_model_fields(self):
        StoryFactory.create(
            id=2,
            title='Hello world',
            subtitle='Hello GraphQL',
            description='A big adventure',
            published_year=2019,
        )
        query_string = self.build_query_with_fields('id', 'title', 'subtitle',
                                                    'description',
                                                    'publishedYear')
        variables = {'id': to_global_id(StoryType, 2)}

        result = self.schema.execute(query_string,
                                     context=self.request,
                                     variables=variables)

        self.assertDictEqual(
            result.data['story'], {
                'id': to_global_id(StoryType, 2),
                'title': 'Hello world',
                'subtitle': 'Hello GraphQL',
                'description': 'A big adventure',
                'publishedYear': '2019',
            })
Exemplo n.º 3
0
    def test_character_node_query__returns_model_fields(self):
        CharacterFactory.create(id=7, name='Lil Bobby')
        query_string = self.build_query_with_fields('id', 'name')
        variables = {'id': to_global_id(CharacterType, 7)}

        result = self.schema.execute(query_string, context=self.request, variables=variables)

        self.assertEqual(result.errors, None)
        self.assertDictEqual(result.data['character'], {
            'id': to_global_id(CharacterType, 7),
            'name': 'Lil Bobby'
        })
Exemplo n.º 4
0
    def test_stories_query__returns_list_of_stories(self):
        StoryFactory.create(id=2)
        StoryFactory.create(id=5)
        query_string = self.build_query_with_fields('id')

        result = self.schema.execute(query_string, context=self.request)

        self.assertListEqual(connection_to_list(result.data['stories']), [
            {
                'id': to_global_id(StoryType, 2)
            },
            {
                'id': to_global_id(StoryType, 5)
            },
        ])
Exemplo n.º 5
0
    def test_story_node_query__returns_related_author(self):
        StoryFactory.create(id=2, author=AuthorFactory(id=5))
        query_string = self.build_query_with_fields(
            'id',
            'author { id }',
        )
        variables = {'id': to_global_id(StoryType, 2)}

        result = self.schema.execute(query_string,
                                     context=self.request,
                                     variables=variables)

        self.assertDictEqual(result.data['story']['author'], {
            'id': to_global_id(AuthorType, 5),
        })
Exemplo n.º 6
0
    def test_choice_node_query__returns_related_to_passage(self):
        passage = PassageFactory(id=2)
        ChoiceFactory.create(id=3, to_passage=passage)
        query_string = self.build_query_with_fields(
            'id',
            'toPassage { id }',
        )
        variables = {'id': to_global_id(ChoiceType, 3)}

        result = self.schema.execute(query_string,
                                     context=self.request,
                                     variables=variables)

        self.assertEqual(result.errors, None)
        self.assertDictEqual(result.data['choice']['toPassage'],
                             {'id': to_global_id(PassageType, 2)})
Exemplo n.º 7
0
    def test_character_node_query__returns_related_passages(self):
        character = CharacterFactory(id=6)
        PassageFactory.create(id=4, pov_character=character)
        PassageFactory.create(id=5, pov_character=character)
        query_string = self.build_query_with_fields(
            'id',
            'inPassages { edges { node { id } } }',
        )
        variables = {'id': to_global_id(CharacterType, 6)}

        result = self.schema.execute(query_string, context=self.request, variables=variables)

        self.assertEqual(result.errors, None)
        self.assertEqual(connection_to_list(result.data['character']['inPassages']), [
            {'id': to_global_id(PassageType, 4)},
            {'id': to_global_id(PassageType, 5)},
        ])
Exemplo n.º 8
0
    def test_character_node_query__returns_empty_field_when_id_does_not_exist(self):
        query_string = self.build_query_with_fields('id')
        variables = {'id': to_global_id(CharacterType, 1)}

        result = self.schema.execute(query_string, context=self.request, variables=variables)

        self.assertEqual(result.errors, None)
        self.assertDictEqual(result.data, {'character': None})
Exemplo n.º 9
0
    def test_to_global_id__can_be_decoded_from_default_name(self):
        class TypeWithGlobalIDWithoutName(graphene.ObjectType):
            class Meta:
                interfaces = (graphene.Node, )

        encoded = to_global_id(TypeWithGlobalIDWithoutName, 3)

        decoded = from_global_id(encoded)
        self.assertEqual(decoded, GlobalID('TypeWithGlobalIDWithoutName', 3))
Exemplo n.º 10
0
    def test_to_global_id__can_be_decoded_from_type_name(self):
        class TypeWithGlobalIDAndName(graphene.ObjectType):
            class Meta:
                interfaces = (graphene.Node, )
                name = 'WithGlobalID'

        encoded = to_global_id(TypeWithGlobalIDAndName, 2)

        decoded = from_global_id(encoded)
        self.assertEqual(decoded, GlobalID('WithGlobalID', 2))
Exemplo n.º 11
0
    def test_choice_node_query__returns_model_fields(self):
        ChoiceFactory.create(id=3,
                             description='Do the right thing',
                             is_main_story=True)
        query_string = self.build_query_with_fields(
            'id',
            'description',
            'isMainStory',
        )
        variables = {'id': to_global_id(ChoiceType, 3)}

        result = self.schema.execute(query_string,
                                     context=self.request,
                                     variables=variables)

        self.assertEqual(result.errors, None)
        self.assertDictEqual(
            result.data['choice'], {
                'id': to_global_id(ChoiceType, 3),
                'description': 'Do the right thing',
                'isMainStory': True,
            })
Exemplo n.º 12
0
    def test_story_node_query__returns_related_passages(self):
        story = StoryFactory.create(id=2)
        PassageFactory.create(id=10, story=story)
        PassageFactory.create(id=5, story=story)
        query_string = self.build_query_with_fields(
            'id',
            'passages { edges { node { id } } }',
        )
        variables = {'id': to_global_id(StoryType, 2)}

        result = self.schema.execute(query_string,
                                     context=self.request,
                                     variables=variables)

        self.assertListEqual(
            connection_to_list(result.data['story']['passages']), [
                {
                    'id': to_global_id(PassageType, 5)
                },
                {
                    'id': to_global_id(PassageType, 10)
                },
            ])
Exemplo n.º 13
0
    def test_author_node_query__returns_related_stories(self):
        author = AuthorFactory(id=5)
        StoryFactory.create(id=2, author=author)
        StoryFactory.create(id=4, author=author)
        query_string = self.build_query_with_fields(
            'id',
            'stories { edges { node { id } } }',
        )
        variables = {'id': to_global_id(AuthorType, 5)}

        result = self.schema.execute(query_string,
                                     context=self.request,
                                     variables=variables)

        self.assertEqual(result.errors, None)
        self.assertEqual(connection_to_list(result.data['author']['stories']),
                         [
                             {
                                 'id': to_global_id(StoryType, 2)
                             },
                             {
                                 'id': to_global_id(StoryType, 4)
                             },
                         ])
Exemplo n.º 14
0
    def test_author_full_name_field__without_required_parameter(self):
        AuthorFactory.create(
            id=3,
            first_name='Buddy',
            last_name='Holly',
        )
        query_string = self.build_query_with_fields(
            'id',
            'firstName',
            'fullName',
        )
        variables = {'id': to_global_id(AuthorType, 3)}

        result = self.schema.execute(query_string,
                                     context=self.request,
                                     variables=variables)
        first_error = result.errors[0]

        expected_error = GraphQLError(
            'Field "fullName" argument "display" of type "AuthorDisplayNameEnum!" is required but not provided.'
        )
        self.assertEqual(first_error.message, expected_error.message)
        self.assertIsNone(result.data)