import os
import fsspec
from fsspec.implementations.memory import MemoryFileSystem
import pickle
import pytest


def test_mapping_prefix(tmpdir):
    tmpdir = str(tmpdir)
    os.makedirs(os.path.join(tmpdir, "afolder"))
    open(os.path.join(tmpdir, "afile"), "w").write("test")
    open(os.path.join(tmpdir, "afolder", "anotherfile"), "w").write("test2")

    m = fsspec.get_mapper("file://" + tmpdir)
    assert "afile" in m
    assert m["afolder/anotherfile"] == b"test2"

    fs = fsspec.filesystem("file")
    m2 = fs.get_mapper(tmpdir)
    m3 = fs.get_mapper("file://" + tmpdir)

    assert m == m2 == m3


def test_ops():
    MemoryFileSystem.store.clear()
    m = fsspec.get_mapper("memory://")
    assert not m
    assert list(m) == []

    with pytest.raises(KeyError):
        m["hi"]

    assert m.pop("key", 0) == 0

    m["key0"] = b"data"
    assert list(m) == ["key0"]
    assert m["key0"] == b"data"

    m.clear()

    assert list(m) == []


def test_pickle():
    m = fsspec.get_mapper("memory://")
    assert isinstance(m.fs, MemoryFileSystem)
    m["key"] = b"data"
    m2 = pickle.loads(pickle.dumps(m))
    assert list(m) == list(m2)


def test_keys_view():
    # https://github.com/intake/filesystem_spec/issues/186
    m = fsspec.get_mapper("memory://")
    m["key"] = b"data"

    keys = m.keys()
    assert len(keys) == 1
    # check that we don't consume the keys
    assert len(keys) == 1

[((14, 8, 14, 45), 'fsspec.get_mapper', 'fsspec.get_mapper', ({(14, 26, 14, 44): "'file://' + tmpdir"}, {}), "('file://' + tmpdir)", False, 'import fsspec\n'), ((18, 9, 18, 34), 'fsspec.filesystem', 'fsspec.filesystem', ({(18, 27, 18, 33): '"""file"""'}, {}), "('file')", False, 'import fsspec\n'), ((26, 4, 26, 34), 'fsspec.implementations.memory.MemoryFileSystem.store.clear', 'MemoryFileSystem.store.clear', ({}, {}), '()', False, 'from fsspec.implementations.memory import MemoryFileSystem\n'), ((27, 8, 27, 38), 'fsspec.get_mapper', 'fsspec.get_mapper', ({(27, 26, 27, 37): '"""memory://"""'}, {}), "('memory://')", False, 'import fsspec\n'), ((46, 8, 46, 38), 'fsspec.get_mapper', 'fsspec.get_mapper', ({(46, 26, 46, 37): '"""memory://"""'}, {}), "('memory://')", False, 'import fsspec\n'), ((55, 8, 55, 38), 'fsspec.get_mapper', 'fsspec.get_mapper', ({(55, 26, 55, 37): '"""memory://"""'}, {}), "('memory://')", False, 'import fsspec\n'), ((10, 16, 10, 47), 'os.path.join', 'os.path.join', ({(10, 29, 10, 35): 'tmpdir', (10, 37, 10, 46): '"""afolder"""'}, {}), "(tmpdir, 'afolder')", False, 'import os\n'), ((31, 9, 31, 32), 'pytest.raises', 'pytest.raises', ({(31, 23, 31, 31): 'KeyError'}, {}), '(KeyError)', False, 'import pytest\n'), ((49, 22, 49, 37), 'pickle.dumps', 'pickle.dumps', ({(49, 35, 49, 36): 'm'}, {}), '(m)', False, 'import pickle\n'), ((11, 9, 11, 38), 'os.path.join', 'os.path.join', ({(11, 22, 11, 28): 'tmpdir', (11, 30, 11, 37): '"""afile"""'}, {}), "(tmpdir, 'afile')", False, 'import os\n'), ((12, 9, 12, 55), 'os.path.join', 'os.path.join', ({(12, 22, 12, 28): 'tmpdir', (12, 30, 12, 39): '"""afolder"""', (12, 41, 12, 54): '"""anotherfile"""'}, {}), "(tmpdir, 'afolder', 'anotherfile')", False, 'import os\n')]