From 159b831e3f46e5487ea04e708bb771b35fd0ece1 Mon Sep 17 00:00:00 2001 From: Tobi Daada Date: Thu, 9 Aug 2018 04:26:10 +0100 Subject: [PATCH 1/3] - Correct Typo - Simplify 'if statement' comparison --- newsapi/newsapi_client.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/newsapi/newsapi_client.py b/newsapi/newsapi_client.py index ef5973d..0607381 100644 --- a/newsapi/newsapi_client.py +++ b/newsapi/newsapi_client.py @@ -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 string') # Sources 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 if type(sources) == str: payload['sources'] = sources else: - raise TypeError('sources param should be a str') + raise TypeError('sources param should be a string') # Language if language is not None: @@ -94,7 +94,7 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c # 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') @@ -227,7 +227,7 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to # 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') From 9e08c5f4e57fa45aaae077f490ecdcc8a9fcbff8 Mon Sep 17 00:00:00 2001 From: Tobi Daada Date: Thu, 9 Aug 2018 04:36:48 +0100 Subject: [PATCH 2/3] - Add Unit Test --- tests/test_newsapi_client.py | 193 +++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 tests/test_newsapi_client.py diff --git a/tests/test_newsapi_client.py b/tests/test_newsapi_client.py new file mode 100644 index 0000000..af36dcd --- /dev/null +++ b/tests/test_newsapi_client.py @@ -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 a string + 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 a string + sources = 0 + with self.assertRaises(TypeError): + self.api.get_top_headlines(sources=sources) + + # Raise TypeError if language param is not a string + 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 a string + 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 a string + 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 a string + sources = 0 + with self.assertRaises(TypeError): + self.api.get_everything(sources=sources) + + # Raise TypeError is domains param is not a string + domains = 0 + with self.assertRaises(TypeError): + self.api.get_everything(domains=domains) + + # Raise TypeError is from_param param is not a string + 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 a string + 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 a string + 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 a string + 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 a string + 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 a string + 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 a string + 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) From 265b8dde1d76ad8d46ba62a53a2fae1dfd54cb4a Mon Sep 17 00:00:00 2001 From: Tobi Daada Date: Tue, 14 Aug 2018 09:46:57 +0100 Subject: [PATCH 3/3] - Change instances of 'string' to 'str' where appropriate, in Comments and Error Messages. --- newsapi/newsapi_client.py | 30 +++++++++++++++--------------- tests/test_newsapi_client.py | 28 ++++++++++++++-------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/newsapi/newsapi_client.py b/newsapi/newsapi_client.py index 0607381..07a20a8 100644 --- a/newsapi/newsapi_client.py +++ b/newsapi/newsapi_client.py @@ -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 string') + 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)): @@ -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 string') + raise TypeError('sources param should be of type str') # Language if language is not None: @@ -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: @@ -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: @@ -89,7 +89,7 @@ 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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -222,7 +222,7 @@ 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: @@ -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: @@ -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: @@ -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) diff --git a/tests/test_newsapi_client.py b/tests/test_newsapi_client.py index af36dcd..f365422 100644 --- a/tests/test_newsapi_client.py +++ b/tests/test_newsapi_client.py @@ -10,7 +10,7 @@ def setUp(self): self.api = NewsApiClient(key) def test_api_top_headline(self): - # Raise TypeError if Keyword/Phrase param is not a string + # Raise TypeError if Keyword/Phrase param is not of type str q = 0 with self.assertRaises(TypeError): self.api.get_top_headlines(q=q) @@ -22,12 +22,12 @@ def test_api_top_headline(self): with self.assertRaises(ValueError): self.api.get_top_headlines(sources=sources, country=country, category=category) - # Raise TypeError if sources param is not a string + # 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 a string + # Raise TypeError if language param is not of type str language = 0 with self.assertRaises(TypeError): self.api.get_top_headlines(language=language) @@ -37,7 +37,7 @@ def test_api_top_headline(self): with self.assertRaises(ValueError): self.api.get_top_headlines(language=language) - # Raise TypeError if country param is not a string + # Raise TypeError if country param is not of type str country = 0 with self.assertRaises(TypeError): self.api.get_top_headlines(country=country) @@ -47,7 +47,7 @@ def test_api_top_headline(self): with self.assertRaises(ValueError): self.api.get_top_headlines(country=country) - # Raises TypeError if category param is not a string + # Raises TypeError if category param is not of type str category = 0 with self.assertRaises(TypeError): self.api.get_top_headlines(category=category) @@ -87,17 +87,17 @@ def test_api_get_everything(self): with self.assertRaises(TypeError): self.api.get_everything(q=q) - # Raise TypeError if sources param is not a string + # 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 a string + # 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 a string + # 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) @@ -107,7 +107,7 @@ def test_api_get_everything(self): with self.assertRaises(ValueError): self.api.get_everything(from_param=from_param) - # Raise TypeError if to param is not a string + # Raise TypeError if to param is not of type str to = 1 with self.assertRaises(TypeError): self.api.get_everything(to=to) @@ -117,7 +117,7 @@ def test_api_get_everything(self): with self.assertRaises(ValueError): self.api.get_everything(to=to) - # Raise TypeError if language param is not a string + # Raise TypeError if language param is not of type str language = 0 with self.assertRaises(TypeError): self.api.get_everything(language=language) @@ -127,7 +127,7 @@ def test_api_get_everything(self): with self.assertRaises(ValueError): self.api.get_everything(language=language) - # Raise TypeError is sort_by param is not a string + # 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) @@ -162,7 +162,7 @@ def test_api_get_everything(self): self.api.get_everything(page=page) def test_api_get_sources(self): - # Raise TypeError if language param is not a string + # Raise TypeError if language param is not of type str language = 0 with self.assertRaises(TypeError): self.api.get_sources(language=language) @@ -172,7 +172,7 @@ def test_api_get_sources(self): with self.assertRaises(ValueError): self.api.get_sources(language=language) - # Raise TypeError if country param is not a string + # Raise TypeError if country param is not of type str country = 0 with self.assertRaises(TypeError): self.api.get_sources(country=country) @@ -182,7 +182,7 @@ def test_api_get_sources(self): with self.assertRaises(ValueError): self.api.get_sources(country=country) - # Raises TypeError if category param is not a string + # Raises TypeError if category param is not of type str category = 0 with self.assertRaises(TypeError): self.api.get_sources(category=category)