def testConfigUniverse(self): context = findings_lib.FileContext('') type_universe = entity_type_lib.EntityTypeUniverse([]) type_universe.AddFinding( findings_lib.IllegalCharacterError('stuff', context)) field_universe = field_lib.FieldUniverse([]) field_universe.AddFinding( findings_lib.InconsistentFileLocationError('', context)) subfield_universe = subfield_lib.SubfieldUniverse([]) subfield_universe.AddFinding( findings_lib.CapitalizationError('Hi', context)) state_universe = state_lib.StateUniverse([]) connection_universe = connection_lib.ConnectionUniverse([]) connection_universe.AddFinding( findings_lib.InvalidConnectionNamespaceError('notglobal', context)) unit_universe = unit_lib.UnitUniverse([]) config_universe = presubmit_validate_types_lib.ConfigUniverse( subfield_universe=subfield_universe, field_universe=field_universe, entity_type_universe=type_universe, state_universe=state_universe, connection_universe=connection_universe, unit_universe=unit_universe) findings = config_universe.GetFindings() self.assertLen(findings, 4) self.assertTrue( config_universe.HasFindingTypes([ findings_lib.InconsistentFileLocationError, findings_lib.IllegalCharacterError, findings_lib.CapitalizationError, findings_lib.InvalidConnectionNamespaceError ])) self.assertFalse(config_universe.IsValid())
def testConfigUniverseGetEntityType(self): context = findings_lib.FileContext('') type_universe = entity_type_lib.EntityTypeUniverse([]) type_universe.AddFinding( findings_lib.IllegalCharacterError('stuff', context)) field_universe = field_lib.FieldUniverse([]) field_universe.AddFinding( findings_lib.InconsistentFileLocationError('', context)) subfield_universe = subfield_lib.SubfieldUniverse([]) subfield_universe.AddFinding( findings_lib.CapitalizationError('Hi', context)) state_universe = state_lib.StateUniverse([]) connection_universe = connection_lib.ConnectionUniverse([]) unit_universe = unit_lib.UnitUniverse([]) config_universe = presubmit_validate_types_lib.ConfigUniverse( subfield_universe=subfield_universe, field_universe=field_universe, entity_type_universe=type_universe, state_universe=state_universe, connection_universe=connection_universe, unit_universe=unit_universe) entity_type = config_universe.GetEntityType('NONEXISTENT', 'NONEXISTENT') self.assertIsNone(entity_type)
def testEntityTypeUniverseGetFindings(self): filepath = _GOOD_PATH + '/file.yaml' context = findings_lib.FileContext(filepath) folder = entity_type_lib.EntityTypeFolder(_GOOD_PATH) folder.AddFinding( findings_lib.InconsistentFileLocationError('', context)) namespace = folder.local_namespace namespace.AddFinding(findings_lib.IllegalCharacterError( 'two', context)) # This will generate a MissingDescriptionWarning on itself entity_type = entity_type_lib.EntityType(typename='one', filepath=filepath) namespace.InsertType(entity_type) types_universe = entity_type_lib.EntityTypeUniverse([folder]) findings = types_universe.GetFindings() self.assertLen(findings, 3) self.assertTrue( types_universe.HasFindingTypes([ findings_lib.InconsistentFileLocationError, findings_lib.IllegalCharacterError, findings_lib.MissingDescriptionWarning ])) self.assertFalse(types_universe.IsValid())
def _ValidateType(self, local_field_names): """Validates that the entity type is formatted correctly. Checks for formatting and duplicate fields and parents. Records any errors found. Args: local_field_names: list of local field names for the type. """ # Make sure the typename is non-empty. if not self.typename: self.AddFinding(findings_lib.MissingTypenameError(self)) elif not isinstance(self.typename, str): self.AddFinding( findings_lib.IllegalKeyTypeError(self.typename, self.file_context)) elif not ENTITY_TYPE_NAME_REGEX.match(self.typename): self.AddFinding( findings_lib.IllegalCharacterError(self.typename, self.file_context)) # Make sure the type description is non-empty. if not self.description: self.AddFinding(findings_lib.MissingDescriptionWarning(self)) # Check for duplicate local fields. # this check is case insensitive to catch dupes earlier in the event that # we stop explicitly rejecting upper case characters check_fields = set() for field in local_field_names: field_lower = field.lower() if field_lower in check_fields: self.AddFinding(findings_lib.DuplicateFieldError(self, field)) continue check_fields.add(field_lower) # TODO(berkoben): Add more checks to validate fields in isolation # (in case we don't have a field set to check against) # (i.e. check for chirality, formatting. Could use actual Field objects) # Check formatting of field name if len(field.split('/')) > 2: self.AddFinding(findings_lib.UnrecognizedFieldFormatError(self, field)) # Check for duplicate parent names. parent_names_check = set() for parent_name in self.unqualified_parent_names: if parent_name in parent_names_check: self.AddFinding(findings_lib.DuplicateParentError(self, parent_name)) continue parent_names_check.add(parent_name) # Check formatting of parent name if len(parent_name.split('/')) > 2: self.AddFinding( findings_lib.UnrecognizedParentFormatError(self, parent_name)) # Enforce that the inherited_fields_expanded field is not set if self.inherited_fields_expanded: self.AddFinding(findings_lib.InheritedFieldsSetError(self))
def __init__(self, name, description=None, context=None): super(State, self).__init__() self.name = name self.description = description self.context = context if not isinstance(name, str): self.AddFinding(findings_lib.IllegalKeyTypeError(name, context)) elif not STATE_NAME_VALIDATOR.match(name): self.AddFinding(findings_lib.IllegalCharacterError(name, context)) if not description: self.AddFinding(findings_lib.MissingStateDescriptionWarning(self))
def __init__(self, name, states=None, context=None): super(Field, self).__init__() self.context = context self.name = name self.subfields = [] self.states = states if not isinstance(name, str): self.AddFinding(findings_lib.IllegalKeyTypeError(name, context)) elif not FIELD_CHARACTER_REGEX.match(name): self.AddFinding(findings_lib.IllegalCharacterError(name, context)) else: self.InitAndValidateSubfields() self.ValidateStates()
def __init__(self, name, measurement_type, is_standard=False, context=None): super(Unit, self).__init__() self.name = name self.measurement_type = measurement_type self.is_standard = is_standard self.context = context if not isinstance(name, str): self.AddFinding(findings_lib.IllegalKeyTypeError(name, context)) elif not UNIT_NAME_VALIDATOR.match(name): self.AddFinding(findings_lib.IllegalCharacterError(name, context))
def __init__(self, name, category, description=None, context=None): super(Subfield, self).__init__() self.context = context self.name = name self.description = description self.category = category if not isinstance(name, str): self.AddFinding(findings_lib.IllegalKeyTypeError(name, context)) elif not _SUBFIELD_NAME_VALIDATOR.match(name): self.AddFinding(findings_lib.IllegalCharacterError(name, context)) if not self.description: self.AddFinding( findings_lib.MissingSubfieldDescriptionWarning( self.name, self.context))
def __init__(self, name: str, description: str = None, context: findings_lib.FileContext = None): super(Connection, self).__init__() self.name = name self.description = description self.context = context if not isinstance(name, str): self.AddFinding(findings_lib.IllegalKeyTypeError(name, context)) elif not CONNECTION_NAME_VALIDATOR.match(name): self.AddFinding(findings_lib.IllegalCharacterError(name, context)) if not description: self.AddFinding( findings_lib.MissingConnectionDescriptionWarning(self))
def __init__(self, name, states=None, context=None): """Init. Args: name: required string representing the field. states: optional list of strings representing valid states for a multistate field. Should be None for non-multistate fields. context: optional object with the config file location of this field. """ super(Field, self).__init__() self.context = context self.name = name self.subfields = [] self.states = states if not isinstance(name, str): self.AddFinding(findings_lib.IllegalKeyTypeError(name, context)) elif not FIELD_CHARACTER_REGEX.match(name): self.AddFinding(findings_lib.IllegalCharacterError(name, context)) else: self.InitAndValidateSubfields() self.ValidateStates()
def __init__(self, name, measurement_type, is_standard=False, context=None): """Init. Args: name: required string name for the unit measurement_type: required string indicating the unit measurement type is_standard: whether this is the standard unit for the measurement type context: optional object with the config file location of this unit. """ super(Unit, self).__init__() self.name = name self.measurement_type = measurement_type self.is_standard = is_standard self.context = context if not isinstance(name, str): self.AddFinding(findings_lib.IllegalKeyTypeError(name, context)) elif not UNIT_NAME_VALIDATOR.match(name): self.AddFinding(findings_lib.IllegalCharacterError(name, context))