Skip to content
Merged
Show file tree
Hide file tree
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
34 changes: 17 additions & 17 deletions newsapi/newsapi_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
if type(q) == str:
payload['q'] = q
else:
raise TypeError('keyword/phrase q param should be a str')
raise TypeError('keyword/phrase q param should be a of type str')

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

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

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

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

# Page Size
if page_size is not None:
if type(page_size) == int:
if page_size >= 0 and page_size <= 100:
if 0 <= page_size <= 100:
payload['pageSize'] = page_size
else:
raise ValueError('page_size param should be an int between 1 and 100')
Expand Down Expand Up @@ -160,21 +160,21 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
if type(q) == str:
payload['q'] = q
else:
raise TypeError('keyword/phrase q param should be a str')
raise TypeError('keyword/phrase q param should be of type str')

# Sources
if sources is not None:
if type(sources) == str:
payload['sources'] = sources
else:
raise TypeError('sources param should be a str')
raise TypeError('sources param should be of type str')

# Domains To Search
if domains is not None:
if type(domains) == str:
payload['domains'] = domains
else:
raise TypeError('domains param should be a string')
raise TypeError('domains param should be of type str')

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

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

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

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

# Page Size
if page_size is not None:
if type(page_size) == int:
if page_size >= 0 and page_size <= 100:
if 0 <= page_size <= 100:
payload['pageSize'] = page_size
else:
raise ValueError('page_size param should be an int between 1 and 100')
Expand Down Expand Up @@ -285,7 +285,7 @@ def get_sources(self, category=None, language=None, country=None):
else:
raise ValueError('invalid language')
else:
raise TypeError('language param should be a string')
raise TypeError('language param should be of type str')

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

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

# Send Request
r = requests.get(const.SOURCES_URL, auth=self.auth, timeout=30, params=payload)
Expand Down
193 changes: 193 additions & 0 deletions tests/test_newsapi_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import unittest
import os
from newsapi.newsapi_client import NewsApiClient


class NewsApiClientTest(unittest.TestCase):

def setUp(self):
key = os.environ.get('news_api_secret')
self.api = NewsApiClient(key)

def test_api_top_headline(self):
# Raise TypeError if Keyword/Phrase param is not of type str
q = 0
with self.assertRaises(TypeError):
self.api.get_top_headlines(q=q)

# Raise ValueError if sources param in not None and country param or category param is not None
sources = 'techcrunch'
country = 'us'
category = 'business'
with self.assertRaises(ValueError):
self.api.get_top_headlines(sources=sources, country=country, category=category)

# Raise TypeError if sources param is not of type str
sources = 0
with self.assertRaises(TypeError):
self.api.get_top_headlines(sources=sources)

# Raise TypeError if language param is not of type str
language = 0
with self.assertRaises(TypeError):
self.api.get_top_headlines(language=language)

# Raise ValueError if language param is invalid
language = 'xx'
with self.assertRaises(ValueError):
self.api.get_top_headlines(language=language)

# Raise TypeError if country param is not of type str
country = 0
with self.assertRaises(TypeError):
self.api.get_top_headlines(country=country)

# Raise ValueError if country param is invalid
country = 'xx'
with self.assertRaises(ValueError):
self.api.get_top_headlines(country=country)

# Raises TypeError if category param is not of type str
category = 0
with self.assertRaises(TypeError):
self.api.get_top_headlines(category=category)

# Raises ValueError if category param is invalid
category = 'x0x'
with self.assertRaises(ValueError):
self.api.get_top_headlines(category=category)

# Raises TypeError if page_size param is not an int
page_size = '1'
with self.assertRaises(TypeError):
self.api.get_top_headlines(page_size=page_size)

# Raises ValueError if page_size param is less than zero(0) or greater than 100
page_size = -1
with self.assertRaises(ValueError):
self.api.get_top_headlines(page_size=page_size)

page_size = 1000
with self.assertRaises(ValueError):
self.api.get_top_headlines(page_size=page_size)

# Raises a TypeError is page param is not an int
page = '1'
with self.assertRaises(TypeError):
self.api.get_top_headlines(page=page)

# Raises a ValueError if page param is less than zero(0)
page = -1
with self.assertRaises(ValueError):
self.api.get_top_headlines(page=page)

def test_api_get_everything(self):
# Raise TypeError if Keyword/Phrase param is None
q = 0
with self.assertRaises(TypeError):
self.api.get_everything(q=q)

# Raise TypeError if sources param is not of type str
sources = 0
with self.assertRaises(TypeError):
self.api.get_everything(sources=sources)

# Raise TypeError is domains param is not of type str
domains = 0
with self.assertRaises(TypeError):
self.api.get_everything(domains=domains)

# Raise TypeError is from_param param is not of type str
from_param = 0
with self.assertRaises(TypeError):
self.api.get_everything(from_param=from_param)

# Raise ValueError if param is not in the format YYYY-MM-DD
from_param = '2016-6-4'
with self.assertRaises(ValueError):
self.api.get_everything(from_param=from_param)

# Raise TypeError if to param is not of type str
to = 1
with self.assertRaises(TypeError):
self.api.get_everything(to=to)

# Raise ValueError if to param is not in the format YYYY-MM-DD
to = '2016-6-24'
with self.assertRaises(ValueError):
self.api.get_everything(to=to)

# Raise TypeError if language param is not of type str
language = 0
with self.assertRaises(TypeError):
self.api.get_everything(language=language)

# Raise ValueError if language param is invalid
language = 'xx'
with self.assertRaises(ValueError):
self.api.get_everything(language=language)

# Raise TypeError is sort_by param is not of type str
sort_by = 1
with self.assertRaises(TypeError):
self.api.get_everything(sort_by=sort_by)

# Raise ValueError if soft_by param is invalid
sort_by = 'sort'
with self.assertRaises(ValueError):
self.api.get_everything(sort_by=sort_by)

# Raises TypeError if page_size param is not an int
page_size = '1'
with self.assertRaises(TypeError):
self.api.get_everything(page_size=page_size)

# Raises ValueError if page_size param is less than zero(0) or greater than 100
page_size = -1
with self.assertRaises(ValueError):
self.api.get_everything(page_size=page_size)

page_size = 1000
with self.assertRaises(ValueError):
self.api.get_everything(page_size=page_size)

# Raises a TypeError is page param is not an int
page = '1'
with self.assertRaises(TypeError):
self.api.get_everything(page=page)

# Raises a ValueError if page param is less than zero(0)
page = -1
with self.assertRaises(ValueError):
self.api.get_everything(page=page)

def test_api_get_sources(self):
# Raise TypeError if language param is not of type str
language = 0
with self.assertRaises(TypeError):
self.api.get_sources(language=language)

# Raise ValueError if language param is invalid
language = 'xx'
with self.assertRaises(ValueError):
self.api.get_sources(language=language)

# Raise TypeError if country param is not of type str
country = 0
with self.assertRaises(TypeError):
self.api.get_sources(country=country)

# Raise ValueError if country param is invalid
country = 'xx'
with self.assertRaises(ValueError):
self.api.get_sources(country=country)

# Raises TypeError if category param is not of type str
category = 0
with self.assertRaises(TypeError):
self.api.get_sources(category=category)

# Raises ValueError if category param is invalid
category = 'x0x'
with self.assertRaises(ValueError):
self.api.get_sources(category=category)