示例#1
0
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
示例#2
0
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()
示例#3
0
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
示例#4
0
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
示例#5
0
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
示例#6
0
def test_book_id_put_correct_auth_does_not_add_book_to_database(
        testapp, testapp_session, one_user):
    """Test that PUT to book-id route does not create a new Book."""
    book = testapp_session.query(User).get(one_user.id).books[0]

    num_books = len(testapp_session.query(Book).all())
    data = {
        'email': one_user.email,
        'password': '******',
        'isbn': FAKE.isbn13(separator="-")
    }
    testapp.put('/books/{}'.format(book.id), data)
    assert len(testapp_session.query(Book).all()) == num_books
示例#7
0
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
示例#8
0
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
示例#9
0
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
示例#10
0
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)
示例#11
0
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)
示例#12
0
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
示例#13
0
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
示例#14
0
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'])
示例#15
0
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)
示例#16
0
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']
示例#17
0
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]