Exemplo n.º 1
0
 def testAddValidSubfield(self):
   sff = subfield_lib.SubfieldFolder(_GOOD_PATH)
   ctx = findings_lib.FileContext('{0}/file.yaml'.format(_GOOD_PATH))
   sff.AddSubfield(
       subfield_lib.Subfield('good', subfield_lib.SubfieldCategory.DESCRIPTOR,
                             'hi', ctx))
   self.assertIn('good', sff.local_namespace.subfields)
Exemplo n.º 2
0
    def testSubfieldUniverseGetFindings(self):
        context = findings_lib.FileContext(_GOOD_PATH + '/file.yaml')
        folder = subfield_lib.SubfieldFolder(_GOOD_PATH)
        folder.AddFinding(
            findings_lib.InconsistentFileLocationError('', context))
        namespace = folder.local_namespace
        namespace.AddFinding(
            findings_lib.DuplicateSubfieldDefinitionError(
                namespace,
                subfield_lib.Subfield(
                    'two', subfield_lib.SubfieldCategory.POINT_TYPE), context))
        subfield_one = subfield_lib.Subfield(
            'one', subfield_lib.SubfieldCategory.POINT_TYPE, 'thing')
        subfield_one.AddFinding(
            findings_lib.MissingSubfieldDescriptionWarning('one', context))
        namespace.InsertSubfield(subfield_one)

        subfields_universe = subfield_lib.SubfieldUniverse([folder])

        findings = subfields_universe.GetFindings()
        self.assertLen(findings, 3)
        self.assertTrue(
            subfields_universe.HasFindingTypes([
                findings_lib.InconsistentFileLocationError,
                findings_lib.DuplicateSubfieldDefinitionError,
                findings_lib.MissingSubfieldDescriptionWarning
            ]))
        self.assertFalse(subfields_universe.IsValid())
Exemplo n.º 3
0
    def testAddFromConfig(self):
        doc = {
            'aggregation': {
                'agg': 'aggD'
            },
            'aggregation_descriptor': {
                'aggdesc': 'aggDescD'
            },
            'component': {
                'comp': 'compD'
            },
            'descriptor': {
                'desc': 'descD'
            },
            'measurement_descriptor': {
                'mdesc': 'mdescD'
            },
            'measurement': {
                'meas': 'measD'
            },
            'point_type': {
                'ptype': 'ptypeD'
            }
        }

        sff = subfield_lib.SubfieldFolder(_GOOD_PATH)
        sff.AddFromConfig([doc], '{0}/file.yaml'.format(_GOOD_PATH))
        ns = sff.local_namespace
        self.assertCountEqual(
            ['agg', 'aggdesc', 'comp', 'desc', 'mdesc', 'meas', 'ptype'],
            ns.subfields)
        self.assertEmpty(sff.GetFindings())
Exemplo n.º 4
0
  def testAddFromConfigEmptyBlock(self):
    doc = {'aggregation': {}}

    sff = subfield_lib.SubfieldFolder(_GOOD_PATH)
    sff.AddFromConfig([doc], '{0}/file.yaml'.format(_GOOD_PATH))

    self.assertIsInstance(sff.GetFindings()[0], findings_lib.EmptyBlockWarning)
Exemplo n.º 5
0
 def testAddInvalidSubfieldFails(self):
   sff = subfield_lib.SubfieldFolder(_GOOD_PATH)
   ctx = findings_lib.FileContext('{0}/file.yaml'.format(_GOOD_PATH))
   sff.AddSubfield(
       subfield_lib.Subfield('1-bad', subfield_lib.SubfieldCategory.DESCRIPTOR,
                             'hi', ctx))
   self.assertIsInstance(sff.GetFindings()[0],
                         findings_lib.IllegalCharacterError)
Exemplo n.º 6
0
 def testAddSubfieldWithUpperFails(self):
     sff = subfield_lib.SubfieldFolder(_GOOD_PATH)
     ctx = findings_lib.FileContext('{0}/file.yaml'.format(_GOOD_PATH))
     sf = subfield_lib.Subfield('gOod',
                                subfield_lib.SubfieldCategory.DESCRIPTOR,
                                'hi', ctx)
     sff.AddSubfield(sf)
     self.assertIsInstance(sff.GetFindings()[0],
                           findings_lib.InvalidSubfieldNameError)
Exemplo n.º 7
0
    def testAddFromConfigNotYaml(self):
        doc = {
            'aggregation': {
                'agg': 'aggD'
            },
        }

        sff = subfield_lib.SubfieldFolder(_GOOD_PATH)
        sff.AddFromConfig([doc], '{0}/file.yaaaml'.format(_GOOD_PATH))

        self.assertIsInstance(sff.GetFindings()[0],
                              findings_lib.InconsistentFileLocationError)
Exemplo n.º 8
0
 def testAddDuplicateSubfieldFails(self):
   sff = subfield_lib.SubfieldFolder(_GOOD_PATH)
   ctx = findings_lib.FileContext('{0}/file.yaml'.format(_GOOD_PATH))
   sf = subfield_lib.Subfield('good', subfield_lib.SubfieldCategory.DESCRIPTOR,
                              'hi', ctx)
   sf2 = subfield_lib.Subfield(
       'good', subfield_lib.SubfieldCategory.POINT_TYPE, 'hi2', ctx)
   sff.AddSubfield(sf)
   self.assertEmpty(sff.local_namespace.GetFindings())
   sff.AddSubfield(sf2)
   self.assertIsInstance(sff.local_namespace.GetFindings()[0],
                         findings_lib.DuplicateSubfieldDefinitionError)
Exemplo n.º 9
0
  def testSubfieldUniverseGetSubfieldsMap(self):
    # Create field folders
    folder = subfield_lib.SubfieldFolder(_GOOD_PATH)
    namespace = folder.local_namespace
    namespace.InsertSubfield(subfield_lib.Subfield(
        'one', subfield_lib.SubfieldCategory.POINT_TYPE))
    namespace.InsertSubfield(subfield_lib.Subfield(
        'two', subfield_lib.SubfieldCategory.POINT_TYPE))
    subfields_universe = subfield_lib.SubfieldUniverse([folder])

    subfields_map = subfields_universe.GetSubfieldsMap('mynamespace')

    self.assertIn('one', subfields_map)
    self.assertIn('two', subfields_map)
Exemplo n.º 10
0
 def testCreateSubfieldFolderWithBadSubfolder(self):
     with self.assertRaises(RuntimeError):
         subfield_lib.SubfieldFolder(_BAD_SUBFOLDER)
Exemplo n.º 11
0
 def testCreateSubfieldFolderWithCorrectPath(self):
     sff = subfield_lib.SubfieldFolder(_GOOD_PATH)
     self.assertEqual('mynamespace', sff.local_namespace.namespace)
Exemplo n.º 12
0
 def CreateSubfieldFolder(folderpath, parent_namespace):
     del parent_namespace  # Unused by SubfieldFolder.
     return subfield_lib.SubfieldFolder(folderpath)
Exemplo n.º 13
0
#
#    https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an AS IS BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations
# under
# the
# License.
"""Sets up a minimal subfield universe required for testing."""

from yamlformat.validator import subfield_lib

SUBFIELD_FOLDER = subfield_lib.SubfieldFolder(folderpath='subfields')
SUBFIELD_UNIVERSE = subfield_lib.SubfieldUniverse(folders=[SUBFIELD_FOLDER])

SUBFIELD_FOLDER.AddFromConfig(config_filename='subfields/subfields.yaml',
                              documents=[{
                                  'aggregation': {
                                      'average': 'foobar',
                                      'max': 'foobar',
                                      'min': 'foobar',
                                      'total': 'foobar',
                                  },
                                  'component': {
                                      'battery': 'foobar',
                                      'cable': 'foobar',
                                      'coil': 'foobar',
                                      'compressor': 'foobar',