blob: 1eb09e61cd085ab5f79522fd0c55b15bad8ab91b [file] [log] [blame]
Illyoung Choi5d59ab62019-06-24 16:15:27 -07001# 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
15from __future__ import absolute_import
16import unittest
17import json
Illyoung Choi5d59ab62019-06-24 16:15:27 -070018import os
19import collections
20
Illyoung Choi39262742019-07-23 13:28:00 -070021from tools.essence_extractor import EssenceExtractor
Illyoung Choife121d02019-07-16 10:47:41 -070022
23
Illyoung Choi5d59ab62019-06-24 16:15:27 -070024test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Illyoung Choife121d02019-07-16 10:47:41 -070025examples_dir = os.path.join(test_path, "workflow_examples")
Illyoung Choi5d59ab62019-06-24 16:15:27 -070026extension_expected_result = ".expected.json"
27
28try:
29 basestring
30except NameError:
31 basestring = str
32
33
Illyoung Choife121d02019-07-16 10:47:41 -070034# convert unicode string object to plain string object
Illyoung Choi5d59ab62019-06-24 16:15:27 -070035def 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 Choife121d02019-07-16 10:47:41 -070052class TestEssenceExtractor(unittest.TestCase):
Illyoung Choi5d59ab62019-06-24 16:15:27 -070053 """
Illyoung Choife121d02019-07-16 10:47:41 -070054 Try extract essence from all examples under workflow-examples dir.
Illyoung Choi5d59ab62019-06-24 16:15:27 -070055 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 Choife121d02019-07-16 10:47:41 -070070 def testExtract(self):
Illyoung Choi5d59ab62019-06-24 16:15:27 -070071 dags = [f for f in os.listdir(examples_dir) if self.isDagFile(f)]
Illyoung Choi5d59ab62019-06-24 16:15:27 -070072 for dag in dags:
73 dag_path = os.path.join(examples_dir, dag)
Illyoung Choi5d59ab62019-06-24 16:15:27 -070074
Illyoung Choife121d02019-07-16 10:47:41 -070075 essence_extractor = EssenceExtractor()
76 essence_extractor.parse_codefile(dag_path)
77 workflow_info = essence_extractor.extract()
78
79 # find its solution file
Illyoung Choi5d59ab62019-06-24 16:15:27 -070080 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
97if __name__ == "__main__":
98 unittest.main()