def test_delete_one(self): author = Author( name='foo', rating=42 ) author.save() result = self.query(''' mutation { delete_author (id: ''' + str(author.id) + ''') { affected { id } } } ''') self.assertIsNone(result.errors) self.assertEqual( result.data, { 'delete_author': { 'affected': { 'id': author.id } } } )
def test_update_one(self): author = Author(name='foo', rating=42) author.save() new_name = 'bar' result = self.query(''' mutation { update_author ( id: ''' + str(author.id) + ''', name: "''' + new_name + '''" ) { affected { id name rating } } } ''') self.assertIsNone(result.errors) self.assertEqual( result.data, { 'update_author': { 'affected': { 'id': author.id, 'name': new_name, 'rating': author.rating } } })
def test_query_empty(self): author = Author( name='foo', rating=42 ) author.save() book = Book( name='bar', year=2000, author=author ) book.save() result = self.query(''' query { book { id } } ''') self.assertIsNone(result.errors) self.assertEqual( result.data, { 'book': { 'id': book.id, } } )
def test_update_one__related(self): author = Author(name='foo', rating=42) author.save() new_name = 'bar' new_book_name = 'Test' new_book_year = 2000 result = self.query(''' mutation { update_author ( id: ''' + str(author.id) + ''', name: "''' + new_name + '''", book_set: [{ name: "''' + new_book_name + '''", year: ''' + str(new_book_year) + ''' }] ) { affected { id name rating book_set { edges { node { id name year } } } } } } ''') self.assertIsNone(result.errors) self.assertIsInstance( result.data['update_author']['affected']['book_set']['edges'][0] ['node']['id'], int) self.assertEqual( result.data, { 'update_author': { 'affected': { 'id': author.id, 'name': new_name, 'rating': author.rating, 'book_set': { 'edges': [{ 'node': { 'id': ANY, 'name': new_book_name, 'year': new_book_year } }] } } } })
def test_query_many_nested_foreign_key_relation(self): author = Author( name='foo', rating=42 ) author.save() book = Book( name='bar', year=2000, author=author ) book.save() page = Page( chapter=4, number=3, book=book ) page.save() result = self.query(''' query { pages { edges { node { book { author { name } } } } } } ''') self.assertIsNone(result.errors) self.assertEqual( result.data, { 'pages': { 'edges': [ { 'node': { 'book': { 'author': { 'name': 'foo' } } } } ] } } )
def test_update_many(self): author1 = Author(name='foo', rating=42) author1.save() author2 = Author(name='bar', rating=9000) author2.save() new_rating = 10 result = self.query(''' mutation { update_authors ( filters: {id__in: ''' + json.dumps([author1.id, author2.id]) + '''}, data: {rating: ''' + str(new_rating) + '''} ) { affected { edges { node { id name rating } } } } } ''') self.assertIsNone(result.errors) self.assertEqual( result.data, { 'update_authors': { 'affected': { 'edges': [{ 'node': { 'id': author1.id, 'name': author1.name, 'rating': new_rating } }, { 'node': { 'id': author2.id, 'name': author2.name, 'rating': new_rating } }] } } })
def test_query_one(self): author = Author( name='foo', rating=42 ) author.save() book = Book( name='bar', year=2000, author=author ) book.save() result = self.query(''' query { book (id: ''' + str(book.id) + ''') { id name year author { id name rating } } } ''') self.assertIsNone(result.errors) self.assertEqual( result.data, { 'book': { 'id': book.id, 'name': book.name, 'year': book.year, 'author': { 'id': author.id, 'name': author.name, 'rating': author.rating } } } )
def test_update_many__deep_filter(self): author = Author(name='foo', rating=42) author.save() book = Book(name='bar', year=2000, author=author) book.save() new_year = 2001 # ^_^ result = self.query(''' mutation { update_books ( filters: {author__name: ''' + json.dumps([author.name]) + '''}, data: {year: ''' + str(new_year) + '''} ) { affected { edges { node { id name year } } } } } ''') self.assertIsNone(result.errors) self.assertEqual( result.data, { 'update_books': { 'affected': { 'edges': [{ 'node': { 'id': book.id, 'name': book.name, 'year': new_year } }] } } })
def test_clone_one(self): author = Author( name='foo', rating=42 ) author.save() new_name = 'bar' result = self.query(''' mutation { clone_author ( id: ''' + str(author.id) + ''', data: {name: "''' + new_name + '''"} ) { affected { id name rating } } } ''') self.assertIsNone(result.errors) new_id = result.data['clone_author']['affected']['id'] self.assertIsInstance(new_id, int) self.assertNotEqual(new_id, author.id) self.assertEqual( result.data, { 'clone_author': { 'affected': { 'id': ANY, 'name': new_name, 'rating': author.rating } } } )
def test_delete_many(self): author1 = Author( name='foo', rating=42 ) author1.save() author2 = Author( name='bar', rating=9000 ) author2.save() result = self.query(''' mutation { delete_authors ( filters: { id__in: ''' + json.dumps([author1.id, author2.id]) + ''' } ) { affected { total } } } ''') self.assertIsNone(result.errors) self.assertEqual( result.data, { 'delete_authors': { 'affected': { 'total': 2 } } } )
def test_clone_one__with_related(self): author = Author( name='foo', rating=42 ) author.save() book = Book( name='bar', year=2000, author=author ) book.save() new_name = 'bar' result = self.query(''' mutation { clone_author ( id: ''' + str(author.id) + ''', data: {name: "''' + new_name + '''"}, related: ["book_set"] ) { affected { id name rating book_set { edges { node { id name author_id } } } } } } ''') self.assertIsNone(result.errors) new_id = result.data['clone_author']['affected']['id'] new_book_id = result.data['clone_author']['affected']['book_set']['edges'][0]['node']['id'] self.assertIsInstance(new_id, int) self.assertIsInstance(new_book_id, int) self.assertNotEqual(new_id, author.id) self.assertNotEqual(new_book_id, book.id) self.assertEqual( result.data, { 'clone_author': { 'affected': { 'id': ANY, 'name': new_name, 'rating': author.rating, 'book_set': { 'edges': [{ 'node': { 'id': ANY, 'name': book.name, 'author_id': new_id, } }] } } } } )
def test_query_many(self): author = Author( name='foo', rating=42 ) author.save() book1 = Book( name='bar1', year=2002, author=author ) book1.save() book2 = Book( name='bar2', year=2001, author=author ) book2.save() book3 = Book( name='bar3', year=2003, author=author ) book3.save() result = self.query(''' query { books ( filters: { author__rating: ''' + str(author.rating) + ''' }, order_by: ["-author__name", "year"], page: 1, paginate_by: 2 ) { total count edges { node { id name year author { id name rating } } } } } ''') self.assertIsNone(result.errors) self.assertEqual( result.data, { 'books': { 'count': 2, 'total': 3, 'edges': [{ 'node': { 'id': book2.id, 'name': book2.name, 'year': book2.year, 'author': { 'id': author.id, 'name': author.name, 'rating': author.rating } } }, { 'node': { 'id': book1.id, 'name': book1.name, 'year': book1.year, 'author': { 'id': author.id, 'name': author.name, 'rating': author.rating } } }] } } )
def test_filter_subset_query(self): author = Author( name='foo', rating=42 ) author.save() author2 = Author( name='bar', rating=42 ) author2.save() book1 = Book( name='bar1', year=2001, author=author ) book1.save() book2 = Book( name='bar2', year=2002, author=author ) book2.save() book3 = Book( name='bar1', year=2002, author=author2 ) book3.save() result = self.query(''' query { authors { count total edges { node { id name book_set (filters: {name: "bar1"}) { count total edges { node { id name } } } } } } } ''') self.assertIsNone(result.errors) self.assertEqual( result.data, { 'authors': { 'count': 2, 'total': 2, 'edges': [ { 'node': { 'id': author.id, 'name': author.name, 'book_set': { 'count': 1, 'total': 2, 'edges': [{ 'node': { 'id': book1.id, 'name': book1.name } }] } } }, { 'node': { 'id': author2.id, 'name': author2.name, 'book_set': { 'count': 1, 'total': 1, 'edges': [{ 'node': { 'id': book3.id, 'name': book3.name } }] } } } ] } } )
def test_query_many_filter_on_foreign_key(self): self.maxDiff = None author = Author( name='foo', rating=42 ) author.save() book1 = Book( name='bar1', year=2002, author=author ) book1.save() book2 = Book( name='bar2', year=2001, author=author ) book2.save() book3 = Book( name='bar3', year=2003, author=author ) book3.save() result = self.query(''' query { books ( filters: { author_id: 1 }, ) { edges { node { id name year } } } } ''') self.assertIsNone(result.errors) self.assertEqual( result.data, { 'books': { 'edges': [{ 'node': { 'id': book1.id, 'name': book1.name, 'year': book1.year } }, { 'node': { 'id': book2.id, 'name': book2.name, 'year': book2.year } }, { 'node': { 'id': book3.id, 'name': book3.name, 'year': book3.year } }] } } )