Illyoung Choi | 5d59ab6 | 2019-06-24 16:15:27 -0700 | [diff] [blame^] | 1 | # Copyright 2019-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from __future__ import absolute_import |
| 16 | import unittest |
| 17 | import json |
| 18 | import cordworkflowessenceextractor.workflow_essence_extractor as extractor |
| 19 | |
| 20 | import os |
| 21 | import collections |
| 22 | |
| 23 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 24 | examples_dir = os.path.join(test_path, "workflow-examples") |
| 25 | extension_expected_result = ".expected.json" |
| 26 | |
| 27 | try: |
| 28 | basestring |
| 29 | except NameError: |
| 30 | basestring = str |
| 31 | |
| 32 | |
| 33 | def convert(data): |
| 34 | if isinstance(data, basestring): |
| 35 | return str(data) |
| 36 | elif isinstance(data, collections.Mapping): |
| 37 | v = {} |
| 38 | for item in data: |
| 39 | v[convert(item)] = convert(data[item]) |
| 40 | return v |
| 41 | elif isinstance(data, collections.Iterable): |
| 42 | v = [] |
| 43 | for item in data: |
| 44 | v.append(convert(item)) |
| 45 | return v |
| 46 | else: |
| 47 | return data |
| 48 | |
| 49 | |
| 50 | class TestParse(unittest.TestCase): |
| 51 | |
| 52 | """ |
| 53 | Try parse all examples under workflow-examples dir. |
| 54 | Then compares results with expected solution. |
| 55 | """ |
| 56 | |
| 57 | def setUp(self): |
| 58 | pass |
| 59 | |
| 60 | def tearDown(self): |
| 61 | pass |
| 62 | |
| 63 | def isDagFile(self, filepath): |
| 64 | _, file_extension = os.path.splitext(filepath) |
| 65 | if file_extension == ".py": |
| 66 | return True |
| 67 | return False |
| 68 | |
| 69 | def test_parse(self): |
| 70 | dags = [f for f in os.listdir(examples_dir) if self.isDagFile(f)] |
| 71 | |
| 72 | for dag in dags: |
| 73 | dag_path = os.path.join(examples_dir, dag) |
| 74 | tree = extractor.parse_codefile(dag_path) |
| 75 | workflow_info = extractor.extract_all(tree) |
| 76 | |
| 77 | # check if its expected solution fil |
| 78 | expected_result_file = dag_path + extension_expected_result |
| 79 | self.assertTrue(os.path.exists(expected_result_file)) |
| 80 | |
| 81 | # compare content |
| 82 | with open(dag_path + extension_expected_result) as json_file: |
| 83 | # this builds a dict with unicode strings |
| 84 | expected_workflow_info_uni = json.load(json_file) |
| 85 | expected_workflow_info = convert(expected_workflow_info_uni) |
| 86 | if workflow_info != expected_workflow_info: |
| 87 | print("Expected") |
| 88 | print(expected_workflow_info) |
| 89 | |
| 90 | print("We got") |
| 91 | print(workflow_info) |
| 92 | self.fail("produced result is different") |
| 93 | |
| 94 | |
| 95 | if __name__ == "__main__": |
| 96 | unittest.main() |