Skip to content

Commit

Permalink
Merge branch origin/main into main via pull
Browse files Browse the repository at this point in the history
  • Loading branch information
klpulliam-37 committed May 7, 2023
2 parents 1700c3d + 6b0b28f commit e36937f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
31 changes: 27 additions & 4 deletions testing/RouteTester.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import requests
import json
import re
import csv

test_url = "http://127.0.0.1:5000"
user = "tester"
passw = "testtest"


class RouteTester:
'''
Implements methods used to test the Greenwatch REST API
Expand All @@ -17,6 +17,8 @@ class RouteTester:
init --> pass the 'base_url' aka "IP:port"
as well as passing the username and password.
can be fed in automatically at start of app
will create fake user and also log them in, noting all key
values needed for later use
request --> used as a helper method for test_routes, able to be
given the data and intelligently form the requests
Expand Down Expand Up @@ -56,10 +58,14 @@ def __init__(self, base_url="127.0.0.1:5000", username="test", password="testtes
except KeyError as err:
print(f"Initialization of RouteTester: Error --> {err}")

except requests.exceptions.RequestException as err:
print(f"Request exception thrown, check IP address given; Error -> {err}")



def __del__(self):
'''
logs out the user created in __init__ for the tests and then deletes the user as well
logs out the fake user created in __init__ for the tests and then deletes it as well
'''
logout_url = self.base_url + "/logout"
del_url = self.base_url + f"/users/{self.fake_user_id}"
Expand Down Expand Up @@ -100,7 +106,7 @@ def request(self, uri:str, method:str, data:dict=None) -> dict:
return {"uri": uri, "method": method, "status_code": req.status_code, "data": obj}


def test_routes(self, file:str, dump:bool=True, json_file_save:str=None) -> dict:
def test_routes(self, file:str, dump:bool=True, json_file_save:str=None, csv_file_save:str=None) -> dict:
'''
file : str --> path to the formatted JSON file, examples below
dump : bool --> determines if test_routes dumps results to terminal
Expand Down Expand Up @@ -208,13 +214,30 @@ def test_routes(self, file:str, dump:bool=True, json_file_save:str=None) -> dict
file = open(f"{json_file_save}.json", "w")
json.dump(results, file, indent=2)

if csv_file_save:
file = open(f"{json_file_save}.csv", "w")
writer = csv.writer(file)

fields = ["Resource", "URI", "Method", "Status Code", "Data"]
writer.writerow(fields)

for resource in results:
for method in results[resource]:
try:
writer.writerow([method['uri'], method['method'],
method['status_code'], method['data']])
except KeyError:
writer.writerow([method['uri'], method['method'],
method['status_code'], method['error']])
print(f"Error: ---> {err}")
return results


test_file_name = "routes.json"

test = RouteTester(base_url=test_url, username=user, password=passw)
test_results = test.test_routes(test_file_name, dump=True, json_file_save="test_results")
test_results = test.test_routes(test_file_name, dump=True,
json_file_save="test_results", csv_file_save="test_results")

del test

Expand Down
3 changes: 3 additions & 0 deletions testing/test_results.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Resource,URI,Method,Status Code,Data
/greenhouses,GET,200,[]
/greenhouses,POST,201,"{'Success': True, 'greenhouse_id': 1}"
Expand Down
18 changes: 5 additions & 13 deletions testing/test_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,10 @@
"method": "GET",
"status_code": 200,
"data": [
{
"email": "[email protected]",
"first_name": "n",
"id": 1,
"is_admin": true,
"last_name": "o",
"username": "test2"
},
{
"email": "[email protected]",
"first_name": "test",
"id": 2,
"id": 1,
"is_admin": true,
"last_name": "name",
"username": "tester"
Expand All @@ -51,7 +43,7 @@
],
"USER": [
{
"uri": "/users/2",
"uri": "/users/1",
"method": "PATCH",
"status_code": 201,
"data": {
Expand Down Expand Up @@ -123,7 +115,7 @@
"data": {
"Success": true,
"agent_id": 1,
"private_key": "QSAV2NDJ9LWZC81NRFGURE0B9OSGRPVMEFZENC6OLIVMFBS2CPZTWQ9EEVM2",
"private_key": "YD0G4R9354O7Q3F67C4JKZ1RWPRJ2W8H9O8Q5HJFCY6EFNQXXTGZGAYTUXAO",
"room_id": 1,
"server_ip": "172.22.0.1"
}
Expand Down Expand Up @@ -164,8 +156,8 @@
"body": "Hello, I am another message of testing",
"id": 1,
"room_id": 1,
"timestamp": "2023-04-29T19:20:10.056921",
"user_id": 2
"timestamp": "2023-05-06T18:58:52.028006",
"user_id": 1
}
]
}
Expand Down

0 comments on commit e36937f

Please sign in to comment.