def testInheritTypesFromDifferentNamespace(self): type1 = entity_type_lib.EntityType(filepath='path/to/ANIMAL/mammal', typename='dog', description='canine animal', local_field_tuples=_F( ['/woof', '/howl'])) type2 = entity_type_lib.EntityType(filepath='path/to/INSECT/fly', typename='moth', description='flying insect.', local_field_tuples=_F(['/wings']), parents=['ANIMAL/dog']) type_namespace_1 = entity_type_lib.TypeNamespace(namespace='ANIMAL') type_namespace_1.InsertType(type1) type_namespace_2 = entity_type_lib.TypeNamespace(namespace='INSECT') type_namespace_2.InsertType(type2) namespace_validate = (namespace_validator.NamespaceValidator( [type_namespace_1, type_namespace_2])) self.assertFalse(namespace_validate.GetFindings()) expanded_type_namespace_1 = namespace_validate.type_namespaces_map.get( type_namespace_1.namespace) expanded_type_namespace_2 = namespace_validate.type_namespaces_map.get( type_namespace_2.namespace) type1_expanded = expanded_type_namespace_1.valid_types_map.get( type1.typename) type2_expanded = expanded_type_namespace_2.valid_types_map.get( type2.typename) self.assertEmpty(type1_expanded.inherited_field_names) self.assertLen(type2_expanded.inherited_field_names, 2)
def testEntityTypeUniverseFindsDupIds(self): filepath = _GOOD_PATH + '/file.yaml' folder = entity_type_lib.EntityTypeFolder(_GOOD_PATH) namespace = folder.local_namespace entity_type1 = entity_type_lib.EntityType(typename='one', filepath=filepath, description='hi', uid='1') namespace.InsertType(entity_type1) entity_type1a = entity_type_lib.EntityType(typename='oneA', filepath=filepath, description='hi', uid='1') namespace.InsertType(entity_type1a) entity_type2 = entity_type_lib.EntityType(typename='two', filepath=filepath, description='hi', uid='2') namespace.InsertType(entity_type2) types_universe = entity_type_lib.EntityTypeUniverse([folder]) findings = types_universe.GetFindings() self.assertLen(findings, 2) self.assertTrue( types_universe.HasFindingTypes([findings_lib.DuplicateIdsError])) self.assertTrue( entity_type1.HasFindingTypes([findings_lib.DuplicateIdsError])) self.assertTrue( entity_type1a.HasFindingTypes([findings_lib.DuplicateIdsError])) self.assertFalse(entity_type2.GetFindings())
def testDuplicateLocalFieldSets(self): type1 = entity_type_lib.EntityType(filepath='path/to/ANIMAL/mammal', typename='dog', description='canine animal', local_field_tuples=_F( ['/woof', '/howl'])) type2 = entity_type_lib.EntityType(filepath='path/to/ANIMAL/mammal', typename='wolf', description='canine animal', local_field_tuples=_F( ['/woof', '/howl'])) type2.inherited_field_names = ({'/bark': _F1('/bark', optional=True)}) type_namespace = entity_type_lib.TypeNamespace(namespace='ANIMAL') type_namespace.InsertType(type1) type_namespace.InsertType(type2) namespace_validator.NamespaceValidator([type_namespace]) self.assertTrue( type1.HasFindingTypes( [findings_lib.DuplicateLocalFieldSetsWarning])) self.assertTrue( type2.HasFindingTypes( [findings_lib.DuplicateLocalFieldSetsWarning]))
def testEntityTypeUniverseHandlesNamespaceMovesWithIds(self): filepath = _GOOD_PATH + '/file.yaml' folder = entity_type_lib.EntityTypeFolder(_GOOD_PATH) namespace = folder.local_namespace filepath2 = _GOOD_PATH_2 + '/file.yaml' folder2 = entity_type_lib.EntityTypeFolder(_GOOD_PATH_2) namespace2 = folder2.local_namespace entity_type1 = entity_type_lib.EntityType(typename='one', filepath=filepath, description='hi', uid='1') namespace.InsertType(entity_type1) entity_type1a = entity_type_lib.EntityType(typename='oneA', filepath=filepath2, description='hi', uid='1') namespace2.InsertType(entity_type1a) types_universe = entity_type_lib.EntityTypeUniverse([folder, folder2]) findings = types_universe.GetFindings() self.assertLen(findings, 2) self.assertTrue( types_universe.HasFindingTypes([findings_lib.DuplicateIdsError])) self.assertTrue( entity_type1.HasFindingTypes([findings_lib.DuplicateIdsError])) self.assertTrue( entity_type1a.HasFindingTypes([findings_lib.DuplicateIdsError]))
def testInheritanceCycleAcrossNamepsaces(self): type1 = entity_type_lib.EntityType(filepath='path/to/ANIMAL/mammal', typename='dog', description='canine animal', local_field_tuples=_F(['/woof']), parents=['ANIMAL/wolf']) type2 = entity_type_lib.EntityType(filepath='path/to/ANIMAL/mammal', typename='wolf', description='canine animal', local_field_tuples=_F(['/growl']), parents=['FUZZY_ANIMAL/dingo']) type3 = entity_type_lib.EntityType( filepath='path/to/FUZZY_ANIMAL/mammal', typename='dingo', description='canine animal', local_field_tuples=_F(['/wag']), parents=['ANIMAL/dog']) namespace = entity_type_lib.TypeNamespace(namespace='ANIMAL') namespace.InsertType(type1) namespace.InsertType(type2) namespace2 = entity_type_lib.TypeNamespace(namespace='FUZZY_ANIMAL') namespace2.InsertType(type3) namespace_validate = (namespace_validator.NamespaceValidator( [namespace, namespace2])) self.assertTrue( namespace_validate.HasFindingTypes( [findings_lib.InheritanceCycleError]))
def testBackwardsCompatibilityRemovedType(self): # Two types. One is abstract. ns1_path = '{0}/entity_types/anyfolder'.format('namespace_one') folder1 = entity_type_lib.EntityTypeFolder(ns1_path) ns1 = folder1.local_namespace type1 = entity_type_lib.EntityType(filepath=ns1_path + '/file.yaml', typename='type1', inherited_fields_expanded=True) ns1.InsertType(type1) type2 = entity_type_lib.EntityType(filepath=ns1_path + '/file.yaml', typename='type2', inherited_fields_expanded=True, is_abstract=True) ns1.InsertType(type2) old_uv = entity_type_lib.EntityTypeUniverse([folder1]) # No Types. folder1a = entity_type_lib.EntityTypeFolder(ns1_path) new_uv = entity_type_lib.EntityTypeUniverse([folder1a]) findings = presubmit_validate_types_lib.CheckBackwardsCompatibility( new_uv, old_uv) self.assertLen(findings, 1) self.assertIsInstance(findings[0], findings_lib.RemovedTypeWarning) self.assertIn('type1', str(findings[0]))
def testBackwardsCompatibilityAcrossNamespaces(self): # Two types. One is abstract. ns1_path = '{0}/entity_types/anyfolder'.format('namespace_one') ns2_path = '{0}/entity_types/anyfolder'.format('namespace_two') folder1 = entity_type_lib.EntityTypeFolder(ns1_path) ns1 = folder1.local_namespace type1 = entity_type_lib.EntityType(filepath=ns1_path + '/file.yaml', typename='type1', inherited_fields_expanded=True, uid='1') ns1.InsertType(type1) old_uv = entity_type_lib.EntityTypeUniverse([folder1]) folder1a = entity_type_lib.EntityTypeFolder(ns1_path) folder2 = entity_type_lib.EntityTypeFolder(ns2_path) ns2 = folder1a.local_namespace type1a = entity_type_lib.EntityType(filepath=ns2_path + '/file.yaml', typename='type1a', inherited_fields_expanded=True, uid='1') ns2.InsertType(type1a) new_uv = entity_type_lib.EntityTypeUniverse([folder1a, folder2]) findings = presubmit_validate_types_lib.CheckBackwardsCompatibility( new_uv, old_uv) self.assertEmpty(findings)
def testAddMultipleTypes(self): fields_universe = field_lib.FieldUniverse([]) fields_universe._namespace_map = { '': [field_lib.Field('animal')], 'ANIMAL': [field_lib.Field('meow')] } folderpath = 'ANIMAL/entity_types' type_folder = entity_type_lib.EntityTypeFolder(folderpath, fields_universe) self.assertFalse(type_folder.GetFindings()) rel_typepath = os.path.join(folderpath, 'mammal') # bad entity type, field 'claws' is undefined. bad_type = entity_type_lib.EntityType( filepath=rel_typepath, typename='cat', description='feline animal', local_field_tuples=_FS(['ANIMAL/meow', 'ANIMAL/claws', '/animal'])) # good entity type good_type = entity_type_lib.EntityType( filepath=rel_typepath, typename='kitty', description='feline animal', local_field_tuples=_FS(['ANIMAL/meow', '/animal'])) type_folder._AddType(good_type) type_folder._AddType(bad_type) self.assertTrue( type_folder.local_namespace.HasFindingTypes( [findings_lib.UndefinedFieldError])) self.assertLen(type_folder.local_namespace.valid_types_map, 1) self.assertIn(good_type.typename, type_folder.local_namespace.valid_types_map) self.assertEqual( type_folder.local_namespace.valid_types_map.get(good_type.typename), good_type)
def testAddDuplicateTypes(self): fields_universe = field_lib.FieldUniverse([]) fields_universe._namespace_map = {'': ('animal'), 'ANIMAL': ('meow')} folderpath = 'ANIMAL/entity_types' type_folder = entity_type_lib.EntityTypeFolder(folderpath, fields_universe) self.assertFalse(type_folder.GetFindings()) rel_typepath = os.path.join(folderpath, 'mammal') # entity type entity_type = entity_type_lib.EntityType( filepath=rel_typepath, typename='kitty', description='feline animal', local_field_tuples=_FS(['ANIMAL/meow', '/animal'])) # duplicate type dup_type = entity_type_lib.EntityType(filepath=rel_typepath, typename='kitty', description='feline animal', local_field_tuples=_FS( ['ANIMAL/meow', '/animal'])) type_folder._AddType(entity_type) type_folder._AddType(dup_type) self.assertTrue( type_folder.local_namespace.HasFindingTypes( [findings_lib.DuplicateTypesError])) self.assertLen(type_folder.local_namespace.valid_types_map, 1) self.assertIn(entity_type.typename, type_folder.local_namespace.valid_types_map) self.assertEqual( type_folder.local_namespace.valid_types_map.get( entity_type.typename), entity_type)
def testAddTypeWithNamespacedField(self): fields_universe = field_lib.FieldUniverse([]) fields_universe._namespace_map = { '': [field_lib.Field('animal')], 'ANIMAL': [field_lib.Field('meow')], 'ATTACK': [field_lib.Field('claws')] } folderpath = 'ANIMAL/entity_types' type_folder = entity_type_lib.EntityTypeFolder(folderpath, fields_universe) self.assertFalse(type_folder.GetFindings()) rel_typepath = os.path.join(folderpath, 'mammal') # field 'claws' is undefined. entity_type = entity_type_lib.EntityType( filepath=rel_typepath, typename='cat', description='feline animal', local_field_tuples=_FS(['ANIMAL/meow', 'ATTACK/claws', '/animal'])) type_folder._AddType(entity_type) self.assertFalse(type_folder.local_namespace.GetFindings()) self.assertLen(type_folder.local_namespace.valid_types_map, 1) self.assertIn(entity_type.typename, type_folder.local_namespace.valid_types_map) self.assertEqual( type_folder.local_namespace.valid_types_map.get( entity_type.typename), entity_type)
def testGetFieldFromConfigText(self): entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', description='canine animal', local_field_tuples=_FS(['HAPPY/wag', '/woof', 'ANIMALS/fuzzy']), inherited_fields_expanded=True, namespace=entity_type_lib.TypeNamespace('ANIMALS')) self.assertEqual( 'fuzzy', entity_type.GetFieldFromConfigText('fuzzy').field.field) self.assertEqual( 'fuzzy', entity_type.GetFieldFromConfigText('ANIMALS/fuzzy').field.field) self.assertEqual( 'wag', entity_type.GetFieldFromConfigText('HAPPY/wag').field.field) self.assertEqual( 'woof', entity_type.GetFieldFromConfigText('woof').field.field) self.assertEqual( 'woof', entity_type.GetFieldFromConfigText('/woof').field.field) self.assertIsNone(entity_type.GetFieldFromConfigText('/wag')) self.assertIsNone(entity_type.GetFieldFromConfigText('wag'))
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 testIllegalTypenameType(self): entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', typename=True, description='false') errors = entity_type.GetFindings() self.assertLen(errors, 1) self.assertTrue( entity_type.HasFindingTypes([findings_lib.IllegalKeyTypeError])) self.assertFalse(entity_type.IsValid())
def testBackwardsCompatibilityOptionalAddedFieldsOk(self): # Two types. One is abstract. ns1_path = '{0}/entity_types/anyfolder'.format('namespace_one') folder1 = entity_type_lib.EntityTypeFolder(ns1_path) ns1 = folder1.local_namespace type1 = entity_type_lib.EntityType(filepath=ns1_path + '/file.yaml', typename='type1', local_field_tuples=_F(['local1']), inherited_fields_expanded=True) ns1.InsertType(type1) type2 = entity_type_lib.EntityType(filepath=ns1_path + '/file.yaml', typename='type2', local_field_tuples=_F(['local1']), inherited_fields_expanded=True) ns1.InsertType(type2) old_uv = entity_type_lib.EntityTypeUniverse([folder1]) # Same Types with added fields folder1a = entity_type_lib.EntityTypeFolder(ns1_path) ns1a = folder1a.local_namespace type1a = entity_type_lib.EntityType( filepath=ns1_path + '/file.yaml', typename='type1', local_field_tuples=[_F1('local1', False), _F1('local2', False)], inherited_fields_expanded=True) ns1a.InsertType(type1a) type2a = entity_type_lib.EntityType( filepath=ns1_path + '/file.yaml', typename='type2', local_field_tuples=[_F1('local1', False), _F1('local2', True)], inherited_fields_expanded=True) ns1a.InsertType(type2a) new_uv = entity_type_lib.EntityTypeUniverse([folder1a]) findings = presubmit_validate_types_lib.CheckBackwardsCompatibility( new_uv, old_uv) self.assertLen(findings, 1) self.assertTrue( type1a.HasFindingTypes([findings_lib.AddedFieldWarning])) self.assertFalse( type2a.HasFindingTypes([findings_lib.AddedFieldWarning]))
def testGoodEntityType(self): entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', typename='dog', description='canine animal', local_field_tuples=_FS(['/woof', '/wag']), parents=['wolf', 'ANIMAL', 'dingo']) self.assertFalse(entity_type.GetFindings()) self.assertTrue(entity_type.IsValid())
def testMissingDescription(self): entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', typename='dog') errors = entity_type.GetFindings() self.assertLen(errors, 1) self.assertTrue( entity_type.HasFindingTypes( [findings_lib.MissingDescriptionWarning])) self.assertTrue(entity_type.IsValid())
def testMissingTypename(self): entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', description='canine animal', local_field_tuples=_FS(['/woof', '/wag'])) errors = entity_type.GetFindings() self.assertLen(errors, 1) self.assertTrue( entity_type.HasFindingTypes([findings_lib.MissingTypenameError])) self.assertFalse(entity_type.IsValid())
def testHasField(self): entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', description='canine animal', local_field_tuples=_FS(['HAPPY/wag']), inherited_fields_expanded=True) entity_type.inherited_field_names['/woof'] = _F('/woof') self.assertTrue(entity_type.HasField('HAPPY/wag')) self.assertTrue(entity_type.HasField('/woof')) self.assertFalse(entity_type.HasField('SAD/wag'))
def testDuplicateFieldsWithDifferentCaps(self): entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', typename='cat', description='feline animal', local_field_tuples=_FS(['/mEow', '/claws', '/meow'])) errors = entity_type.GetFindings() self.assertLen(errors, 1) self.assertTrue( entity_type.HasFindingTypes([findings_lib.DuplicateFieldError])) self.assertFalse(entity_type.IsValid())
def testSmallDeviationFindingEqualityKey(self): parents = ['wolf', 'ANIMAL', 'dingo'] entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', typename='dog', description='canine animal') key = object() finding1 = findings_lib.SmallFieldDeviationWarning( entity_type, parents, 3, _F(['a', 'b']), key) finding2 = findings_lib.SmallFieldDeviationWarning( entity_type, parents, 3, _F(['a', 'b']), key) self.assertEqual(finding1, finding2)
def testDuplicateParents(self): entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', typename='dog', description='canine animal', parents=['wolf', 'wolf']) errors = entity_type.GetFindings() self.assertLen(errors, 1) self.assertTrue( entity_type.HasFindingTypes([findings_lib.DuplicateParentError])) self.assertFalse(entity_type.IsValid())
def testTypesWithEmptyFields(self): type1 = entity_type_lib.EntityType(filepath='path/to/ANIMAL/mammal', typename='dog', description='canine animal') type2 = entity_type_lib.EntityType(filepath='path/to/ANIMAL/mammal', typename='wolf', description='canine animal') type_namespace = entity_type_lib.TypeNamespace(namespace='ANIMAL') type_namespace.InsertType(type1) type_namespace.InsertType(type2) namespace_validator.NamespaceValidator([type_namespace]) self.assertFalse( type1.HasFindingTypes( [findings_lib.DuplicateLocalFieldSetsWarning])) self.assertFalse( type2.HasFindingTypes( [findings_lib.DuplicateLocalFieldSetsWarning]))
def testSetsInheritedOptionality(self): type1 = entity_type_lib.EntityType(filepath='path/to/ANIMAL/mammal', typename='dog', description='canine animal', local_field_tuples=[ _F1('/woof', optional=True), _F1('/growl', optional=True) ], parents=[]) type2 = entity_type_lib.EntityType(filepath='path/to/ANIMAL/mammal', typename='wolf', description='canine animal', local_field_tuples=[ _F1('/woof', optional=True), _F1('/growl', optional=False) ], parents=[]) type3 = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', typename='dingo', description='canine animal', local_field_tuples=[_F1('/wag', optional=False)], parents=['ANIMAL/dog', 'ANIMAL/wolf']) type_namespace_1 = entity_type_lib.TypeNamespace(namespace='ANIMAL') type_namespace_1.InsertType(type1) type_namespace_1.InsertType(type2) type_namespace_1.InsertType(type3) namespace_validate = (namespace_validator.NamespaceValidator( [type_namespace_1])) self.assertTrue(namespace_validate.IsValid()) field_map = type3.GetAllFields() self.assertFalse(field_map['/growl'].optional) self.assertTrue(field_map['/woof'].optional) self.assertFalse(field_map['/wag'].optional)
def testGoodFieldIncrement(self): entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', typename='dog', description='canine animal', local_field_tuples=_F(['/woof', '/woof_1'])) type_namespace = entity_type_lib.TypeNamespace(namespace='ANIMAL') type_namespace.InsertType(entity_type) namespace_validate = (namespace_validator.NamespaceValidator( [type_namespace])) self.assertTrue(namespace_validate.IsValid())
def testInheritedFieldsSet(self): entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', typename='dog', description='canine animal', local_field_tuples=_FS(['/woof', '/wag']), inherited_fields_expanded=True) errors = entity_type.GetFindings() self.assertLen(errors, 1) self.assertTrue( entity_type.HasFindingTypes([findings_lib.InheritedFieldsSetError])) self.assertFalse(entity_type.IsValid())
def testInheritingFromPassthroughIsError(self): passthrough_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/gateway', typename='dog', description='a gateway to dogs', allow_undefined_fields=True) entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', typename='dingo', description='canine animal', local_field_tuples=_F(['/woof']), parents=['ANIMAL/dog']) type_namespace = entity_type_lib.TypeNamespace(namespace='ANIMAL') type_namespace.InsertType(passthrough_type) type_namespace.InsertType(entity_type) namespace_validate = (namespace_validator.NamespaceValidator( [type_namespace])) self.assertTrue( namespace_validate.HasFindingTypes( [findings_lib.PassthroughParentError]))
def testBadFieldFormat(self): entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', typename='cat', description='feline animal', local_field_tuples=_FS(['/meow', 'NS/wrong/claws'])) errors = entity_type.GetFindings() self.assertLen(errors, 1) self.assertTrue( entity_type.HasFindingTypes( [findings_lib.UnrecognizedFieldFormatError])) self.assertFalse(entity_type.IsValid())
def testGoodPassthroughType(self): passthrough_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/gateway', typename='dog', description='a gateway to dogs', allow_undefined_fields=True, local_field_tuples=_F(['/woof_1', '/woof_2', '/woof_3'])) type_namespace = entity_type_lib.TypeNamespace(namespace='ANIMAL') type_namespace.InsertType(passthrough_type) namespace_validate = (namespace_validator.NamespaceValidator( [type_namespace])) self.assertTrue(namespace_validate.IsValid())
def testInheritTypesFromDifferentNamespaceError(self): type1 = entity_type_lib.EntityType(filepath='path/to/ANIMAL/mammal', typename='dog', description='canine animal', local_field_tuples=_F( ['/woof', '/howl'])) type2 = entity_type_lib.EntityType(filepath='path/to/INSECT/fly', typename='moth', description='flying insect.', local_field_tuples=_F(['/wings']), parents=['ANIMALLLLL/dog']) type_namespace_1 = entity_type_lib.TypeNamespace(namespace='ANIMAL') type_namespace_1.InsertType(type1) type_namespace_2 = entity_type_lib.TypeNamespace(namespace='INSECT') type_namespace_2.InsertType(type2) namespace_validate = (namespace_validator.NamespaceValidator( [type_namespace_1, type_namespace_2])) self.assertTrue( namespace_validate.HasFindingTypes( [findings_lib.NonexistentParentError]))
def testIllegalAbstractPassthroughType(self): entity_type = entity_type_lib.EntityType( filepath='path/to/ANIMAL/mammal', typename='dog', description='canine animal', is_abstract=True, allow_undefined_fields=True) errors = entity_type.GetFindings() self.assertLen(errors, 1) self.assertTrue( entity_type.HasFindingTypes([findings_lib.AbstractPassthroughTypeError ])) self.assertFalse(entity_type.IsValid())