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