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_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_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_python_types(self): """ description """ test_dir = 'python_generation' test_name = 'python_types' python_name = f'{test_dir}/{test_name}.py' env.generate_single_file( python_name, 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(python_name)), value_is_returned=True) module = compile_python(env.expected_path(python_name)) self.check_expecteds(module.Strings, "Strings") self.check_expecteds(module.Booleans, "Booleans") self.check_expecteds(module.Integers, "Integers")
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)
def test_notebook_model_2(self): file = 'notebook_model_2' 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)) t = module.Sample("Something", [ module.UnusualEnumPatterns.M, module.UnusualEnumPatterns['% ! -- whoo'] ]) print(str(t))
def test_notebook_model_4(self): file = 'notebook_model_4' 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)) colorrec = module.FavoriteColor("Harold", module.Colors['2']) print(colorrec) print(str(colorrec.position)) print(colorrec.position.meaning) cr2 = module.FavoriteColor("Donald", module.Colors['4']) print(cr2.position.meaning)