Skip to content
10 changes: 6 additions & 4 deletions nisystemlink/clients/file/_file_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,9 @@ def __upload_file(

Args:
file: The file to upload.
metadata: JSON Dictionary with key/value pairs
metadata: Multipart part for file metadata, typically ``None`` or a
``(None, json_string, "application/json")`` tuple where
``json_string`` contains the metadata key/value pairs.
id: Specify an unique (among all file) 24-digit Hex string ID of the file once it is uploaded.
Defaults to None.
workspace: The id of the workspace the file belongs to. Defaults to None.
Expand Down Expand Up @@ -378,13 +380,13 @@ def upload_file(
ApiException: if unable to communicate with the File Service.
"""
if metadata:
metadata_str = json.dumps(metadata)
metadata_part = (None, json.dumps(metadata), "application/json")
else:
metadata_str = None
metadata_part = None
Comment thread
AMoldova-NI marked this conversation as resolved.

file_id = self.__upload_file(
file=file,
metadata=metadata_str,
metadata=metadata_part,
id=id,
Comment thread
AMoldova-NI marked this conversation as resolved.
workspace=workspace,
)
Expand Down
36 changes: 27 additions & 9 deletions tests/integration/file/test_file_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def client(enterprise_config) -> FileClient:
return FileClient(enterprise_config)


@pytest.fixture(scope="class")
@pytest.fixture
def binary_file_data() -> BinaryIO:
"""Test Binary file content."""
return io.BytesIO(TEST_FILE_DATA)
Expand All @@ -44,10 +44,12 @@ def test_file(client: FileClient):
"""Fixture to return a factory that uploads a file."""
file_ids = []

def _test_file(file_name: str = TEST_FILE_NAME, cleanup: bool = True) -> str:
def _test_file(
file_name: str = TEST_FILE_NAME, cleanup: bool = True, metadata: dict = {}
) -> str:
test_file = io.BytesIO(TEST_FILE_DATA)
test_file.name = file_name
file_id = client.upload_file(file=test_file)
file_id = client.upload_file(file=test_file, metadata=metadata)

if cleanup:
file_ids.append(file_id)
Expand Down Expand Up @@ -91,6 +93,28 @@ def test__api_info__returns(self, client: FileClient):
api_info = client.api_info()
assert len(api_info.model_dump()) != 0

def test__upload_file_with_metadata__succeeds(
self,
client: FileClient,
test_file,
random_filename_extension: str,
):
Comment thread
AMoldova-NI marked this conversation as resolved.
file_name = random_filename_extension
metadata = {"CustomProp": "CustomValue"}

file_id = test_file(file_name=file_name, metadata=metadata)

# Verify the file was created with correct metadata
files = client.get_files(ids=[file_id])
assert files.total_count == 1
assert len(files.available_files) == 1
assert files.available_files[0].id == file_id
assert files.available_files[0].properties is not None
assert files.available_files[0].properties["Name"] == file_name
assert (
len(files.available_files[0].properties.keys()) == len(metadata) + 1
) # Name + 1 custom property
Comment thread
AMoldova-NI marked this conversation as resolved.

def test__upload_get_delete_files__succeeds(
self, client: FileClient, test_file, random_filename_extension
):
Expand All @@ -105,12 +129,6 @@ def test__upload_get_delete_files__succeeds(
assert files.available_files[0].properties is not None
assert files.available_files[0].properties["Name"] == random_filename_extension

client.delete_file(id=file_id)

# confirm that file was deleted
files = client.get_files(ids=[file_id])
assert files.total_count == 0

def test__delete_file__invalid_id_raises(
self, client: FileClient, invalid_file_id: str
):
Expand Down
Loading