def testQuery_repeatedKeyProperty(self): tk1 = Tag(name="t1").put() tk2 = Tag(name="t2").put() tk3 = Tag(name="t3").put() tk4 = Tag(name="t4").put() Article(headline="h1", summary="s1", tags=[tk1, tk2, tk3, tk4]).put() result = schema.execute(''' query ArticleWithAuthorID { articles { headline authorId tagIds tags { name } } } ''') self.assertEmpty(result.errors) article = dict(result.data['articles'][0]) self.assertListEqual( map(lambda k: to_global_id('TagType', k.urlsafe()), [tk1, tk2, tk3, tk4]), article['tagIds']) self.assertLength(article['tags'], 4) for i in range(0, 3): self.assertEqual(article['tags'][i]['name'], 't%s' % (i + 1))
def test_m2m_add(self): p = Post.get(id=1) t1 = Tag.get(id=1) t2 = Tag.get(id=2) p.tags.add(t1) p.tags.add(t2) self.assertEqual([p.id for p in p.tags.all()], [t1.id, t2.id]) self.assertEqual([p.id for p in t1.posts.all()], [p.id]) self.assertEqual([p.id for p in t2.posts.all()], [p.id])
def test_keyProperty(self): Article( headline="Test1", summary="1", author_key=Author(name="John Dow", email="*****@*****.**").put(), tags=[ Tag(name="tag1").put(), Tag(name="tag2").put(), Tag(name="tag3").put(), ] ).put() result = schema.execute(""" query Articles { articles(first:2) { edges { cursor, node { headline, summary, author { name }, tags { name } } } } } """) self.assertEmpty(result.errors, msg=str(result.errors)) articles = result.data.get('articles', {}).get('edges', []) self.assertLength(articles, 1) article = articles[0]['node'] self.assertEqual(article['headline'], 'Test1') self.assertEqual(article['summary'], '1') author = article['author'] self.assertLength(author.keys(), 1) self.assertEqual(author['name'], 'John Dow') tags = article['tags'] tag_names = [t['name'] for t in tags] self.assertListEqual(tag_names, ['tag1', 'tag2', 'tag3'])
def test_m2m_remove(self): p = Post.get(id=5) self.assertEqual(p.tags.all(), []) t = Tag.get(id=5) p.tags.add(t) self.assertEqual([t.id for t in p.tags.all()], [t.id]) self.assertEqual([p.id for p in t.posts.all()], [p.id]) p.tags.remove(t) self.assertEqual(p.tags.all(), []) self.assertEqual(t.posts.all(), [])
def test_create_page(self): tag_1 = Tag(title='Tag 1', slug="tag-1") tag_1.save(request=None) created_page = Page(title="Example", slug="example") created_page.save(request=None) created_page.tags.add(tag_1.document) tag_2 = Tag(title='Tag 2', slug="tag-2") tag_2.save(request=None) updated_page = Page.objects.get(slug=created_page.slug) updated_page.title = "Title updated" updated_page.save(request=None) updated_page.tags.clear() updated_page.tags.add(tag_1.document) updated_page.tags.add(tag_2.document) # must have 2 page revisions: page_revisions = created_page.revisions.all().order_by( 'revision__created_at') self.assertEqual(2, page_revisions.count()) # just 1 page on the database self.assertEqual(1, Page.objects.all().count()) # updated page must be the tip page retrived_page = Page.objects.get(slug=created_page.slug) self.assertEqual(retrived_page, updated_page) # compare m2m relationships self.assertEqual(1, created_page.tags.count()) self.assertEqual(created_page.tags.first().get_object(), tag_1) self.assertEqual(2, updated_page.tags.count()) self.assertEqual(updated_page.tags.first().get_object(), tag_1) self.assertEqual(updated_page.tags.last().get_object(), tag_2) self.assertEqual(1, tag_1.pages.count()) self.assertEqual(tag_1.pages.first(), updated_page) # compare db versions with local variable versions self.assertEqual([created_page, updated_page], list(page_revisions)) self.assertEqual(updated_page.document.revision_created_id, created_page.revision_id) self.assertEqual(updated_page.document.revision_tip_id, updated_page.revision_id) # revert to previous version self._do_login() response = self.graphql({ 'query': ''' mutation M($input_0: RevisionRevertInput!) { revisionRevert(input: $input_0) { clientMutationId errors { code } } } ''', 'variables': { 'input_0': { 'clientMutationId': '11', 'id': created_page.revision_id, } } }) self.assertEqual(response.json(), { 'data': { 'revisionRevert': { 'clientMutationId': '11', 'errors': None }, } }) reverted_page = Page.objects.get(slug=created_page.slug) self.assertEqual(reverted_page, created_page) # delete page reverted_page.delete(request=None) self.assertEqual(0, Page.objects.all().count()) self.assertEqual(0, tag_1.pages.count())
def test_m2m_count(self): p = Post.get(id=3) self.assertEqual(p.tags.count(), 0) p.tags.add(Tag.get(id=3)) p.tags.add(Tag.get(id=4)) self.assertEqual(p.tags.count(), 2)