import pytest from book import Book _default_title = 'My Boke' _default_authors = ['Eliott Wootton', 'John Smith'] _default_isbn = '1234567890123' _default_cost = 14.99 @pytest.fixture def valid_book(): return Book(_default_title, _default_authors, _default_isbn, _default_cost) # test if book setter works def test_create_valid_book(valid_book: Book): assert valid_book.get_title() == _default_title assert valid_book.get_authors() == _default_authors assert valid_book.get_isbn() == _default_isbn assert valid_book.get_cost() == _default_cost # test if title setter works def test_update_title(valid_book: Book): valid_book.set_title('My Book') assert valid_book.get_title() == 'My Book' # test if authors setter works def test_update_authors(valid_book: Book): valid_book.set_authors(['John Smith', 'Jane Smith']) assert valid_book.get_authors() == ['John Smith', 'Jane Smith'] # test if isbn setter works def test_update_isbn(valid_book: Book): valid_book.set_isbn('3210987654321') assert valid_book.get_isbn() == '3210987654321' # test is cost setter works def test_update_cost(valid_book: Book): valid_book.set_cost(9.99) assert valid_book.get_cost() == 9.99 # test if repr works def test_repr_str(valid_book): rep = str(valid_book) assert _default_title in rep assert ', '.join(_default_authors) in rep assert _default_isbn in rep assert str(_default_cost) in rep """ - Title must not be blank, and cannot be longer than 255 characters. - Authors is a list of names. None of the names should be blank or over 255 characters. Names should not have any characters except for alphabetical ones. - ISBN is a string, but it can only contain numbers and there must be 13 of them. - Cost cannot be negative. """ # test if empty, length > 255, and if string @pytest.mark.parametrize("invalid_title", [ "", "A" * 256, 6.7 ]) def test_invalid_title_raises(invalid_title: str): with pytest.raises((ValueError, TypeError)): Book(invalid_title, _default_authors, _default_isbn, _default_cost) # test if empty, is array, each author's name is str, each author's name > 255 and each author's name alpha @pytest.mark.parametrize("invalid_authors", [ [], 'Eliott Wootton', [41, 'Charleston White'], ['LeBron James', 'A'*256], ['3liott W00tt0n', 'hello world'] ]) def test_invalid_authors_raised(invalid_authors: str): with pytest.raises((ValueError, TypeError)): Book(_default_title, invalid_authors, _default_isbn, _default_cost) # test if string, only contains numbers and length is 13 @pytest.mark.parametrize("invalid_isbn", [ 127, '123456789o123', '12345678901234' ]) def test_invalid_isbn_raises(invalid_isbn: str): with pytest.raises((ValueError, TypeError)): Book(_default_title, _default_authors, invalid_isbn, _default_cost) # test if float and not negative @pytest.mark.parametrize("invalid_cost", [ '14.99', -14.99 ]) def test_invalid_cost_raises(invalid_cost: str): with pytest.raises((ValueError, TypeError)): Book(_default_title, _default_authors, _default_isbn, invalid_cost)