From 6b0b28f978d8b09e7f16fce7e2774e9ac348a3fa Mon Sep 17 00:00:00 2001 From: Garrett Mathers Date: Sat, 6 May 2023 19:00:04 -0500 Subject: [PATCH] Added CSV file option to tester --- testing/RouteTester.py | 31 +++++++++++++++++++++++++++---- testing/test_results.csv | 3 +++ testing/test_results.json | 18 +++++------------- 3 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 testing/test_results.csv diff --git a/testing/RouteTester.py b/testing/RouteTester.py index 928e107..c88e258 100644 --- a/testing/RouteTester.py +++ b/testing/RouteTester.py @@ -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 @@ -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 @@ -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}" @@ -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 @@ -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 diff --git a/testing/test_results.csv b/testing/test_results.csv new file mode 100644 index 0000000..d492789 --- /dev/null +++ b/testing/test_results.csv @@ -0,0 +1,3 @@ +Resource,URI,Method,Status Code,Data +/greenhouses,GET,200,[] +/greenhouses,POST,201,"{'Success': True, 'greenhouse_id': 1}" diff --git a/testing/test_results.json b/testing/test_results.json index aa71bde..9d0111b 100644 --- a/testing/test_results.json +++ b/testing/test_results.json @@ -30,18 +30,10 @@ "method": "GET", "status_code": 200, "data": [ - { - "email": "gmathers@gmail.commm", - "first_name": "n", - "id": 1, - "is_admin": true, - "last_name": "o", - "username": "test2" - }, { "email": "test@testmail.com", "first_name": "test", - "id": 2, + "id": 1, "is_admin": true, "last_name": "name", "username": "tester" @@ -51,7 +43,7 @@ ], "USER": [ { - "uri": "/users/2", + "uri": "/users/1", "method": "PATCH", "status_code": 201, "data": { @@ -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" } @@ -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 } ] }