Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add more unit tests #62

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 159 additions & 1 deletion adlfs/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def spawn_azurite():

def test_connect(storage):
adlfs.AzureBlobFileSystem(
account_name=storage.account_name, connection_string=CONN_STR,
account_name=storage.account_name, connection_string=CONN_STR
)


Expand Down Expand Up @@ -97,6 +97,67 @@ def test_info(storage):
assert file_info == {"name": "data/root/a/file.txt", "type": "file", "size": 10}


def test_find(storage):
fs = adlfs.AzureBlobFileSystem(
account_name=storage.account_name, connection_string=CONN_STR
)

## just the directory name
assert fs.find("data/root/a") == ["data/root/a/file.txt"] # NOQA
assert fs.find("data/root/a/") == ["data/root/a/file.txt"] # NOQA

assert fs.find("data/root/c") == [
"data/root/c/file1.txt",
"data/root/c/file2.txt",
]
assert fs.find("data/root/c/") == [
"data/root/c/file1.txt",
"data/root/c/file2.txt",
]

## all files
assert fs.find("data/root") == [
"data/root/a/file.txt",
"data/root/b/file.txt",
"data/root/c/file1.txt",
"data/root/c/file2.txt",
"data/root/rfile.txt",
]
assert fs.find("data/root", withdirs=False) == [
"data/root/a/file.txt",
"data/root/b/file.txt",
"data/root/c/file1.txt",
"data/root/c/file2.txt",
"data/root/rfile.txt",
]

# all files and directories
assert fs.find("data/root", withdirs=True) == [
"data/root/a",
"data/root/a/file.txt",
"data/root/b",
"data/root/b/file.txt",
"data/root/c",
"data/root/c/file1.txt",
"data/root/c/file2.txt",
"data/root/rfile.txt",
]
assert fs.find("data/root/", withdirs=True) == [
"data/root/a",
"data/root/a/file.txt",
"data/root/b",
"data/root/b/file.txt",
"data/root/c",
"data/root/c/file1.txt",
"data/root/c/file2.txt",
"data/root/rfile.txt",
]

## missing
assert fs.find("data/missing") == []
assert fs.find("data/roo") == []


def test_glob(storage):
fs = adlfs.AzureBlobFileSystem(
account_name=storage.account_name, connection_string=CONN_STR
Expand All @@ -119,6 +180,7 @@ def test_glob(storage):
]

assert fs.glob("data/root/b/*") == ["data/root/b/file.txt"] # NOQA
assert fs.glob("data/root/b/**") == ["data/root/b/file.txt"] # NOQA

## across directories
assert fs.glob("data/root/*/file.txt") == [
Expand Down Expand Up @@ -149,6 +211,29 @@ def test_glob(storage):
"data/root/rfile.txt",
]

## all files
assert fs.glob("data/root/**") == [
"data/root/a",
"data/root/a/file.txt",
"data/root/b",
"data/root/b/file.txt",
"data/root/c",
"data/root/c/file1.txt",
"data/root/c/file2.txt",
"data/root/rfile.txt",
]
assert fs.glob("data/roo**") == [
"data/root",
"data/root/a",
"data/root/a/file.txt",
"data/root/b",
"data/root/b/file.txt",
"data/root/c",
"data/root/c/file1.txt",
"data/root/c/file2.txt",
"data/root/rfile.txt",
]

## missing
assert fs.glob("data/missing/*") == []

Expand All @@ -174,13 +259,31 @@ def test_rm(storage):
fs.ls("/data/root/a/file.txt")


def test_rm_recursive(storage):
fs = adlfs.AzureBlobFileSystem(
account_name=storage.account_name, connection_string=CONN_STR
)

assert "data/root/c/" in fs.ls("/data/root")
assert fs.ls("data/root/c") == [
"data/root/c/file1.txt",
"data/root/c/file2.txt",
]
fs.rm("data/root/c", recursive=True)
assert "data/root/c/" not in fs.ls("/data/root")

with pytest.raises(FileNotFoundError):
fs.ls("data/root/c")


def test_mkdir_rmdir(storage):
fs = adlfs.AzureBlobFileSystem(
account_name=storage.account_name, connection_string=CONN_STR
)

fs.mkdir("new-container")
assert "new-container/" in fs.ls("")
assert fs.ls("new-container") == []

with fs.open("new-container/file.txt", "wb") as f:
f.write(b"0123456789")
Expand Down Expand Up @@ -226,6 +329,61 @@ def test_mkdir_rmdir(storage):
assert "new-container/" not in fs.ls("")


def test_mkdir_rm_recursive(storage):
fs = adlfs.AzureBlobFileSystem(
account_name=storage.account_name, connection_string=CONN_STR
)

fs.mkdir("test_mkdir_rm_recursive")
assert "test_mkdir_rm_recursive/" in fs.ls("")

with fs.open("test_mkdir_rm_recursive/file.txt", "wb") as f:
f.write(b"0123456789")

with fs.open("test_mkdir_rm_recursive/dir/file.txt", "wb") as f:
f.write(b"ABCD")

with fs.open("test_mkdir_rm_recursive/dir/file2.txt", "wb") as f:
f.write(b"abcdef")

assert fs.find("test_mkdir_rm_recursive") == [
"test_mkdir_rm_recursive/dir/file.txt",
"test_mkdir_rm_recursive/dir/file2.txt",
"test_mkdir_rm_recursive/file.txt",
]

fs.rm("test_mkdir_rm_recursive", recursive=True)

assert "test_mkdir_rm_recursive/" not in fs.ls("")
assert fs.find("test_mkdir_rm_recursive") == []


def test_deep_paths(storage):
fs = adlfs.AzureBlobFileSystem(
account_name=storage.account_name, connection_string=CONN_STR
)

fs.mkdir("test_deep")
assert "test_deep/" in fs.ls("")

with fs.open("test_deep/a/b/c/file.txt", "wb") as f:
f.write(b"0123456789")

assert fs.ls("test_deep") == ["test_deep/a/"]
assert fs.ls("test_deep/") == ["test_deep/a/"]
assert fs.ls("test_deep/a") == ["test_deep/a/b/"]
assert fs.ls("test_deep/a/") == ["test_deep/a/b/"]
assert fs.find("test_deep") == ["test_deep/a/b/c/file.txt"]
assert fs.find("test_deep/") == ["test_deep/a/b/c/file.txt"]
assert fs.find("test_deep/a") == ["test_deep/a/b/c/file.txt"]
assert fs.find("test_deep/a/") == ["test_deep/a/b/c/file.txt"]

fs.rm("test_deep", recursive=True)

assert "test_deep/" not in fs.ls("")
assert fs.find("test_deep") == []


def test_large_blob(storage):
import tempfile
import hashlib
Expand Down