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 |
Illyoung Choi | 5d59ab6 | 2019-06-24 16:15:27 -0700 | [diff] [blame] | 18 | import os |
| 19 | import collections |
| 20 | |
Illyoung Choi | fe121d0 | 2019-07-16 10:47:41 -0700 | [diff] [blame^] | 21 | from cord_workflow_airflow_extensions.essence_extractor import EssenceExtractor |
| 22 | |
| 23 | |
Illyoung Choi | 5d59ab6 | 2019-06-24 16:15:27 -0700 | [diff] [blame] | 24 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
Illyoung Choi | fe121d0 | 2019-07-16 10:47:41 -0700 | [diff] [blame^] | 25 | examples_dir = os.path.join(test_path, "workflow_examples") |
Illyoung Choi | 5d59ab6 | 2019-06-24 16:15:27 -0700 | [diff] [blame] | 26 | extension_expected_result = ".expected.json" |
| 27 | |
| 28 | try: |
| 29 | basestring |
| 30 | except NameError: |
| 31 | basestring = str |
| 32 | |
| 33 | |
Illyoung Choi | fe121d0 | 2019-07-16 10:47:41 -0700 | [diff] [blame^] | 34 | # convert unicode string object to plain string object |
Illyoung Choi | 5d59ab6 | 2019-06-24 16:15:27 -0700 | [diff] [blame] | 35 | def convert(data): |
| 36 | if isinstance(data, basestring): |
| 37 | return str(data) |
| 38 | elif isinstance(data, collections.Mapping): |
| 39 | v = {} |
| 40 | for item in data: |
| 41 | v[convert(item)] = convert(data[item]) |
| 42 | return v |
| 43 | elif isinstance(data, collections.Iterable): |
| 44 | v = [] |
| 45 | for item in data: |
| 46 | v.append(convert(item)) |
| 47 | return v |
| 48 | else: |
| 49 | return data |
| 50 | |
| 51 | |
Illyoung Choi | fe121d0 | 2019-07-16 10:47:41 -0700 | [diff] [blame^] | 52 | class TestEssenceExtractor(unittest.TestCase): |
Illyoung Choi | 5d59ab6 | 2019-06-24 16:15:27 -0700 | [diff] [blame] | 53 | """ |
Illyoung Choi | fe121d0 | 2019-07-16 10:47:41 -0700 | [diff] [blame^] | 54 | Try extract essence from all examples under workflow-examples dir. |
Illyoung Choi | 5d59ab6 | 2019-06-24 16:15:27 -0700 | [diff] [blame] | 55 | Then compares results with expected solution. |
| 56 | """ |
| 57 | |
| 58 | def setUp(self): |
| 59 | pass |
| 60 | |
| 61 | def tearDown(self): |
| 62 | pass |
| 63 | |
| 64 | def isDagFile(self, filepath): |
| 65 | _, file_extension = os.path.splitext(filepath) |
| 66 | if file_extension == ".py": |
| 67 | return True |
| 68 | return False |
| 69 | |
Illyoung Choi | fe121d0 | 2019-07-16 10:47:41 -0700 | [diff] [blame^] | 70 | def testExtract(self): |
Illyoung Choi | 5d59ab6 | 2019-06-24 16:15:27 -0700 | [diff] [blame] | 71 | dags = [f for f in os.listdir(examples_dir) if self.isDagFile(f)] |
Illyoung Choi | 5d59ab6 | 2019-06-24 16:15:27 -0700 | [diff] [blame] | 72 | for dag in dags: |
| 73 | dag_path = os.path.join(examples_dir, dag) |
Illyoung Choi | 5d59ab6 | 2019-06-24 16:15:27 -0700 | [diff] [blame] | 74 | |
Illyoung Choi | fe121d0 | 2019-07-16 10:47:41 -0700 | [diff] [blame^] | 75 | essence_extractor = EssenceExtractor() |
| 76 | essence_extractor.parse_codefile(dag_path) |
| 77 | workflow_info = essence_extractor.extract() |
| 78 | |
| 79 | # find its solution file |
Illyoung Choi | 5d59ab6 | 2019-06-24 16:15:27 -0700 | [diff] [blame] | 80 | expected_result_file = dag_path + extension_expected_result |
| 81 | self.assertTrue(os.path.exists(expected_result_file)) |
| 82 | |
| 83 | # compare content |
| 84 | with open(dag_path + extension_expected_result) as json_file: |
| 85 | # this builds a dict with unicode strings |
| 86 | expected_workflow_info_uni = json.load(json_file) |
| 87 | expected_workflow_info = convert(expected_workflow_info_uni) |
| 88 | if workflow_info != expected_workflow_info: |
| 89 | print("Expected") |
| 90 | print(expected_workflow_info) |
| 91 | |
| 92 | print("We got") |
| 93 | print(workflow_info) |
| 94 | self.fail("produced result is different") |
| 95 | |
| 96 | |
| 97 | if __name__ == "__main__": |
| 98 | unittest.main() |