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_user_not_added_to_database(db_session): """Test that a Book cannot be added without required fields.""" book = Book( title=FAKE.sentence(nb_words=3), 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_data_without_values_sets_values_to_none( testapp, one_user): """Test that POST to book-list route sets missing values to None.""" data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), } res = testapp.post('/books', data) assert res.json['author'] is None assert res.json['isbn'] is None assert res.json['pub_date'] is None
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_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_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_creates_new_book_with_none_values(dummy_request, db_session, one_user): """Test that create sets values to None when not given.""" db_session.add(one_user) data = { 'email': one_user.email, 'password': '******', 'title': FAKE.sentence(nb_words=3), } dummy_request.POST = data res = _create_book(dummy_request, one_user) assert res['author'] is None assert res['isbn'] is None assert res['pub_date'] is 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_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_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]