def test_no_matching_adaptor(): disp = dispatch.Dispatch() with pytest.raises(TypeError): disp.write("foo.txt", "foo") err = disp.write_safely("foo.txt", "foo") assert err is not None
def test_failed_read(): disp = dispatch.Dispatch() disp.register(text.TextAdaptor()) # Attempt to read hdf5 using text adaptor hdf5_filename = "tests/data/hdf5_format_v1/training.scale=0.50,sigma=10.h5" x, err = disp.read_safely(hdf5_filename) # There should be an error assert err is not None
def test_text_adaptor(tmpdir): disp = dispatch.Dispatch() disp.register(text.TextAdaptor()) filename = os.path.join(tmpdir, "textfile.txt") some_text = "some text to save in a file" disp.write(filename, some_text) read_text = disp.read(filename) assert some_text == read_text
def test_invalid_json(tmpdir): # Write an "invalid" json file filename = os.path.join(tmpdir, "textfile.json") some_text = "some text to save in a file" with open(filename, "w") as f: f.write(some_text) disp = dispatch.Dispatch() disp.register(genericjson.GenericJsonAdaptor()) assert not disp.open(filename).is_json with pytest.raises(TypeError): disp.read(filename)
def test_json_adaptor(tmpdir): disp = dispatch.Dispatch() disp.register(genericjson.GenericJsonAdaptor()) filename = os.path.join(tmpdir, "jsonfile.json") d = dict(foo=123, bar="zip") disp.write(filename, d) read_dict = disp.read(filename) assert d == read_dict assert disp.open(filename).is_json
def test_missing_file(): disp = dispatch.Dispatch() disp.register(text.TextAdaptor()) with pytest.raises(FileNotFoundError): disp.read("missing_file.txt")