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

[((365, 402), 'fsspec.get_mapper', 'fsspec.get_mapper', (["('file://' + tmpdir)"], {}), "('file://' + tmpdir)\n", (382, 402), False, 'import fsspec\n'), ((485, 510), 'fsspec.filesystem', 'fsspec.filesystem', (['"""file"""'], {}), "('file')\n", (502, 510), False, 'import fsspec\n'), ((633, 663), 'fsspec.implementations.memory.MemoryFileSystem.store.clear', 'MemoryFileSystem.store.clear', ([], {}), '()\n', (661, 663), False, 'from fsspec.implementations.memory import MemoryFileSystem\n'), ((672, 702), 'fsspec.get_mapper', 'fsspec.get_mapper', (['"""memory://"""'], {}), "('memory://')\n", (689, 702), False, 'import fsspec\n'), ((987, 1017), 'fsspec.get_mapper', 'fsspec.get_mapper', (['"""memory://"""'], {}), "('memory://')\n", (1004, 1017), False, 'import fsspec\n'), ((1248, 1278), 'fsspec.get_mapper', 'fsspec.get_mapper', (['"""memory://"""'], {}), "('memory://')\n", (1265, 1278), False, 'import fsspec\n'), ((187, 218), 'os.path.join', 'os.path.join', (['tmpdir', '"""afolder"""'], {}), "(tmpdir, 'afolder')\n", (199, 218), False, 'import os\n'), ((755, 778), 'pytest.raises', 'pytest.raises', (['KeyError'], {}), '(KeyError)\n', (768, 778), False, 'import pytest\n'), ((1109, 1124), 'pickle.dumps', 'pickle.dumps', (['m'], {}), '(m)\n', (1121, 1124), False, 'import pickle\n'), ((229, 258), 'os.path.join', 'os.path.join', (['tmpdir', '"""afile"""'], {}), "(tmpdir, 'afile')\n", (241, 258), False, 'import os\n'), ((288, 334), 'os.path.join', 'os.path.join', (['tmpdir', '"""afolder"""', '"""anotherfile"""'], {}), "(tmpdir, 'afolder', 'anotherfile')\n", (300, 334), False, 'import os\n')]