import json import unittest import inspect import basic from basic.program import Job import basic.constants as B import utils.data_const as D import utils.match_tool import utils.match_const as M import basic.component import test.testtools # the list of TEST_FUNCTIONS defines which function will be really tested. # if you minimize the list you can check the specific test-function TEST_FUNCTIONS = ["test_01matchstart", "test_02hitmanage", "test_03similarity", "test_04bestfit", "test_11compareRow", "test_12compareRows", "test_21match"] # TEST_FUNCTIONS = ["test_getCsvSpec_data"] # with this variable you can switch prints on and off verbose = False tdata = { M.MATCH_SIDE_POSTEXPECT: { "path": "/home/match/postexpect.csv", "data": { "database": { "scheme": { "table": { "_data": [] } } } } }, M.MATCH_SIDE_PREACTUAL: { "path": "/home/match/peract.csv", "data": { "database": { "scheme": { "table": { D.DATA_ATTR_COUNT: 4, D.DATA_ATTR_DATE: "15.05.2022", B.DATA_NODE_HEADER: ["id", "name", "year", "position", "hobby"], B.DATA_NODE_DATA: [ {"id": 1, "name": "Brecht", "year": 2014, "position": "top", "hobby": "meaning"}, {"id": 2, "name": "Meier", "year": 2015, "position": "first", "hobby": "reading" }, {"id": 3, "name": "Smith", "year": 2018, "position": "second", "hobby": "writing"}, {"id": 4, "name": "Smith", "year": 2019, "position": "third", "hobby": "executing"} ] } } } } }, M.MATCH_SIDE_POSTACTUAL: { "path": "/home/match/postact.csv", "data": { "database": { "scheme": { "table": { D.DATA_ATTR_COUNT: 4, D.DATA_ATTR_DATE: "15.05.2022", B.DATA_NODE_HEADER: ["id", "name", "year", "position", "hobby"], B.DATA_NODE_DATA: [ {"id": 1, "name": "Brecht", "year": 2014, "position": "top", "hobby": "meaning"}, {"id": 2, "name": "Maier", "year": 2015, "position": "first", "hobby": "reading"}, {"id": 4, "name": "Smith", "year": 2018, "position": "second", "hobby": "writing"}, {"id": 3, "name": "Smith", "year": 2019, "position": "third", "hobby": "executing"} ] } } } } } } conf = { B.DATA_NODE_DDL: { "database": { "scheme": { "table": { "id": { D.DDL_FNAME: "id", D.DDL_TYPE: "int", D.DDL_ACCEPTANCE: "ignore", D.DDL_KEY: "T:3" }, "name": { D.DDL_FNAME: "name", D.DDL_TYPE: "string", D.DDL_ACCEPTANCE: "must", D.DDL_KEY: M.SIM_BUSINESS+":1" }, "year": { D.DDL_FNAME: "year", D.DDL_TYPE: "int", D.DDL_ACCEPTANCE: "must", D.DDL_KEY: M.SIM_BUSINESS+":2" }, "position": { D.DDL_FNAME: "id", D.DDL_TYPE: "string", D.DDL_ACCEPTANCE: "must", D.DDL_KEY: "" }, "hobby": { D.DDL_FNAME: "id", D.DDL_TYPE: "string", D.DDL_ACCEPTANCE: "must", D.DDL_KEY: "" }, "_header": ["id", "name", "year", "position", "hobby"] } } } } } class MyTestCase(unittest.TestCase): def runTest(self): self.test_matchstart() #self.test_hitmanage() #self.test_similarity() #self.test_bestfit() #self.test_compareRow() #self.test_compareRows() #self.test_match() def test_01matchstart(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, B.PAR_TESTCASE, "TC0001") comp = basic.component.Component() comp.files = { "A": "/home/match/per.csv", "B": "/home/match/post.csv"} comp.conf = conf comp.name = "component" matching = utils.match_tool.Matching(comp) matching.setData(tdata, M.MATCH_SUCCESS) print(matching.htmltext) def test_02hitmanage(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return comp = basic.component.Component() comp.files = { "A": "/home/match/per.csv", "B": "/home/match/post.csv"} comp.name = "component" comp.conf = conf matching = utils.match_tool.Matching(comp) matching.linksA = { "a0001": "null", "a0002": "null", "a0003": "null", "a0004": "null"} matching.linksB = { "b0001": "null", "b0002": "null", "b0003": "null", "b0004": "null"} self.assertEqual(matching.isHitA("a0001"), False, "with value null") self.assertEqual(matching.isHitB("b0005"), False, "doesnt exist") self.assertEqual(("b0005" not in matching.linksB), True, "doesnt exist") matching.setHit("a0001", "b0005") self.assertEqual(matching.isHitA("a0001"), True, "with value null") self.assertEqual(matching.isHitB("b0005"), True, "doesnt exist") self.assertEqual(("b0005" in matching.linksB), True, "doesnt exist") def test_03similarity(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return matching = self.getMatching() utils.match_tool.setMatchkeys(matching, ":database:scheme:table:_data") utils.match_tool.getSimilarity(matching, tdata[M.MATCH_SIDE_PREACTUAL]["data"]["database"]["scheme"]["table"][B.DATA_NODE_DATA][0], tdata[M.MATCH_SIDE_POSTACTUAL]["data"]["database"]["scheme"]["table"][B.DATA_NODE_DATA][0], 1) def test_04bestfit(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, B.PAR_TESTCASE, "TC0001") comp = basic.component.Component() comp.name = "component" comp.files = { "A": "/home/match/per.csv", "B": "/home/match/post.csv"} comp.conf = conf matching = utils.match_tool.Matching(comp) matching.matchtype = M.MATCH_SUCCESS matching.sideA = tdata[M.MATCH_SIDE_PREACTUAL]["data"]["database"]["scheme"]["table"][B.DATA_NODE_DATA] matching.sideB = tdata[M.MATCH_SIDE_POSTACTUAL]["data"]["database"]["scheme"]["table"][B.DATA_NODE_DATA] utils.match_tool.matchBestfit(matching, ":database:scheme:table:_data") print(json.dumps(matching.linksA)) print(json.dumps(matching.linksB)) print(json.dumps(matching.nomatch)) utils.match_tool.matchRestfit(matching) print(json.dumps(matching.linksA)) print(json.dumps(matching.linksB)) print(json.dumps(matching.nomatch)) def test_11compareRow(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, B.PAR_TESTCASE, "TC0001") comp = basic.component.Component() comp.files = { "A": "/home/match/per.csv", "B": "/home/match/post.csv"} comp.name = "component" comp.conf = conf matching = self.getMatching() matching.matchtype = M.MATCH_SUCCESS matching.sideA = tdata[M.MATCH_SIDE_PREACTUAL]["data"]["database"]["scheme"]["table"][B.DATA_NODE_DATA] matching.sideB = tdata[M.MATCH_SIDE_POSTACTUAL]["data"]["database"]["scheme"]["table"][B.DATA_NODE_DATA] ddl = conf[B.DATA_NODE_DDL]["database"]["scheme"]["table"] header = [] for f in ddl["_header"]: header.append({D.DDL_FNAME: f, D.DDL_TYPE: ddl[f][D.DDL_TYPE], D.DDL_ACCEPTANCE: ddl[f][D.DDL_ACCEPTANCE]}) i = 1 text = utils.match_tool.compareRow(matching, header, tdata[M.MATCH_SIDE_PREACTUAL]["data"]["database"]["scheme"]["table"][B.DATA_NODE_DATA][i], tdata[M.MATCH_SIDE_POSTACTUAL]["data"]["database"]["scheme"]["table"][B.DATA_NODE_DATA][i]) print(text) def test_12compareRows(self): global mymsg actfunction = str(inspect.currentframe().f_code.co_name) cnttest = 0 if actfunction not in TEST_FUNCTIONS: return job = test.testtools.getJob() comp = basic.component.Component() comp.files = { "A": "/home/match/pre.csv", "B": "/home/match/post.csv"} comp.name = "component" comp.conf = conf cm = basic.componentHandling.ComponentManager() basic.componentHandling.comps["component"] = comp matching = self.getMatching() comp.name = "component" matching.sideA = tdata[M.MATCH_SIDE_PREACTUAL]["data"]["database"]["scheme"]["table"][B.DATA_NODE_DATA] matching.sideB = tdata[M.MATCH_SIDE_POSTACTUAL]["data"]["database"]["scheme"]["table"][B.DATA_NODE_DATA] linksA = {"a0001": "b0001", "a0002": "b0002" } matching.linksA = linksA text = utils.match_tool.compareRows(matching, ":database:scheme:table:_data") print(text) def test_21match(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, B.PAR_TESTCASE, "TC0001") setattr(job.par, "tctime", "2022-03-25_19-25-32") comp = basic.component.Component() comp.files = {"A": "/home/match/per.csv", "B": "/home/match/post.csv"} comp.name = "component" cm = basic.componentHandling.ComponentManager() cm.comps["component"] = comp # tdata["postReq"] = tdata["preAct"] comp.conf = conf matching = utils.match_tool.Matching(comp) matching.htmltext = "" matching.setData(tdata, M.MATCH_POSTCOND) text = utils.match_tool.matchTree(matching) print("\n-------------\n") print(text) print("\n-------------\n") print(matching.difftext) def getMatching(self): job = test.testtools.getJob() setattr(job.par, B.PAR_TESTCASE, "TC0001") setattr(job.par, "tctime", "2022-03-25_19-25-31") comp = basic.component.Component() comp.files = { "A": "/home/match/per.csv", "B": "/home/match/post.csv"} comp.name = "component" comp.conf = conf matching = utils.match_tool.Matching(comp) matching.setData(tdata, M.MATCH_SUCCESS) matching.difftext = "" return matching if __name__ == '__main__': unittest.main()