def test_node_for_viewer(self): query = ''' query { viewer { id } } ''' schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) viewer_gid = result.data['viewer']['id'] query = ''' query { node(id: "%s") { id } } ''' % viewer_gid expected = { 'node': { 'id': viewer_gid, } } result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def test_all_links_ordered_by(self): create_Link_orderBy_test_data() # descending order of creation: b.com, c.com, a.com query = ''' query AllLinksTest { viewer { allLinks(orderBy: createdAt_DESC) { edges { node { url } } } } } ''' expected = { 'viewer': { 'allLinks': { 'edges': [ { 'node': { 'url': 'http://b.com' } }, { 'node': { 'url': 'http://c.com' } }, { 'node': { 'url': 'http://a.com' } }, ] } } } schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) assert result.data == expected, '\n'+repr(expected)+'\n'+repr(result.data) # ascending order on description: c.com, b.com, a.com query = ''' query AllLinksTest { viewer { allLinks(orderBy: description_ASC) { edges { node { url } } } } } ''' expected = { 'viewer': { 'allLinks': { 'edges': [ { 'node': { 'url': 'http://c.com' } }, { 'node': { 'url': 'http://b.com' } }, { 'node': { 'url': 'http://a.com' } }, ] } } } schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) assert result.data == expected, '\n'+repr(expected)+'\n'+repr(result.data)
def test_create_link_with_neither(self): """createLink with neither user auth token nor postedById""" result = self.schema.execute(self.query, variable_values=self.variables(None), context_value=self.context_without_token()) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) expected = self.expected(with_id=False) self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def test_create_link_with_user_both(self): """createLink with both user auth token and postedById""" result = self.schema.execute(self.query, variable_values=self.variables(self.user_gid), context_value=self.context_with_token()) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) expected = self.expected() self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def test_root_query(self): """Make sure the root query is 'Query'. This test is pretty redundant, given that every other query in this file will fail if this is not the case, but it's a nice simple example of testing query execution. """ query = ''' query RootQueryQuery { __schema { queryType { name # returns the type of the root query } } } ''' expected = { '__schema': { 'queryType': { 'name': 'Query' } } } schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) assert result.data == expected, '\n'+repr(expected)+'\n'+repr(result.data)
def test_node_for_vote(self): link = LinkModel.objects.create(description='Test', url='http://a.com') user = create_test_user() vote = VoteModel.objects.create(link_id=link.pk, user_id=user.pk) vote_gid = Node.to_global_id('Vote', vote.pk) query = ''' query { node(id: "%s") { id ...on Vote { link { url } } } } ''' % vote_gid expected = { 'node': { 'id': vote_gid, 'link': { 'url': 'http://a.com', } } } schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def test_signin_user(self): """normal user sign-in""" variables = { 'signinUserInput': { 'email': { 'email': self.user.email, 'password': self.user.password, }, 'clientMutationId': 'give_this_back_to_me', } } expected = { 'signinUser': { 'token': 'REDACTED', 'user': { 'name': self.user.name, }, 'clientMutationId': 'give_this_back_to_me', } } result = self.schema.execute(self.query, variable_values=variables) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) try: token = result.data['signinUser']['token'] result.data['signinUser']['token'] = 'REDACTED' except KeyError: raise Exception('malformed mutation result') self.assertRegex(token, r'^[0-9a-f]{40,}') self.assertEqual(result.data, expected, msg='\n' + repr(expected) + '\n' + repr(result.data))
def test_create_user_duplicate(self): """should not be able to create two users with the same email""" result = self.schema.execute(self.query, variable_values=self.variables) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, self.expected, msg='\n' + repr(self.expected) + '\n' + repr(result.data)) # now try to create a second one self.variables['createUserInput']['name'] = 'Just Spock to Humans' auth = self.variables['createUserInput']['authProvider']['email'] auth['password'] = '******' # -- email address stays the same result = self.schema.execute(self.query, variable_values=self.variables) self.assertIsNotNone( result.errors, msg='Creating user with duplicate email should have failed') self.assertIn('user with that email address already exists', repr(result.errors)) expected = {'createUser': None} # empty result self.assertEqual(result.data, expected, msg='\n' + repr(expected) + '\n' + repr(result.data))
def test_node_for_user(self): user = create_test_user() user_gid = Node.to_global_id('User', user.pk) query = ''' query { node(id: "%s") { id ...on User { name } } } ''' % user_gid expected = { 'node': { 'id': user_gid, 'name': user.name, } } schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n' + repr(expected) + '\n' + repr(result.data))
def test_votes_count_on_link_test(self): """test count field on votes field on Link type""" # first link will have one vote, last link will have two create_Link_orderBy_test_data() # creates more than one link user = create_test_user() first_link_id = None for link in LinkModel.objects.all(): vote = VoteModel.objects.create(link_id=link.pk, user_id=user.pk) # save these for below first_link_id = first_link_id or link.pk last_link_id = link.pk user2 = create_test_user(name='Another User', password='******', email='*****@*****.**') VoteModel.objects.create(link_id=last_link_id, user_id=user2.pk) # check vote counts first_link_gid = Node.to_global_id('Link', first_link_id) last_link_gid = Node.to_global_id('Link', last_link_id) query = ''' query VotesOnLinkTest($linkId: ID!) { node(id: $linkId) { ... on Link { votes { count } } } } ''' variables = { 'linkId': first_link_gid, } expected = { 'node': { 'votes': { 'count': 1, } } } schema = graphene.Schema(query=Query) result = schema.execute(query, variable_values=variables) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data)) variables['linkId'] = last_link_gid expected['node']['votes']['count'] = 2 result = schema.execute(query, variable_values=variables) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def test_ad_hoc_check_vote_query(self): """As of 11/4/2017, the tutorial contains an query done outside Relay, to check whether a vote already exists. (On the client side? Really? Ask forgiveness rather than permisson, and save a round trip.) The query is done using a private API function (relay.environment._network.fetch) that, sure enough, went away in recent versions of Relay. Test that that query works (for older Relay versions, and in the event the tutorial is fixed for newer versions.) """ create_Link_orderBy_test_data() # creates more than one link user = create_test_user() user_gid = Node.to_global_id('User', user.pk) user2 = create_test_user(name='Another User', password='******', email='*****@*****.**') # create multiple votes for link in LinkModel.objects.all(): last_vote = VoteModel.objects.create(link_id=link.pk, user_id=user.pk) VoteModel.objects.create(link_id=link.pk, user_id=user2.pk) last_link = link link_gid = Node.to_global_id('Link', last_link.pk) vote_gid = Node.to_global_id('Vote', last_vote.pk) # make sure the query only returns one vote query = ''' query CheckVoteQuery($userId: ID!, $linkId: ID!) { viewer { allVotes(filter: { user: { id: $userId }, link: { id: $linkId } }) { edges { node { id } } } } } ''' variables = { 'userId': user_gid, 'linkId': link_gid, } expected = { 'viewer': { 'allVotes': { 'edges': [ { 'node': { 'id': vote_gid, } } ] } } } schema = graphene.Schema(query=Query) result = schema.execute(query, variable_values=variables) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def test_create_user(self): """sucessfully create a user""" result = self.schema.execute(self.query, variable_values=self.variables) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, self.expected, msg='\n' + repr(self.expected) + '\n' + repr(result.data)) # check that the user was created properly user = UserModel.objects.get( name=result.data['createUser']['user']['name']) self.assertEqual(user.name, 'Jim Kirk') self.assertEqual(user.email, '*****@*****.**') self.assertEqual(user.password, 'abc123') self.assertRegex(user.token, r'^[0-9a-f]{40,}')
def test_create_vote(self): """test normal vote creation, and that duplicate votes are not allowed""" result = self.schema.execute( self.query, variable_values=self.variables(self.link_gid, self.user_gid), context_value=self.context_with_token() ) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) expected = self.expected() self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data)) # verify that a second vote can't be created result = self.schema.execute(self.query, variable_values=self.variables(self.link_gid, self.user_gid), context_value=self.context_with_token()) self.assertIsNotNone(result.errors, msg='createVote should have failed: duplicate votes not allowed') self.assertIn('vote already exists', repr(result.errors)) expected['createVote'] = None self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def test_create_link(self): """Test link creation without user information (for early in the tutorial).""" query = ''' mutation CreateLinkMutation($input: CreateLinkInput!) { createLink(input: $input) { link { url description } clientMutationId } } ''' variables = { 'input': { 'description': 'Description', 'url': 'http://example.com', 'clientMutationId': 'give_this_back_to_me', } } class Context(object): META = {} expected = { 'createLink': { 'link': { 'description': 'Description', 'url': 'http://example.com', }, 'clientMutationId': 'give_this_back_to_me', } } schema = graphene.Schema(query=Query, mutation=Mutation) result = schema.execute(query, variable_values=variables, context_value=Context) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data)) # check that the link was created properly link = LinkModel.objects.get(description='Description') self.assertEqual(link.description, 'Description') self.assertEqual(link.url, 'http://example.com')
def test_node_for_link(self): link = LinkModel.objects.create(description='Test', url='http://a.com') link_gid = Node.to_global_id('Link', link.pk) query = ''' query { node(id: "%s") { id ...on Link { url } } } ''' % link_gid expected = { 'node': { 'id': link_gid, 'url': 'http://a.com', } } schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def test_all_links(self): link = LinkModel(description='Description', url='http://') link.save() query = ''' query AllLinksTest { viewer { allLinks { edges { node { id description url } } } } } ''' expected = { 'viewer': { 'allLinks': { 'edges': [ { 'node': { 'id': 'TGluazox', 'description': 'Description', 'url': 'http://', } } ] } } } schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) assert result.data == expected, '\n'+repr(expected)+'\n'+repr(result.data)
def test_viewer_schema(self): """Check the Viewer type schema contains the fields we need.""" query = ''' query ViewerSchemaTest { __type(name: "Viewer") { name fields { name type { name kind ofType { name } } } } } ''' expected = { '__type': { 'name': 'Viewer', 'fields': [ { 'name': 'id', 'type': { 'name': None, 'kind': 'NON_NULL', 'ofType': { 'name': 'ID', } } }, { 'name': 'allLinks', 'type': { 'name': 'LinkConnection', 'kind': 'OBJECT', 'ofType': None, } }, { 'name': 'allVotes', 'type': { 'name': 'VoteConnection', 'kind': 'OBJECT', 'ofType': None, } }, ] } } schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) # Check that the fields we need are there, but don't fail on extra fields. NEEDED_FIELDS = ('id', 'allLinks', 'allVotes') result.data['__type']['fields'] = list(filter( lambda f: f['name'] in NEEDED_FIELDS, result.data['__type']['fields'] )) assert result.data == expected, '\n'+repr(expected)+'\n'+repr(result.data)
def test_all_links_pagination(self): """Make sure that pagination still works on the custom LinkConnection.""" create_Link_orderBy_test_data() # retrieve the first two links, in url order, plus a cursor for the next page query = ''' query AllLinksTest { viewer { allLinks(orderBy: url_ASC, first: 2) { edges { node { url } } pageInfo { endCursor } } } } ''' expected = { 'viewer': { 'allLinks': { 'edges': [ { 'node': { 'url': 'http://a.com' } }, { 'node': { 'url': 'http://b.com' } }, ], 'pageInfo': { 'endCursor': 'REDACTED', } } } } schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) # save cursor, and remove it from results (don't depend on cursor representation) cursor = result.data['viewer']['allLinks']['pageInfo']['endCursor'] result.data['viewer']['allLinks']['pageInfo']['endCursor'] = 'REDACTED' assert result.data == expected, '\n'+repr(expected)+'\n'+repr(result.data) # get next page of results query = (''' query AllLinksTest { viewer { allLinks(orderBy: url_ASC, first: 1, after: "''' + cursor + '''") { edges { node { url } } } } } ''') expected = { 'viewer': { 'allLinks': { 'edges': [ { 'node': { 'url': 'http://c.com' } }, ], } } } schema = graphene.Schema(query=Query) result = schema.execute(query) self.assertIsNone(result.errors, msg=format_graphql_errors(result.errors)) assert result.data == expected, '\n'+repr(expected)+'\n'+repr(result.data)