import json import unittest import basic from basic.program import Job import utils.match_tool import utils.match_const as M import components.component 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": { "_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": { "_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 = { "ddl": { "database": { "scheme": { "table": { "id": { "feld": "id", "type": "int", "acceptance": "ignore", "key": "T:3" }, "name": { "feld": "name", "type": "string", "acceptance": "must", "key": "F:1" }, "year": { "feld": "year", "type": "int", "acceptance": "must", "key": "F:2" }, "position": { "feld": "id", "type": "string", "acceptance": "must", "key": "" }, "hobby": { "feld": "id", "type": "string", "acceptance": "must", "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_matchstart(self): job = Job("unit") setattr(job.par, B.PAR_TESTCASE, "TC0001") comp = components.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_hitmanage(self): comp = components.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 xtest_similarity(self): 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"]["_data"][0], tdata[M.MATCH_SIDE_POSTACTUAL]["data"]["database"]["scheme"]["table"]["_data"][0], 1) def test_bestfit(self): job = Job("unit") setattr(job.par, B.PAR_TESTCASE, "TC0001") comp = components.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.sideA = tdata[M.MATCH_SIDE_PREACTUAL]["data"]["database"]["scheme"]["table"]["_data"] matching.sideB = tdata[M.MATCH_SIDE_POSTACTUAL]["data"]["database"]["scheme"]["table"]["_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_compareRow(self): job = Job("unit") setattr(job.par, B.PAR_TESTCASE, "TC0001") comp = components.component.Component() comp.files = { "A": "/home/match/per.csv", "B": "/home/match/post.csv"} comp.name = "component" comp.conf = conf matching = self.getMatching() matching.sideA = tdata[M.MATCH_SIDE_PREACTUAL]["data"]["database"]["scheme"]["table"]["_data"] matching.sideB = tdata[M.MATCH_SIDE_POSTACTUAL]["data"]["database"]["scheme"]["table"]["_data"] ddl = conf["ddl"]["database"]["scheme"]["table"] header = [] for f in ddl["_header"]: header.append({"field": f, "type": ddl[f]["type"], "acceptance": ddl[f]["acceptance"]}) i = 1 text = utils.match_tool.compareRow(matching, header, tdata[M.MATCH_SIDE_PREACTUAL]["data"]["database"]["scheme"]["table"]["_data"][i], tdata[M.MATCH_SIDE_POSTACTUAL]["data"]["database"]["scheme"]["table"]["_data"][i]) print(text) def test_compareRows(self): job = Job("unit") comp = components.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"]["_data"] matching.sideB = tdata[M.MATCH_SIDE_POSTACTUAL]["data"]["database"]["scheme"]["table"]["_data"] linksA = {"a0001": "b0001", "a0002": "b0002" } matching.linksA = linksA text = utils.match_tool.compareRows(matching, ":database:scheme:table:_data") print(text) def test_match(self): job = Job("unit") setattr(job.par, B.PAR_TESTCASE, "TC0001") setattr(job.par, "tctime", "2022-03-25_19-25-31") comp = components.component.Component() comp.files = {"A": "/home/match/per.csv", "B": "/home/match/post.csv"} comp.name = "component" cm = basic.componentHandling.ComponentManager() basic.componentHandling.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 = Job("unit") setattr(job.par, B.PAR_TESTCASE, "TC0001") setattr(job.par, "tctime", "2022-03-25_19-25-31") comp = components.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()