def test_duplicate_name_raises(source_doc, source_spec, target_spec): source_doc["list_source_1"]["Stacy"] = {"Name": "Another Stacy"} with pytest.raises(ValueError): source_track: Track = Track.build(source_spec, None, "Source") target_track: Track = Track.build(target_spec, source_track, "Target") translate: Translate = Translate(target_track) translate(source_doc)
def test_list_in_folder(source, target): source_spec, source_doc = source target_spec, expected = target() source_track: Track = Track.build(source_spec, None, "Source") target_track: Track = Track.build(target_spec, source_track, "Target") translate: Translate = Translate(target_track) actual: Dict = translate(source_doc) assert actual == expected
def test_rearrange(source_doc: Dict, source_spec: Dict, target_doc: Dict, target_spec: Dict): """Verify that translate respects the sort order property of the variables in the target spec, and ignores the order in which the variables happen to be defined in the spec. """ shuffled_source_spec = shuffle(source_spec) shuffled_target_spec = shuffle(target_spec) source_track: Track = Track.build(shuffled_source_spec, None, "Source") target_track: Track = Track.build(shuffled_target_spec, source_track, "Target") translate: Translate = Translate(target_track) actual: Dict = translate(source_doc) assert actual == target_doc
def test_named_list_in_list(source, target): """Reversing the order of the sources in the target list spec results in an equivalent change in the order of the resulting list.""" source_spec, source_doc = source target_spec, target_doc = target source_track: Track = Track.build(source_spec, None, "Source") target_track: Track = Track.build(target_spec, source_track, "Target") translate: Translate = Translate(target_track) actual: Dict = translate(source_doc) assert actual == target_doc
def test_translate_with_folders(source: Callable, target: Callable): """Summary: verify that source topology doesn't matter for a given target spec. Long version: Try every combination of the above examples. Because a target variable's source is defined by ID, the particular location of the source variable in the source hierarchy is irrelevant. That is, no matter how the source hierarchy is arranged, a target spec should produce the same target hierarchy as long as all the source variables exist.""" source_doc, source_spec = source() expected, target_spec = target() source_track: Track = Track.build(source_spec, None, "Source") target_track: Track = Track.build(target_spec, source_track, "Target") translate: Translate = Translate(target_track) actual: Dict = translate(source_doc) assert actual == expected
def test_dumps_pretty(simple_spec): """Verify that, regardless of how the input dict was formatted, dumps is pretty and alphabetized by variable ID.""" # Dictionary insertion order is preserved (as a language feature) from Python 3.7 onward assert sys.version_info >= (3, 7), "This module requires Python 3.7+" out_of_order_spec: Dict = { "target_var_id": simple_spec["target_var_id"], "target_folder": simple_spec["target_folder"] } track: Track = Track.build(out_of_order_spec, None, "OOO") actual: str = track.dumps() expected: str = inspect.cleandoc(""" { "target_folder": { "name": "the_folder", "data_type": "Folder", "sort_order": 0 }, "target_var_id": { "name": "the_target", "data_type": "Integer", "sort_order": 0, "parent": "target_folder" } } """) assert actual == expected
def target_nested_dict_track(source_nested_dict_track) -> Track: spec: Dict = { "target_var_1": { "name": "first_target", "data_type": "Integer", "sources": ["source_var_1"], "parent": "target_folder_1", "sort_order": 0 }, "target_var_2": { "name": "second_target", "data_type": "Integer", "sources": ["source_var_2"], "parent": "target_folder_2", "sort_order": 0 }, "target_folder_1": { "name": "outer_s", "data_type": "Folder", "sort_order": 0 }, "target_folder_2": { "name": "inner_s", "data_type": "Folder", "parent": "target_folder_1", "sort_order": 1 } } return Track.build(spec, source_nested_dict_track, "Target")
def simple_ev_track() -> Track: spec: Dict = { "with_ev": { "name": "var_with_ev", "data_type": "Text", "sort_order": 0, "simple_expected_values": { "case_0": "ABC", "case_1": "CDE" } }, "without_ev": { "name": "var_without_ev", "data_type": "Text", "sort_order": 1 }, "another_with_ev": { "name": "some_other_var", "data_type": "Text", "sort_order": 2, "simple_expected_values": { "case_1": "AAA" } } } return Track.build(spec, None, "Test")
def load(cls, path_locator, path, source_schema=None): if path is None: return None source_invariant = source_schema.invariant if source_schema else None source_temporal = source_schema.temporal if source_schema else None with open( os.path.join(path_locator.schemas_dir, path, 'temporal.json'), 'r') as temporal: with open( os.path.join(path_locator.schemas_dir, path, 'invariant.json'), 'r') as invariant: return cls(temporal=Track.build(specs=json.load(temporal), source=source_temporal, name='temporal'), invariant=Track.build(specs=json.load(invariant), source=source_invariant, name='invariant'))
def target_list_track(source_list_track) -> Track: spec: Dict = { "target_folder_outer": { "name": "outer", "data_type": "Folder", "sort_order": 0 }, "target_folder_inner": { "name": "inner", "data_type": "Folder", "parent": "target_folder_outer", "sort_order": 0 }, "target_list": { "name": "the_list", "data_type": "List", "parent": "target_folder_outer", "sort_order": 1, "sources": ["source_list"], "source_child_mappings": { "source_list": { "target_list_name": ["source_list_name"], "target_list_color": ["source_list_color"] } } }, "target_list_name": { "name": "name", "data_type": "Text", "parent": "target_list", "sort_order": 0 }, "target_list_color": { "name": "color", "data_type": "Text", "parent": "target_list", "sort_order": 1 }, "target_named_list": { "name": "the_named_list", "data_type": "NamedList", "parent": "target_folder_inner", "sort_order": 0, "sources": ["source_named_list"], "source_child_mappings": { "source_named_list": { "target_named_list_color": ["source_named_list_color"] } } }, "target_named_list_color": { "name": "color", "data_type": "Text", "parent": "target_named_list", "sort_order": 0 } } return Track.build(spec, source_list_track, "Target")
def source_named_list_track() -> Track: spec: Dict = { "source_root_1": { "name": "list_source_1", "data_type": "NamedList", "sort_order": 0 }, "source_root_1_name": { "name": "name", "data_type": "Text", "parent": "source_root_1", "sort_order": 0 }, "source_root_1_age": { "name": "age", "data_type": "Integer", "parent": "source_root_1", "sort_order": 1 }, "source_root_1_ice_cream": { "name": "ice cream", "data_type": "Text", "parent": "source_root_1", "sort_order": 2 }, "source_root_1_sport": { "name": "sport", "data_type": "Text", "parent": "source_root_1", "sort_order": 3 }, "source_root_2": { "name": "list_source_2", "data_type": "NamedList", "sort_order": 1, }, "source_root_2_nombre": { "name": "nombre", "data_type": "Text", "parent": "source_root_2", "sort_order": 0 }, "source_root_2_edad": { "name": "edad", "data_type": "Integer", "parent": "source_root_2", "sort_order": 1 }, "source_root_2_helado": { "name": "helado", "data_type": "Text", "parent": "source_root_2", "sort_order": 2 } } return Track.build(spec, None, "Source")
def _simple_track(data_type: str) -> Track: spec: Dict = { "the_var_id": { "name": "the_variable", "data_type": data_type, "sort_order": 0 } } track: Track = Track.build(spec, None, "Test") return track
def has_none_track() -> Track: spec: Dict = { "the_var_id": { "name": "var with no metadata", "data_type": "Integer", "sort_order": 0 } } track: Track = Track.build(spec, None, "Test") return track
def has_all_track() -> Track: spec: Dict = { "the_var_id": { "name": "var with all metadata", "data_type": "Integer", "sort_order": 0, "notes": "This is a note that I left for myself about this variable.", "earliest_epoch": "2011", "latest_epoch": "2019-01-02" } } track: Track = Track.build(spec, None, "Test") return track
def target_named_list_track(source_named_list_track) -> Track: spec: Dict = { "target_root": { "name": "People", "data_type": "NamedList", "sources": ["source_root_1", "source_root_2"], "sort_order": 0, "source_child_mappings": { "source_root_1": { "target_root_name": ["source_root_1_name"], "target_root_age": ["source_root_1_age"], "target_root_ice_cream": ["source_root_1_ice_cream"], "target_root_sport": ["source_root_1_sport"] }, "source_root_2": { "target_root_name": ["source_root_2_nombre"], "target_root_age": ["source_root_2_edad"], "target_root_ice_cream": ["source_root_2_helado"], "target_root_sport": [] } } }, "target_root_name": { "name": "Name", "data_type": "Text", "sort_order": 0, "parent": "target_root" }, "target_root_age": { "name": "Age", "data_type": "Integer", "sort_order": 1, "parent": "target_root" }, "target_root_ice_cream": { "name": "Ice cream", "data_type": "Text", "sort_order": 2, "parent": "target_root" }, "target_root_sport": { "name": "Sport", "data_type": "Text", "sort_order": 3, "parent": "target_root" } } return Track.build(spec, source_named_list_track, "Target")
def test_add_creates_nested_lists_raises(): spec: Dict = { "outer_list": { "name": "the outer list", "data_type": "List", "sort_order": 0 } } new_var: Dict = { "name": "the inner list", "data_type": "List", "parent": "outer_list", "sort_order": 0 } track: Track = Track.build(spec, None, "Test") with pytest.raises(ValueError): track.add(new_var, "a")
def source_list_track() -> Track: spec: Dict = { "source_folder": { "name": "source_outer_folder", "data_type": "Folder", "sort_order": 0 }, "source_list": { "name": "source_inner_list", "data_type": "List", "parent": "source_folder", "sort_order": 0, }, "source_list_name": { "name": "name", "data_type": "Text", "parent": "source_list", "sort_order": 0 }, "source_list_color": { "name": "color", "data_type": "Text", "parent": "source_list", "sort_order": 1 }, "source_named_list": { "name": "source_inner_named_list", "data_type": "NamedList", "parent": "source_folder", "sort_order": 1 }, "source_named_list_color": { "name": "color", "data_type": "Text", "parent": "source_named_list", "sort_order": 0 } } return Track.build(spec, None, "Source")
def source_nested_dict_track() -> Track: spec: Dict = { "source_var_1": { "name": "first_source", "data_type": "Integer", "parent": "source_folder_1", "sort_order": 0 }, "source_var_2": { "name": "second_source", "data_type": "Integer", "parent": "source_folder_2", "sort_order": 0 }, "source_folder_1": { "name": "outer_s", "data_type": "Folder", "sort_order": 0 }, "source_folder_2": { "name": "inner_s", "data_type": "Folder", "parent": "source_folder_1", "sort_order": 1 }, "source_var_3": { "name": "third_source", "data_type": "Integer", "parent": "source_folder_2", "sort_order": 1 }, "source_folder_3": { "name": "initially_empty_folder", "data_type": "Folder", "sort_order": 1 } } return Track.build(spec, None, "Source")
def test_delete_variable_with_expected_values_updates_test_cases(): spec: Dict = { "var_1": { "name": "the_first_var", "data_type": "Integer", "sort_order": 0, "simple_expected_values": { "entity 1": 5, "entity 2": 7 } }, "var_2": { "name": "the_second_var", "data_type": "Text", "sort_order": 1, "simple_expected_values": { "entity 2": "foo", "entity 3": "bar" } } } track: Track = Track.build(spec, None, "Test") track.delete("var_2") assert {"entity 1", "entity 2"} == set(track.test_cases)
def simple_flat_track(simple_flat_spec) -> Track: return Track.build(simple_flat_spec, None, "Sample")
def do_test(s_doc, s_spec, t_doc, t_spec): source_track: Track = Track.build(s_spec, None, "Source") target_track: Track = Track.build(t_spec, source_track, "Target") translate: Translate = Translate(target_track) actual: Dict = translate(s_doc) assert actual == t_doc
def translate(source_spec: Dict, target_spec: Dict) -> Translate: source_track: Track = Track.build(source_spec, None, "Source") target_track: Track = Track.build(target_spec, source_track, "Target") translate: Translate = Translate(target_track) return translate
def named_list_ev_track() -> Track: spec: Dict = { "named_list_without_evs": { "name": "A named list that has no EVs", "data_type": "NamedList", "sort_order": 0 }, "named_list_without_evs_field": { "name": "A", "data_type": "Integer", "parent": "named_list_without_evs", "sort_order": 0 }, "first_named_list_with_evs": { "name": "First of two named lists that have EVs", "data_type": "NamedList", "sort_order": 1, "list_expected_values_fields": [ # Note that both lists and named lists use this same property "first_named_list_with_evs_field_1", "first_named_list_with_evs_field_2" ], "named_list_expected_values": { "case_0": { "steve": { "first_named_list_with_evs_field_1": 17, "first_named_list_with_evs_field_2": 23 }, "mary": { "first_named_list_with_evs_field_1": -5, "first_named_list_with_evs_field_2": None } }, "case_1": {} } }, "first_named_list_with_evs_field_1": { "name": "B", "data_type": "Integer", "parent": "first_named_list_with_evs", "sort_order": 0 }, "first_named_list_with_evs_field_2": { "name": "C", "data_type": "Integer", "parent": "first_named_list_with_evs", "sort_order": 1 }, "first_named_list_with_evs_field_3": { "name": "D", "data_type": "Integer", "parent": "first_named_list_with_evs", "sort_order": 1 }, "second_named_list_with_evs": { "name": "Second of two named lists that have EVs", "data_type": "NamedList", "sort_order": 2, "named_list_expected_values": { "case_1": { "steve": {}, "mary": {}, }, "case_2": {} } }, "second_named_list_with_evs_field": { "name": "E", "data_type": "Integer", "parent": "second_named_list_with_evs", "sort_order": 0 } } track: Track = Track.build(spec, None, "Test") return track
def _do_test(source_spec, source_doc, target_spec, expected): source_track: Track = Track.build(source_spec, None, "Source") target_track: Track = Track.build(target_spec, source_track, "Target") translate: Translate = Translate(target_track) actual: Dict = translate(source_doc) assert actual == expected