def test_meta(self): self.maxDiff = None self.do_test([], 'meta.py', filtr=metadata_filter, comparator=lambda exp, act: compare_python(exp, act, self.env.expected_path('meta.py'))) self.do_test('-f py', 'meta.py', filtr=metadata_filter, comparator=lambda exp, act: compare_python(exp, act, self.env.expected_path('meta.py'))) self.do_test('-f xsv', 'meta_error', expected_error=click.exceptions.BadParameter)
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))
def test_local_imports(self): """ Check the local import behavior """ test_dir = 'issue_260' # Useful to have an __init__.py available init_path = env.actual_path(test_dir, '__init__.py') if not os.path.exists(init_path): with open(init_path, 'w'): pass fp = f'{test_dir}/issue_260a.py' env.generate_single_file(fp, lambda: PythonGenerator(env.input_path(test_dir, 'issue_260a.yaml'), importmap=env.import_map, mergeimports=False).serialize(), comparator=lambda expected, actual: compare_python(expected, actual, env.expected_path(fp)), value_is_returned=True) fp = f'{test_dir}/issue_260b.py' env.generate_single_file(fp, lambda: PythonGenerator(env.input_path(test_dir, 'issue_260b.yaml'), importmap=env.import_map, mergeimports=False).serialize(), comparator=lambda expected, actual: compare_python(expected, actual, env.expected_path(fp)), value_is_returned=True) fp = f'{test_dir}/issue_260c.py' env.generate_single_file(fp, lambda: PythonGenerator(env.input_path(test_dir, 'issue_260c.yaml'), importmap=env.import_map, mergeimports=False).serialize(), comparator=lambda expected, actual: compare_python(expected, actual, env.expected_path(fp)), value_is_returned=True) fp = f'{test_dir}/issue_260.py' env.generate_single_file(fp, lambda: PythonGenerator(env.input_path(test_dir, 'issue_260.yaml'), importmap=env.import_map, mergeimports=False).serialize(), comparator=lambda expected, actual: compare_python(expected, actual, env.expected_path(fp)), value_is_returned=True)
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')
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)
def do_test(self): """ Test the metadata options""" self.single_file_generator( 'py', PythonGenerator, comparator=lambda exp, act: compare_python( exp, act, self.env.expected_path('x.py')))
def test_notebook_model_1(self): file = 'notebook_model_1' python_name = f'{self.testdir}/{file}.py' env.generate_single_file( python_name, lambda: PythonGenerator(env.input_path(self.testdir, f'{file}.yaml' ), importmap=env.import_map, mergeimports=False, gen_classvars=False, gen_slots=False).serialize(), comparator=lambda exp, act: compare_python( exp, act, self.env.expected_path(python_name)), value_is_returned=True) module = compile_python(env.expected_path(python_name)) c1 = module.PositionalRecord('my location', 'a') print(str(c1)) print(str(c1.position)) print(repr(c1.position)) try: c2 = module.PositionalRecord('your location', 'z') except ValueError as e: print(e) x = module.PositionalRecord("117493", "c") self.assertEqual('c: bottom', str(x.position)) self.assertEqual( "PositionalRecord(id='117493', position=(text='c', description='bottom'))", repr(x)) self.assertEqual("(text='c', description='bottom')", repr(x.position))
def test_enum_alternatives(self): """ test various variants on enum constraints """ file = "alternatives" env.generate_single_file( env.expected_path(self.testdir, f'{file}.yaml'), lambda: YAMLGenerator(env.input_path(self.testdir, f'{file}.yaml') ).serialize(), filtr=yaml_filter, value_is_returned=True) python_name = f'{self.testdir}/{file}.py' YAMLGenerator(env.input_path(self.testdir, f'{file}.yaml'), mergeimports=False, log_level=logging.INFO).serialize() env.generate_single_file( python_name, lambda: PythonGenerator(env.input_path(self.testdir, f'{file}.yaml' ), importmap=env.import_map, mergeimports=False).serialize(), comparator=lambda exp, act: compare_python( exp, act, self.env.expected_path(python_name)), value_is_returned=True) module = compile_python(env.expected_path(python_name))
def test_biolink_namespaces(self): """ Test the python generator for the biolink model """ self.output_name = 'namespaces' self.single_file_generator('py', NamespaceGenerator, generator_args={'emit_metadata': True}, filtr=metadata_filter, comparator=lambda exp, act: compare_python(exp, act, self.env.expected_path('namespaces.py')), output_name='namespaces')
def test_uri_and_curie(self): """ Compile a model of URI's and Curies and then test the various types """ self.single_file_generator( 'py', PythonGenerator, filtr=metadata_filter, comparator=lambda exp, act: compare_python( exp, act, self.env.expected_path('foo.py'))) # Check that the interpretations are correct self.single_file_generator( 'jsonld', ContextGenerator, filtr=ldcontext_metadata_filter, comparator=lambda expected, actual: compare_rdf( expected, actual, fmt="json-ld")) self.single_file_generator('json', JSONLDGenerator, filtr=json_metadata_filter) module = compile_python(env.expected_path(self.model_name + '.py')) curie_obj = module.C1("ex:obj1", hasCurie="ex:curie", hasURI="http://example.org/test/uri", hasNcName="A123", id2="ex:id2") instance_jsonld = loads('{ "ex": "http://example.org/test/inst#" }') g = as_rdf( curie_obj, [env.input_path(self.model_name + '.jsonld'), instance_jsonld]) env.eval_single_file(env.expected_path('uriandcurie.ttl'), g.serialize(format='ttl').decode(), lambda s: s, compare_rdf)
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=lambda exp, act: compare_python( exp, act, self.env.expected_path('issue_121.py')), 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 linkml_model.types "): assert line == "from linkml_model.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))
def test_template_basics(self): """ Test the basics of a string template """ file = "templated_classes" env.generate_single_file( f'{self.testdir}/{file}.py', lambda: PythonGenerator(env.input_path(self.testdir, f'{file}.yaml' ), importmap=env.import_map, mergeimports=False).serialize(), comparator=lambda exp, act: compare_python( exp, act, self.env.expected_path(f'{self.testdir}/{file}.py')), value_is_returned=True) module = compile_python(env.expected_path(self.testdir, f"{file}.py")) inst = module.FirstClass("Sam Sneed", 42, "Male") self.assertEqual('Sam Sneed - a 42 year old Male', str(inst)) inst2 = module.FirstClass.parse( "Jillian Johnson - a 93 year old female") self.assertEqual( "FirstClass(name='Jillian Johnson', age=93, gender='female')", repr(inst2)) self.assertEqual('Jillian Johnson - a 93 year old female', str(inst2)) with open(env.input_path(self.testdir, 'jones.yaml')) as yf: inst3 = from_yaml(yf, module.FirstClass) self.assertEqual('Freddy Buster Jones - a 11 year old Undetermined', str(inst3))
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))
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())
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)
def test_biolink_python(self): """ Test the python generator for the biolink model """ self.single_file_generator('py', PythonGenerator, filtr=metadata_filter, comparator=lambda exp, act: compare_python( exp, act, self.env.expected_path('py')), output_name='model')
def test_mappings_in_metamodel(self): """ Generate a copy of mappyings.py """ self.single_file_generator( 'py', PythonGenerator, subdir='includes', filtr=metadata_filter, comparator=lambda exp, act: compare_python( exp, act, self.env.expected_path('includes.py')))
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=lambda exp, act: compare_python( exp, act, self.env.expected_path('issue_56_good.py')), 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=lambda exp, act: compare_python( exp, act, self.env.expected_path('issue_56.py')), value_is_returned=True)
def test_inheritence(self): env.generate_single_file('issue_14.py', lambda: PythonGenerator(env.input_path('issue_14.yaml')).serialize(), comparator=lambda exp, act: compare_python(exp, act, self.env.expected_path('issue_14.py')), filtr=metadata_filter, value_is_returned=True) # Added test for issue #183, where sex_qualifier disappeared from MixinOwner class module = compile_python(env.expected_path('issue_14.py')) subject = module.SubjectRange1(id='sr1',name="SubjectRange1", subject='thing1', object='thing2') mixin_owner = module.MixinOwner(id='mo1', subject='sr1', name='MixinOwner1', object='thing2', sex_qualifier="ntx")
def test_slot_usage_only(self): """ Slot_usages without parents don't generate slots period. """ env.generate_single_file( 'issue_ttcm_1.py', lambda: PythonGenerator(env.input_path('issue_tccm', 'resourcedescription.yaml'), importmap=env.import_map, mergeimports=True).serialize(), comparator=lambda exp, act: compare_python( exp, act, env.expected_path('issue_ttcm_1.py')), value_is_returned=True)
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=lambda exp, act: compare_python( exp, act, env.expected_path('importee.py')), 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=lambda exp, act: compare_python( exp, act, env.expected_path('importer.py')), value_is_returned=True)
def test_metamodel_python(self): """ Build meta.py """ env.generate_single_file( 'meta.py', lambda: PythonGenerator(env.meta_yaml, importmap=env.import_map ).serialize(), value_is_returned=True, filtr=metadata_filter, comparator=lambda exp, act: compare_python( exp, act, self.env.expected_path('meta.py')), use_testing_root=True)
def test_annotations_python(self): """ Build includes/annotations.py """ env.generate_single_file( 'annotations.py', lambda: PythonGenerator(LOCAL_ANNOTATIONS_YAML_FILE, importmap=env.import_map, genmeta=True).serialize(), value_is_returned=True, filtr=metadata_filter, comparator=lambda exp, act: compare_python( exp, act, self.env.expected_path('annotations.py')), use_testing_root=True)
def test_evidence(self): """ Test evidence enumeration """ file = "evidence" env.generate_single_file( f'{self.testdir}/{file}.py', lambda: PythonGenerator(env.input_path(self.testdir, f'{file}.yaml' ), importmap=env.import_map, mergeimports=False).serialize(), comparator=lambda exp, act: compare_python( exp, act, self.env.expected_path(f'{self.testdir}/{file}.py')), value_is_returned=True)
def test_annotations_python(self): """ Build includes/annotations.py """ env.generate_single_file( 'includes/annotations.py', lambda: PythonGenerator(env.input_path('includes', 'annotations.yaml'), importmap=env.import_map).serialize(), value_is_returned=True, filtr=metadata_filter, comparator=lambda exp, act: compare_python( exp, act, self.env.expected_path('includes/annotations.py')), use_testing_root=True)
def test_pattern_1(self): """ Test the pattern ehnancement """ file = "pattern_1" env.generate_single_file(f'{self.testdir}/{file}.py', lambda: PythonGenerator(env.input_path(self.testdir, f'{file}.yaml'), importmap=env.import_map, mergeimports=False).serialize(), comparator=lambda exp, act: compare_python(exp, act, self.env.expected_path(f'{self.testdir}/{file}.py')), value_is_returned=True) module = compile_python(env.expected_path(self.testdir, f"{file}.py")) d1 = yaml.load(StringIO(d1_test), yaml.loader.SafeLoader) dev1 = module.DiskDevice(**d1) print("HERE")
def test_enum_import(self): """ Enum reference isn't getting merged on module import """ # env.generate_single_file('file1.py', # lambda: PythonGenerator(env.input_path(self.directory, 'file1.yaml'), # mergeimports=True).serialize(), # comparator=lambda exp, act: compare_python(exp, act, env.expected_path(self.directory, 'file1.py')), # value_is_returned=True) env.generate_single_file( 'file2.py', lambda: PythonGenerator(env.input_path(self.directory, 'file2.yaml' ), mergeimports=True).serialize(), comparator=lambda exp, act: compare_python( exp, act, env.expected_path(self.directory, 'file2.py')), value_is_returned=True)
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=lambda exp, act: compare_python( exp, act, self.env.expected_path('issue_120.py'))) env.generate_single_file( 'issue_120.json', lambda: JsonSchemaGenerator(env.input_path('issue_120.yaml') ).serialize(), value_is_returned=True)
def test_python_complex_ranges(self): """ description """ test_dir = 'python_generation' test_name = 'python_complex_ranges' env.generate_single_file( f'{test_dir}/{test_name}.py', lambda: PythonGenerator(env.input_path(test_dir, f'{test_name}.yaml'), importmap=env.import_map, mergeimports=False).serialize(), comparator=lambda exp, act: compare_python( exp, act, self.env.expected_path(f'{test_dir}/{test_name}.py') ), value_is_returned=True)
def test_python_lists_and_keys(self): """ description """ test_dir = 'python_generation' test_name = 'python_lists_and_keys' test_path = f'{test_dir}/{test_name}.py' env.generate_single_file( test_path, lambda: PythonGenerator(env.input_path(test_dir, f'{test_name}.yaml'), importmap=env.import_map, mergeimports=False).serialize(), comparator=lambda expected, actual: compare_python( expected, actual, env.expected_path(test_path)), value_is_returned=True)