def api_search():

    if request.is_json:
        content = request.get_json()
        if 'query' in content:
            query = content['query'].strip()
            books = search_book(query)
            if books:
                l = []
                books_json = {}
                for book in books:
                    d = {}
                    d['isbn'] = book.isbn
                    d['title'] = book.title
                    d['author'] = book.author
                    d['year'] = int(book.year)
                    l.append(d)
                books_json['books'] = l
                return (jsonify(books_json), 200) 
            else:
                return (jsonify({'Error': "No book was found."}), 404) 
        else:
            return (jsonify({'Error': "Invalid JSON data"}), 400)
    else:
        return (jsonify({"Error": "Takes only json as input"}), 422)    
Beispiel #2
0
 def setUp(self):
     self.file_to_persist = 'test_file_persist'
     books = search_book('python')
     assert books
     self.book_id_to_add = books[0]['id']
     add_book_to_shelf(self.book_id_to_add, books)
     self.assertEqual(1, len(bookshelf))
     persist_shelf_to_disk(self.file_to_persist)
     self.assertTrue(os.path.isfile(self.file_to_persist))
     self.assertGreater(os.path.getsize(self.file_to_persist), 0)
Beispiel #3
0
 def setUp(self):
     """
     Adds some books to bookshelf.
     """
     self.file_to_persist = 'test_file_persist'
     books = search_book('python')
     assert books
     book_id_to_add = books[0]['id']
     add_book_to_shelf(book_id_to_add, books)
     self.assertEqual(1, len(bookshelf))
def search():
    if not request.args.get("book"):
        return render_template("error.html", 
        message="Please provide details of a book.",
        prev_page = "index")

    books = search_book(request.args.get("book"))
    

    if len(books) == 0:
        return render_template("error.html", message="We can't find any books.",
         prev_page = "index")
    
    return render_template("search.html", books=books)
Beispiel #5
0
 def test_invalid_string(self):
     """Test Case 5 checking the validity of search function
        using an empty string"""
     self.assertEqual(search_book("tulasi"), [])
Beispiel #6
0
 def test_invalid_isbn(self):
     """Test Case 4 checking the validity of search function
        using a invalid ISBN."""
     self.assertEqual(search_book("99597333117"), [])
Beispiel #7
0
 def test_valid_author(self):
     """""Test Case 3 checking the validity of search function
         using a string."""
     book_data = search_book("beth")
     self.assertEqual(book_data[0].isbn, "0312364687")
Beispiel #8
0
 def test_valid_title(self):
     """""Test Case 2 checking the validity of search function
         using a string."""
     book_data = search_book("the beTRAy")
     self.assertEqual(book_data[0].title, "Krondor: The Betrayal")
Beispiel #9
0
 def test_valid_isbn(self):
     """Test Case 1 checking the validity of search function
         using a valid ISBN."""
     book_data = search_book("0380795272")
     self.assertEqual(book_data[0].title, "Krondor: The Betrayal")