|
| 1 | +import json |
| 2 | +from chris.client import ChrisClient |
| 3 | + |
| 4 | +expected = r""" |
| 5 | +{"template": |
| 6 | + {"data":[{"name":"name","value":"Example branching pipeline"}, |
| 7 | + {"name": "authors", "value": "Jennings Zhang <[email protected]>"}, |
| 8 | + {"name": "category", "value": "Example"}, |
| 9 | + {"name": "description", "value": |
| 10 | + "A more complicated but nonetheless useless pipeline."}, |
| 11 | + {"name":"locked","value":false}, |
| 12 | + {"name":"plugin_tree","value":"[ |
| 13 | + {\"plugin_name\":\"pl-simpledsapp\",\"plugin_version\":\"2.0.2\",\"previous_index\":null, |
| 14 | + \"plugin_parameter_defaults\":[{\"name\":\"prefix\",\"default\":\"a\"}]}, |
| 15 | + {\"plugin_name\":\"pl-simpledsapp\",\"plugin_version\":\"2.0.2\",\"previous_index\":0, |
| 16 | + \"plugin_parameter_defaults\":[{\"name\":\"prefix\",\"default\":\"b\"}]}, |
| 17 | + {\"plugin_name\":\"pl-simpledsapp\",\"plugin_version\":\"2.0.2\",\"previous_index\":0, |
| 18 | + \"plugin_parameter_defaults\":[{\"name\":\"prefix\",\"default\":\"c\"}]}, |
| 19 | + {\"plugin_name\":\"pl-simpledsapp\",\"plugin_version\":\"2.0.2\",\"previous_index\":1, |
| 20 | + \"plugin_parameter_defaults\":[{\"name\":\"prefix\",\"default\":\"d\"}]}, |
| 21 | + {\"plugin_name\":\"pl-simpledsapp\",\"plugin_version\":\"2.0.2\",\"previous_index\":2, |
| 22 | + \"plugin_parameter_defaults\":[{\"name\":\"prefix\",\"default\":\"e\"}]}, |
| 23 | + {\"plugin_name\":\"pl-simpledsapp\",\"plugin_version\":\"2.0.2\",\"previous_index\":2, |
| 24 | + \"plugin_parameter_defaults\":[{\"name\":\"prefix\",\"default\":\"f\"}]}, |
| 25 | + {\"plugin_name\":\"pl-simpledsapp\",\"plugin_version\":\"2.0.2\",\"previous_index\":2, |
| 26 | + \"plugin_parameter_defaults\":[{\"name\":\"prefix\",\"default\":\"g\"}]}, |
| 27 | + {\"plugin_name\":\"pl-simpledsapp\",\"plugin_version\":\"2.0.2\",\"previous_index\":6, |
| 28 | + \"plugin_parameter_defaults\":[{\"name\":\"prefix\",\"default\":\"h\"}]} |
| 29 | + ]"}]}} |
| 30 | +""" |
| 31 | +expected = expected.replace('\n', ' ') |
| 32 | + |
| 33 | + |
| 34 | +def test_deserialize(): |
| 35 | + client = ChrisClient.from_login(address='http://localhost:8000/api/v1/', |
| 36 | + username='chris', password='chris1234') |
| 37 | + pipeline = client.get_pipeline('Example branching pipeline') |
| 38 | + expected_plugin_tree = json.loads(json.loads(expected)['template']['data'][-1]['value']) |
| 39 | + actual = pipeline.get_root().deserialize_tree() |
| 40 | + |
| 41 | + # limitation: retrieved plugin tree will have all parameter defaults |
| 42 | + for piping in actual: |
| 43 | + __remove_defaults_except_prefix(piping) |
| 44 | + |
| 45 | + assert sorted_pipings(actual) == sorted_pipings(expected_plugin_tree) |
| 46 | + |
| 47 | + |
| 48 | +def __remove_defaults_except_prefix(piping: dict) -> None: |
| 49 | + piping['plugin_parameter_defaults'] = [ |
| 50 | + p for p in piping['plugin_parameter_defaults'] |
| 51 | + if p['name'] == 'prefix' |
| 52 | + ] |
| 53 | + |
| 54 | + |
| 55 | +def sorted_pipings(plugin_tree: list) -> list: |
| 56 | + return sorted(plugin_tree, key=SerializedPiping) |
| 57 | + |
| 58 | + |
| 59 | +class SerializedPiping: |
| 60 | + def __init__(self, __d: dict): |
| 61 | + self.plugin_name = __d['plugin_name'] |
| 62 | + self.plugin_version = __d['plugin_version'] |
| 63 | + self.previous_index = __d['previous_index'] |
| 64 | + |
| 65 | + def __lt__(self, other: 'SerializedPiping') -> bool: |
| 66 | + if self.previous_index is None: |
| 67 | + return True |
| 68 | + if other.previous_index is None: |
| 69 | + return False |
| 70 | + if self.previous_index < other.previous_index: |
| 71 | + return True |
| 72 | + if self.plugin_name < other.plugin_name: |
| 73 | + return True |
| 74 | + if self.plugin_version < other.plugin_version: |
| 75 | + return True |
| 76 | + return False |
0 commit comments