Esempio n. 1
0
def pick_at_random():
    categories = get_all_categories(BOOKS, ordered=True)
    template_get_file = 'random_picker_get.html'
    template_post_file = 'random_picker_post.html'

    if request.method == POST:
        desired_category = request.form['category']

        try:
            book = get_random_unread_book(BOOKS, desired_category)
        except TypeError:
            return render_template(template_get_file, categories=categories)
        else:
            if book:
                return render_template(
                    template_post_file,
                    title=book.title,
                    author=book.author,
                    tags=book.tags,
                    categories=categories,
                    desired_category=desired_category,
                    no_unread_books=False,
                )
            else:
                return render_template(template_post_file,
                                       no_unread_books=True)

    return render_template(template_get_file, categories=categories)
Esempio n. 2
0
    def test_return_none_if_no_unread_book(self):
        df = pd.DataFrame(
            data={
                'read': [1, 1],
                'title': ['title1', 'title2'],
                'author': ['author1', 'author2'],
                'tags': ['novel', 'bio, history']
            })

        actual = get_random_unread_book(df)
        assert actual is None
Esempio n. 3
0
    def test_return_none_if_no_unread_book_with_category(self):
        df = pd.DataFrame(
            data={
                'read': [0, 0, 1],
                'title': ['title1', 'title2', 'title3'],
                'author': ['author1', 'author2', 'author3'],
                'tags': ['novel', 'bio, history', 'sci-fi'],
            })

        actual = get_random_unread_book(df, 'sci-fi')
        assert actual is None
Esempio n. 4
0
    def test_return_one_book_at_random_with_category(self):
        df = pd.DataFrame(
            data={
                'read': [0, 0, 0],
                'title': ['title1', 'title2', 'title3'],
                'author': ['author1', 'author2', 'author3'],
                'tags': ['novel', 'bio', 'bio'],
            })

        actual = get_random_unread_book(df, 'bio')
        assert actual.title in ['title2', 'title3']
        assert actual.author in ['author2', 'author3']
        assert actual.tags == 'bio'
Esempio n. 5
0
    def test_replace_nan_by_empty_string(self):
        df = pd.DataFrame(
            data={
                'read': [1, 1, 0],
                'title': ['title1', 'title2', 'title3'],
                'author': ['author1', 'author2', np.nan],
                'tags': ['novel', 'sci-fi', np.nan],
            })

        random_book = get_random_unread_book(df)
        assert random_book.title == 'title3'
        assert random_book.author == ''
        assert random_book.tags == ''
Esempio n. 6
0
    def test_return_one_book_at_random_with_category_among_multiple_categories(
            self):
        df = pd.DataFrame(
            data={
                'read': [0, 0],
                'title': ['title1', 'title2'],
                'author': ['author1', 'author2'],
                'tags': ['novel', 'history, bio'],
            })

        actual = get_random_unread_book(df, 'bio')
        assert actual.title == 'title2'
        assert actual.author == 'author2'
        assert actual.tags == 'history, bio'
Esempio n. 7
0
    def test_return_one_book_at_random(self):
        unread_titles = ['title1', 'title2']
        read_titles = ['title3', 'title4']

        unread_authors = ['author1', 'author2']
        read_authors = ['author3', 'author4']

        unread_tags = ['bio', 'history, classic']
        read_tags = ['novel', 'sci-fi']

        df = pd.DataFrame(
            data={
                'read': [1, 1, 0, 0],
                'title': read_titles + unread_titles,
                'author': read_authors + unread_authors,
                'tags': read_tags + unread_tags,
            })

        random_book = get_random_unread_book(df)
        assert random_book.title in unread_titles
        assert random_book.author in unread_authors
        assert random_book.tags in unread_tags