-
Notifications
You must be signed in to change notification settings - Fork 37
add tests for BooksCollector #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,89 +1,133 @@ | ||
| import pytest | ||
| from main import BooksCollector | ||
|
|
||
|
|
||
| class TestBooksCollector: | ||
|
|
||
| def test_initial_state(self): | ||
| collector = BooksCollector() | ||
| assert collector.get_books_genre() == {} and collector.get_list_of_favorites_books() == [] | ||
|
|
||
| def test_add_new_book_correct_add_book_successful_add(self): | ||
| def test_add_new_book_add_two_books(self): | ||
| collector = BooksCollector() | ||
| collector.add_new_book('Азбука') | ||
| assert collector.get_book_genre('Азбука') == '' | ||
| collector.add_new_book('Гордость и предубеждение и зомби') | ||
| collector.add_new_book('Что делать, если ваш кот хочет вас убить') | ||
| assert len(collector.get_books_genre()) == 2 | ||
|
|
||
| @pytest.mark.parametrize("name, expected_count", [ | ||
| ('Азбука', 1), | ||
| ('', 0), | ||
| ('Азбука' * 10, 0) | ||
| @pytest.mark.parametrize('name', [ | ||
| 'Книга', | ||
| 'Очень длинное название книги, которое должно быть больше сорока символов, чтобы проверить граничное значение' | ||
| ]) | ||
| def test_add_new_book_incorrect_add_book_unsuccessful_add(self, name, expected_count): | ||
| def test_add_new_book_name_length_validation(self, name): | ||
| collector = BooksCollector() | ||
| collector.add_new_book(name) | ||
| assert len(collector.get_books_genre()) == expected_count | ||
|
|
||
| def test_set_book_genre_correct_genre_success(self): | ||
| if len(name) <= 40 and len(name) > 0: | ||
| assert name in collector.get_books_genre() | ||
| else: | ||
| assert name not in collector.get_books_genre() | ||
|
|
||
| def test_add_new_book_duplicate_not_allowed(self): | ||
| collector = BooksCollector() | ||
| collector.add_new_book('Азбука') | ||
| collector.set_book_genre('Азбука', 'Ужасы') | ||
| assert collector.books_genre['Азбука'] == 'Ужасы' | ||
| collector.add_new_book('Дюна') | ||
| collector.add_new_book('Дюна') | ||
| assert len(collector.get_books_genre()) == 1 | ||
|
|
||
| def test_set_book_genre_incorrect_genre_unsuccess(self): | ||
| def test_new_book_has_no_genre(self): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Отлично: в каждом тесте создается собственный экземпляр BooksCollector, но можно лучше - перенести создание экземпляра в фикстуры в отдельный файл conftest, а не повторять это предусловие в каждом тесте |
||
| collector = BooksCollector() | ||
| collector.add_new_book('Азбука') | ||
| collector.set_book_genre('Азбука', 'FFFFFF') | ||
| assert collector.books_genre['Азбука'] == '' | ||
| collector.add_new_book('1984') | ||
| assert collector.get_book_genre('1984') == '' | ||
|
|
||
| def test_get_book_genre(self): | ||
| @pytest.mark.parametrize('genre', ['Фантастика', 'Ужасы', 'Детективы', 'Мультфильмы', 'Комедии']) | ||
| def test_set_book_genre_valid_genre(self, genre): | ||
| collector = BooksCollector() | ||
| collector.add_new_book('Азбука') | ||
| collector.set_book_genre('Азбука', 'Ужасы') | ||
| assert collector.get_book_genre('Азбука') == 'Ужасы' | ||
| collector.add_new_book('Война и мир') | ||
| collector.set_book_genre('Война и мир', genre) | ||
| assert collector.get_book_genre('Война и мир') == genre | ||
|
|
||
| def test_get_books_with_specific_genre(self): | ||
| def test_set_book_genre_invalid_genre(self): | ||
| collector = BooksCollector() | ||
| books = ['Азбука', 'Алгебра', 'Маленький принц'] | ||
| for book in books: | ||
| collector.add_new_book(book) | ||
| collector.set_book_genre(book, 'Ужасы') | ||
|
|
||
| collector.add_new_book('Ну погоди') | ||
| collector.set_book_genre('Ну погоди', 'Мультфильмы') | ||
| collector.add_new_book('Бойцовский Клуб') | ||
| collector.set_book_genre('Бойцовский Клуб', 'Драма') | ||
| assert collector.get_book_genre('Бойцовский Клуб') == '' | ||
|
|
||
| assert collector.get_books_with_specific_genre('Ужасы') == books | ||
| def test_set_book_genre_for_nonexistent_book(self): | ||
| collector = BooksCollector() | ||
| collector.set_book_genre('Несуществующая книга', 'Фантастика') | ||
| assert collector.get_book_genre('Несуществующая книга') is None | ||
|
|
||
| def test_get_books_genre(self): | ||
| @pytest.mark.parametrize('genre, expected_books', [ | ||
| ('Фантастика', ['Дюна', 'Марсианин']), | ||
| ('Ужасы', ['Оно']), | ||
| ('Детективы', []) | ||
| ]) | ||
| def test_get_books_with_specific_genre(self, genre, expected_books): | ||
| collector = BooksCollector() | ||
| collector.add_new_book('Дюна') | ||
| collector.add_new_book('Марсианин') | ||
| collector.add_new_book('Оно') | ||
| collector.set_book_genre('Дюна', 'Фантастика') | ||
| collector.set_book_genre('Марсианин', 'Фантастика') | ||
| collector.set_book_genre('Оно', 'Ужасы') | ||
| assert collector.get_books_with_specific_genre(genre) == expected_books | ||
|
|
||
| def test_get_books_for_children_excludes_age_rating(self): | ||
| collector = BooksCollector() | ||
| collector.add_new_book('Азбука') | ||
| assert collector.get_books_genre() == {'Азбука': ''} | ||
| collector.add_new_book('Гарри Поттер') | ||
| collector.add_new_book('Вий') | ||
| collector.add_new_book('Шерлок Холмс') | ||
| collector.add_new_book('Колобок') | ||
| collector.set_book_genre('Гарри Поттер', 'Фантастика') | ||
| collector.set_book_genre('Вий', 'Ужасы') | ||
| collector.set_book_genre('Шерлок Холмс', 'Детективы') | ||
| collector.set_book_genre('Колобок', 'Мультфильмы') | ||
| books_for_children = collector.get_books_for_children() | ||
| assert 'Гарри Поттер' in books_for_children | ||
| assert 'Колобок' in books_for_children | ||
| assert 'Вий' not in books_for_children | ||
| assert 'Шерлок Холмс' not in books_for_children | ||
|
|
||
| @pytest.mark.parametrize('name, genre', [ | ||
| ('Вий', 'Ужасы'), | ||
| ('Шерлок Холмс', 'Детективы'), | ||
| ('Колобок', 'Мультфильмы') | ||
| ]) | ||
| def test_books_with_age_rating_not_in_children_books(self, name, genre): | ||
| collector = BooksCollector() | ||
| collector.add_new_book(name) | ||
| collector.set_book_genre(name, genre) | ||
| books_for_children = collector.get_books_for_children() | ||
| if genre in collector.genre_age_rating: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нужно исправить: условий в тестах быть не может, тест всегда однозначен. Если это не вписывается в параметризацию, значит ее быть не должно и это просто несколько отдельных тестовых методов |
||
| assert name not in books_for_children | ||
| else: | ||
| assert name in books_for_children | ||
|
|
||
| def test_get_books_for_children(self): | ||
| def test_add_book_in_favorites(self): | ||
| collector = BooksCollector() | ||
| collector.add_new_book('Детская') | ||
| collector.add_new_book('Взрослая') | ||
| collector.set_book_genre('Детская', 'Фантастика') | ||
| collector.set_book_genre('Взрослая', 'Ужасы') | ||
| collector.add_new_book('Преступление и Наказание') | ||
| collector.add_book_in_favorites('Преступление и Наказание') | ||
| assert 'Преступление и Наказание' in collector.get_list_of_favorites_books() | ||
| assert len(collector.get_list_of_favorites_books()) == 1 | ||
|
|
||
| assert collector.get_books_for_children() == ['Детская'] | ||
| def test_add_book_in_favorites_not_in_books_genre(self): | ||
| collector = BooksCollector() | ||
| collector.add_book_in_favorites('Несуществующая книга') | ||
| assert len(collector.get_list_of_favorites_books()) == 0 | ||
|
|
||
| def test_add_book_in_favorites(self): | ||
| def test_add_duplicate_book_in_favorites(self): | ||
| collector = BooksCollector() | ||
| collector.add_new_book('Азбука') | ||
| collector.add_book_in_favorites('Азбука') | ||
| assert collector.get_list_of_favorites_books() == ['Азбука'] | ||
| collector.add_new_book('Волшебник Изумрудного Города') | ||
| collector.add_book_in_favorites('Волшебник Изумрудного Города') | ||
| collector.add_book_in_favorites('Волшебник Изумрудного Города') | ||
| assert len(collector.get_list_of_favorites_books()) == 1 | ||
|
|
||
| def test_delete_book_from_favorites(self): | ||
| collector = BooksCollector() | ||
| collector.add_new_book('Азбука') | ||
| collector.add_book_in_favorites('Азбука') | ||
| collector.delete_book_from_favorites('Азбука') | ||
| assert collector.get_list_of_favorites_books() == [] | ||
| collector.add_new_book('Волшебник Изумрудного Города') | ||
| collector.add_book_in_favorites('Волшебник Изумрудного Города') | ||
| collector.delete_book_from_favorites('Волшебник Изумрудного Города') | ||
| assert 'Волшебник Изумрудного Города' not in collector.get_list_of_favorites_books() | ||
| assert len(collector.get_list_of_favorites_books()) == 0 | ||
|
|
||
| def test_get_list_of_favorites_books(self): | ||
| def test_delete_nonexistent_book_from_favorites(self): | ||
| collector = BooksCollector() | ||
| collector.add_new_book('Азбука') | ||
| collector.add_new_book('Алгебра') | ||
| collector.add_book_in_favorites('Азбука') | ||
| collector.add_book_in_favorites('Алгебра') | ||
| assert collector.get_list_of_favorites_books() == ['Азбука', 'Алгебра'] | ||
| collector.add_new_book('Богомолов') | ||
| collector.add_book_in_favorites('Богомолов') | ||
| collector.delete_book_from_favorites('Несуществующая книга') | ||
| assert len(collector.get_list_of_favorites_books()) == 1 | ||
| assert 'Богомолов' in collector.get_list_of_favorites_books() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нужно исправить: отсутствуют тесты на проверку методов get_books_genre. На каждый метод должен быть отдельный тест, вне зависимости, вызывается он в других тестах или нет |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Нужно исправить: условий в тестах быть не может, тест всегда однозначен. Если это не вписывается в параметризацию, значит ее быть не должно и это просто несколько отдельных тестовых методов