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_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_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 create_test_post(): post = Post() post.title = "New post" post.date_posted = "2020-02-08" user = create_test_user() post.author = user post.save() return post, user
def test_create_vote_user_mismatch(self): """ensure logged user must match supplied userId""" user2 = create_test_user(name='Another User', password='******', email='*****@*****.**') user2_gid = Node.to_global_id('User', user2.pk) result = self.schema.execute( self.query, variable_values=self.variables(self.link_gid, user2_gid), context_value=self.context_with_token() ) self.assertIsNotNone(result.errors, msg='createVote should have failed: userId and logged user mismatch') self.assertIn('user id does not match logged-in user', repr(result.errors)) expected = { 'createVote': None } self.assertEqual(result.data, expected, msg='\n'+repr(expected)+'\n'+repr(result.data))
def setUp(self): self.user = create_test_user() self.user_gid = Node.to_global_id('User', self.user.pk) self.query = ''' mutation CreateLinkMutation($input: CreateLinkInput!) { createLink(input: $input) { link { url description postedBy { id } } clientMutationId } } ''' self.schema = graphene.Schema(query=Query, mutation=Mutation)
def setUp(self): create_Link_orderBy_test_data() self.link_gid = Node.to_global_id('Link', LinkModel.objects.latest('created_at').pk) self.user = create_test_user() self.user_gid = Node.to_global_id('User', self.user.pk) self.query = ''' mutation CreateVoteMutation($input: CreateVoteInput!) { createVote(input: $input) { vote { link { id votes { count } } } clientMutationId } } ''' self.schema = graphene.Schema(query=Query, mutation=Mutation)