import unittest
import inspect
import test.testtools
import basic.program
import basic.component
from basic.componentHandling import ComponentManager
import test.constants
import utils.report_tool
import utils.match_tool
import utils.match_const as M
import basic.constants as B
HOME_PATH = test.constants.HOME_PATH
DATA_PATH = test.constants.DATA_PATH
TEST_FUNCTIONS = ["test_20cssClass", "test_21title", "test_22overview", "test_23filename",
                  "test_24headlines", "test_30reportS"]
TEST_FUNCTIONS = ["test_21title"]
class MyTestCase(unittest.TestCase):
    mymsg = "--------------------------------------------------------------"
    def getReport(self, job):
        # job = test.testtools.getJob()
        report = utils.report_tool.Report(job)
        archiv = job.conf[B.SUBJECT_PATH][B.ATTR_PATH_ARCHIV]+"/"
        i = 0
        for m in M.MATCH_TYPES:
            report.setPaths("TC0001", "comp01", "arte01", m, archiv+"path0111"+str(i), archiv+"path0111"+str(i)+"02")
            report.setPaths("TC0001", "comp01", "arte02", m, archiv+"path0112"+str(i)+"", archiv+"path0112"+str(i)+"02")
            report.setPaths("TC0001", "comp02", "arte01", m, archiv+"path0121"+str(i)+"", archiv+"path0121"+str(i)+"02")
            report.setPaths("TC0002", "comp01", "arte01", m, archiv+"path0211"+str(i)+"", archiv+"path0211"+str(i)+"02")
            report.setPaths("TC0002", "comp02", "arte01", m, archiv+"path0221"+str(i)+"", archiv+"path0221"+str(i)+"02")
            report.setMatchResult("TC0001", "comp01", "arte01", m, "result" + str(i), "
")
            report.setMatchResult("TC0001", "comp01", "arte02", m, "result" + str(i), "")
            report.setMatchResult("TC0001", "comp02", "arte01", m, "result" + str(1), "")
            report.setMatchResult("TC0002", "comp01", "arte01", m, "result" + str(1), "")
            report.setMatchResult("TC0002", "comp02", "arte01", m, "result" + str(i), "")
            i += 1
            if i > 4:
                i = 0
        return report
    def test_20cssClass(self):
        global mymsg
        actfunction = str(inspect.currentframe().f_code.co_name)
        cnttest = 0
        if actfunction not in TEST_FUNCTIONS:
            return
        job = test.testtools.getJob()
        #job = basic.program.Job("unit")
        #args = { "application": "TEST", "environment": "ENV01", "modus": "unit", "tstime": "2022-03-19_12-09-09",
        #         "tsdir": '/home/ulrich/6_Projekte/Programme/datest/test/conf/lauf/testlauf/TST001_2022-03-19_12-09-09',
        #         "step": 2 }
        ##         "usecase": "TST001", "tstime": "2022-03-17_17-28"}
        #job.par.setParameterArgs(args)
        #job.setProgram("test_executer")
        job = test.testtools.getJob()
        report = self.getReport(job)
        i = 0
        for m in M.MATCH_TYPES:
            cssClass = report.getCssClass("TC0001", "comp01", "arte01", m)
            print(m + " test0111 " + cssClass)
            self.assertEqual(cssClass, "result"+str(i))
            cnttest += 1
            i += 1
            if i > 4:
                i = 0
            cssClass = report.getCssClass("TC0002", "comp01", "arte01", m)
            print(m + " test0121 " + cssClass)
        cssClass = report.getCssClass("TC0001", "comp01", "arte02")
        self.assertEqual(cssClass, "result4")
        print("test0112 "+cssClass)
        cssClass = report.getCssClass("TC0001", "comp02", "arte01")
        print("test0121 "+cssClass)
        self.assertEqual(cssClass, "result1")
        cssClass = report.getCssClass("TC0002", "comp01")
        self.assertEqual(cssClass, "result1")
        cssClass = report.getCssClass("TC0002")
        self.assertEqual(cssClass, "result4")
        cnttest += 4
        MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest)
    def test_21title(self):
        global mymsg
        actfunction = str(inspect.currentframe().f_code.co_name)
        cnttest = 0
        if actfunction not in TEST_FUNCTIONS:
            return
        job = test.testtools.getJob()
        print(" ---------- test_title")
        setattr(job.par, B.PAR_TESTSUITE, "TST001")
        report = self.getReport(job)
        html = report.getTitle("TC0001", "comp01", "arte01", M.MATCH_POSTCOND)
        print(html)
        html = report.getTitle("TC0001")
        self.assertEqual((utils.report_tool.REP_TITLE in html), True)
        self.assertEqual((utils.report_tool.REP_TC in html), True)
        self.assertEqual(("TC0001" in html), True)
        cnttest += 3
        setattr(job.par, B.PAR_TESTSUITE, "TST001")
        html = report.getTitle()
        self.assertEqual((utils.report_tool.REP_TITLE in html), True)
        self.assertEqual((utils.report_tool.REP_TS in html), True)
        self.assertEqual(("TST001" in html), True)
        cnttest += 3
        MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest)
    def test_22overview(self):
        global mymsg
        actfunction = str(inspect.currentframe().f_code.co_name)
        cnttest = 0
        if actfunction not in TEST_FUNCTIONS:
            return
        job = test.testtools.getJob()
        print(" ---------- test_overview")
        report = self.getReport(job)
        html = report.getOverview("TC0001")
        print(html)
        MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest)
    def test_23filename(self):
        global mymsg
        actfunction = str(inspect.currentframe().f_code.co_name)
        cnttest = 0
        if actfunction not in TEST_FUNCTIONS:
            return
        #job = basic.program.Job.getInstance()
        job = test.testtools.getJob()
        setattr(job.par, "testcase", "TC0001")
        setattr(job.par, "tctime", "2022-03-23_21-23-32")
        print(" ---------- test_filename")
        cm = basic.componentHandling.ComponentManager.getInstance(job)
        for c in ["comp02"]:
            comp = basic.component.Component()
            comp.conf = {}
            comp.name = c
            cm.comps[c] = comp
        report = self.getReport(job)
        html = report.getFilepath("TC0001", "comp02", "arte01", M.MATCH_POSTCOND)
        print(html)
        MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest)
    def test_24headlines(self):
        global mymsg
        actfunction = str(inspect.currentframe().f_code.co_name)
        cnttest = 0
        if actfunction not in TEST_FUNCTIONS:
            return
        job = test.testtools.getJob()
        setattr(job.par, "testcase", "TC0001")
        setattr(job.par, "tctime", "2022-03-23_21-23-32")
        cm = basic.componentHandling.ComponentManager.getInstance(job)
        for c in ["comp02"]:
            comp = basic.component.Component()
            comp.conf = {}
            comp.name = c
            cm.comps[c] = comp
        print(" ---------- test_headlines")
        report = self.getReport(job)
        html = report.getTestcaseHead("TC0001")
        print(html)
        html = report.getComponentHead("TC0001", "comp02")
        print(html)
        html = report.getArtefactBlock("TC0001", "comp02", "arte01")
        print(html)
        MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest)
    def test_30reportS(self):
        global mymsg
        actfunction = str(inspect.currentframe().f_code.co_name)
        cnttest = 0
        if actfunction not in TEST_FUNCTIONS:
            return
        job = test.testtools.getJob()
        setattr(job.par, "testcase", "TC0001")
        setattr(job.par, "testcases", ["TC0001", "TC0002"])
        setattr(job.par, "tctime", "2022-03-23_21-23-32")
        print(" ---------- reportTestcase")
        report = self.getReport(job)
        cm = basic.componentHandling.ComponentManager.getInstance(job)
        for compname in ["comp01", "comp02"]:
            conf = {}
            comp = basic.component.Component()
            comp.files = { "A": "/home/match/pre.csv", "B": "/home/match/post.csv"}
            comp.name = compname
            comp.conf = conf
            cm.comps[compname] = comp
        html_1 = report.reportTestcase("TC0001")
        print(html_1)
        print("<<---------------------------------- TC0001")
        report.extractTestcase("TC0001", html_1)
        setattr(job.par, "testcase", "TC0002")
        html_2 = report.reportTestcase("TC0002")
        print(html_2)
        print("<<---------------------------------- TC0002")
        report.extractTestcase("TC0002", html_2)
        # setattr(job.par, B.PAR_TESTCASES, ["TST001"])
        setattr(job.par, B.PAR_TESTCASES, ["TC0001", "TC0002"])
        #html = report.reportTestsuite()
        #print(html)
        MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest)
        print("<<---------------------------------- TST001")
    def test_zzz(self):
        print(MyTestCase.mymsg)
if __name__ == '__main__':
    unittest.main()
#            report.setPaths("TC0001", "comp01", "arte01", m, archiv+"path0111"+str(i), archiv+"path0111"+str(i)+"02")