Skip to content

Commit 6c41431

Browse files
committed
Add unit tests for main
1 parent c72cb10 commit 6c41431

File tree

5 files changed

+145
-71
lines changed

5 files changed

+145
-71
lines changed

jokeapi/main.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ def build_request(
2323
for c in category:
2424
if not c.lower() in ["programming", "miscellaneous", "dark"]:
2525
raise Exception(
26-
'Invalid category selected. Available categories are "programming", "miscellaneous", and "dark". Leave blank for any.'
26+
'''Invalid category selected. Available categories are:
27+
"programming"
28+
"miscellaneous"
29+
"dark".
30+
Leave blank for any.'''
2731
)
2832
return
2933
cats = ",".join(category)
@@ -32,21 +36,38 @@ def build_request(
3236

3337
if len(blacklist) > 0:
3438
for b in blacklist:
35-
if not b in ["nsfw", "religious", "political", "racist", "sexist"]:
39+
if b not in [
40+
"nsfw",
41+
"religious",
42+
"political",
43+
"racist",
44+
"sexist"
45+
]:
3646
raise Exception(
37-
'You have blacklisted flags which are not available. Available flags are:\n"racist"\n"religious"\n"political"\n"sexist"\n"nsfw"'
47+
'''\n\n
48+
You have blacklisted flags which are not available.
49+
Available flags are:
50+
"racist"
51+
"religious"
52+
"political"
53+
"sexist"
54+
"nsfw"
55+
'''
3856
)
3957
return
4058
blacklistFlags = ",".join(blacklist)
4159
else:
4260
blacklistFlags = None
4361

4462
if response_format not in ["json", "xml", "yaml"]:
45-
raise Exception("Response format must be either json, xml or yaml.")
63+
raise Exception(
64+
"Response format must be either json, xml or yaml."
65+
)
4666
if type:
4767
if type not in ["single", "twopart"]:
4868
raise Exception(
49-
'Invalid joke type. Available options are "single" or "twopart".'
69+
'''Invalid joke type.
70+
Available options are "single" or "twopart".'''
5071
)
5172
return
5273
else:
@@ -60,17 +81,22 @@ def build_request(
6081
search_string = urllib.parse.quote(search_string)
6182
if id_range:
6283

63-
response = self.http.request('GET', "https://sv443.net/jokeapi/v2/info")
84+
response = self.http.request(
85+
'GET',
86+
"https://sv443.net/jokeapi/v2/info"
87+
)
6488
dict = json.loads(response.data)
6589
range_limit = dict["jokes"]["totalCount"]
6690

6791
if len(id_range) > 2:
6892
raise Exception("id_range must be no longer than 2 items.")
6993
elif id_range[0] < 0:
70-
raise Exception("id_range[0] must be greater than or equal to 0.")
94+
raise Exception(
95+
"id_range[0] must be greater than or equal to 0."
96+
)
7197
elif id_range[1] > range_limit:
7298
raise Exception(
73-
f"id_range[1] must be less than or equal to {range_limit-1}."
99+
f"id_range[1] must be less than or equal to {range_limit-1}."
74100
)
75101

76102
r += cats

requirements.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
attrs==19.3.0
2+
coverage==5.0.3
3+
entrypoints==0.3
4+
flake8==3.7.9
5+
mccabe==0.6.1
6+
more-itertools==8.2.0
7+
packaging==20.1
8+
pluggy==0.13.1
9+
py==1.8.1
10+
pycodestyle==2.5.0
11+
pyflakes==2.1.1
12+
pyparsing==2.4.6
13+
pytest==5.3.5
14+
pytest-cov==2.8.1
15+
six==1.14.0
16+
wcwidth==0.1.8
17+
urllib3==1.25.8

setup.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
from distutils.core import setup
22
setup(
3-
name = 'jokeapi',
4-
packages = ['jokeapi'], # Chose the same as "name"
5-
version = '0.1',
3+
name='jokeapi',
4+
packages=['jokeapi'], # Chose the same as "name"
5+
version='0.1',
66
license='GNU General Public License v3 (GPLv3)',
7-
description = 'An API Wrapper for Sv443\'s JokeAPI',
8-
author = 'thenamesweretakenalready',
9-
author_email = 'leet_haker@cyber-wizard.com',
10-
url = 'https://github.com/thenamesweretakenalready/Sv443s-JokeAPI-Python-Wrapper',
11-
download_url = 'https://github.com/user/reponame/archive/v0.1.tar.gz',
12-
keywords = ['api wrapper', 'wrapper', 'api', 'jokes'],
7+
description='An API Wrapper for Sv443\'s JokeAPI',
8+
author='thenamesweretakenalready',
9+
author_email='leet_haker@cyber-wizard.com',
10+
url="""https://github.com/
11+
thenamesweretakenalready/Sv443s-JokeAPI-Python-Wrapper""",
12+
download_url='https://github.com/user/reponame/archive/v0.1.tar.gz',
13+
keywords=['api wrapper', 'wrapper', 'api', 'jokes'],
1314
install_requires=[],
1415
classifiers=[
1516
'Development Status :: 3 - Alpha',

test_main.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from jokeapi import Jokes
2+
3+
j = Jokes()
4+
errors = []
5+
6+
try:
7+
j.get_joke()
8+
except Exception as e:
9+
errors.append({'Error in': 'blank joke get', 'Error': e})
10+
11+
"""Testing for errors in categories"""
12+
try:
13+
j.get_joke(category=["programming"])
14+
except Exception as e:
15+
errors.append({'Error in': 'category programming', 'Error': e})
16+
try:
17+
j.get_joke(category=["miscellaneous"])
18+
except Exception as e:
19+
errors.append({'Error in': 'category miscellaneous', 'Error': e})
20+
try:
21+
j.get_joke(category=["dark"])
22+
except Exception as e:
23+
errors.append({'Error in': 'category dark', 'Error': e})
24+
25+
"""Testing for errors in blacklist"""
26+
try:
27+
j.get_joke(blacklist=["nsfw"])
28+
except Exception as e:
29+
errors.append({'Error in': 'blacklist nsfw', 'Error': e})
30+
try:
31+
j.get_joke(blacklist=["religious"])
32+
except Exception as e:
33+
errors.append({'Error in': 'blacklist religious', 'Error': e})
34+
try:
35+
j.get_joke(blacklist=["political"])
36+
except Exception as e:
37+
errors.append({'Error in': 'blacklist political', 'Error': e})
38+
try:
39+
j.get_joke(blacklist=["racist"])
40+
except Exception as e:
41+
errors.append({'Error in': 'blacklist political', 'Error': e})
42+
try:
43+
j.get_joke(blacklist=["sexist"])
44+
except Exception as e:
45+
errors.append({'Error in': 'blacklist sexist', 'Error': e})
46+
47+
"""Testing for errors in response_format"""
48+
try:
49+
j.get_joke(response_format="xml")
50+
except Exception as e:
51+
errors.append({'Error in': 'response_format xml', 'Error': e})
52+
try:
53+
j.get_joke(response_format="yaml")
54+
except Exception as e:
55+
errors.append({'Error in': 'response_format yaml', 'Error': e})
56+
57+
"""Testing for errors in type"""
58+
try:
59+
j.get_joke(type="single")
60+
except Exception as e:
61+
errors.append({'Error in': 'type single', 'Error': e})
62+
try:
63+
j.get_joke(type="twopart")
64+
except Exception as e:
65+
errors.append({'Error in': 'type double', 'Error': e})
66+
67+
"""Testing for errors in search_string"""
68+
try:
69+
j.get_joke(search_string="search")
70+
# as long as this gets a response, the api wrapper is fine;
71+
# it probably doesn't exist in a joke.
72+
except Exception as e:
73+
errors.append({'Error in': 'search_string', 'Error': e})
74+
75+
"""Testing for errors in id_range"""
76+
try:
77+
j.get_joke(id_range=[30, 151])
78+
except Exception as e:
79+
errors.append({'Error in': 'id_range', 'Error': e})
80+
81+
if len(errors):
82+
for e in errors:
83+
print(f"Error in: {e['Error in']}\nError: {e['Error']}")
84+
raise Exception("Errors boii")

version_testing.py

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)