Skip to content

Commit aff88ca

Browse files
committed
Revert "Initial commit"
This reverts commit bf1a049.
1 parent bf1a049 commit aff88ca

File tree

7 files changed

+392
-0
lines changed

7 files changed

+392
-0
lines changed

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
import sys
3+
from pathlib import Path
4+
5+
sys.path.append(str(Path(__file__).parent.parent / "src"))
6+
7+
from summarize_gutenberg.api import Book
8+
9+
@pytest.fixture()
10+
def book_fixture():
11+
"""
12+
Create a Book.
13+
"""
14+
book = Book(
15+
id=1,
16+
title="Yesterday's Tomorrows",
17+
author="Wilfred Sinecure",
18+
url="https://www.gutenberg.org/",
19+
filename="yesterdaystomorrows.txt",
20+
)
21+
22+
return book

tests/test_author_parse.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
from summarize_gutenberg.get_books import author_parse
3+
4+
authors = [
5+
('Aristotle', 'Aristotle'), # single name
6+
('Austen, Jane', 'Jane Austen'), # first & last
7+
('Stevenson, Robert Louis', 'Robert Louis Stevenson'), # first last middle
8+
('Chesterton, G. K. (Gilbert Keith)', 'G. K. Chesterton'), # parenthetical
9+
('H. D. (Hilda Doolittle)', 'H. D.'), # irregular parenthetical
10+
('Tolkien, J. R. R. (John Ronald Reuel)', 'J. R. R. Tolkien'), # parenthetical with three initials
11+
('Von Arnim, Elizabeth', 'Elizabeth Von Arnim'), # von
12+
('Sanchez, Nellie Van de Grift', 'Nellie Van de Grift Sanchez'), # van
13+
('Martinez de la Torre, Rafael', 'Rafael Martinez de la Torre'), # de la
14+
('Cervantes Saavedra, Miguel de', 'Miguel de Cervantes Saavedra'), # de
15+
('Alger, Horatio, Jr.', 'Horatio Alger Jr.'), # jr
16+
(None, '') # none
17+
]
18+
19+
@pytest.mark.parametrize('input, expected', authors)
20+
def test_author_parse(input, expected):
21+
assert author_parse(input) == expected, f'Expected {expected}, but got {author_parse(input)}'

tests/test_booksdb.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import pytest
2+
from pathlib import Path
3+
from tempfile import TemporaryDirectory
4+
5+
from summarize_gutenberg.api import Book, BooksDB
6+
7+
@pytest.fixture()
8+
def books_db():
9+
with TemporaryDirectory() as db_dir:
10+
db_path = Path(db_dir)
11+
db = BooksDB(db_path)
12+
yield db
13+
db.close()
14+
15+
def test_empty_db(books_db):
16+
assert books_db.count() == 0
17+
18+
def test_add_one_book(books_db):
19+
books_db.add_book(Book())
20+
21+
assert books_db.count() == 1
22+
23+
def test_add_two_books(books_db):
24+
books_db.add_book(Book())
25+
books_db.add_book(Book())
26+
27+
assert books_db.count() == 2
28+
29+
def test_delete_book(books_db):
30+
books_db.add_book(Book(id=1))
31+
books_db.add_book(Book(id=2))
32+
books_db.delete_book(2)
33+
34+
assert books_db.count() == 1
35+
36+
def test_delete_all(books_db):
37+
books_db.add_book(Book(id=1))
38+
books_db.add_book(Book(id=2))
39+
books_db.delete_all()
40+
41+
assert books_db.count() == 0
42+
43+
def test_get_book(books_db, book_fixture):
44+
book = book_fixture
45+
books_db.add_book(book)
46+
gotten_book = books_db.get_book(1)
47+
48+
assert book == gotten_book
49+
50+
def test_list_books(books_db):
51+
books_db.add_book(Book(id=1))
52+
books_db.add_book(Book(id=2))
53+
books_db.add_book(Book(id=3))
54+
listed_books = books_db.list_books()
55+
expected_ids = {1, 2, 3}
56+
57+
assert len(listed_books) == 3
58+
for book in listed_books:
59+
assert book.id in expected_ids
60+
61+
def test_update_book(books_db, book_fixture):
62+
book = book_fixture
63+
books_db.add_book(book)
64+
new_data = {
65+
"title": "Tomorrow's Yesterdays",
66+
"author": "Clifton Semaphore",
67+
"url": "https://www.csemaphore.com/2",
68+
"filename": "tomorrowsyesterdays.txt",
69+
}
70+
71+
new_book = Book.from_dict(new_data)
72+
books_db.update_book(book.id, new_book)
73+
updated_book_data = books_db.get_book(book.id).to_dict()
74+
75+
assert updated_book_data['id'] == book.id
76+
assert updated_book_data['title'] == new_data['title']
77+
assert updated_book_data['author'] == new_data['author']
78+
assert updated_book_data['url'] == new_data['url']
79+
assert updated_book_data['filename'] == new_data['filename']
80+
81+
82+
83+

tests/test_create_filename.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
from summarize_gutenberg.get_books import create_filename
3+
4+
titles = [
5+
('Dracula', 'dracula.txt'), # one word
6+
('Franny and Zooey', 'frannyandzooey.txt'), # spaces
7+
('Frankenstein; Or, The Modern Prometheus', 'frankenstein.txt'), # semicolon
8+
('"Left-Wing" Communism: an Infantile Disorder', 'leftwingcommunism.txt'), # colon, quoation marks, hyphen
9+
('We Who Are About To...', 'wewhoareaboutto.txt'), # periods
10+
("Swann's Way", 'swannsway.txt'), # apostrophe/single quote
11+
("My Life — Volume 1", 'mylifevolume1.txt') # em dash which is weird but one of the top gutenberg books has one
12+
]
13+
14+
@pytest.mark.parametrize('input, expected', titles)
15+
def test_create_filename(input, expected):
16+
assert create_filename(input) == expected, f'Expected {expected}, but got {create_filename(input)}'

tests/test_get_books.py

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
import pytest
2+
import requests
3+
4+
from summarize_gutenberg.get_books import fetch_default_books, process_books, author_check, url_check
5+
6+
def test_gutendex_api():
7+
base_url = 'https://gutendex.com/books?languages=en'
8+
book_request = requests.get(base_url, params={'q': 'requests+lang:en'})
9+
assert book_request.status_code == 200
10+
assert 'results' in book_request.json()
11+
assert len(book_request.json()['results']) == 32
12+
13+
def test_fetch_default_books_success(mocker):
14+
mock_response = mocker.Mock()
15+
mock_response.json.return_value = {'results': ['book1', 'book2']}
16+
mock_response.raise_for_status.return_value = None
17+
mocker.patch('requests.get', return_value=mock_response)
18+
19+
books = fetch_default_books()
20+
assert books == ['book1', 'book2']
21+
22+
def test_fetch_default_books_http_error(mocker):
23+
mocker.patch('requests.get', side_effect=requests.exceptions.HTTPError("Http Error"))
24+
25+
with pytest.raises(requests.exceptions.HTTPError):
26+
fetch_default_books()
27+
28+
def test_fetch_default_books_connection_error(mocker):
29+
mocker.patch('requests.get', side_effect=requests.exceptions.ConnectionError("Connection Error"))
30+
31+
with pytest.raises(requests.exceptions.ConnectionError):
32+
fetch_default_books()
33+
34+
def test_fetch_default_books_timeout(mocker):
35+
mocker.patch('requests.get', side_effect=requests.exceptions.Timeout("Timeout Error"))
36+
37+
with pytest.raises(requests.exceptions.Timeout):
38+
fetch_default_books()
39+
40+
def test_fetch_default_books_request_exception(mocker):
41+
mocker.patch('requests.get', side_effect=requests.exceptions.RequestException("Exotic Error"))
42+
43+
with pytest.raises(requests.exceptions.RequestException):
44+
fetch_default_books()
45+
46+
test_authors_no_list = {
47+
'name': 'Doe, John'
48+
}
49+
50+
test_authors_no_name = [
51+
{
52+
'mame': 'Doe, John'
53+
}
54+
]
55+
56+
def test_author_check_no_list():
57+
with pytest.raises(KeyError) as excinfo:
58+
author_check(test_authors_no_list)
59+
assert "'authors' in unexpected format (should be list)" in str(excinfo.value)
60+
61+
def test_author_check_no_name():
62+
with pytest.raises(KeyError) as excinfo:
63+
author_check(test_authors_no_name)
64+
assert "No field named 'name' in authors" in str(excinfo.value)
65+
66+
test_formats_no_plaintext = {
67+
'wrong_format': 'book_url',
68+
}
69+
70+
def test_url_check_no_plaintext():
71+
with pytest.raises(KeyError) as excinfo:
72+
url_check(test_formats_no_plaintext)
73+
assert "No plaintext URL or plaintext key format has changed" in str(excinfo.value)
74+
75+
book_data = [
76+
# Normal data
77+
(
78+
{
79+
'id': 1,
80+
'title': 'Sample Book',
81+
'authors': [{'name': 'Doe, John'}],
82+
'formats': {'text/plain; charset=us-ascii': 'http://example.com'}
83+
},
84+
{
85+
1: {
86+
'title': 'Sample Book',
87+
'author': 'John Doe',
88+
'url': 'http://example.com',
89+
'filename': 'samplebook.txt',
90+
}
91+
}
92+
),
93+
# No title
94+
(
95+
{
96+
'id': 1,
97+
'authors': [{'name': 'Doe, John'}],
98+
'formats': {'text/plain; charset=us-ascii': 'http://example.com'}
99+
},
100+
{
101+
1: {
102+
'title': None,
103+
'author': 'John Doe',
104+
'url': 'http://example.com',
105+
'filename': None,
106+
}
107+
}
108+
),
109+
# No author
110+
(
111+
{
112+
'id': 1,
113+
'title': 'Sample Book',
114+
'formats': {'text/plain; charset=us-ascii': 'http://example.com'}
115+
},
116+
{
117+
1: {
118+
'title': 'Sample Book',
119+
'author': 'No author found.',
120+
'url': 'http://example.com',
121+
'filename': 'samplebook.txt',
122+
}
123+
}
124+
),
125+
# No formats
126+
(
127+
{
128+
'id': 1,
129+
'title': 'Sample Book',
130+
'authors': [{'name': 'Doe, John'}]
131+
},
132+
{
133+
1: {
134+
'title': 'Sample Book',
135+
'author': 'John Doe',
136+
'url': 'No URLs found.',
137+
'filename': 'samplebook.txt',
138+
}
139+
}
140+
),
141+
# No title or author
142+
(
143+
{
144+
'id': 1,
145+
'formats': {'text/plain; charset=us-ascii': 'http://example.com'}
146+
},
147+
{
148+
1: {
149+
'title': None,
150+
'author': 'No author found.',
151+
'url': 'http://example.com',
152+
'filename': None,
153+
}
154+
}
155+
),
156+
# no title or formats
157+
(
158+
{
159+
'id': 1,
160+
'authors': [{'name': 'Doe, John'}],
161+
},
162+
{
163+
1: {
164+
'title': None,
165+
'author': 'John Doe',
166+
'url': 'No URLs found.',
167+
'filename': None,
168+
}
169+
}
170+
),
171+
# no author or formats
172+
(
173+
{
174+
'id': 1,
175+
'title': 'Sample Book',
176+
},
177+
{
178+
1: {
179+
'title': 'Sample Book',
180+
'author': 'No author found.',
181+
'url': 'No URLs found.',
182+
'filename': 'samplebook.txt',
183+
}
184+
}
185+
),
186+
# no title author or formats
187+
(
188+
{
189+
'id': 1,
190+
},
191+
{
192+
1: {
193+
'title': None,
194+
'author': 'No author found.',
195+
'url': 'No URLs found.',
196+
'filename': None,
197+
}
198+
}
199+
),
200+
]
201+
202+
203+
@pytest.mark.parametrize('input, expected', book_data)
204+
def test_process_books(input, expected):
205+
result = process_books([input])
206+
assert result == expected, f'Expected {expected}, but got {result}'

0 commit comments

Comments
 (0)