#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Reporting methods for test outcomes
"""
import pandas as pd
[docs]class Reporter:
"""Class to manage information return
Deals with arrays of tests.
Objects:
* self.results: holding configuration for tests (of type TestConfig)
* self.reportconfigs: holding input objects to be tested (e.g. files)
"""
def __init__(self, configs = {}, tests = {}):
self.results = {}
self.reportconfigs = configs
self.tests = tests
[docs] def create_report(self, tests = {}, configs = {}):
if not self.tests and not tests:
print ("Need tests to create a report, please add!")
if tests:
self.tests = tests
for entry in self.reportconfigs:
repconf = self.reportconfigs[entry]
if repconf["datatype"] == "table":
if not "parameters" in repconf:
repconf["parameters"] = "default"
self.make_table(entry, self.tests, repconf["parameters"])
if "outfilepath" in repconf:
self.write_file(self.results[entry], repconf["outfilepath"], repconf["datatype"])
if repconf["datatype"] == "bool":
if self.return_bool(self.tests):
print ("Test passed")
else:
print ("Test failed")
[docs] def write_file(self, reportobject, outfilename, reporttype):
if reporttype == "table":
reportobject.to_csv(outfilename, index=False)
[docs] def make_table(self, tablename, tests, parameters = "default"):
"""needs a name for the table, test, and parameters which should be retrieved."""
if parameters == "default":
colnames = []
for tname in tests:
if "objectinfo" in tests[tname].config:
for par in tests[tname].config["objectinfo"]:
if not par in colnames:
colnames.append(par)
parameters = colnames + ["outcome"]
table = {}
for entry in parameters:
table.setdefault(entry, [])
if type(parameters) != list and parameters != "default":
print ("Did not get list of parameters for table creation. Will use default.")
parameters = "default"
rowcounter = 0
for testname in tests:
test = tests[testname]
for ent in table:
if "objectinfo" in test.config:
if ent in test.config["objectinfo"]:
table[ent].append(test.config["objectinfo"][ent])
if ent in ["objectfilepath", "objecttype", "objectvalues", "checkconfig", "checkfuntion"]:
table[ent].append(test.config[ent])
if ent in ["outcometype", "valuetype", "values", "outcome"]:
table[ent].append(test.result[ent])
rowcounter += 1
for ent in table:
if len(table[ent]) < rowcounter:
table[ent].append("")
self.results.setdefault(tablename, pd.DataFrame(data=table))
[docs] def return_bool(self, tests ={}):
"""Takes dictionary with tests, checks all for success."""
if not tests:
tests = self.tests
allpassed = True
for testname in tests:
if not self._return_bool_on_test(tests[testname]):
allpassed = False
return allpassed
@staticmethod
def _return_bool_on_test(test):
if test.result["outcometype"] is "bool":
return test.result["outcome"]
else:
return False
@staticmethod
def _return_result_on_test(test):
return test.result["outcome"]