Ejemplo n.º 1
0
    def test_issue_113(self):
        """ Make sure that types are generated as part of the output """
        env.generate_single_file(
            'issue_113.py',
            lambda: PythonGenerator(env.input_path('issue_113.yaml')
                                    ).serialize(),
            comparator=lambda exp, act: compare_python(exp, act, 'issue_113.py'
                                                       ),
            value_is_returned=True)
        module = compile_python(env.expected_path('issue_113.py'))
        example = module.TestClass(test_attribute_2="foo")
        assert hasattr(example, "test_attribute_2")
        assert hasattr(example, "test_attribute_1")
        example.wiible = "foo"
        example.test_attribute_1 = "foo"
        example.test_attribute_2 = "foo"

        env.generate_single_file(
            'issue_113.json',
            lambda: PythonGenerator(env.input_path('issue_113.yaml')
                                    ).serialize(),
            comparator=lambda exp, act: compare_python(exp, act, 'issue_113.py'
                                                       ),
            value_is_returned=True)

        def output_generator(dirname) -> None:
            with open(os.path.join(dirname, 'issue_113.json'), 'w') as f:
                f.write(as_json(example))

        env.generate_directory('issue_113',
                               lambda dirname: output_generator(dirname))
Ejemplo n.º 2
0
    def test_local_imports(self):
        """ Check the local import behavior """
        dir = 'issue_260'

        # Useful to have an __init__.py available
        init_path = env.actual_path(dir, '__init__.py')
        if not os.path.exists(init_path):
            with open(init_path, 'w'):
                pass

        env.generate_single_file('issue_260/issue_260a.py',
                                 lambda: PythonGenerator(env.input_path(dir, 'issue_260a.yaml'),
                                                         importmap=env.import_map, mergeimports=False).serialize(),
                                 comparator=compare_python, value_is_returned=True)
        env.generate_single_file('issue_260/issue_260b.py',
                                 lambda: PythonGenerator(env.input_path(dir, 'issue_260b.yaml'),
                                                         importmap=env.import_map, mergeimports=False).serialize(),
                                 comparator=compare_python, value_is_returned=True)
        env.generate_single_file('issue_260/issue_260c.py',
                                 lambda: PythonGenerator(env.input_path(dir, 'issue_260c.yaml'),
                                                         importmap=env.import_map, mergeimports=False).serialize(),
                                 comparator=compare_python, value_is_returned=True)
        env.generate_single_file('issue_260/issue_260.py',
                                 lambda: PythonGenerator(env.input_path(dir, 'issue_260.yaml'),
                                                         importmap=env.import_map, mergeimports=False).serialize(),
                                 comparator=compare_python, value_is_returned=True)
Ejemplo n.º 3
0
    def test_minimal_model(self):
        """ Test to make the absolute minimal model work """
        YAMLGenerator(env.input_path('issue_tccm', 'minimalmodel.yaml'),
                      mergeimports=False,
                      log_level=logging.INFO).serialize(validateonly=True)

        env.make_testing_directory(env.expected_path('issue_tccm'))
        for generator in Generator.__subclasses__():
            if not generator.__module__.startswith('linkml.generators'):
                pass
            elif not generator.directory_output:
                env.generate_single_file(
                    [
                        'issue_tccm',
                        'minimalmodel.' + generator.valid_formats[0]
                    ],
                    lambda: generator(env.input_path('issue_tccm',
                                                     'minimalmodel.yaml'),
                                      importmap=env.import_map,
                                      mergeimports=False,
                                      emit_metadata=False).serialize(),
                    value_is_returned=True)
            else:
                env.generate_directory(
                    ['issue_tccm', generator.__name__], lambda d: generator(
                        env.input_path('issue_tccm', 'minimalmodel.yaml'),
                        importmap=env.import_map,
                        mergeimports=False,
                        emit_metadata=False).serialize(directory=d))
Ejemplo n.º 4
0
 def _do_test(self, tfn):
     env.generate_single_file(f'{tfn}.yaml',
                              lambda: YAMLGenerator(env.input_path(f'{tfn}.yaml'), log_level=INFO).serialize(),
                              filtr=yaml_filter, value_is_returned=True)
     env.generate_single_file(f'{tfn}.context.jsonld',
                              lambda: ContextGenerator(env.input_path(f'{tfn}.yaml')).serialize(),
                              filtr=ldcontext_metadata_filter, value_is_returned=True)
Ejemplo n.º 5
0
 def test_domain_slots(self):
     """ Subsets need to be imported as well """
     CsvGenerator(env.input_path('issue_38.yaml'),
                  log_level=DEFAULT_LOG_LEVEL,
                  importmap=env.input_path(
                      'biolink-model-importmap.json')).serialize()
     # We never get here if the imports fails
     self.assertEqual(True, True)
Ejemplo n.º 6
0
    def test_issue_368(self):
        """ Make sure that types are generated as part of the output """
        env.generate_single_file(
            'issue_368_imports.py',
            lambda: PythonGenerator(env.input_path('issue_368_imports.yaml'),
                                    mergeimports=False).serialize(),
            comparator=lambda exp, act: compare_python(
                exp, act, self.env.expected_path('issue_368_imports.py')),
            value_is_returned=True)
        env.generate_single_file(
            'issue_368.py',
            lambda: PythonGenerator(env.input_path('issue_368.yaml'),
                                    mergeimports=False).serialize(),
            comparator=lambda exp, act: compare_python(
                exp, act, self.env.expected_path('issue_368.py')),
            value_is_returned=True)

        with open(env.expected_path('issue_368.py')) as f:
            python = f.read()

        has_imports = False
        for line in python.split("\n"):
            if line.startswith("from . issue_368_imports"):
                imps = line.replace("from . issue_368_imports import ",
                                    "").split(", ")
                assert 'SampleEnum' in imps
                assert 'ParentClass' in imps
                has_imports = True
        assert has_imports
        module = compile_python(env.expected_path('issue_368.py'))

        enum_inst = module.SampleEnum("pva")  # EnumInstanceImpl
        example = module.SampleClass(slot_1="pva")
        assert hasattr(example, "slot_1")
        assert example.slot_1.code.text == enum_inst.code.text
        assert str(example.slot_1) == "pva: PVA description"

        def dump_and_load(dumper: Callable, sfx: str) -> None:
            fname = env.actual_path(f'issue_368_1.{sfx}')
            dumper(example, fname)
            with open(fname) as f:
                print(f'\n----- {sfx} -----')
                print(f.read())

        dump_and_load(json_dumper.dump, 'json')
        dump_and_load(yaml_dumper.dump, 'yaml')

        env.generate_single_file(
            'issue_368.context.jsonld',
            lambda: ContextGenerator(env.input_path('issue_368.yaml'),
                                     emit_metadata=False).serialize(),
            filtr=ldcontext_metadata_filter,
            value_is_returned=True)
        dump_and_load(
            lambda obj, fname: rdf_dumper.dump(
                obj, fname, env.expected_path("issue_368.context.jsonld")),
            'ttl')
Ejemplo n.º 7
0
 def test_python_import(self):
     """ Import generates for biolink-model """
     python = PythonGenerator(
         env.input_path('issue_38.yaml'),
         importmap=env.input_path(
             'biolink-model-importmap.json')).serialize()
     msg = validate_python(python)
     if msg:
         self.fail(msg)
Ejemplo n.º 8
0
 def test_local_imports(self):
     """ Make sure there is a '.' on a local import in python """
     env.generate_single_file('importee.py',
                              lambda: PythonGenerator(env.input_path('issue_tccm', 'importee.yaml'),
                                                      importmap=env.import_map, mergeimports=False).serialize(),
                              comparator=compare_python, value_is_returned=True)
     env.generate_single_file('importer.py',
                              lambda: PythonGenerator(env.input_path('issue_tccm', 'importer.yaml'),
                                                      importmap=env.import_map, mergeimports=False).serialize(),
                              comparator=compare_python, value_is_returned=True)
Ejemplo n.º 9
0
 def test_issue_167b_python(self):
     """ Annotations python example """
     env.generate_single_file('issue_167b.py',
                              lambda: PythonGenerator(env.input_path('issue_167b.yaml'),
                                                      importmap=env.import_map, emit_metadata=False).serialize(),
                              comparator=lambda exp, act: compare_python(exp, act, self.env.expected_path('issue_167b.py')),
                              value_is_returned=True)
     env.generate_single_file('issue_167b2.py',
                              lambda: PythonGenerator(env.input_path('issue_167b.yaml'),
                                                      importmap=env.import_map, mergeimports=False, emit_metadata=False).serialize(),
                              comparator=lambda exp, act: compare_python(exp, act, self.env.expected_path('issue_167b_nomerged.py')),
                              value_is_returned=True)
Ejemplo n.º 10
0
    def test_issue_120(self):
        """ Courses not inlining """
        env.generate_single_file(
            'issue_120.py',
            lambda: PythonGenerator(env.input_path('issue_120.yaml')
                                    ).serialize(),
            value_is_returned=True,
            comparator=compare_python)

        env.generate_single_file(
            'issue_120.json',
            lambda: JsonSchemaGenerator(env.input_path('issue_120.yaml')
                                        ).serialize(),
            value_is_returned=True)
Ejemplo n.º 11
0
    def test_issue_121(self):
        """ Make sure that types are generated as part of the output """
        env.generate_single_file('issue_121.py',
                                 lambda: PythonGenerator(env.input_path('issue_121.yaml')).serialize(),
                                 comparator=compare_python, value_is_returned=True)
        with open(env.expected_path('issue_121.py')) as f:
            python= f.read()

        has_includes = False
        for line in python.split("\n"):
            if line.startswith("from includes.types"):
                assert line == "from includes.types import String"
                has_includes = True
        assert has_includes
        module = compile_python(env.expected_path('issue_121.py'))

        example = module.Biosample(depth="test")
        assert hasattr(example, "depth")
        assert example.depth == "test"

        example2 = module.ImportedClass()

        def output_generator(dirname) -> None:
            with open(os.path.join(dirname, 'issue_121_1.json'), 'w') as f:
                f.write(as_json(example))
            with open(os.path.join(dirname, 'issue_121_2.json'), 'w') as f:
                f.write(as_json(example2))

        env.generate_directory('issue_121', lambda dirname: output_generator(dirname))
Ejemplo n.º 12
0
 def test_domain_slots(self):
     """ has_phenotype shouldn't appear in the UML graph """
     yuml = YumlGenerator(env.input_path('issue_12.yaml')).serialize()
     self.assertEqual('http://yuml.me/diagram/nofunky;dir:TB/class/[BiologicalEntity]++- '
                      'required thing 0..1>[PhenotypicFeature],[BiologicalEntity]', yuml)
     resp = requests.get(yuml)
     self.assertTrue(resp.ok)
Ejemplo n.º 13
0
 def test_multiple_postinit(self):
     """ Generate postinit code for a multi-occurring element """
     env.generate_single_file(
         'issue_44.py',
         lambda: PythonGenerator(env.input_path('issue_44.yaml'),
                                 emit_metadata=False).serialize(),
         value_is_returned=True)
Ejemplo n.º 14
0
 def test_issue_50(self):
     env.generate_single_file(
         'issue_84.py',
         lambda: PythonGenerator(env.input_path('issue_84.yaml')).serialize(
         ),
         comparator=compare_python,
         value_is_returned=True)
Ejemplo n.º 15
0
    def test_issue_80(self):
        """ Make sure that types are generated as part of the output """
        env.generate_single_file(
            'issue_80.py',
            lambda: PythonGenerator(env.input_path('issue_80.yaml')).serialize(
            ),
            comparator=lambda exp, act: compare_python(
                exp, act, self.env.expected_path('issue_80.py')),
            value_is_returned=True)
        module = compile_python(env.expected_path('issue_80.py'))
        example = module.Person("http://example.org/person/17", "Fred Jones",
                                43)

        # Create output for various forms
        def output_generator(dirname) -> None:
            with open(os.path.join(dirname, 'issue_80.json'), 'w') as f:
                f.write(as_json(example))
            context = os.path.join(dirname, 'issue_80.context.jsonld')
            with open(context, 'w') as f:
                f.write(
                    ContextGenerator(
                        env.input_path('issue_80.yaml')).serialize())
            with open(os.path.join(dirname, 'issue_80.ttl'), 'w') as f:
                f.write(
                    as_rdf(
                        example,
                        contexts=context).serialize(format="turtle").decode())

        env.generate_directory('issue_80',
                               lambda dirname: output_generator(dirname))
Ejemplo n.º 16
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())
Ejemplo n.º 17
0
 def test_issue_167b_json(self):
     env.generate_single_file(
         'issue_167b.json',
         lambda: JSONLDGenerator(env.input_path('issue_167b.yaml'),
                                 importmap=env.import_map).serialize(),
         filtr=json_metadata_filter,
         value_is_returned=True)
Ejemplo n.º 18
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())
Ejemplo n.º 19
0
 def test_issue_167b_rdf(self):
     env.generate_single_file(
         'issue_167b.ttl',
         lambda: RDFGenerator(env.input_path('issue_167b.yaml'),
                              importmap=env.import_map).serialize(),
         comparator=compare_rdf,
         value_is_returned=True)
Ejemplo n.º 20
0
    def test_slot_subclass(self):
        """ Test slot domain as superclass of parent """
        env.generate_single_file(
            'issue_56_good.py',
            lambda: PythonGenerator(env.input_path('issue_56_good.yaml')
                                    ).serialize(),
            comparator=compare_python,
            value_is_returned=True)

        with self.assertRaises(Exception) as e:
            env.generate_single_file(
                'issue_56_bad.py',
                lambda: PythonGenerator(env.input_path('issue_56_bad.yaml')
                                        ).serialize(),
                comparator=compare_python,
                value_is_returned=True)
Ejemplo n.º 21
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())
Ejemplo n.º 22
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)
Ejemplo n.º 23
0
 def test_issue_371(self):
     """ Infer a model from a CSV """
     CLS = 'my_class'
     obj = infer_model(env.input_path('issue_371.csv'), sep=',', class_name=CLS)
     print(yaml.dump(obj, default_flow_style=False, sort_keys=False))
     assert len(obj['classes'][CLS]['slots']) == 4
     assert obj['slots']['age']['range'] == 'integer'
     assert obj['slots']['name']['range'] == 'string'
Ejemplo n.º 24
0
 def test_issue_167b_python(self):
     """ Annotations python example """
     env.generate_single_file(
         'issue_167b.py',
         lambda: PythonGenerator(env.input_path('issue_167b.yaml'),
                                 importmap=env.import_map).serialize(),
         comparator=compare_python,
         value_is_returned=True)
Ejemplo n.º 25
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)
Ejemplo n.º 26
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)
Ejemplo n.º 27
0
 def test_context(self):
     """ Test no context in the argument"""
     self.assertIn(
         '''"@context": [
 [
   "https://w3id.org/biolink/biolinkml/context.jsonld"
 ],''',
         JSONLDGenerator(env.input_path('issue_332.yaml')).serialize())
Ejemplo n.º 28
0
 def test_namespace(self):
     context = "https://biolink.github.io/biolink-model/context.jsonld"
     env.generate_single_file(
         'issue_namespace.ttl',
         lambda: RDFGenerator(env.input_path('issue_namespace.yaml')
                              ).serialize(context=context),
         comparator=compare_rdf,
         value_is_returned=True)
Ejemplo n.º 29
0
 def test_dictionary_name(self):
     """ Allow dictionaries w/ explicit keys or identifiers through as long as they match """
     yaml = env.generate_single_file(['issue_tccm', 'explicit_key_id.yaml'],
                                     lambda: YAMLGenerator(env.input_path('issue_tccm', 'explicit_key_id.yaml'),
                                     importmap=env.import_map, mergeimports=False,
                                     emit_metadata=False).serialize(),
                                     value_is_returned = True)
     print(yaml)
Ejemplo n.º 30
0
 def test_prefix(self):
     env.generate_single_file(
         'issue_107.py',
         lambda: PythonGenerator(env.input_path('issue_107.yaml')
                                 ).serialize(),
         comparator=lambda exp, act: compare_python(
             exp, act, self.env.expected_path('issue_107.py')),
         value_is_returned=True)