Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 170 additions & 14 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,180 @@
import pytest
from main import BooksCollector

# класс TestBooksCollector объединяет набор тестов, которыми мы покрываем наше приложение BooksCollector
# обязательно указывать префикс Test
class TestBooksCollector:

# пример теста:
# обязательно указывать префикс test_
# дальше идет название метода, который тестируем add_new_book_
# затем, что тестируем add_two_books - добавление двух книг
def test_add_new_book_add_two_books(self):
# создаем экземпляр (объект) класса BooksCollector
@pytest.mark.parametrize("book_name", [
'Гордость и предубеждение',
'Война и мир',
'Преступление и наказание',
])
def test_add_new_book_valid_name(self, book_name):
collector = BooksCollector()
collector.add_new_book(book_name)
books = collector.get_books_genre()
assert book_name in books
assert books[book_name] == ''

# добавляем две книги
def test_add_new_book_min_length_name(self):
collector = BooksCollector()
min_name = 'A'
collector.add_new_book(min_name)
books = collector.get_books_genre()
assert min_name in books
assert books[min_name] == ''

def test_add_new_book_max_length_name(self):
collector = BooksCollector()
max_name = 'a' * 40
collector.add_new_book(max_name)
books = collector.get_books_genre()
assert max_name in books
assert books[max_name] == ''

def test_add_new_book_empty_name(self):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Необходимо исправить: не хватает позитивных тестов на проверку границы имени

collector = BooksCollector()
empty_name = ''
collector.add_new_book(empty_name)
books = collector.get_books_genre()
assert empty_name not in books

def test_add_new_book_long_name(self):
collector = BooksCollector()
long_name = 'a' * 41
collector.add_new_book(long_name)
books = collector.get_books_genre()
assert long_name not in books
Comment on lines +34 to +46

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно лучше: шаги этих тестов схожи. Можно их объединить с помощью параметризации


def test_add_new_book_two_books(self):
collector = BooksCollector()
collector.add_new_book('Гордость и предубеждение и зомби')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Необходимо исправить: не хватает теста на повторное добавление книги в коллекцию

collector.add_new_book('Что делать, если ваш кот хочет вас убить')
assert len(collector.get_books_genre()) == 2

# проверяем, что добавилось именно две
# словарь books_rating, который нам возвращает метод get_books_rating, имеет длину 2
assert len(collector.get_books_rating()) == 2
def test_add_new_book_duplicate(self):
collector = BooksCollector()
book_name = 'Гарри Поттер'
collector.add_new_book(book_name)
collector.add_new_book(book_name)
books = collector.get_books_genre()
assert len(books) == 1
assert book_name in books

# напиши свои тесты ниже
# чтобы тесты были независимыми в каждом из них создавай отдельный экземпляр класса BooksCollector()
@pytest.mark.parametrize("genre", [
'Фантастика',
'Детективы',
'Мультфильмы',
])
def test_set_book_genre_valid_genre(self, genre):
collector = BooksCollector()
book_name = 'Фантастическая книга'
collector.add_new_book(book_name)
collector.set_book_genre(book_name, genre)
assert collector.get_book_genre(book_name) == genre

@pytest.mark.parametrize("invalid_genre", [
'Неизвестный жанр',
'Ужасы для детей',
'Фэнтези-ужасы',
])
def test_set_book_genre_invalid_genre(self, invalid_genre):
collector = BooksCollector()
book_name = 'Книга без жанра'
collector.add_new_book(book_name)
collector.set_book_genre(book_name, invalid_genre)
assert collector.get_book_genre(book_name) == ''

def test_get_book_genre_existing_book(self):
collector = BooksCollector()
book_name = 'Детектив'
expected_genre = 'Детективы'
collector.add_new_book(book_name)
collector.set_book_genre(book_name, expected_genre)
result = collector.get_book_genre(book_name)
assert result == expected_genre

def test_get_book_genre_nonexistent_book(self):
collector = BooksCollector()
nonexistent_book = 'Несуществующая книга'
result = collector.get_book_genre(nonexistent_book)
assert result is None

def test_get_books_with_specific_genre_existing(self):
collector = BooksCollector()
collector.add_new_book('Мультфильм 1')
collector.add_new_book('Мультфильм 2')
collector.set_book_genre('Мультфильм 1', 'Мультфильмы')
collector.set_book_genre('Мультфильм 2', 'Мультфильмы')
result = collector.get_books_with_specific_genre('Мультфильмы')
assert len(result) == 2
assert 'Мультфильм 1' in result
assert 'Мультфильм 2' in result

def test_get_books_with_specific_genre_nonexistent(self):
collector = BooksCollector()
result = collector.get_books_with_specific_genre('Фантастика')
assert len(result) == 0

def test_get_books_genre_returns_dict(self):
collector = BooksCollector()
books_genre = collector.get_books_genre()
assert isinstance(books_genre, dict)
assert len(books_genre) == 0

def test_get_books_for_children_includes_cartoons(self):
collector = BooksCollector()
book_name = 'Мультфильм'
collector.add_new_book(book_name)
collector.set_book_genre(book_name, 'Мультфильмы')
result = collector.get_books_for_children()
assert book_name in result

def test_get_books_for_children_excludes_horror(self):
collector = BooksCollector()
book_name = 'Ужас'
collector.add_new_book(book_name)
collector.set_book_genre(book_name, 'Ужасы')
result = collector.get_books_for_children()
assert book_name not in result

@pytest.mark.parametrize("book_name", [
'Любимая книга',
'Книга на лето',
'Классика',
])
def test_add_book_in_favorites_existing_book(self, book_name):
collector = BooksCollector()
collector.add_new_book(book_name)
collector.add_book_in_favorites(book_name)
favorites = collector.get_list_of_favorites_books()
assert book_name in favorites

def test_add_book_in_favorites_nonexistent_book(self):
collector = BooksCollector()
nonexistent_book = 'Неизвестная книга'
collector.add_book_in_favorites(nonexistent_book)
favorites = collector.get_list_of_favorites_books()
assert nonexistent_book not in favorites

def test_delete_book_from_favorites_success(self):
collector = BooksCollector()
book_name = 'Книга для удаления'
collector.add_new_book(book_name)
collector.add_book_in_favorites(book_name)
collector.delete_book_from_favorites(book_name)
favorites = collector.get_list_of_favorites_books()
assert book_name not in favorites

def test_get_list_of_favorites_books_empty_initially(self):
collector = BooksCollector()
favorites = collector.get_list_of_favorites_books()
assert favorites == []

def test_get_list_of_favorites_books_after_adding(self):
collector = BooksCollector()
book_name = 'Книга 1'
collector.add_new_book(book_name)
collector.add_book_in_favorites(book_name)
favorites = collector.get_list_of_favorites_books()
assert book_name in favorites
assert len(favorites) == 1