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]