def setUp(self): super().setUp() self.session.add( models.Book(title="This", author="Author", suggestion=False)) self.session.add( models.Book(title="Thi", author="Author", suggestion=False)) self.session.add( models.Book(title="The", author="Author", suggestion=False)) self.session.commit()
def test_books_below_limit(self): for n in range(queries.SEARCH_LIMIT * 2): self.session.add( models.Book(title=f"The {n} book", suggestion=False)) books = (self.graphql_request( self.BOOKS_QUERY % "The", headers=self.get_headers_with_auth()).get("data", {}).get( "booksByTitle", [])) self.assertEqual(len(books), queries.SEARCH_LIMIT)
def testBooksReturnNoSuggestion(self): self.session.add( models.Book(id=101, title="thi", author="", suggestion=True)) self.session.commit() books = (self.graphql_request( self.BOOKS_QUERY % "thi", headers=self.get_headers_with_auth()).get("data", {}).get( "booksByTitle", [])) # It would be 3 if the previous added Book was not a suggestion self.assertEqual(len(books), 2)
def mutate(root, info, title, author, cover_url=None, release_date=None): _ = utils.validate_user_id(request, info.context["secret"]) new_book = models.Book( title=title, author=author, release_date=release_date, cover_url=cover_url, suggestion=True, ) session = info.context["session"] session.add(new_book) session.commit() return CreateBookSuggestion(book=new_book)
def test_books_filter_reviewed(self): user_id = 256 book_id = 512 self.session.add( models.Book(id=book_id, title="This is", author="Another author", suggestion=False)) self.session.add( models.Review(user_id=user_id, book_id=book_id, value="GOOD")) self.session.commit() books = (self.graphql_request( self.BOOKS_QUERY % "thi", headers=self.get_headers_with_auth(user_id=user_id)).get( "data", {}).get("booksByTitle", [])) self.assertEqual(len(books), 2)
def test_delete_review(self): # Setup review to delete user_id = 128 book_id = 512 self.session.add( models.Book(id=book_id, title="Book title", author="Another author")) self.session.add( models.Review( user_id=user_id, book_id=book_id, value="GREAT", created=datetime.strptime("2020-01-01", "%Y-%m-%d"), )) self.session.commit() # Check review exists query = "{ reviews(year: 2020) { items { comment } } }" resp_data = self.graphql_request(query, headers=self.get_headers_with_auth( user_id=user_id)).get("data", {}) reviews = resp_data.get("reviews", {}).get("items", []) self.assertEqual(len(reviews), 1) # Delete review variables = {"book_id": book_id} self.graphql_request( self.delete_mutation, variables, headers=self.get_headers_with_auth(user_id=user_id)) # Check review no longer exists query = "{ reviews(year: 2020) { items { comment } } }" resp_data = self.graphql_request(query, headers=self.get_headers_with_auth( user_id=user_id)).get("data", {}) reviews = resp_data.get("reviews", {}).get("items", []) self.assertFalse(reviews)