|
1 | 1 | from os import getenv
|
2 |
| -from jokeapi import Jokes |
3 | 2 | from dotenv import load_dotenv
|
| 3 | +import sys, os |
| 4 | + |
| 5 | +sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, "src")) |
| 6 | +from jokeapi import Jokes |
4 | 7 |
|
5 | 8 | load_dotenv()
|
6 | 9 |
|
|
9 | 12 | token = getenv("token")
|
10 | 13 |
|
11 | 14 |
|
12 |
| -try: |
13 |
| - j.get_joke() |
14 |
| -except Exception as e: |
15 |
| - errors.append({"Error in": "blank joke get", "Error": e}) |
16 |
| - |
17 |
| -"""Testing auth tokens""" |
18 |
| -try: |
19 |
| - j.get_joke(auth_token=token) |
20 |
| -except Exception as e: |
21 |
| - auth_token = None |
22 |
| - errors.append({"Error in": "auth usage", "Error": e}) |
23 |
| - |
24 |
| -"""Testing for errors in categories""" |
25 |
| -try: |
26 |
| - j.get_joke(category=["programming"], auth_token=token) |
27 |
| -except Exception as e: |
28 |
| - errors.append({"Error in": "category programming", "Error": e}) |
29 |
| -try: |
30 |
| - j.get_joke(category=["misc"], auth_token=token) |
31 |
| -except Exception as e: |
32 |
| - errors.append({"Error in": "category miscellaneous", "Error": e}) |
33 |
| -try: |
34 |
| - j.get_joke(category=["dark"], auth_token=token) |
35 |
| -except Exception as e: |
36 |
| - errors.append({"Error in": "category dark", "Error": e}) |
37 |
| - |
38 |
| -"""Testing for errors in blacklist""" |
39 |
| -try: |
40 |
| - j.get_joke(blacklist=["nsfw"], auth_token=token) |
41 |
| -except Exception as e: |
42 |
| - errors.append({"Error in": "blacklist nsfw", "Error": e}) |
43 |
| - |
44 |
| -try: |
45 |
| - j.get_joke(blacklist=["religious"], auth_token=token) |
46 |
| -except Exception as e: |
47 |
| - errors.append({"Error in": "blacklist religious", "Error": e}) |
48 |
| - |
49 |
| -try: |
50 |
| - j.get_joke(blacklist=["political"], auth_token=token) |
51 |
| -except Exception as e: |
52 |
| - errors.append({"Error in": "blacklist political", "Error": e}) |
53 |
| - |
54 |
| -try: |
55 |
| - j.get_joke(blacklist=["racist"], auth_token=token) |
56 |
| -except Exception as e: |
57 |
| - errors.append({"Error in": "blacklist political", "Error": e}) |
58 |
| - |
59 |
| -try: |
60 |
| - j.get_joke(blacklist=["sexist"], auth_token=token) |
61 |
| -except Exception as e: |
62 |
| - errors.append({"Error in": "blacklist sexist", "Error": e}) |
63 |
| - |
64 |
| - |
65 |
| -"""Testing for errors in response_format""" |
66 |
| -try: |
67 |
| - j.get_joke(response_format="xml", auth_token=token) |
68 |
| -except Exception as e: |
69 |
| - errors.append({"Error in": "response_format xml", "Error": e}) |
70 |
| - |
71 |
| -try: |
72 |
| - j.get_joke(response_format="yaml", auth_token=token) |
73 |
| -except Exception as e: |
74 |
| - errors.append({"Error in": "response_format yaml", "Error": e}) |
75 |
| - |
76 |
| - |
77 |
| -"""Testing for errors in type""" |
78 |
| -try: |
79 |
| - j.get_joke(joke_type="single", auth_token=token) |
80 |
| -except Exception as e: |
81 |
| - errors.append({"Error in": "type single", "Error": e}) |
82 |
| - |
83 |
| -try: |
84 |
| - j.get_joke(joke_type="twopart", auth_token=token) |
85 |
| -except Exception as e: |
86 |
| - errors.append({"Error in": "type double", "Error": e}) |
87 |
| - |
88 |
| - |
89 |
| -"""Testing for errors in search_string""" |
90 |
| -try: |
91 |
| - j.get_joke(search_string="search", auth_token=token) |
92 |
| - # as long as this gets a response, the api wrapper is fine; |
93 |
| - # it probably doesn't exist in a joke. |
94 |
| -except Exception as e: |
95 |
| - errors.append({"Error in": "search_string", "Error": e}) |
96 |
| - |
97 |
| - |
98 |
| -"""Testing for errors in id_range""" |
99 |
| -try: |
100 |
| - j.get_joke(id_range=[30, 151], auth_token=token) |
101 |
| -except Exception as e: |
102 |
| - errors.append({"Error in": "id_range", "Error": e}) |
103 |
| - |
104 |
| - |
105 |
| -if len(errors): |
106 |
| - for e in errors: |
107 |
| - print(f"Error in: {e['Error in']}\nError: {e['Error']}") |
108 |
| - if len(errors) == 1: |
109 |
| - raise Exception("1 error occured") |
110 |
| - |
111 |
| - raise Exception(f"{len(errors)} errors occurred") |
| 15 | +def test_main(): |
| 16 | + try: |
| 17 | + j.get_joke() |
| 18 | + except Exception as e: |
| 19 | + errors.append({"Error in": "blank joke get", "Error": e}) |
| 20 | + |
| 21 | + """Testing auth tokens""" |
| 22 | + try: |
| 23 | + j.get_joke(auth_token=token) |
| 24 | + except Exception as e: |
| 25 | + auth_token = None |
| 26 | + errors.append({"Error in": "auth usage", "Error": e}) |
| 27 | + |
| 28 | + """Testing for errors in categories""" |
| 29 | + try: |
| 30 | + j.get_joke(category=["programming"], auth_token=token) |
| 31 | + except Exception as e: |
| 32 | + errors.append({"Error in": "category programming", "Error": e}) |
| 33 | + try: |
| 34 | + j.get_joke(category=["misc"], auth_token=token) |
| 35 | + except Exception as e: |
| 36 | + errors.append({"Error in": "category miscellaneous", "Error": e}) |
| 37 | + try: |
| 38 | + j.get_joke(category=["dark"], auth_token=token) |
| 39 | + except Exception as e: |
| 40 | + errors.append({"Error in": "category dark", "Error": e}) |
| 41 | + |
| 42 | + """Testing for errors in blacklist""" |
| 43 | + try: |
| 44 | + j.get_joke(blacklist=["nsfw"], auth_token=token) |
| 45 | + except Exception as e: |
| 46 | + errors.append({"Error in": "blacklist nsfw", "Error": e}) |
| 47 | + |
| 48 | + try: |
| 49 | + j.get_joke(blacklist=["religious"], auth_token=token) |
| 50 | + except Exception as e: |
| 51 | + errors.append({"Error in": "blacklist religious", "Error": e}) |
| 52 | + |
| 53 | + try: |
| 54 | + j.get_joke(blacklist=["political"], auth_token=token) |
| 55 | + except Exception as e: |
| 56 | + errors.append({"Error in": "blacklist political", "Error": e}) |
| 57 | + |
| 58 | + try: |
| 59 | + j.get_joke(blacklist=["racist"], auth_token=token) |
| 60 | + except Exception as e: |
| 61 | + errors.append({"Error in": "blacklist political", "Error": e}) |
| 62 | + |
| 63 | + try: |
| 64 | + j.get_joke(blacklist=["sexist"], auth_token=token) |
| 65 | + except Exception as e: |
| 66 | + errors.append({"Error in": "blacklist sexist", "Error": e}) |
| 67 | + |
| 68 | + """Testing for errors in response_format""" |
| 69 | + try: |
| 70 | + j.get_joke(response_format="xml", auth_token=token) |
| 71 | + except Exception as e: |
| 72 | + errors.append({"Error in": "response_format xml", "Error": e}) |
| 73 | + |
| 74 | + try: |
| 75 | + j.get_joke(response_format="yaml", auth_token=token) |
| 76 | + except Exception as e: |
| 77 | + errors.append({"Error in": "response_format yaml", "Error": e}) |
| 78 | + |
| 79 | + """Testing for errors in type""" |
| 80 | + try: |
| 81 | + j.get_joke(joke_type="single", auth_token=token) |
| 82 | + except Exception as e: |
| 83 | + errors.append({"Error in": "type single", "Error": e}) |
| 84 | + |
| 85 | + try: |
| 86 | + j.get_joke(joke_type="twopart", auth_token=token) |
| 87 | + except Exception as e: |
| 88 | + errors.append({"Error in": "type double", "Error": e}) |
| 89 | + |
| 90 | + """Testing for errors in search_string""" |
| 91 | + try: |
| 92 | + j.get_joke(search_string="search", auth_token=token) |
| 93 | + # as long as this gets a response, the api wrapper is fine; |
| 94 | + # it probably doesn't exist in a joke. |
| 95 | + except Exception as e: |
| 96 | + errors.append({"Error in": "search_string", "Error": e}) |
| 97 | + |
| 98 | + """Testing for errors in id_range""" |
| 99 | + try: |
| 100 | + j.get_joke(id_range=[30, 151], auth_token=token) |
| 101 | + except Exception as e: |
| 102 | + errors.append({"Error in": "id_range", "Error": e}) |
| 103 | + |
| 104 | + if len(errors): |
| 105 | + for e in errors: |
| 106 | + print(f"Error in: {e['Error in']}\nError: {e['Error']}") |
| 107 | + if len(errors) == 1: |
| 108 | + raise Exception("1 error occured") |
| 109 | + |
| 110 | + raise Exception(f"{len(errors)} errors occurred") |
0 commit comments