Commit 7d4d1e92 authored by axiiom's avatar axiiom

added unit test framework and some simple tests

parent 2f788e79
class Code():
def __init__(self, code, msg):
self.code = code
self.msg = msg
def __str__(self):
return str(self.code) + " " + self.msg
# success
SUCCESS = Code(200, "OK")
CREATED = Code(201, "Created")
# redirection
MOVED = Code(301, "Moved Permanently")
REDIRECT = Code(308, "Permanent Redirect")
# client errors
BAD_REQUEST = Code(400, "Bad Request")
UNAUTHORIZED = Code(401, "Unauthorized")
FORBIDDEN = Code(403, "Forbidden")
NOT_FOUND = Code(404, "Not Found")
INVALID_METHOD = Code(405, "Method Not Allowed")
# server errors
SERVER_ERR = Code(500, "Internal Server Error")
codes = {
200: SUCCESS,
201: CREATED,
301: MOVED,
308: REDIRECT,
400: BAD_REQUEST,
401: UNAUTHORIZED,
403: FORBIDDEN,
404: NOT_FOUND,
405: INVALID_METHOD,
500: SERVER_ERR
}
{
"attributes": {
"radius": {
"value": 2
}
},
"category": 2,
"children": [
{
"attributes": {
"bottle_filler": {
"value": true
},
"coldness": {
"average_rating": 0.5,
"num_reviews": 32
}
},
"category": 1,
"id": 2,
"lat": 38.829791,
"lon": -77.307043,
"name": null,
"parent": 1
}
],
"id": 1,
"lat": 38,
"lon": -77,
"name": "Johnson Center",
"parent": null
}
import requests
import json
import os
from colorama import init, Fore, Style
from termcolor import colored
from codes import *
# initialize terminal colors
init()
def get_status(expected, actual):
'''
Returns whether or not the test passed
:param expected (tuple) : tuple containing expected value(s)
:param actual (tuple) : tuple containing actual value(s) returned by the test
:returns: success of the test
'''
success = True
msg = ""
for e, a in zip(expected, actual):
if e != a:
success = False
msg += colored(f"Expected: {e}\nActual: {a}\n", "red")
status = colored("failed", "red") if not success else \
colored("success", "green")
print(f"Status: {status}")
if not success:
# expected = [str(e) for e in expected]
# actual = [str(a) for a in actual]
# print(colored(f"Test: {expected}\nActual: {actual}\n", "red"))
print(msg)
print("\n")
return success
def print_test(test_num, endpoint):
'''
Prints the test number and endpoint
:param: test_num (int): test number
:param: endpoint (string): endpoint that is being tested
'''
print(f"----- Test {test_num} -----")
print(f"Endpoint: " + colored(endpoint, "yellow"))
def setup(test_num):
'''
Tests the endpoint /test_data with valid input
'''
endpoint = "/test_data"
print_test(test_num, endpoint)
print("Method: " + colored("GET", "yellow"))
data = requests.get(f"http://localhost:5000/{endpoint}")
expected = [SUCCESS, REDIRECT]
actual = [ codes[data.status_code], codes[data.history[0].status_code] ]
return get_status(expected, actual)
def setup2(test_num):
'''
Tests the endpoint /test_data with an invalid method
'''
endpoint = "test_data"
print_test(test_num, f"/{endpoint}")
print("Method: " + colored("POST", "yellow"))
data = requests.post(f"http://localhost:5000/{endpoint}")
expected = [INVALID_METHOD]
actual = [codes[data.status_code]]
return get_status(expected, actual)
def valid_point(test_num):
'''
Test that getting the data for point 1 is proper
'''
endpoint = "point/1"
print_test(test_num, f"/{endpoint}")
print("Method: " + colored("GET", "yellow"))
data = requests.get(f"http://localhost:5000/{endpoint}")
try:
js_path = os.path.join(os.path.dirname(__file__), "data", "point_1.json")
actual = [codes[data.status_code],
list(data.json().keys())]
expected = [SUCCESS,
list(json.loads(open(js_path).read()).keys())]
return get_status(expected, actual)
except Exception as e:
print(e)
return False
def invalid_point(test_num):
'''
Test that sending a string to the "point" endpoint returns a 404
'''
endpoint = "point/adba"
print_test(test_num, f"/{endpoint}")
print("Method: " + colored("GET", "yellow"))
data = requests.get(f"http://localhost:5000/{endpoint}")
try:
actual = [codes[data.status_code]]
expected = [NOT_FOUND]
return get_status(expected, actual)
except Exception as e:
print(e)
return False
num_errors = 0
num_errors += 0 if setup(1) else 1
num_errors += 0 if setup2(2) else 1
num_errors += 0 if valid_point(2) else 1
num_errors += 0 if invalid_point(3) else 1
print(f"Errors: {num_errors}")
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment