Beispiel #1
0
 def test_issue_18(self):
     """ Make sure that inverses are automatically generated """
     env.generate_single_file(
         'issue_18.yaml',
         lambda: as_yaml(
             SchemaLoader(env.input_path('issue_18.yaml')).resolve()),
         filtr=yaml_filter,
         value_is_returned=True)
Beispiel #2
0
 def test_issue_177_dup(self):
     env.generate_single_file(
         'issue_177_error.yaml',
         lambda: as_yaml(
             SchemaLoader(env.input_path('issue_177_error.yaml')).resolve()
         ),
         value_is_returned=True,
         filtr=yaml_filter)
Beispiel #3
0
    def test_contained_constructor(self):
        test_name = 'issue_355'
        """ Make sure that types are generated as part of the output """
        env.generate_single_file(
            f'{test_name}.py',
            lambda: PythonGenerator(env.input_path(f'{test_name}.yaml')
                                    ).serialize(),
            comparator=lambda exp, act: compare_python(
                exp, act, self.env.expected_path(f'{test_name}.py')),
            value_is_returned=True)
        module = compile_python(env.expected_path(f'{test_name}.py'))
        c = module.Container(module.Containee('11111', "Glaubner's disease"))
        self.assertEqual(
            '''entry:
  '11111':
    id: '11111'
    value: Glaubner's disease''',
            as_yaml(c).strip())

        c = module.Container({'22222': dict(id='22222', value='Phrenooscopy')})
        self.assertEqual(
            '''entry:
  '22222':
    id: '22222'
    value: Phrenooscopy''',
            as_yaml(c).strip())
        alt_object = YAMLRoot()
        alt_object.id = '33333'
        alt_object.value = 'test'
        c = module.Container(alt_object)
        self.assertEqual(
            '''entry:
  '33333':
    id: '33333'
    value: test''',
            as_yaml(c).strip())
        c = module.Container([dict(id='44444', value="Gracken's curse")])
        self.assertEqual(
            '''entry:
  '44444':
    id: '44444'
    value: Gracken's curse''',
            as_yaml(c).strip())
Beispiel #4
0
 def test_issue_58(self):
     """ Reject non NSNAME model names"""
     with self.assertRaises(ValueError) as ve:
         env.generate_single_file(
             'issue_58_error1.yaml',
             lambda: as_yaml(
                 SchemaLoader(env.input_path('issue_58_error1.yaml')).
                 resolve()),
             value_is_returned=True)
     self.assertIn('issue 58: Not a valid NCName', str(ve.exception))
Beispiel #5
0
 def test_missing_inverse(self):
     with self.assertRaises(ValueError) as e:
         env.generate_single_file(
             'issue_18_error2.yaml',
             lambda: as_yaml(
                 SchemaLoader(env.input_path('issue_18_error2.yaml')).
                 resolve()),
             value_is_returned=True)
     self.assertEqual('Slot s1.inverse (s2) is not defined',
                      str(e.exception).strip())
Beispiel #6
0
 def test_no_inverse_domain(self):
     with self.assertRaises(ValueError) as e:
         env.generate_single_file(
             'issue_18_error3.yaml',
             lambda: as_yaml(
                 SchemaLoader(env.input_path('issue_18_error3.yaml')).
                 resolve()),
             value_is_returned=True)
     self.assertEqual(
         "Unable to determine the range of slot `s1'. Its inverse (s2) has no declared domain",
         str(e.exception).strip())
Beispiel #7
0
 def test_multi_domains(self):
     with self.redirect_logstream() as logger:
         env.generate_single_file(
             'issue_18_warning1.yaml',
             lambda: as_yaml(
                 SchemaLoader(env.input_path('issue_18_warning1.yaml'),
                              logger=logger).resolve()),
             filtr=yaml_filter,
             value_is_returned=True)
     self.assertIn(
         'Slot s2.inverse (s1), has multi domains (c1, c2)  Multi ranges not yet implemented',
         logger.result)
Beispiel #8
0
 def test_inverse_mismatch(self):
     """ Test error detection when inverses don't match """
     with self.assertRaises(ValueError) as e:
         env.generate_single_file(
             'issue_18_error1.yaml',
             lambda: as_yaml(
                 SchemaLoader(env.input_path('issue_18_error1.yaml')).
                 resolve()),
             value_is_returned=True)
     self.assertEqual(
         'Slot s1.inverse (s2) does not match slot s2.inverse (s3)',
         str(e.exception).strip())
Beispiel #9
0
 def test_multi_usages_2(self):
     """ Slot usage chain with starting alias """
     schema = SchemaLoader(env.input_path('multi_usages_2.yaml')).resolve()
     self._eval_expected(schema, 's1', 'value', 'root_class', None, None,
                         'string')
     self._eval_expected(schema, 'child_class1_s1', 'value', 'child_class1',
                         's1', 's1', 'boolean')
     self._eval_expected(schema, 'child_class2_s1', 'value', 'child_class2',
                         'child_class1_s1', 's1', 'integer')
     self._eval_expected(schema, 'child_class3_s1', 'value', 'child_class3',
                         'child_class2_s1', 's1', 'integer')
     env.eval_single_file(env.expected_path('multi_usages_2.yaml'),
                          as_yaml(schema),
                          filtr=yaml_filter)
Beispiel #10
0
    def test_roundtrip(self):
        """ Test as_yaml emitter """
        # env.generate_single_file('issue_154.py',
        #                          lambda: PythonGenerator(env.input_path('issue_134.yaml')).serialize(),
        #                          comparator=lambda exp, act: compare_python(exp, act, self.env.expected_path('issue_154.py')),
        #                          value_is_returned=True)
        # yaml_fname = env.input_path('issue_134.yaml')

        # We use the PythonGenerator as a generic generator instance.  We don't actually serialize

        yaml_fname = env.input_path('issue_134.yaml')
        gen = PythonGenerator(yaml_fname)
        yaml_str = as_yaml(gen.schema)
        generated = yaml.safe_load(yaml_str)
        with open(yaml_fname) as yaml_file:
            original = yaml.safe_load(yaml_file)

        # The generated YAML contains many added fields. Some with default values. Therefore, we can't directly
        # compare it to the original.
        for key in original:
            self.assertEqual(len(original[key]), len(generated[key]))
            if isinstance(original[key], dict):
                for subkey in original[key]:
                    self.assertTrue(subkey in generated[key])
Beispiel #11
0
 def end_class(self, cls: ClassDefinition) -> None:
     fn = os.path.join(self.dirname, underscore(cls.name + '-config.yaml'))
     if len(self.class_obj.fields) > 1:
         with open(fn, 'w') as f:
             f.write(as_yaml(self.class_obj))
Beispiel #12
0
 def test_as_yaml(self):
     """ Test the YAML output representation """
     schema = self.fix_schema_metadata(load_raw_schema(env.input_path('schema4.yaml')))
     env.eval_single_file(env.expected_path('schema4.yaml'), as_yaml(schema), filtr=lambda s: s)
Beispiel #13
0
 def test_issue_167b_yaml(self):
     """ Annotations yaml example """
     env.generate_single_file('issue_167b.yaml',
                              lambda: as_yaml(SchemaLoader(env.input_path('issue_167b.yaml'),
                                                           importmap=env.import_map).resolve()),
                              value_is_returned=True, filtr=yaml_filter)
Beispiel #14
0
 def test_issue_167(self):
     """ Test extensions to the four basic types """
     env.generate_single_file('issue_167.yaml',
                              lambda: as_yaml(SchemaLoader(env.input_path('issue_167.yaml')).resolve()),
                              value_is_returned=True, filtr=yaml_filter)
Beispiel #15
0
 def serialize(self, validateonly: bool = False, **kwargs) -> str:
     if validateonly:
         return self.synopsis.summary()
     else:
         return as_yaml(self.schema)