import inspect
import posixpath

from utils import *
import numpy as np
import numpy.testing as npt
import pickle


def test_io_bytes(hdfs):
    testname = inspect.stack()[0][3]
    fname = posixpath.join(TEST_DIR, testname)

    data = b'a' * 10 * 2**20
    data += b'b' * 10 * 2**20
    data += b'c' * 10 * 2**20

    with hdfs.open(fname, 'w', replication=1) as f:
        f.write(data)

    with hdfs.open(fname, 'r') as f:
        read = f.read(len(data))
        assert len(data) == len(read)
        assert read == data


def test_io_pickle(hdfs):
    testname = inspect.stack()[0][3]
    fname = posixpath.join(TEST_DIR, testname)

    arr = np.random.normal(10, 2, size=(100, 100))
    data = pickle.dumps(arr)

    with hdfs.open(fname, 'w') as f:
        f.write(data)

    with hdfs.open(fname, 'r') as f:
        read = f.read(len(data))
        assert len(data) == len(read)
        assert data == read
        read = pickle.loads(read)
        npt.assert_equal(arr, read)

@@ -976,12 +976,651 @@
 (arr, read)%0A
+%0A%0Adef test_io_read_nonexisten(hdfs):%0A    with pytest.raises(IOError):%0A        f = hdfs.open('/tmp/NOFILE', 'r')%0A%0A%0Adef test_io_open_write_read(hdfs):%0A    testname = inspect.stack()%5B0%5D%5B3%5D%0A    fname = posixpath.join(TEST_DIR, testname)%0A%0A    f = hdfs.open(fname, 'w')%0A    with pytest.raises(IOError):%0A        f.read()%0A    f.close()%0A%0A%0Adef test_io_open_read_write(hdfs):%0A    testname = inspect.stack()%5B0%5D%5B3%5D%0A    fname = posixpath.join(TEST_DIR, testname)%0A%0A    data = b'a' * 10 * 2**20%0A    with hdfs.open(fname, 'w') as f:%0A        f.write(data)%0A%0A%0A    f = hdfs.open(fname, 'r')%0A    with pytest.raises(IOError):%0A        f.write(data)%0A    f.close()%0A

Add tests for open errors