Skip to content

Commit

Permalink
Merge pull request #56 from appliedAI-Initiative/add-missing-fileop-t…
Browse files Browse the repository at this point in the history
…ests

Add missing tests for `fs.exists()` and `fs.rm()`
  • Loading branch information
nicholasjng authored Sep 4, 2023
2 parents 3902d28 + 8b382e3 commit b1807df
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lakefs_spec/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def commit(self, fsevent: FSEvent, repository: str, branch: str, resource: str)
def exists(self, path, **kwargs):
repository, ref, resource = parse(path)
try:
self.client.objects_api.head_object(repository, ref, path)
self.client.objects_api.head_object(repository, ref, resource)
return True
except NotFoundException:
return False
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def ensurerepo(lakefs_client: LakeFSClient) -> str:
RepositoryCreation(
name=_TEST_REPO,
storage_namespace=storage_namespace,
sample_data=True,
)
)
return _TEST_REPO
Expand Down
28 changes: 28 additions & 0 deletions tests/test_exists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from lakefs_spec import LakeFSFileSystem
from tests.util import RandomFileFactory


def test_exists(fs: LakeFSFileSystem, repository: str) -> None:
"""Test `fs.exists` on an existing file. Requires a populated repository."""

example_file = f"{repository}/main/README.md"
assert fs.exists(example_file)

nonexistent_file = f"{repository}/main/nonexistent.parquet"
assert not fs.exists(nonexistent_file)


def test_exists_on_staged_file(
random_file_factory: RandomFileFactory,
fs: LakeFSFileSystem,
repository: str,
temp_branch: str,
) -> None:
random_file = random_file_factory.make()

lpath = str(random_file)
rpath = f"{repository}/{temp_branch}/{random_file.name}"

# upload, verify existence.
fs.put(lpath, rpath)
assert fs.exists(rpath)
31 changes: 31 additions & 0 deletions tests/test_rm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from lakefs_spec import LakeFSFileSystem


def test_rm(
fs: LakeFSFileSystem,
repository: str,
temp_branch: str,
) -> None:
path = f"{repository}/{temp_branch}/README.md"

fs.rm(path)
assert not fs.exists(path)


def test_rm_with_postcommit(
fs: LakeFSFileSystem,
repository: str,
temp_branch: str,
) -> None:
with fs.scope(postcommit=True):
path = f"{repository}/{temp_branch}/README.md"

fs.rm(path)
assert not fs.exists(path)

commits = fs.client.refs_api.log_commits(
repository=repository,
ref=temp_branch,
)
latest_commit = commits.results[0]
assert latest_commit.message == "Remove file README.md"

0 comments on commit b1807df

Please sign in to comment.