def test_book_list_post_missing_auth_gets_400_status_code(testapp): """Test that POST to book-list route gets 400 status code for missing auth.""" data = { 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } res = testapp.post('/books', data, status=400) assert res.status_code == 400
def test_incomplete_book_no_title_not_added_to_database(db_session, one_user): """Test that a Book cannot be added without required fields.""" book = Book( user=one_user, author=FAKE.name(), isbn=FAKE.isbn13(separator="-"), pub_date=FAKE.date_object() ) db_session.add(book) with pytest.raises(IntegrityError): db_session.flush()
def test_book_list_post_complete_data_gets_201_status_code(testapp, one_user): """Test that POST to book-list route gets 201 status code.""" data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } res = testapp.post('/books', data) assert res.status_code == 201
def test_book_list_post_incorrect_date_gets_400_status_code(testapp, one_user): """Test that POST to book-list route gets 400 status code for bad data.""" data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%Y-%m-%d') } res = testapp.post('/books', data, status=400) assert res.status_code == 400
def test_book_list_post_incomplete_data_gets_400_status_code( testapp, one_user): """Test that POST to book-list route gets 400 status code for missing data.""" data = { 'email': one_user.email, 'password': '******', 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } res = testapp.post('/books', data, status=400) assert res.status_code == 400
def test_book_id_put_correct_auth_has_200_response_code( testapp, testapp_session, one_user): """Test that PUT to book-id route gets 200 status code for good auth.""" book = testapp_session.query(User).get(one_user.id).books[0] data = { 'email': one_user.email, 'password': '******', 'author': FAKE.name() } res = testapp.put('/books/{}'.format(book.id), data) assert res.status_code == 200
def test_book_list_post_sets_email_user_as_book_owner(testapp, testapp_session, one_user): """Test that POST to book-list route sets user with email as book owner.""" data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } res = testapp.post('/books', data) new_book = testapp_session.query(Book).get(res.json['id']) assert new_book.user.email == one_user.email
def test_book_list_post_complete_data_adds_book_to_database( testapp, testapp_session, one_user): """Test that POST to book-list route creates a new Book.""" num_books = len(testapp_session.query(Book).all()) data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } testapp.post('/books', data) assert len(testapp_session.query(Book).all()) == num_books + 1
def test_book_id_put_correct_auth_updates_book_in_database( testapp, testapp_session, one_user): """Test that PUT to book-id route updates the correct book in the database.""" book = testapp_session.query(User).get(one_user.id).books[0] data = { 'email': one_user.email, 'password': '******', 'author': FAKE.name() } testapp.put('/books/{}'.format(book.id), data) updated_book = testapp.get('/books/{}'.format(book.id), data) assert updated_book.json['author'] == data['author']
def test_create_raises_error_for_incomplete_post_data(dummy_request, db_session, one_user): """Test that create raises HTTPBadRequest for missing title.""" db_session.add(one_user) data = { 'email': one_user.email, 'password': '******', 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } dummy_request.POST = data with pytest.raises(HTTPBadRequest): _create_book(dummy_request, one_user)
def test_book_list_post_complete_data_returns_json_with_new_book_info( testapp, one_user): """Test that POST to book-list route gets JSON with details for new Book.""" data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } res = testapp.post('/books', data) for prop in ['title', 'author', 'isbn', 'pub_date']: assert res.json[prop] == data[prop] assert res.json['id'] is not None
def test_create_raises_error_for_bad_date_format(dummy_request, db_session, one_user): """Test that create raises HTTPBadRequest for incorrect date format.""" db_session.add(one_user) data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%Y-%m-%d') } dummy_request.POST = data with pytest.raises(HTTPBadRequest): _create_book(dummy_request, one_user)
def test_create_adds_new_book_to_the_database(dummy_request, db_session, one_user): """Test that create adds a new Book to the database.""" db_session.add(one_user) assert len(db_session.query(Book).all()) == 0 data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } dummy_request.POST = data _create_book(dummy_request, one_user) assert len(db_session.query(Book).all()) == 1
def test_update_changes_single_value_for_given_book_using_post_data( dummy_request, db_session, one_user): """Test that update changes the values on the given book from POST data.""" db_session.add(one_user) book = db_session.query(Book).first() new_author = FAKE.name() assert new_author != book.author data = { 'email': one_user.email, 'password': '******', 'author': new_author } dummy_request.POST = data _update_book(dummy_request, book) assert book.author == new_author
def test_create_sets_email_user_as_owner_of_new_book(dummy_request, db_session, one_user): """Test that create uses email from POST data to set Book owner.""" db_session.add(one_user) data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } dummy_request.POST = data res = _create_book(dummy_request, one_user) new_book = db_session.query(Book).get(res['id']) assert one_user is new_book.user
def test_create_returns_dict_with_new_book_data(dummy_request, db_session, one_user): """Test that create returns dict with the new Book's data.""" db_session.add(one_user) data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } dummy_request.POST = data res = _create_book(dummy_request, one_user) assert isinstance(res, dict) assert all(prop in res for prop in ['id', 'title', 'author', 'isbn', 'pub_date'])
def test_book_id_put_correct_auth_returns_json_with_updated_book_info( testapp, testapp_session, one_user): """Test that PUT to book-id route returns JSON with updated book data.""" book = testapp_session.query(User).get(one_user.id).books[0] data = { 'email': one_user.email, 'password': '******', 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } res = testapp.put('/books/{}'.format(book.id), data) for prop in ['id', 'title', 'author', 'isbn', 'pub_date']: if prop in data: assert res.json[prop] == data[prop] else: assert res.json[prop] == getattr(book, prop)
def test_create_creates_new_book_using_post_data(dummy_request, db_session, one_user): """Test that create uses POST data to create the new Book.""" db_session.add(one_user) data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } dummy_request.POST = data res = _create_book(dummy_request, one_user) new_book = db_session.query(Book).get(res['id']) for prop in ['title', 'author', 'isbn']: assert getattr(new_book, prop) == data[prop] assert new_book.pub_date.strftime('%m/%d/%Y') == data['pub_date']
def test_update_changes_all_values_for_given_book_using_post_data( dummy_request, db_session, one_user): """Test that update changes the values on the given book from POST data.""" db_session.add(one_user) book = db_session.query(Book).first() data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), 'author': FAKE.name(), 'isbn': FAKE.isbn13(separator="-"), 'pub_date': FAKE.date(pattern='%m/%d/%Y') } for prop in ['title', 'author', 'isbn', 'pub_date']: assert getattr(book, prop) != data[prop] dummy_request.POST = data _update_book(dummy_request, book) for prop in ['title', 'author', 'isbn', 'pub_date']: assert getattr(book, prop) == data[prop]