Пример #1
0
def test_search_book_by_id():
    library = []
    war_and_piece = create_book('Война и мир', 'Толстой', 1000, True,
                                'война, любовь, толстой')

    anna_karenina = create_book('Анна Каренина', 'Толстой', 500, False,
                                'поезд, любовь, толстой')

    library = add_book(library, war_and_piece)
    library = add_book(library, anna_karenina)

    expected = anna_karenina

    result = search_book_by_id(library, anna_karenina['id'])

    assert result == expected
Пример #2
0
def test_add_one_book_to_empty_library():
    library = []
    book = create_book('Война и мир', 'Толстой', 1000, True,
                       'война, любовь, толстой')

    library = add_book(library, book)

    assert len(library) == 1
    assert book in library
Пример #3
0
def test_search_books_by_tag():
    library = []
    war_and_piece = create_book('Война и мир', 'Толстой', 1000, True,
                                'война, любовь, толстой')

    anna_karenina = create_book('Анна Каренина', 'Толстой', 500, False,
                                'поезд, любовь, толстой')

    pushkin = create_book('Собрание сочинений', 'Пушкин', 500, True,
                          'пушкин, сказки, стихи')

    library = add_book(library, war_and_piece)
    library = add_book(library, anna_karenina)
    library = add_book(library, pushkin)

    expected = [war_and_piece, anna_karenina]

    result = search_books(library, '#любовь')

    assert result == expected
Пример #4
0
def test_remove_book_by_id():
    library = []
    war_and_piece = create_book('Война и мир', 'Толстой', 1000, True,
                                'война, любовь, толстой')

    anna_karenina = create_book('Анна Каренина', 'Толстой', 500, False,
                                'поезд, любовь, толстой')

    pushkin = create_book('Собрание сочинений', 'Пушкин', 500, True,
                          'пушкин, сказки, стихи')

    library = add_book(library, war_and_piece)
    library = add_book(library, anna_karenina)
    library = add_book(library, pushkin)

    expected = library[:]
    del expected[1]

    result = remove_book_by_id(library, anna_karenina['id'])

    assert result == expected
Пример #5
0
def test_list_books():
    library = []
    war_and_piece = create_book('Война и мир', 'Толстой', 1000, True,
                                'война, любовь, толстой')

    anna_karenina = create_book('Анна Каренина', 'Толстой', 500, False,
                                'поезд, любовь, толстой')

    pushkin = create_book('Собрание сочинений', 'Пушкин', 500, True,
                          'пушкин, сказки, стихи')

    library = add_book(library, war_and_piece)
    library = add_book(library, anna_karenina)
    library = add_book(library, pushkin)

    expected_on_first_page = [war_and_piece, anna_karenina]
    expected_on_second_page = [pushkin]

    first_page_result = list_books(library, 1, 2)
    second_page_result = list_books(library, 2, 2)

    assert first_page_result == expected_on_first_page
    assert second_page_result == expected_on_second_page
Пример #6
0
    def book_save(book_id):
        nonlocal library

        title = request.form['title']
        author = request.form['author']
        price = request.form['price']
        available = bool(int(request.form['available']))
        tags = request.form['tags']

        empty_id = str(generate_empty_id())
        if book_id == empty_id:
            book = create_book(title=title, author=author, price=price, available=available, tags_str=tags)
            library = add_book(library, book)
        else:
            book = search_book_by_id(library, book_id)
            update_book(book, title=title, author=author, price=price, available=available, tags_str=tags)

        return redirect(url_for('book_details', book_id=book['id']))
Пример #7
0
def start():
    app = Flask(__name__)

    library = []

    war_and_piece = create_book(
        'Война и мир',
        'Толстой',
        1000,
        True,
        'война, любовь, толстой'
    )

    anna_karenina = create_book(
        'Анна Каренина',
        'Толстой',
        500,
        False,
        'поезд, любовь, толстой'
    )

    pushkin = create_book(
        'Собрание сочинений',
        'Пушкин',
        500,
        True,
        'пушкин, сказки, стихи'
    )

    library = add_book(library, war_and_piece)
    library = add_book(library, anna_karenina)
    library = add_book(library, pushkin)

    @app.route('/')
    def index():
        empty_id = generate_empty_id()
        search = request.args.get('search')
        if search:
            results = search_books(library, search)
            return render_template('index.html', books=results, search=search, empty_id=empty_id)

        return render_template('index.html', books=library, empty_id=empty_id)

    @app.route('/books/<book_id>')
    def book_details(book_id):
        result = search_book_by_id(library, book_id)
        return render_template('book-details.html', book=result)

    @app.route('/books/<book_id>/edit')
    def book_edit(book_id):
        book = None
        empty_id = str(generate_empty_id())
        if book_id == empty_id:
            book = create_empty_book()
        else:
            book = search_book_by_id(library, book_id)
        return render_template('book-edit.html', book=book)

    @app.route('/books/<book_id>/save', methods=['POST'])
    def book_save(book_id):
        nonlocal library

        title = request.form['title']
        author = request.form['author']
        price = request.form['price']
        available = bool(int(request.form['available']))
        tags = request.form['tags']

        empty_id = str(generate_empty_id())
        if book_id == empty_id:
            book = create_book(title=title, author=author, price=price, available=available, tags_str=tags)
            library = add_book(library, book)
        else:
            book = search_book_by_id(library, book_id)
            update_book(book, title=title, author=author, price=price, available=available, tags_str=tags)

        return redirect(url_for('book_details', book_id=book['id']))

    @app.route('/books/<book_id>/remove', methods=['POST'])
    def book_remove(book_id):
        nonlocal library
        library = remove_book_by_id(library, book_id)
        return redirect(url_for('index'))

    if os.getenv('APP_ENV') == 'PROD' and os.getenv('PORT'):
        waitress.serve(app, port=os.getenv('PORT'))
    else:
        app.run(port=9876, debug=True)