Exemple #1
0
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
Exemple #2
0
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
Exemple #3
0
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
Exemple #4
0
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)
Exemple #5
0
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
Exemple #6
0
def test_missing_file():
    disp = dispatch.Dispatch()
    disp.register(text.TextAdaptor())

    with pytest.raises(FileNotFoundError):
        disp.read("missing_file.txt")