def test_parse_header(): r"""Test parsing header.""" header = [ b"# name\tnumber\tvalue\tcomplex\n", b"# n/a\tg\tcm\tn/a\n", b"# %5s\t%ld\t%lf\t%g%+gj\n" ] res = dict(delimiter=b'\t', comment=b'# ', newline=b'\n', format_str=b"%5s\t%ld\t%lf\t%g%+gj\n", fmts=[b'%5s', b'%ld', b'%lf', b'%g%+gj'], field_names=[b'name', b'number', b'value', b'complex'], field_units=[b'n/a', b'g', b'cm', b'n/a']) assert_equal(serialize.parse_header(header), res) assert_equal(serialize.parse_header(header[::-1]), res) _empty = b'' assert_equal(serialize.parse_header(_empty.join(header)), res) # Test without formats header2 = header[:2] res2 = dict(**res) del res2['format_str'] res2['fmts'] = [] assert_equal(serialize.parse_header(header2), res2) # Test with explicit line numbers assert_equal( serialize.parse_header(header, lineno_names=0, lineno_units=1), res) # Test errors header3 = [header[0], header[0]] assert_raises(RuntimeError, serialize.parse_header, header3) header4 = [header[1], header[1]] assert_raises(RuntimeError, serialize.parse_header, header4)
def test_dict2list(): r"""Test conversion of a dictionary to a list and back.""" assert_raises(TypeError, serialize.dict2list, None) assert_raises(TypeError, serialize.list2dict, None) x = {'c': 0, 'b': 1, 'a': 2} y = [2, 1, 0] assert_equal(serialize.dict2list(x), y)
def test_combine_flds(): r"""Test combine_flds.""" names0 = ['f0', 'f1', 'f2', 'f3'] names1 = ["name", "number", "value", "complex"] dtypes = ['S5', 'i8', 'f8', 'c16'] dtype0 = np.dtype([(n, f) for n, f in zip(names0, dtypes)]) dtype1 = np.dtype([(n, f) for n, f in zip(names1, dtypes)]) nfld = len(names0) nele = 5 arrs = [np.zeros(nele, dtype=dtype0[i]) for i in range(nfld)] np.testing.assert_array_equal(serialize.combine_flds(arrs), np.zeros(nele, dtype0)) np.testing.assert_array_equal(serialize.combine_flds(arrs, dtype=dtype1), np.zeros(nele, dtype1)) # Version of test where width of string field needs to be found dtype = np.dtype([("name", "S"), ("number", "i8"), ("value", "f8"), ("complex", "c16")]) arrs[0][0] = b"hello" result = np.zeros(nele, dtype1) result["name"][0] = b"hello" np.testing.assert_array_equal(serialize.combine_flds(arrs, dtype=dtype), result) # Errors assert_raises(ValueError, serialize.combine_flds, arrs[:-1], dtype=dtype0) arrs[0] = np.zeros(nele - 1, dtype=dtype0[0]) assert_raises(ValueError, serialize.combine_flds, arrs)
def test_pandas2dict(): r"""Test conversion of a Pandas data frame to a dictionary and back.""" assert_raises(TypeError, serialize.dict2pandas, None) assert_raises(TypeError, serialize.pandas2dict, None) nele = 5 names = ["complex", "name", "number", "value"] dtypes = ['c16', 'S5', 'i8', 'f8'] dtype = np.dtype([(n, f) for n, f in zip(names, dtypes)]) arr_mix = np.zeros(nele, dtype) arr_mix['name'][0] = 'hello' arr_obj = np.array([list(), 'hello', 5], dtype='O') test_arrs = [ arr_mix, np.zeros(nele, 'float'), arr_mix['name'], arr_obj, np.array([]) ] for ans in test_arrs: frame = serialize.numpy2pandas(ans) # Sorted d = serialize.pandas2dict(frame) res = serialize.dict2pandas(d) np.testing.assert_array_equal(res, frame) # Provided d = serialize.pandas2dict(frame) res = serialize.dict2pandas(d, order=ans.dtype.names) np.testing.assert_array_equal(res, frame)
def test_definition2dtype_errors(): r"""Check that error raised if type not specified.""" assert_raises(KeyError, ScalarMetaschemaProperties.definition2dtype, {}) assert_raises(RuntimeError, ScalarMetaschemaProperties.definition2dtype, {'type': 'float'}) assert_equal(ScalarMetaschemaProperties.definition2dtype({'type': 'bytes'}), np.dtype((ScalarMetaschemaProperties._valid_types['bytes'])))
def test_CMakeModelDriver_error_nofile(): r"""Test CMakeModelDriver error for missing CMakeLists.txt.""" assert_raises(RuntimeError, CMakeModelDriver, 'test', 'invalid', target_language='c')
def test_get_schema_subtype(): r"""Test get_schema_subtype for allow_instance=True.""" component = 'serializer' subtype = 'direct' doc = {'seritype': subtype} valid = components.create_component(component, seritype=subtype) invalid = components.create_component(component, seritype='json') s = schema.get_schema() kwargs = {'subtype': subtype, 'allow_instance': True} s.validate_component(component, doc, **kwargs) s.validate_component(component, valid, **kwargs) assert_raises(ValidationError, s.validate_component, component, invalid, **kwargs) s.validate_component(component, doc, subtype=subtype) assert_raises(ValidationError, s.validate_component, component, valid, subtype=subtype) # Test for base s.validate_component(component, valid, subtype='base', allow_instance=True) s.validate_component(component, invalid, subtype='base', allow_instance=True)
def test_get_type_from_def(): r"""Test get_type_from_def.""" datatypes.get_type_from_def('float') datatypes.get_type_from_def({'type': 'float'}) datatypes.get_type_from_def({'a': 'float', 'b': 'int'}) datatypes.get_type_from_def(['float', 'int']) assert_raises(TypeError, datatypes.get_type_from_def, None)
def test_load_yaml_git(): r"""Test loading a yaml from a remote git repository.""" yml = "https://github.com/cropsinsilico/example-fakemodel/fakemodel.yml" assert_raises(Exception, yamlfile.load_yaml, yml) assert('model' in yamlfile.load_yaml('git:' + yml)) yml = "cropsinsilico/example-fakemodel/fakemodel.yml" assert('model' in yamlfile.load_yaml('git:' + yml))
def test_format_header(): r"""Test formatting header.""" kws_all = dict(field_names=['name', 'number', 'value', 'complex'], field_units=['n/a', 'g', 'cm', 'n/a']) res_all = dict(names="# name\tnumber\tvalue\tcomplex\n", units="# n/a\tg\tcm\tn/a\n") if platform._is_win: # pragma: windows kws_all['format_str'] = "%5s\t%l64d\t%g\t%g%+gj\n" res_all['format'] = "# " + kws_all['format_str'] else: kws_all['format_str'] = "%5s\t%ld\t%g\t%g%+gj\n" res_all['format'] = "# " + kws_all['format_str'] kws_all['dtype'] = serialize.cformat2nptype(kws_all['format_str'], names=kws_all['field_names']) for x in [kws_all, res_all]: for k, v in x.items(): if isinstance(v, str): x[k] = backwards.as_bytes(v) elif isinstance(v, list): x[k] = [backwards.as_bytes(iv) for iv in v] test_list = [(['format_str', 'field_names', 'field_units'], ['names', 'units', 'format']), (['field_names', 'field_units', 'dtype'], ['names', 'units', 'format']), (['field_units', 'dtype'], ['names', 'units', 'format']), (['field_names'], ['names']), (['field_units'], ['units'])] for kws_keys, res_keys in test_list: kws = {k: kws_all[k] for k in kws_keys} res = b''.join([res_all[k] for k in res_keys]) assert_equal(serialize.format_header(**kws), res) assert_raises(ValueError, serialize.format_header)
def test_guess_type_from_obj(): r"""Test guess_type_from_obj.""" invalid_objects = [object, object()] for t, x in _valid_objects.items(): assert_equal(datatypes.guess_type_from_obj(x).name, t) for x in invalid_objects: assert_raises(datatypes.MetaschemaTypeError, datatypes.guess_type_from_obj, x)
def test_get_compilation_tool(): r"""Test get_compilation_tool for different name variations.""" from yggdrasil.drivers.CModelDriver import CModelDriver if CModelDriver.is_language_installed(): tooltype = 'compiler' out = CModelDriver.get_tool('compiler').__class__ toolname = out.toolname.lower() toolpath = os.path.join('somedir', toolname) toolfile = toolpath + '.exe' vals = [toolpath, toolfile] if platform._is_win: vals += [toolname.upper(), toolfile.upper()] for v in vals: assert_equal(CompiledModelDriver.get_compilation_tool(tooltype, v), out) assert_raises(ValueError, CompiledModelDriver.get_compilation_tool, 'compiler', 'invalid') # else: # assert_raises(NotImplementedError, CModelDriver.get_tool, 'compiler') # assert_equal(CModelDriver.get_tool( # 'compiler', default='invalid'), 'invalid') assert_equal( CompiledModelDriver.get_compilation_tool('compiler', 'invalid', default='invalid'), 'invalid')
def test_ModelDriver_implementation(): r"""Test that NotImplementedError raised for base class.""" assert_raises(NotImplementedError, ModelDriver.language_executable) assert_raises(NotImplementedError, ModelDriver.executable_command, None) assert_raises(NotImplementedError, ModelDriver.is_library_installed, None) assert_raises(NotImplementedError, CompiledModelDriver.get_tool, 'compiler') assert_raises(NotImplementedError, InterpretedModelDriver.get_interpreter)
def test_get_example_languages(): r"""Test get_example_languages.""" assert_raises(KeyError, get_example_languages, 'invalid') get_example_languages('ascii_io') get_example_languages('ascii_io', language='python') get_example_languages('ascii_io', language='all') get_example_languages('ascii_io', language='all_nomatlab')
def test_register_metaschema_property(): r"""Test errors in register_metaschema_property.""" # Error when property class already registered args = ('ReplacementClassSchema', (MetaschemaProperty, ), { 'name': existing_class }) assert_raises(ValueError, type, *args) # Error when replacement class has schema args = ('ReplacementClassSchema', (MetaschemaProperty, ), { 'name': existing_validator, 'schema': {} }) assert_raises(ValueError, type, *args) # Error when validate set def fake_validate(*args, **kwargs): # pragma: no cover return args = ('ReplacementClassSchema', (MetaschemaProperty, ), { 'name': existing_validator, '_validate': fake_validate }) assert_raises(ValueError, type, *args) args = ('ReplacementClassSchema', (MetaschemaProperty, ), { 'name': existing_validator, 'schema': {} }) assert_raises(ValueError, type, *args) # Error when property not in existing metaschema get_metaschema() # ensures it has been initialized args = ('ReplacementClassSchema', (MetaschemaProperty, ), { 'name': non_existant }) assert_raises(ValueError, type, *args)
def test_match_stype(): r"""Test string type matching.""" slist = ['hello', b'hello', u'hello', bytearray('hello', 'utf-8')] for s1 in slist: for s2 in slist: assert_equal(backwards.match_stype(s1, s2), s1) assert_raises(TypeError, backwards.match_stype, 1, 'hello')
def test_ConnectionDriverTranslate_errors(): r"""Test that errors are raised for invalid translators.""" assert (not hasattr(invalid_translate, '__call__')) assert_raises(ValueError, ConnectionDriver, 'test', translator=invalid_translate)
def test_invalid_protocol(): r"""Test raise of an error in the event of an invalid protocol.""" assert_raises(ValueError, new_comm, 'test_invalid_protocol', comm='ZMQComm', protocol='invalid')
def test_CMakeModelDriver_error_notarget(): r"""Test CMakeModelDriver error for invalid target.""" makedir, target = os.path.split(scripts['cmake']) assert_raises(RuntimeError, CMakeModelDriver, 'test', 'invalid', sourcedir=makedir)
def test_register_type_errors(): r"""Test errors in register_type for duplicate.""" assert_raises(ValueError, datatypes.register_type, ScalarMetaschemaType) type_args = ('FakeType', (ScalarMetaschemaType, ), { 'name': 'new', 'properties': ['invalid'] }) assert_raises(ValueError, type, *type_args)
def test_platform_error(): r"""Test error when test cannot be performed.""" test_platform_map = {'MacOS': 'Linux', 'Linux': 'Windows', 'Windows': 'MacOS'} test_platform = test_platform_map[platform._platform] x = timing.TimedRun(_test_lang, _test_lang, platform=test_platform) assert_raises(RuntimeError, x.can_run, raise_error=True)
def test_error_valgrind_strace(): r"""Test error if both valgrind and strace set.""" assert_raises(RuntimeError, ModelDriver, 'test', 'test', with_strace=True, with_valgrind=True)
def test_error_code(self): r"""Test that error is raised when code generates one.""" if (((not self.import_cls.is_installed()) or (self.import_cls.function_param is None))): return error_msg = 'Test error' lines = [self.import_cls.function_param['error'].format(error_msg=error_msg)] assert_raises(RuntimeError, self.import_cls.run_code, lines)
def test_save(self): r"""Test save with/without overwrite.""" self.check_filename() old_text = self.get_raw_data() assert_raises(RuntimeError, self.instance.save, self.instance.data) self.instance.save(self.instance.data, overwrite=True) new_text = self.get_raw_data() self.assert_equal(new_text, old_text)
def test_error_valgrind_strace_windows(): # pragma: windows r"""Test error if strace or valgrind called on windows.""" assert_raises(RuntimeError, ModelDriver, 'test', 'test', with_strace=True) assert_raises(RuntimeError, ModelDriver, 'test', 'test', with_valgrind=True)
def test_singular2plural(): r"""Test conversion from singular element names to plural ones and back.""" pairs = [('face', 'faces'), ('vertex', 'vertices'), ('vertex_index', 'vertex_indices')] for s, p in pairs: assert_equal(PlyMetaschemaType.singular2plural(s), p) assert_equal(PlyMetaschemaType.plural2singular(p), s) assert_raises(ValueError, PlyMetaschemaType.plural2singular, 'invalid')
def test_platform_error(): r"""Test error when test cannot be performed.""" if platform._is_mac: test_platform = 'Linux' else: test_platform = 'MacOS' x = timing.TimedRun(_test_lang, _test_lang, platform=test_platform) assert_raises(RuntimeError, x.can_run, raise_error=True)
def test_CMakeModelDriver_error_cmake(): r"""Test CMakeModelDriver error for invalid cmake args.""" makedir, target = os.path.split(scripts['cmake']) assert_raises(RuntimeError, CMakeModelDriver, 'test', target, sourcedir=makedir, cmakeargs='-P')
def test_normalizer_iter_errors_js2(): r"""Test normalizer iter_errors_js2 method.""" cls = get_validator() s = {'type': 'array', 'items': [True]} x = cls(s) x.validate([True]) s = {'type': 'array', 'items': [False]} x = cls(s) assert_raises(jsonschema.exceptions.ValidationError, x.validate, [True])
def test_get_compatible_tool(): r"""Test get_compatible_tool when default provided.""" assert_raises(ValueError, CompiledModelDriver.get_compatible_tool, 'invalid', 'compiler', 'c') assert_equal( CompiledModelDriver.get_compatible_tool('invalid', 'compiler', 'c', default=None), None)