Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vminfant committed Sep 4, 2024
1 parent 1f19adb commit 4e162b9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
6 changes: 5 additions & 1 deletion cognite/cdffs/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ def split_path(self, path: str, validate_suffix: bool = True, directory_prefix:
"""
if directory_prefix or self.file_metadata.directory:
root_dir = directory_prefix.strip("/") if directory_prefix else self.file_metadata.directory.strip("/")
external_id = path.replace(root_dir, "").lstrip("/")
if root_dir in path:
external_id = path.replace(root_dir, "").lstrip("/")
else:
external_id = path.split("/")[-1]
external_id_prefix = Path(external_id).parts[0]

elif self._suffix_exists(path):
Expand Down Expand Up @@ -510,6 +513,7 @@ def open(
if isinstance(kwargs.get("file_metadata"), FileMetadata):
file_metadata: FileMetadata = kwargs.get("file_metadata")
root_dir, _, external_id = self.split_path(path, directory_prefix=file_metadata.directory)
print(root_dir, _, external_id)
else:
root_dir, _, external_id = self.split_path(path)

Expand Down
42 changes: 41 additions & 1 deletion tests/test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ def test_open_write(filesystem, test_path, test_mode, test_data, expected_len):
with filesystem.open(test_path, mode=test_mode, file_metadata=FileMetadata(metadata={}), block_size=5) as cdf_file:
test_len = cdf_file.write(test_data.encode("utf-8"))

assert test_len == expected_len
assert test_len == expected_len


@pytest.mark.parametrize(
Expand Down Expand Up @@ -924,3 +924,43 @@ def test_open_read_exception_with_retry(test_path, test_out_length):
with pytest.raises(FileNotFoundError):
cdf_file = fs.open(test_path, mode="rb")
cdf_file.read(length=test_out_length)


@pytest.mark.parametrize(
"filesystem, test_path, test_mode, test_data, expected_len, mock_files_upload_response",
[
(
"fs",
"/sample_data/out/sample/df.csv",
"wb",
",A,B\n0,1,2\n1,4,5\n",
17,
"successful",
),
(
"az_fs",
"/sample_data/out/sample/df.csv",
"wb",
",A,B\n0,1,2\n1,4,5\n",
17,
"successful",
),
(
"gcp_fs",
"/sample_data/out/sample/df.csv",
"wb",
",A,B\n0,1,2\n1,4,5\n",
17,
"successful",
),
],
indirect=["filesystem", "mock_files_upload_response"],
)
@pytest.mark.usefixtures("mock_files_upload_response")
def test_open_write_with_metadata(filesystem, test_path, test_mode, test_data, expected_len):
with filesystem.open(
test_path, mode=test_mode, file_metadata=FileMetadata(directory="/data01/", metadata={}), block_size=5
) as cdf_file:
test_len = cdf_file.write(test_data.encode("utf-8"))
assert cdf_file.file_metadata.directory == "/data01/"
assert test_len == expected_len

0 comments on commit 4e162b9

Please sign in to comment.