def test_get_all_tag_attributes(self):
        test_string = HedString("Jerk-rate/#")
        test_string.convert_to_canonical_forms(self.hed_schema_3g)
        tag_props = self.hed_schema_3g.get_all_tag_attributes(test_string)
        expected_props = {
            "takesValue": "true",
            "valueClass": "numericClass",
            "unitClass": 'jerkUnits'
        }
        self.assertCountEqual(tag_props, expected_props)

        tag_props = self.hed_schema_3g.get_all_tag_attributes("This/Is/Not/A/Tag")
        expected_props = {
        }
        self.assertCountEqual(tag_props, expected_props)

        test_string = HedString("Statistical-value")
        test_string.convert_to_canonical_forms(self.hed_schema_3g)
        tag_props = self.hed_schema_3g.get_all_tag_attributes(test_string)
        expected_props = {
            HedKey.ExtensionAllowed: "true",
        }
        self.assertCountEqual(tag_props, expected_props)
        # also test long form.
        tag_props = self.hed_schema_3g.get_all_tag_attributes("Property/Data-property/Data-value/Statistical-value")
        self.assertCountEqual(tag_props, expected_props)
    def test_expand_def_tags_placeholder_invalid(self):
        def_dict = DefDict()
        def_string = HedString(self.placeholder_def_string)
        def_string.convert_to_canonical_forms(None)
        def_dict.check_for_definitions(def_string)
        def_mapper = DefinitionMapper(def_dict)

        placeholder_label_def_string_no_placeholder = "def/TestDefPlaceholder"

        test_string = HedString(placeholder_label_def_string_no_placeholder)
        test_string.convert_to_canonical_forms(None)
        def_issues = def_mapper.expand_def_tags(test_string)
        self.assertEqual(str(test_string), placeholder_label_def_string_no_placeholder)
        self.assertTrue(def_issues)

        def_dict = DefDict()
        def_string = HedString(self.basic_definition_string)
        def_string.convert_to_canonical_forms(None)
        def_dict.check_for_definitions(def_string)
        def_mapper = DefinitionMapper(def_dict)

        label_def_string_has_invalid_placeholder = "def/TestDef/54687"

        test_string = HedString(label_def_string_has_invalid_placeholder)
        test_string.convert_to_canonical_forms(None)
        def_issues = def_mapper.expand_def_tags(test_string)
        self.assertEqual(str(test_string), label_def_string_has_invalid_placeholder)
        self.assertTrue(def_issues)
    def test_expand_def_tags_with_validator(self):
        def_dict = DefDict()
        def_string = HedString(self.basic_definition_string)
        def_string.convert_to_canonical_forms(None)
        def_dict.check_for_definitions(def_string)
        def_mapper = DefinitionMapper(def_dict)
        validator = HedValidator(self.hed_schema)
        validators = [validator, def_mapper]

        test_string = HedString(self.basic_definition_string)
        def_issues = test_string.validate(validators, expand_defs=True)
        self.assertEqual(test_string.get_as_short(), "")

        test_string = HedString(self.basic_definition_string_no_paren)
        def_issues = test_string.validate(validators, expand_defs=True)
        self.assertTrue(def_issues)
        self.assertEqual(test_string.get_as_short(), self.basic_definition_string_no_paren)

        test_string = HedString(self.basic_hed_string + "," + self.basic_definition_string)
        def_issues = test_string.validate(validators, expand_defs=True)
        self.assertFalse(def_issues)
        self.assertEqual(test_string.get_as_short(), self.basic_hed_string)

        test_string = HedString(self.basic_definition_string + "," + self.basic_hed_string)
        def_issues = test_string.validate(validators, expand_defs=True)
        self.assertFalse(def_issues)
        self.assertEqual(test_string.get_as_short(), self.basic_hed_string)

        test_string = HedString(self.basic_hed_string_with_def)
        def_issues = test_string.validate(validators, expand_defs=True)
        self.assertFalse(def_issues)
        self.assertEqual(test_string.get_as_short(), self.basic_hed_string + "," + self.expanded_def_string)

        test_string = HedString(self.basic_hed_string_with_def_first)
        def_issues = test_string.validate(validators, expand_defs=True)
        self.assertFalse(def_issues)
        self.assertEqual(test_string.get_as_short(), self.expanded_def_string + "," + self.basic_hed_string)

        test_string = HedString(self.basic_hed_string_with_def_first_paren)
        def_issues = test_string.validate(validators, expand_defs=True)
        self.assertFalse(def_issues)
        self.assertEqual(test_string.get_as_short(), "(" + self.expanded_def_string + "," + self.basic_hed_string + ")")
    def test_expand_def_tags_no_expand(self):
        def_dict = DefDict()
        def_string = HedString(self.basic_definition_string)
        def_string.convert_to_canonical_forms(None)
        def_dict.check_for_definitions(def_string)
        def_mapper = DefinitionMapper(def_dict)

        test_string = HedString(self.basic_definition_string)
        def_issues = test_string.validate(def_mapper, expand_defs=False)
        self.assertFalse(def_issues)
        self.assertEqual(str(test_string), "")

        test_string = HedString(self.basic_definition_string_no_paren)
        def_issues = test_string.validate(def_mapper, expand_defs=False)
        self.assertFalse(def_issues)
        self.assertEqual(str(test_string), self.basic_definition_string_no_paren)

        test_string = HedString(self.basic_hed_string + "," + self.basic_definition_string)
        def_issues = test_string.validate(def_mapper, expand_defs=False)
        self.assertFalse(def_issues)
        self.assertEqual(str(test_string), self.basic_hed_string)

        test_string = HedString(self.basic_definition_string + "," + self.basic_hed_string)
        def_issues = test_string.validate(def_mapper, expand_defs=False)
        self.assertFalse(def_issues)
        self.assertEqual(str(test_string), self.basic_hed_string)

        test_string = HedString(self.basic_hed_string_with_def)
        def_issues = test_string.validate(def_mapper, expand_defs=False)
        self.assertFalse(def_issues)
        self.assertEqual(str(test_string), self.basic_hed_string_with_def)

        test_string = HedString(self.basic_hed_string_with_def_first)
        def_issues = test_string.validate(def_mapper, expand_defs=False)
        self.assertFalse(def_issues)
        self.assertEqual(str(test_string), self.basic_hed_string_with_def_first)

        test_string = HedString(self.basic_hed_string_with_def_first_paren)
        def_issues = test_string.validate(def_mapper, expand_defs=False)
        self.assertFalse(def_issues)
        self.assertEqual(str(test_string), self.basic_hed_string_with_def_first_paren)
    def test_changing_tag_then_def_mapping(self):
        def_dict = DefDict()
        def_string = HedString(self.basic_definition_string)
        def_string.convert_to_canonical_forms(None)
        def_dict.check_for_definitions(def_string)
        def_mapper = DefinitionMapper(def_dict)
        validator = HedValidator(self.hed_schema)
        validators = [validator, def_mapper]

        test_string = HedString(self.label_def_string)
        tag = test_string.get_direct_children()[0]
        tag.tag = "Organizational-property/" + str(tag)
        def_issues = test_string.validate(validators, expand_defs=True)
        self.assertFalse(def_issues)
        self.assertEqual(test_string.get_as_short(), f"{self.expanded_def_string}")

        test_string = HedString(self.label_def_string)
        tag = test_string.get_direct_children()[0]
        tag.tag = "Organizational-property22/" + str(tag)
        def_issues = test_string.validate(validators, expand_defs=True)
        self.assertTrue(def_issues)
Exemple #6
0
 def tag_form_base(self, test_strings, expected_results, expected_errors):
     for test_key in test_strings:
         test_string_obj = HedString(test_strings[test_key])
         test_errors = test_string_obj.convert_to_canonical_forms(
             hed_schema=self.hed_schema)
         expected_error = expected_errors[test_key]
         expected_result = expected_results[test_key]
         for tag in test_string_obj.tags():
             self.assertEqual(tag.base_tag, expected_result,
                              test_strings[test_key])
             self.assertCountEqual(test_errors, expected_error,
                                   test_strings[test_key])
    def converter_base(self, test_strings, expected_results, expected_errors, convert_to_short=True):
        for test_key in test_strings:
            test_string_obj = HedString(test_strings[test_key])
            error_handler = ErrorHandler()
            error_handler.push_error_context(ErrorContext.HED_STRING, test_string_obj, increment_depth_after=False)
            test_issues = test_string_obj.convert_to_canonical_forms(self.hed_schema)
            if convert_to_short:
                string_result = test_string_obj.get_as_short()
            else:
                string_result = test_string_obj.get_as_long()
            expected_params = expected_errors[test_key]
            expected_result = expected_results[test_key]

            expected_issue = self.format_errors_fully(error_handler, hed_string=test_string_obj,
                                                      params=expected_params)
            error_handler.add_context_to_issues(test_issues)

            # print(test_key)
            # print(expected_issue)
            # print(test_issues)
            self.assertEqual(string_result, expected_result, test_strings[test_key])
            self.assertCountEqual(test_issues, expected_issue, test_strings[test_key])