import json import unittest from basic.program import Job import utils.match_tool import components.component tdata = { "postReq": { "database": { "scheme": { "table": { "_data": [] } } } }, "preAct": { "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"} ] } } } }, "postAct": { "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") comp = components.component.Component() comp.files = { "A": "/home/match/per.csv", "B": "/home/match/post.csv"} comp.conf = conf matching = utils.match_tool.Matching(comp) matching.setData(tdata, utils.match_tool.MATCH_PREPOST) print(matching.htmltext) def test_hitmanage(self): comp = components.component.Component() comp.files = { "A": "/home/match/per.csv", "B": "/home/match/post.csv"} 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_similarity(self): matching = self.getMatching() utils.match_tool.getSimilarity(matching, ":database:scheme:table:_data", tdata["preAct"]["database"]["scheme"]["table"]["_data"][0], tdata["postAct"]["database"]["scheme"]["table"]["_data"][0],1) def test_bestfit(self): job = Job("unit") comp = components.component.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["preAct"]["database"]["scheme"]["table"]["_data"] matching.sideB = tdata["postAct"]["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") comp = components.component.Component() comp.files = { "A": "/home/match/per.csv", "B": "/home/match/post.csv"} comp.conf = conf matching = self.getMatching() matching.sideA = tdata["preAct"]["database"]["scheme"]["table"]["_data"] matching.sideB = tdata["postAct"]["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["preAct"]["database"]["scheme"]["table"]["_data"][i], tdata["postAct"]["database"]["scheme"]["table"]["_data"][i]) print(text) def test_compareRows(self): job = Job("unit") comp = components.component.Component() comp.files = { "A": "/home/match/per.csv", "B": "/home/match/post.csv"} comp.conf = conf matching = self.getMatching() matching.sideA = tdata["preAct"]["database"]["scheme"]["table"]["_data"] matching.sideB = tdata["postAct"]["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") comp = components.component.Component() comp.files = {"A": "/home/match/per.csv", "B": "/home/match/post.csv"} # tdata["postReq"] = tdata["preAct"] comp.conf = conf matching = utils.match_tool.Matching(comp) matching.setData(tdata, utils.match_tool.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") comp = components.component.Component() comp.files = { "A": "/home/match/per.csv", "B": "/home/match/post.csv"} comp.conf = conf matching = utils.match_tool.Matching(comp) matching.setData(tdata, utils.match_tool.MATCH_PREPOST) matching.difftext = "" return matching if __name__ == '__main__': unittest.main()