Ejemplo n.º 1
0
 def test_read_no_type(self):
   manifest_source, _ = self.get_manifest_source()
   manifest_source[1].pop(sample_manifest.Manifest.SCHEMA_TYPE_KEY)
   manifest = sample_manifest.Manifest('language', 'sample')
   manifest.read_sources([manifest_source])
   with self.assertRaises(Exception):
     manifest.index()
    def test_get_one(self):
        manifest_source, (expect_alice, expect_bob, expect_carol,
                          expect_dan) = self.get_manifest_source()

        manifest = sample_manifest.Manifest('language', 'sample')
        manifest.read_sources([manifest_source])
        manifest.index()

        self.assertEqual(expect_alice, manifest.get_one('python', 'alice'))
        self.assertEqual(
            expect_alice,
            manifest.get_one('python', 'alice', canonical='trivial'))
        self.assertEqual(
            expect_alice,
            manifest.get_one('python', 'alice', path=expect_alice['path']))
        self.assertIsNone(manifest.get_one('python', 'alice', foo='bar'))

        self.assertEqual(expect_bob, manifest.get_one('python', 'robert'))
        self.assertEqual(expect_bob,
                         manifest.get_one('python', 'robert', tag='guide'))
        self.assertEqual(
            expect_bob,
            manifest.get_one('python', 'robert', path=expect_bob['path']))

        self.assertEqual(
            expect_carol,
            manifest.get_one('', 'math', path=expect_carol['path']))
        self.assertEqual(expect_dan,
                         manifest.get_one('', 'math', path=expect_dan['path']))

        self.assertIsNone(manifest.get_one('foo', 'alice'))

        self.assertRaises(sample_manifest.ItemNotUniqueError, manifest.get_one,
                          '', 'math')
Ejemplo n.º 3
0
  def test_braces(self):   ### combine with above
    list_name = 'mysamples'
    manifest_content = {
        sample_manifest.Manifest.SCHEMA_TYPE_KEY:
            '{}/{}'.format(sample_manifest.Manifest.SCHEMA_TYPE_VALUE,
                           list_name),
        sample_manifest.Manifest.SCHEMA_VERSION_KEY: 3,
        list_name: [
            {
                'greetings': 'teatime-{name}{{',
                'fond_wish': 'hope{{',
                'form': 'Would you like some {drink}? {{maybe}}',
                'base_drink': 'tea',
                'name': 'Mary',
                'drink': '{base_drink} with milk',
                'interruption': 'Excuse me, {name}. {form}'
            }
        ]
    }
    manifest = sample_manifest.Manifest('greetings')
    manifest.read_sources([('manifest_with_braces', manifest_content, {})])
    manifest.index()

    expect_mary = {
        'name': 'Mary',
        'fond_wish': 'hope{',
        'greetings': 'teatime-Mary{',
        'form': 'Would you like some tea with milk? {maybe}',
        'base_drink': 'tea',
        'drink': 'tea with milk',
        'interruption': 'Excuse me, Mary. Would you like some tea with milk? {maybe}'
    }
    self.assertEqual(expect_mary, manifest.get_one('teatime-Mary{', name='Mary'))
 def test_braces_error_loop(self):
     manifest_content = {
         sample_manifest.Manifest.VERSION_KEY_v1v2:
         2,
         sample_manifest.Manifest.SETS_KEY_v1v2: [{
             'greetings':
             '{drink} time',
             'form':
             'It is {greetings}. Would you like some {drink}?',
             'drink':
             'tea',
             sample_manifest.Manifest.ELEMENTS_KEY_v1v2: [{
                 'name':
                 'Mary',
                 'drink':
                 ' with milk',
                 'greetings':
                 ' {form}',  # cycle
                 'interruption':
                 'Excuse me, {name}. {form}'
             }]
         }]
     }
     manifest = sample_manifest.Manifest('greetings')
     manifest.read_sources([('erroring manifest', manifest_content, {})])
     self.assertRaises(sample_manifest.CycleError, manifest.index)
Ejemplo n.º 5
0
  def do_test_get_(self, getter):
    manifest_source, (expect_alice, expect_bob, expect_carol, expect_dan) = getter()

    manifest = sample_manifest.Manifest('language', 'sample')
    manifest.read_sources([manifest_source])
    manifest.index()

    self.assertEqual([expect_alice], manifest.get('python', 'alice'))
    self.assertEqual([expect_alice],
                     manifest.get('python', 'alice', canonical='trivial'))
    self.assertEqual([expect_alice],
                     manifest.get('python', 'alice', path=expect_alice['path']))
    self.assertEqual([], manifest.get('python', 'alice', foo='bar'))

    self.assertEqual([expect_bob], manifest.get('python', 'robert'))
    self.assertEqual([expect_bob], manifest.get(
        'python', 'robert', tag='guide'))
    self.assertEqual([expect_bob],
                     manifest.get('python', 'robert', path=expect_bob['path']))

    self.assertEqual([expect_carol],
                     manifest.get('', 'math', path=expect_carol['path']))
    self.assertEqual([expect_dan],
                     manifest.get('', 'math', path=expect_dan['path']))

    self.assertIsNone(manifest.get('foo', 'alice'))

    math = list(manifest.get('', 'math'))
    for x in [expect_carol, expect_dan]:
      math.remove(x)
    self.assertEqual(0, len(math))
Ejemplo n.º 6
0
def test_environments(manifest_paths, convention_parameters):
  if convention_parameters is None:
    convention_parameters = []
  num_params = len(convention_parameters)
  if num_params < 1:
    raise Exception('expected at least 1 parameter to convention "tag", got %d: %s'
                    .format(num_params, convention_parameters))

  manifest_files = []
  for path in manifest_paths:
    manifest_files.extend(
        glob.glob(path)
    )  # can do this?: _ = [a_m.extend(g.g(path)) for path in manifest_paths]
  manifest = sample_manifest.Manifest(ENVIRONMENT_KEY, *convention_parameters) # read only, so don't need a copy
  manifest.read_files(*manifest_files)
  manifest.index()

  env_names = []
  env_names = manifest.get_keys()
  if len(env_names) == 0:
    env_names = ['(nolang)']

  environments = []
  for name in env_names:
    description = 'Tags by environment:{} {}'.format(name,
                                                     convention_parameters)
    env  = ManifestEnvironment(name, description, manifest, [name], testcase_settings={'call.target': convention_parameters[0]})
    environments.append(env)
  logging.info('convention "tag" generated environments: {}'.format([env.name() for env in environments]))
  all_env_names.extend(env_names)
  all_environments.extend(environments)
  all_manifests.extend(manifest_files)
  return environments
Ejemplo n.º 7
0
 def setUp(self):
   filename = full_path('testdata/tag_test.manifest.yaml')
   manifest = sample_manifest.Manifest('situation')
   manifest.from_docs(inputs.create_indexed_docs(filename))
   manifest.index()
   self.env = tag.ManifestEnvironment(
       filename, '', manifest, [],
       manifest_options = {'chdir': 'switch-dir-to'})
Ejemplo n.º 8
0
 def setUp(self):
     filename = full_path('testdata/tag_test.manifest.yaml')
     manifest = sample_manifest.Manifest('situation')
     manifest.read_files(filename)
     manifest.index()
     self.env = tag.ManifestEnvironment(
         filename,
         '',
         manifest, [],
         manifest_options={'invocation': 'callthisway'})
    def test_get_all_elements(self):
        manifest_source, (expect_alice, expect_bob, expect_carol,
                          expect_dan) = self.get_manifest_source()

        manifest = sample_manifest.Manifest('language', 'sample')
        manifest.read_sources([manifest_source])
        manifest.index()

        all_elements = [x for x in manifest.get_all_elements()]
        self.assertEqual([expect_alice, expect_bob, expect_carol, expect_dan],
                         all_elements)
Ejemplo n.º 10
0
  def test_braces_v2(self):
    manifest_source = self.get_manifest_source_braces_correct(2)
    manifest = sample_manifest.Manifest('greetings')
    manifest.read_sources([manifest_source])
    manifest.index()

    expect_mary = {
        'name': 'Mary',
        'fond_wish': 'hope{',
        'greetings': 'teatime-Mary{',
        'form': 'Would you like some tea with milk? {maybe}',
        'drink': 'tea with milk',
        'interruption': 'Excuse me, Mary. Would you like some tea with milk? {maybe}'
    }

    self.assertEqual(expect_mary, manifest.get_one('teatime-Mary{', name='Mary'))
Ejemplo n.º 11
0
  def test_read_files_with_implicit_tags(self):
    manifest_dir = os.path.abspath(os.path.join(_ABS_DIR,
                                                'testdata',
                                                'sample_manifest'))
    manifest_h_he_path = os.path.join(manifest_dir,
                                      'h_he',
                                      '12.manifest.yaml')
    manifest_li_be_path = os.path.join(manifest_dir,
                                       'li_be',
                                       '34.manifest.yaml')
    manifest_h_he_dir = os.path.dirname(manifest_h_he_path)
    manifest_li_be_dir = os.path.dirname(manifest_li_be_path)


    manifest = sample_manifest.Manifest('model', 'sample')
    manifest.from_docs(inputs.create_indexed_docs(manifest_h_he_path,
                                                  manifest_li_be_path))
    manifest.index()

    expect_hydrogen = {
        'model': 'periodic',
        'sample': 'hydrogen',
        sample_manifest.IMPLICIT_TAG_SOURCE: manifest_h_he_path,
        sample_manifest.IMPLICIT_TAG_DIR: manifest_h_he_dir,
    }
    expect_helium = {
        'model': 'periodic',
        'sample': 'helium',
        sample_manifest.IMPLICIT_TAG_SOURCE: manifest_h_he_path,
        sample_manifest.IMPLICIT_TAG_DIR: manifest_h_he_dir,
    }
    expect_lithium = {
        'model': 'periodic',
        'sample': 'lithium',
        sample_manifest.IMPLICIT_TAG_SOURCE: manifest_li_be_path,
        sample_manifest.IMPLICIT_TAG_DIR: manifest_li_be_dir,
    }
    expect_beryllium = {
        'model': 'periodic',
        'sample': 'beryllium',
        sample_manifest.IMPLICIT_TAG_SOURCE: manifest_li_be_path,
        sample_manifest.IMPLICIT_TAG_DIR: manifest_li_be_dir,
    }
    self.assertEqual([expect_hydrogen], manifest.get('periodic', 'hydrogen'))
    self.assertEqual([expect_helium], manifest.get('periodic', 'helium'))
    self.assertEqual([expect_lithium], manifest.get('periodic', 'lithium'))
    self.assertEqual([expect_beryllium], manifest.get('periodic', 'beryllium'))
Ejemplo n.º 12
0
  def test_get_keys(self):
    manifest_source, (expect_alice, expect_bob, expect_carol,
                      expect_dan) = self.get_manifest_source()

    manifest = sample_manifest.Manifest('language', 'sample')
    manifest.read_sources([manifest_source])
    manifest.index()
    self.assertEqual(['python', ''], manifest.get_keys())
    self.assertEqual(['alice', 'robert'], manifest.get_keys('python'))
    self.assertEqual([], manifest.get_keys('python', 'alice'))
    self.assertEqual([], manifest.get_keys('python', 'robert'))
    self.assertEqual(['math'], manifest.get_keys(''))
    self.assertEqual([], manifest.get_keys('', 'math'))

    self.assertEqual([], manifest.get_keys('python', 'alice','zoe'))
    self.assertEqual([], manifest.get_keys('zoe'))
    self.assertEqual([], manifest.get_keys('python', 'zoe'))
Ejemplo n.º 13
0
def test_environments(indexed_docs: parser.IndexedDocs, convention_parameters,
                      manifest_options):
    if convention_parameters is None:
        convention_parameters = []
    num_params = len(convention_parameters)
    if num_params < 1:
        raise Exception(
            'expected at least 1 parameter to convention "tag", got %d: %s'.
            format(num_params, convention_parameters))

    manifest = sample_manifest.Manifest(
        ENVIRONMENT_KEY,
        *convention_parameters)  # read only, so don't need a copy
    manifest.from_docs(indexed_docs)
    manifest.index()
    logging.debug('manifest >>> \n{}\n<<<\n'.format(manifest.string()))

    env_names = []
    env_names = manifest.get_keys()
    if len(env_names) == 0:
        env_names = ['(nolang)']

    manifest_options_dict = {}
    num_options = len(manifest_options) if manifest_options else 0
    manifest_options_dict[INVOCATION_KEY] = manifest_options[
        0] if num_options > 0 else INVOCATION_KEY
    manifest_options_dict[
        CHDIR_KEY] = manifest_options[1] if num_options > 1 else CHDIR_KEY

    environments = []
    for name in env_names:
        description = 'Tags by environment:{} {}'.format(
            name, convention_parameters)
        env = ManifestEnvironment(
            name,
            description,
            manifest, [name],
            testcase_settings={'call.target': convention_parameters[0]},
            manifest_options=manifest_options_dict)
        environments.append(env)
    logging.info('convention "tag" generated environments: {}'.format(
        [env.name() for env in environments]))
    return environments
Ejemplo n.º 14
0
 def test_braces_error_key_with_braces(self):
   manifest_content = {
       sample_manifest.Manifest.VERSION_KEY: 2,
       sample_manifest.Manifest.SETS_KEY:
           [{
               'greetings': 'teatime',
               'form': 'Would you like some {drink{yea}}}?', # error
               'drink{yea}}': 'tea',
               sample_manifest.Manifest.ELEMENTS_KEY:
                   [{
                       'name': 'Mary',
                       'drink': ' with milk',
                       'interruption': 'Excuse me, {name}. {form}'
                   }]
               }]
       }
   manifest = sample_manifest.Manifest('greetings')
   manifest.read_sources([('erroring manifest', manifest_content)])
   self.assertRaises(sample_manifest.SyntaxError, manifest.index)
Ejemplo n.º 15
0
 def test_braces_error_key_with_braces(self):
   list_name = 'mysamples'
   manifest_content = {
        sample_manifest.Manifest.SCHEMA_TYPE_KEY:
           '{}/{}'.format(sample_manifest.Manifest.SCHEMA_TYPE_VALUE,
                          list_name),
       sample_manifest.Manifest.SCHEMA_VERSION_KEY: 3,
       list_name: [
           {
               'greetings': 'teatime',
               'form': 'Would you like some {drink{yea}}}?', # error
               'base_drink{yea}}': 'tea',
               'name': 'Mary',
               'drink': '{base_drink} with milk',
               'interruption': 'Excuse me, {name}. {form}'
               }
       ]
   }
   manifest = sample_manifest.Manifest('greetings')
   manifest.read_sources([('erroring manifest', manifest_content, {})])
   self.assertRaises(sample_manifest.SyntaxError, manifest.index)
Ejemplo n.º 16
0
 def test_braces_error_empty(self):
   list_name = 'mysamples'
   manifest_content = {
        sample_manifest.SCHEMA.type_key:
           '{}/{}'.format(sample_manifest.SCHEMA.primary_type,
                          list_name),
       sample_manifest.SCHEMA.version_key: 3,
       list_name: [
           {
               'greetings': 'teatime',
               'form': 'Would you like some {}?', # error
               'base_drink': 'tea',
               'name': 'Mary',
               'drink': '{base_drink} with milk',
               'interruption': 'Excuse me, {name}. {form}'
               }
       ]
   }
   manifest = sample_manifest.Manifest('greetings')
   manifest.read_sources([('erroring manifest', manifest_content, {})])
   self.assertRaises(sample_manifest.ManifestSyntaxError, manifest.index)
Ejemplo n.º 17
0
def test_environments(manifest_paths, convention_parameters):
  num_params = 0 if convention_parameters is None else len(convention_parameters)
  if num_params < 1:
    raise Exception('expected at least 1 parameter to convention "tag", got %d: %s'
                    .format(num_params, convention_parameters))

  for path in manifest_paths:
    all_manifests.extend(
        glob.glob(path)
    )  # can do this?: _ = [a_m.extend(g.g(path)) for path in manifest_paths]
  manifest = sample_manifest.Manifest(LANGUAGE_KEY, *convention_parameters) # read only, so don't need a copy
  manifest.read_files(*all_manifests)
  manifest.index()
  languages = manifest.get_keys()
  if len(languages) == 0:
    languages = ['(nolang)']
  for language in languages:
    description = 'Tags by language:{} {}'.format(language, convention_parameters)
    name = language
    env  = ManifestEnvironment(name, description, manifest,
                               [language])
    environments.append(env)
  logging.info('convention "tag" generated environments: {}'.format([env.name() for env in environments]))
  return environments
Ejemplo n.º 18
0
 def setUp(self):
   self.manifest_file_name = full_path('testdata/tag_test.manifest.yaml')
   manifest = sample_manifest.Manifest('situation')
   manifest.from_docs(inputs.create_indexed_docs(self.manifest_file_name))
   manifest.index()
   self.env = tag.ManifestEnvironment(self.manifest_file_name, '', manifest, [])
 def test_read_invalid_version(self):
     manifest_source, _ = self.get_manifest_source()
     manifest_source[1][sample_manifest.Manifest.VERSION_KEY_v1v2] = 'foo'
     manifest = sample_manifest.Manifest('language', 'sample')
     with self.assertRaises(Exception):
         manifest.read_sources([manifest_source])
Ejemplo n.º 20
0
 def test_read_no_version(self):
   manifest_source = self.get_manifest_source()
   manifest_source[0][1].pop('version')
   manifest = sample_manifest.Manifest('language', 'region_tag')
   with self.assertRaises(Exception):
     manifest.read_sources([manifest_source])
Ejemplo n.º 21
0
 def test_read_invalid_version(self):
   manifest_source = self.get_manifest_source()
   manifest_source[0][1]['version'] = 'foo'
   manifest = sample_manifest.Manifest('language', 'region_tag')
   with self.assertRaises(Exception):
     manifest.read_sources(manifest_source)
Ejemplo n.º 22
0
 def test_read_no_version(self):
   manifest_source, _ = self.get_manifest_source()
   manifest_source[1].pop(sample_manifest.Manifest.SCHEMA_VERSION_KEY)
   manifest = sample_manifest.Manifest('language', 'sample')
   with self.assertRaises(Exception):
     manifest.read_sources([manifest_source])
Ejemplo n.º 23
0
 def test_read_nonmanifest_type(self):
   manifest_source, _ = self.get_manifest_source()
   manifest_source[1][sample_manifest.Manifest.SCHEMA_TYPE_KEY] = "random"
   manifest = sample_manifest.Manifest('language', 'sample')
   manifest.read_sources([manifest_source])
   manifest.index()