I'd like some constructive feedback on the code that I have written to test the API for smartsh33t.com I wrote it in Python.
I wrote the program to take .json files and read them into the testrunner. then invoke the companies API for the create sheet functions, then record the results.
I am interested in general structural/design/architecture improvements and other feedback to improve future work.
#! usr/bin/env python3
import json
import logging
import smartsheet
import smartsheet.exceptions
import smartsheet.models
import access_token
import urllib.error
# logger setup
logging.basicConfig(filename="err_log.txt", level=logging.ERROR, format=' %(asctime)s - %(levelname)s - %(message)s')
open("err_log.txt", "w")
# Initialize client, actual access token kept in local file only.
smartsheet_client = smartsheet.Smartsheet(access_token.token)
# Make sure we don't miss any errors
smartsheet_client.errors_as_exceptions(True)
class TestRunner:
passed: int
failed: int
test_count: int
# initial test setup
def __init__(self):
self.passed = 0
self.failed = 0
self.test_count = 0
# sheet 0 happy path test while sheets 1 & 2 throw errors
self.tests = ["Testrc/sheet0.json", "Testrc/sheet1.json", "Testrc/sheet2.json"]
self.json_files_to_test(self.tests)
# function to iterate through the files.
def json_files_to_test(self, json_tests):
for test in json_tests:
self.prepare_json_file(test)
# printing the pass/fail report for the tests.
print('\n\nTest Report')
print('====================================================')
print('tests passed = ' + str(self.passed))
print('tests failed = ' + str(self.failed))
print('percent of tests passing = ' + str(100*(self.passed/self.test_count)) + "%")
print('total number of tests = ' + str(self.test_count))
print('====================================================')
# load json files to be tested and then the tests to be run.
def prepare_json_file(self, test):
with open(test, "r+") as read_file:
read_json: object = json.load(read_file)
spec_sheet = smartsheet.models.Sheet(read_json)
self.create_test(smartsheet_client, spec_sheet)
self.create_in_folder_test(smartsheet_client, spec_sheet)
self.create_in_workspace_test(smartsheet_client, spec_sheet)
# sends a minimal sheet to be created in the Default folder(Home)
def create_test(self, ss_client, spec_sheet):
self.test_count += 1
try:
response = ss_client.Home.create_sheet(spec_sheet)
self.record_success(response)
except (smartsheet.exceptions.ApiError, urllib.error.HTTPError) as e:
logging.exception(e)
self.handle_exception(e, spec_sheet)
# sends a minimal sheet to be created in a folder by id
def create_in_folder_test(self, ss_client, spec_sheet):
self.test_count += 1
# found id via curl -X GET -H "Authorization: Bearer ttctt6u346l1wya2oxc0g80g2k"
# "https://api.smartsheet.com/2.0/home/folders"
folder_id = 2843491320522628
try:
response = ss_client.Folders.create_sheet_in_folder(folder_id, spec_sheet)
self.record_success(response)
except (smartsheet.exceptions.ApiError, urllib.error.HTTPError) as e:
logging.exception(e)
self.handle_exception(e, spec_sheet)
# sends a minimal sheet to be created in a workspace by id
def create_in_workspace_test(self, ss_client, spec_sheet):
self.test_count += 1
# found id via curl -X GET -H "Authorization: Bearer vpj1fg3b92qlorn6yibodw2w0u"
# "https://ai.smartsheet.com/2.0/workspaces"
workspace_id = 7730152571463556
try:
response = ss_client.Workspaces.create_sheet_in_workspace(workspace_id, spec_sheet)
self.record_success(response)
except (smartsheet.exceptions.ApiError, urllib.error.HTTPError) as e:
logging.exception(e)
self.handle_exception(e, spec_sheet)
def record_success(self, response):
parsed_json = json.loads(str(response))
print(parsed_json["result"]["name"] + " passed in the default instance")
self.passed += 1
def handle_exception(self, e, spec_sheet):
parsed_spec_sheet = json.loads(str(spec_sheet))
parsed_err = json.loads(str(e))
print(parsed_spec_sheet["name"] + "\nfailure = " + parsed_err["result"]["name"] + " " + str(
parsed_err["result"]["errorCode"]))
print(json.dumps(parsed_err, sort_keys=True, indent=4))
self.failed += 1
def print_test_results(self):
print('\n\n Test Report')
print('====================================================')
print('tests succeed = ' + str(self.passed))
print("tests failed = " + str(self.failed))
print('percent of tests passing = ' + str(100 * (self.passed / self.test_count)) + "%")
print('total number of tests = ' + str(self.test_count))
print('====================================================')
if __name__ == "__main__":
TestRunner()