Ejemplo n.º 1
0
    def test_search(self):
        with self.app.test_request_context():
            # init
            db.session.add_all([
                                Book(title=u'bAzInGus'),
                                Book(title=u'Туда и обратно'),
                                Book(title=u'туда и назад'),
                                Book(title=u'Война и мир')
                                ])

            db.session.add_all([
                                Author(name=u'Толстой Лев Николаевич'),
                                Author(name=u'Горький'),
                                Author(name=u'голохвостов'),
                                Author(name=u'Тудашкин')
                                ])
            db.session.commit()
            book1 = Book.query.filter_by(title=u'bAzInGus').first()
            book1.authors.append(Author.query.filter_by(name=u'Толстой Лев Николаевич').first())
            book1.authors.append(Author.query.filter_by(name=u'Горький').first())
            book1.authors.append(Author.query.filter_by(name=u'голохвостов').first())
            book1.authors.append(Author.query.filter_by(name=u'Тудашкин').first())

            db.session.commit()

            book2 = Book.query.filter_by(title=u'Война и мир').first()
            book2.authors.append(Author.query.filter_by(name=u'Толстой Лев Николаевич').first())

            # test search
            f = SearchForm()
            f.search_input.data = u'Война и мир'
            assert f.find_books().count() == 1

            f.search_input.data = u'война и мир'
            assert f.find_books().count() == 1

            f.search_input.data = u'Baz'
            assert f.find_books().count() == 1

            f.search_input.data = u'Туда'
            assert f.find_books().count() == 2

            f.search_input.data = u'Толстой'
            assert f.find_books().count() == 1

            f.search_input.data = u'Николаевич'
            assert f.find_books().count() == 1

            f.search_input.data = u'николаевич'
            assert f.find_books().count() == 1

            # test page
            resp = self.client.post(url_for('search'),
                                    data={'search_input': u'голо'})
            assert u'голо' in resp.data.decode('utf-8')
Ejemplo n.º 2
0
def search(page, per_page=app.config.get('PER_PAGE')):
    """
    search view
    actual search query is implemented in forms.SearchForm
    some external solution like whoosh would be an overhead in this case
    simple search looks for like %query_string% nothing more

    2.    Поиск книг по названию либо автору (c)
    """
    form = SearchForm(csrf_enabled=False)
    search_query = ''
    books = []
    pagination = []
    if form.validate_on_submit():
        search_query = form.search_input.data
        books = form.find_books()
        pagination = books.paginate(page, per_page)
    return render_template('search_results.html',
                           search_query=search_query,
                           form=form,
                           pagination=pagination)