Example #1
0
    def testFieldsUniverseGetFindings(self):
        context = findings_lib.FileContext(_GOOD_PATH + '/file.yaml')
        folder = field_lib.FieldFolder(_GOOD_PATH)
        folder.AddFinding(
            findings_lib.InconsistentFileLocationError('', context))
        namespace = folder.local_namespace
        namespace.AddFinding(
            findings_lib.UnrecognizedSubfieldError(['any'],
                                                   field_lib.Field(
                                                       'two',
                                                       file_context=context)))
        field = field_lib.Field('one', file_context=context)
        # Currently there are no warnings for fields, so using a subfield warning
        field.AddFinding(
            findings_lib.MissingSubfieldDescriptionWarning('one', context))
        namespace.InsertField(field)

        fields_universe = field_lib.FieldUniverse([folder])

        findings = fields_universe.GetFindings()
        self.assertLen(findings, 3)
        self.assertTrue(
            fields_universe.HasFindingTypes([
                findings_lib.InconsistentFileLocationError,
                findings_lib.UnrecognizedSubfieldError,
                findings_lib.MissingSubfieldDescriptionWarning
            ]))
        self.assertFalse(fields_universe.IsValid())
    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)
Example #3
0
  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)
Example #4
0
    def testConfigUniverseGetStatesByField(self):
        meow_states = ['HUNGRY', 'SNUGGLY']
        meow_cat = field_lib.Field('meow_cat')
        meow_cat.states = meow_states
        claw_states = ['HISSY', 'BITEY']
        claws_cat = field_lib.Field('claws_cat')
        claws_cat.states = claw_states

        global_folder = field_lib.FieldFolder('fields/anyfolder')
        folder = field_lib.FieldFolder('localnamespace/fields/anyfolder',
                                       global_folder.local_namespace)
        folder.local_namespace.PutIfAbsent(meow_cat)
        global_folder.local_namespace.PutIfAbsent(claws_cat)

        field_universe = field_lib.FieldUniverse([folder, global_folder])

        config_universe = presubmit_validate_types_lib.ConfigUniverse(
            subfield_universe=None,
            field_universe=field_universe,
            entity_type_universe=None,
            state_universe=None,
            connection_universe=None,
            unit_universe=None)

        self.assertSameElements(claw_states,
                                config_universe.GetStatesByField('/claws_cat'))
        self.assertSameElements(
            meow_states,
            config_universe.GetStatesByField('localnamespace/meow_cat'))
Example #5
0
    def testInsertFieldRejectsReversals(self):
        ns = field_lib.FieldNamespace('')
        field = field_lib.Field('field_name')
        field_rev = field_lib.Field('name_field')

        ns.InsertField(field)
        self.assertEmpty(ns.GetFindings())
        ns.InsertField(field_rev)
        self.assertIsInstance(ns.GetFindings()[0],
                              findings_lib.DuplicateFieldDefinitionError)
  def testFieldUniverse(self):
    # Create field folders
    folder = field_lib.FieldFolder(_GOOD_PATH)
    namespace = folder.local_namespace
    namespace.InsertField(field_lib.Field('meow'))
    namespace.InsertField(field_lib.Field('claws'))
    fields_universe = field_lib.FieldUniverse([folder])

    self.assertTrue(fields_universe.IsFieldDefined('meow', 'mynamespace'))
    self.assertTrue(fields_universe.IsFieldDefined('claws', 'mynamespace'))
    self.assertFalse(fields_universe.IsFieldDefined('clawsss', 'mynamespace'))
Example #7
0
    def testAddFieldRejectsDuplicateSubfields(self):
        folder = field_lib.FieldFolder('/fields')
        field = 'field_name'
        field_dup = 'field_field_name'
        rel_filepath = '/fields/f.yaml'
        context = findings_lib.FileContext(rel_filepath)

        folder.AddField(field_lib.Field(field, file_context=context))
        self.assertEmpty(folder.GetFindings())
        folder.AddField(field_lib.Field(field_dup, file_context=context))
        self.assertLen(folder.GetFindings(), 1)
        self.assertIsInstance(folder.GetFindings()[0],
                              findings_lib.DuplicateSubfieldError)
  def testInsertDuplicateFieldInGlobalNamespace(self):
    ns = field_lib.FieldNamespace('')

    field = field_lib.Field('field_name')
    field_clone = field_lib.Field('field_name')

    ns.InsertField(field)
    self.assertEmpty(ns.GetFindings())
    ns.InsertField(field_clone)
    self.assertEqual(ns.fields, {frozenset({'field', 'name'}): field})
    self.assertEqual(id(ns.fields[frozenset({'field', 'name'})]), id(field))
    self.assertLen(ns.GetFindings(), 1)
    self.assertIsInstance(ns.GetFindings()[0],
                          findings_lib.DuplicateFieldDefinitionError)
Example #9
0
    def testInsertFieldInGlobalNamespaceNoSubfields(self):
        ns = field_lib.FieldNamespace('')
        field = field_lib.Field('field_name')

        ns.InsertField(field)
        self.assertEmpty(ns.GetFindings())
        self.assertEqual(ns.fields[frozenset({'field', 'name'})], field)
  def testInsertFieldInLocalNamespaceCanUpLevel(self):
    global_ns = field_lib.FieldNamespace('')
    ns = field_lib.FieldNamespace('local', parent_namespace=global_ns)
    field = field_lib.Field('field_name')

    ns.InsertField(field)
    self.assertEmpty(ns.GetFindings())
    self.assertEqual(global_ns.fields, {frozenset({'field', 'name'}): field})
  def testInsertFieldInLocalNamespaceMatchesGlobalSubs(self):
    sf_dict = {'field': subfield_lib.Subfield('field', DESCRIPTOR)}
    sf_glob_dict = {'name': subfield_lib.Subfield('name', POINT_TYPE)}
    global_ns = field_lib.FieldNamespace('', subfields=sf_glob_dict)
    ns = field_lib.FieldNamespace(
        'local', subfields=sf_dict, parent_namespace=global_ns)

    field = field_lib.Field('field_name')
    bad_field = field_lib.Field('field_name_unmatched')

    ns.InsertField(field)
    self.assertEmpty(ns.GetFindings())
    ns.InsertField(bad_field)
    self.assertEqual(ns.fields, {frozenset({'field', 'name'}): field})
    self.assertLen(ns.GetFindings(), 1)
    self.assertIsInstance(ns.GetFindings()[0],
                          findings_lib.MissingSubfieldError)
  def testInsertDuplicateFieldInLocalNamespace(self):
    sf_dict = {'field': subfield_lib.Subfield('field', DESCRIPTOR)}
    sf_glob_dict = {'name': subfield_lib.Subfield('name', POINT_TYPE)}
    global_ns = field_lib.FieldNamespace('', subfields=sf_glob_dict)
    ns = field_lib.FieldNamespace(
        'local', subfields=sf_dict, parent_namespace=global_ns)

    field = field_lib.Field('field_name')
    field_clone = field_lib.Field('field_name')

    ns.InsertField(field)
    self.assertEmpty(ns.GetFindings())
    ns.InsertField(field_clone)
    self.assertEqual(ns.fields, {frozenset({'field', 'name'}): field})
    self.assertEqual(id(ns.fields[frozenset({'field', 'name'})]), id(field))
    self.assertLen(ns.GetFindings(), 1)
    self.assertIsInstance(ns.GetFindings()[0],
                          findings_lib.DuplicateFieldDefinitionError)
Example #13
0
    def testInsertRespectsRequiredSubfields(self):
        sf_dict = {
            'first': subfield_lib.Subfield('first', DESCRIPTOR),
            'second': subfield_lib.Subfield('second', DESCRIPTOR),
        }
        ns = field_lib.FieldNamespace('local', subfields=sf_dict)
        field = field_lib.Field('first_second')

        ns.InsertField(field)
        self.assertIsInstance(ns.GetFindings()[0],
                              findings_lib.InvalidFieldConstructionError)
  def testInsertFieldInLocalNamespace(self):
    sf_dict = {'field': subfield_lib.Subfield('field', DESCRIPTOR)}
    sf_glob_dict = {'name': subfield_lib.Subfield('name', POINT_TYPE)}
    global_ns = field_lib.FieldNamespace('', subfields=sf_glob_dict)
    ns = field_lib.FieldNamespace(
        'local', subfields=sf_dict, parent_namespace=global_ns)
    field = field_lib.Field('field_name')

    ns.InsertField(field)
    self.assertEmpty(ns.GetFindings())
    self.assertEqual(ns.fields, {frozenset({'field', 'name'}): field})
Example #15
0
    def testInsertFieldValidatesMultipleDescriptors(self):
        sf_dict = {
            'first': subfield_lib.Subfield('first', DESCRIPTOR),
            'second': subfield_lib.Subfield('second', DESCRIPTOR),
            'third': subfield_lib.Subfield('third', POINT_TYPE)
        }
        ns = field_lib.FieldNamespace('local', subfields=sf_dict)
        field = field_lib.Field('first_second_third')

        ns.InsertField(field)
        self.assertEmpty(ns.GetFindings())
Example #16
0
    def testInsertFieldInGlobalNamespaceMissingSubfields(self):
        sf_dict = {'field': subfield_lib.Subfield('field', POINT_TYPE)}
        ns = field_lib.FieldNamespace('', subfields=sf_dict)

        field = field_lib.Field('field_name')

        ns.InsertField(field)
        self.assertEqual(ns.fields, {})
        self.assertLen(ns.GetFindings(), 1)
        self.assertIsInstance(ns.GetFindings()[0],
                              findings_lib.UnrecognizedSubfieldError)
Example #17
0
    def testAggregationDescriptorFailsWithoutAggregation(self):
        """Check that aggregation descriptors fail without associated aggregation."""
        sf_dict = {
            'first': subfield_lib.Subfield('first', AGGREGATION_DESCRIPTOR),
            'second': subfield_lib.Subfield('second', POINT_TYPE)
        }
        ns = field_lib.FieldNamespace('local', subfields=sf_dict)
        field = field_lib.Field('first_second')

        ns.InsertField(field)
        self.assertIsInstance(ns.GetFindings()[0],
                              findings_lib.InvalidFieldConstructionError)
Example #18
0
    def testInsertRejectsSetXWithNoMeasurement(self):
        sf_dict = {
            'first': subfield_lib.Subfield('first', COMPONENT),
            'sensor': subfield_lib.Subfield('sensor', POINT_TYPE),
            'setpoint': subfield_lib.Subfield('setpoint', POINT_TYPE),
            'accumulator': subfield_lib.Subfield('accumulator', POINT_TYPE)
        }
        ns = field_lib.FieldNamespace('local', subfields=sf_dict)
        field = field_lib.Field('first_sensor')
        field2 = field_lib.Field('first_setpoint')
        field3 = field_lib.Field('first_accumulator')

        ns.InsertField(field)
        self.assertIsInstance(ns.GetFindings()[0],
                              findings_lib.InvalidFieldConstructionError)
        ns.InsertField(field2)
        self.assertIsInstance(ns.GetFindings()[1],
                              findings_lib.InvalidFieldConstructionError)
        ns.InsertField(field3)
        self.assertIsInstance(ns.GetFindings()[1],
                              findings_lib.InvalidFieldConstructionError)
Example #19
0
    def testInsertRejectsBadOrder(self):
        sf_dict = {
            'first': subfield_lib.Subfield('first', DESCRIPTOR),
            'second': subfield_lib.Subfield('second', COMPONENT),
            'third': subfield_lib.Subfield('third', POINT_TYPE)
        }
        ns = field_lib.FieldNamespace('local', subfields=sf_dict)
        field = field_lib.Field('second_first_third')

        ns.InsertField(field)
        self.assertIsInstance(ns.GetFindings()[0],
                              findings_lib.InvalidFieldConstructionError)
Example #20
0
    def testInsertRespectsPointTypeCount(self):
        sf_dict = {
            'first': subfield_lib.Subfield('first', MEASUREMENT),
            'second': subfield_lib.Subfield('second', POINT_TYPE),
            'third': subfield_lib.Subfield('third', POINT_TYPE)
        }
        ns = field_lib.FieldNamespace('local', subfields=sf_dict)
        field = field_lib.Field('first_second_third')

        ns.InsertField(field)
        self.assertIsInstance(ns.GetFindings()[0],
                              findings_lib.InvalidFieldConstructionError)
Example #21
0
    def testFieldUniverseGetFieldMap(self):
        meow_cat = field_lib.Field('meow_cat')
        claws_cat = field_lib.Field('claws_cat')
        global_folder = field_lib.FieldFolder(_GOOD_GLOBAL_PATH)
        folder = field_lib.FieldFolder(_GOOD_PATH,
                                       global_folder.local_namespace)
        folder.local_namespace.PutIfAbsent(meow_cat)
        global_folder.local_namespace.PutIfAbsent(claws_cat)

        universe = field_lib.FieldUniverse([folder, global_folder])

        local_fields = universe.GetFieldsMap(_GOOD_NAMESPACE)
        global_fields = universe.GetFieldsMap('')
        all_fields = universe.GetFieldsMap()

        expected_local = {_GOOD_NAMESPACE + '/meow_cat': meow_cat}
        expected_global = {'/claws_cat': claws_cat}
        expected_all = {**expected_local, **expected_global}

        self.assertDictEqual(local_fields, expected_local)
        self.assertDictEqual(global_fields, expected_global)
        self.assertDictEqual(all_fields, expected_all)
Example #22
0
    def testInsertRespectsMeasurementDescriptorCount(self):
        sf_dict = {
            'first': subfield_lib.Subfield('first', MEASUREMENT_DESCRIPTOR),
            'second': subfield_lib.Subfield('second', MEASUREMENT_DESCRIPTOR),
            'third': subfield_lib.Subfield('third', MEASUREMENT),
            'fourth': subfield_lib.Subfield('fourth', POINT_TYPE)
        }
        ns = field_lib.FieldNamespace('local', subfields=sf_dict)
        field = field_lib.Field('first_second_third_fourth')

        ns.InsertField(field)
        self.assertIsInstance(ns.GetFindings()[0],
                              findings_lib.InvalidFieldConstructionError)
Example #23
0
    def testInsertRespectsAggregationCount(self):
        sf_dict = {
            'first': subfield_lib.Subfield('first', AGGREGATION_DESCRIPTOR),
            'second': subfield_lib.Subfield('second', AGGREGATION_DESCRIPTOR),
            'third': subfield_lib.Subfield('third', AGGREGATION),
            'fourth': subfield_lib.Subfield('fourth', POINT_TYPE)
        }
        ns = field_lib.FieldNamespace('local', subfields=sf_dict)
        field = field_lib.Field('first_second_third_fourth')

        ns.InsertField(field)
        self.assertIsInstance(ns.GetFindings()[0],
                              findings_lib.InvalidFieldConstructionError)
Example #24
0
    def testInsertFieldInGlobalNamespaceMatchSubfields(self):
        sf_dict = {
            'field': subfield_lib.Subfield('field', DESCRIPTOR),
            'name': subfield_lib.Subfield('name', POINT_TYPE)
        }

        ns = field_lib.FieldNamespace('', subfields=sf_dict)

        field = field_lib.Field('field_name')

        ns.InsertField(field)
        self.assertEmpty(ns.GetFindings())
        self.assertEqual(ns.fields[frozenset({'field', 'name'})], field)
  def testInsertFieldValidatesSubfieldsInMultipleNamespaces(self):
    sf_dict = {
        'first': subfield_lib.Subfield('first', DESCRIPTOR),
        'third': subfield_lib.Subfield('third', POINT_TYPE)
    }
    sf_glob_dict = {'second': subfield_lib.Subfield('second', DESCRIPTOR)}
    global_ns = field_lib.FieldNamespace('', subfields=sf_glob_dict)
    ns = field_lib.FieldNamespace(
        'local', subfields=sf_dict, parent_namespace=global_ns)
    field = field_lib.Field('first_second_third')

    ns.InsertField(field)
    self.assertEmpty(ns.GetFindings())
Example #26
0
    def testInsertFieldValidatesCorrectConstruction(self):
        sf_dict = {
            'first': subfield_lib.Subfield('first', AGGREGATION),
            'second': subfield_lib.Subfield('second', DESCRIPTOR),
            'third': subfield_lib.Subfield('third', COMPONENT),
            'fourth': subfield_lib.Subfield('fourth', MEASUREMENT_DESCRIPTOR),
            'fifth': subfield_lib.Subfield('fifth', MEASUREMENT),
            'sixth': subfield_lib.Subfield('sixth', POINT_TYPE)
        }
        ns = field_lib.FieldNamespace('local', subfields=sf_dict)
        field = field_lib.Field('first_second_third_fourth_fifth_sixth')

        ns.InsertField(field)
        self.assertEmpty(ns.GetFindings())
    def testAddDuplicateTypes(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')
        # 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)
Example #28
0
    def testInsertRespectsDescriptorLimit(self):
        sf_dict = {
            'a': subfield_lib.Subfield('a', DESCRIPTOR),
            'b': subfield_lib.Subfield('b', DESCRIPTOR),
            'c': subfield_lib.Subfield('c', DESCRIPTOR),
            'd': subfield_lib.Subfield('d', DESCRIPTOR),
            'e': subfield_lib.Subfield('e', DESCRIPTOR),
            'f': subfield_lib.Subfield('f', DESCRIPTOR),
            'g': subfield_lib.Subfield('g', DESCRIPTOR),
            'h': subfield_lib.Subfield('h', DESCRIPTOR),
            'i': subfield_lib.Subfield('i', DESCRIPTOR),
            'j': subfield_lib.Subfield('j', DESCRIPTOR),
            'k': subfield_lib.Subfield('k', DESCRIPTOR),
            'l': subfield_lib.Subfield('l', POINT_TYPE)
        }
        ns = field_lib.FieldNamespace('local', subfields=sf_dict)
        field = field_lib.Field('a_b_c_d_e_f_g_h_i_j_k_l')

        ns.InsertField(field)
        self.assertIsInstance(ns.GetFindings()[0],
                              findings_lib.InvalidFieldConstructionError)
Example #29
0
 def testGetSubFieldList(self):
     field = field_lib.Field('test_name')
     expected = ['test', 'name']
     self.assertEqual(field.subfields, expected)