Skip to content

Commit f59f9d6

Browse files
authored
Merge pull request #19 from TobiAlbert/master
Correct Error Message and Add Unit Tests
2 parents c953afb + 265b8dd commit f59f9d6

2 files changed

Lines changed: 210 additions & 17 deletions

File tree

newsapi/newsapi_client.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
4848
if type(q) == str:
4949
payload['q'] = q
5050
else:
51-
raise TypeError('keyword/phrase q param should be a str')
51+
raise TypeError('keyword/phrase q param should be a of type str')
5252

5353
# Sources
5454
if (sources is not None) and ((country is not None) or (category is not None)):
@@ -59,7 +59,7 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
5959
if type(sources) == str:
6060
payload['sources'] = sources
6161
else:
62-
raise TypeError('sources param should be a str')
62+
raise TypeError('sources param should be of type str')
6363

6464
# Language
6565
if language is not None:
@@ -69,7 +69,7 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
6969
else:
7070
raise ValueError('invalid language')
7171
else:
72-
raise TypeError('language param should be a string')
72+
raise TypeError('language param should be of type str')
7373

7474
# Country
7575
if country is not None:
@@ -79,7 +79,7 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
7979
else:
8080
raise ValueError('invalid country')
8181
else:
82-
raise TypeError('country param should be a string')
82+
raise TypeError('country param should be of type str')
8383

8484
# Category
8585
if category is not None:
@@ -89,12 +89,12 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
8989
else:
9090
raise ValueError('invalid category')
9191
else:
92-
raise TypeError('category param should be a string')
92+
raise TypeError('category param should be of type str')
9393

9494
# Page Size
9595
if page_size is not None:
9696
if type(page_size) == int:
97-
if page_size >= 0 and page_size <= 100:
97+
if 0 <= page_size <= 100:
9898
payload['pageSize'] = page_size
9999
else:
100100
raise ValueError('page_size param should be an int between 1 and 100')
@@ -160,21 +160,21 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
160160
if type(q) == str:
161161
payload['q'] = q
162162
else:
163-
raise TypeError('keyword/phrase q param should be a str')
163+
raise TypeError('keyword/phrase q param should be of type str')
164164

165165
# Sources
166166
if sources is not None:
167167
if type(sources) == str:
168168
payload['sources'] = sources
169169
else:
170-
raise TypeError('sources param should be a str')
170+
raise TypeError('sources param should be of type str')
171171

172172
# Domains To Search
173173
if domains is not None:
174174
if type(domains) == str:
175175
payload['domains'] = domains
176176
else:
177-
raise TypeError('domains param should be a string')
177+
raise TypeError('domains param should be of type str')
178178

179179
# Search From This Date ...
180180
if from_param is not None:
@@ -188,7 +188,7 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
188188
else:
189189
raise ValueError('from_param should be in the format of YYYY-MM-DD')
190190
else:
191-
raise TypeError('from_param should be a string')
191+
raise TypeError('from_param should be of type str')
192192

193193
# ... To This Date
194194
if to is not None:
@@ -202,7 +202,7 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
202202
else:
203203
raise ValueError('to param should be in the format of YYYY-MM-DD')
204204
else:
205-
raise TypeError('to param should be a string')
205+
raise TypeError('to param should be of type str')
206206

207207
# Language
208208
if language is not None:
@@ -212,7 +212,7 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
212212
else:
213213
payload['language'] = language
214214
else:
215-
raise TypeError('language param should be a string')
215+
raise TypeError('language param should be of type str')
216216

217217
# Sort Method
218218
if sort_by is not None:
@@ -222,12 +222,12 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
222222
else:
223223
raise ValueError('invalid sort')
224224
else:
225-
raise TypeError('sort_by param should be a string')
225+
raise TypeError('sort_by param should be of type str')
226226

227227
# Page Size
228228
if page_size is not None:
229229
if type(page_size) == int:
230-
if page_size >= 0 and page_size <= 100:
230+
if 0 <= page_size <= 100:
231231
payload['pageSize'] = page_size
232232
else:
233233
raise ValueError('page_size param should be an int between 1 and 100')
@@ -285,7 +285,7 @@ def get_sources(self, category=None, language=None, country=None):
285285
else:
286286
raise ValueError('invalid language')
287287
else:
288-
raise TypeError('language param should be a string')
288+
raise TypeError('language param should be of type str')
289289

290290
# Country
291291
if country is not None:
@@ -295,7 +295,7 @@ def get_sources(self, category=None, language=None, country=None):
295295
else:
296296
raise ValueError('invalid country')
297297
else:
298-
raise TypeError('country param should be a string')
298+
raise TypeError('country param should be of type str')
299299

300300
# Category
301301
if category is not None:
@@ -305,7 +305,7 @@ def get_sources(self, category=None, language=None, country=None):
305305
else:
306306
raise ValueError('invalid category')
307307
else:
308-
raise TypeError('category param should be a string')
308+
raise TypeError('category param should be of type str')
309309

310310
# Send Request
311311
r = requests.get(const.SOURCES_URL, auth=self.auth, timeout=30, params=payload)

tests/test_newsapi_client.py

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
import unittest
2+
import os
3+
from newsapi.newsapi_client import NewsApiClient
4+
5+
6+
class NewsApiClientTest(unittest.TestCase):
7+
8+
def setUp(self):
9+
key = os.environ.get('news_api_secret')
10+
self.api = NewsApiClient(key)
11+
12+
def test_api_top_headline(self):
13+
# Raise TypeError if Keyword/Phrase param is not of type str
14+
q = 0
15+
with self.assertRaises(TypeError):
16+
self.api.get_top_headlines(q=q)
17+
18+
# Raise ValueError if sources param in not None and country param or category param is not None
19+
sources = 'techcrunch'
20+
country = 'us'
21+
category = 'business'
22+
with self.assertRaises(ValueError):
23+
self.api.get_top_headlines(sources=sources, country=country, category=category)
24+
25+
# Raise TypeError if sources param is not of type str
26+
sources = 0
27+
with self.assertRaises(TypeError):
28+
self.api.get_top_headlines(sources=sources)
29+
30+
# Raise TypeError if language param is not of type str
31+
language = 0
32+
with self.assertRaises(TypeError):
33+
self.api.get_top_headlines(language=language)
34+
35+
# Raise ValueError if language param is invalid
36+
language = 'xx'
37+
with self.assertRaises(ValueError):
38+
self.api.get_top_headlines(language=language)
39+
40+
# Raise TypeError if country param is not of type str
41+
country = 0
42+
with self.assertRaises(TypeError):
43+
self.api.get_top_headlines(country=country)
44+
45+
# Raise ValueError if country param is invalid
46+
country = 'xx'
47+
with self.assertRaises(ValueError):
48+
self.api.get_top_headlines(country=country)
49+
50+
# Raises TypeError if category param is not of type str
51+
category = 0
52+
with self.assertRaises(TypeError):
53+
self.api.get_top_headlines(category=category)
54+
55+
# Raises ValueError if category param is invalid
56+
category = 'x0x'
57+
with self.assertRaises(ValueError):
58+
self.api.get_top_headlines(category=category)
59+
60+
# Raises TypeError if page_size param is not an int
61+
page_size = '1'
62+
with self.assertRaises(TypeError):
63+
self.api.get_top_headlines(page_size=page_size)
64+
65+
# Raises ValueError if page_size param is less than zero(0) or greater than 100
66+
page_size = -1
67+
with self.assertRaises(ValueError):
68+
self.api.get_top_headlines(page_size=page_size)
69+
70+
page_size = 1000
71+
with self.assertRaises(ValueError):
72+
self.api.get_top_headlines(page_size=page_size)
73+
74+
# Raises a TypeError is page param is not an int
75+
page = '1'
76+
with self.assertRaises(TypeError):
77+
self.api.get_top_headlines(page=page)
78+
79+
# Raises a ValueError if page param is less than zero(0)
80+
page = -1
81+
with self.assertRaises(ValueError):
82+
self.api.get_top_headlines(page=page)
83+
84+
def test_api_get_everything(self):
85+
# Raise TypeError if Keyword/Phrase param is None
86+
q = 0
87+
with self.assertRaises(TypeError):
88+
self.api.get_everything(q=q)
89+
90+
# Raise TypeError if sources param is not of type str
91+
sources = 0
92+
with self.assertRaises(TypeError):
93+
self.api.get_everything(sources=sources)
94+
95+
# Raise TypeError is domains param is not of type str
96+
domains = 0
97+
with self.assertRaises(TypeError):
98+
self.api.get_everything(domains=domains)
99+
100+
# Raise TypeError is from_param param is not of type str
101+
from_param = 0
102+
with self.assertRaises(TypeError):
103+
self.api.get_everything(from_param=from_param)
104+
105+
# Raise ValueError if param is not in the format YYYY-MM-DD
106+
from_param = '2016-6-4'
107+
with self.assertRaises(ValueError):
108+
self.api.get_everything(from_param=from_param)
109+
110+
# Raise TypeError if to param is not of type str
111+
to = 1
112+
with self.assertRaises(TypeError):
113+
self.api.get_everything(to=to)
114+
115+
# Raise ValueError if to param is not in the format YYYY-MM-DD
116+
to = '2016-6-24'
117+
with self.assertRaises(ValueError):
118+
self.api.get_everything(to=to)
119+
120+
# Raise TypeError if language param is not of type str
121+
language = 0
122+
with self.assertRaises(TypeError):
123+
self.api.get_everything(language=language)
124+
125+
# Raise ValueError if language param is invalid
126+
language = 'xx'
127+
with self.assertRaises(ValueError):
128+
self.api.get_everything(language=language)
129+
130+
# Raise TypeError is sort_by param is not of type str
131+
sort_by = 1
132+
with self.assertRaises(TypeError):
133+
self.api.get_everything(sort_by=sort_by)
134+
135+
# Raise ValueError if soft_by param is invalid
136+
sort_by = 'sort'
137+
with self.assertRaises(ValueError):
138+
self.api.get_everything(sort_by=sort_by)
139+
140+
# Raises TypeError if page_size param is not an int
141+
page_size = '1'
142+
with self.assertRaises(TypeError):
143+
self.api.get_everything(page_size=page_size)
144+
145+
# Raises ValueError if page_size param is less than zero(0) or greater than 100
146+
page_size = -1
147+
with self.assertRaises(ValueError):
148+
self.api.get_everything(page_size=page_size)
149+
150+
page_size = 1000
151+
with self.assertRaises(ValueError):
152+
self.api.get_everything(page_size=page_size)
153+
154+
# Raises a TypeError is page param is not an int
155+
page = '1'
156+
with self.assertRaises(TypeError):
157+
self.api.get_everything(page=page)
158+
159+
# Raises a ValueError if page param is less than zero(0)
160+
page = -1
161+
with self.assertRaises(ValueError):
162+
self.api.get_everything(page=page)
163+
164+
def test_api_get_sources(self):
165+
# Raise TypeError if language param is not of type str
166+
language = 0
167+
with self.assertRaises(TypeError):
168+
self.api.get_sources(language=language)
169+
170+
# Raise ValueError if language param is invalid
171+
language = 'xx'
172+
with self.assertRaises(ValueError):
173+
self.api.get_sources(language=language)
174+
175+
# Raise TypeError if country param is not of type str
176+
country = 0
177+
with self.assertRaises(TypeError):
178+
self.api.get_sources(country=country)
179+
180+
# Raise ValueError if country param is invalid
181+
country = 'xx'
182+
with self.assertRaises(ValueError):
183+
self.api.get_sources(country=country)
184+
185+
# Raises TypeError if category param is not of type str
186+
category = 0
187+
with self.assertRaises(TypeError):
188+
self.api.get_sources(category=category)
189+
190+
# Raises ValueError if category param is invalid
191+
category = 'x0x'
192+
with self.assertRaises(ValueError):
193+
self.api.get_sources(category=category)

0 commit comments

Comments
 (0)