Skip to content

Commit c84a30a

Browse files
test_model: Tests for get_file_upload_uri.
There are three tests based on upload result and existence of file. tempfile is used to create and write file in case of existing files and in csase of non-existing files a invalid temp_file_path is created.
1 parent 2254409 commit c84a30a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/model/test_model.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import json
2+
import os
3+
import tempfile
24
from collections import OrderedDict
35
from copy import deepcopy
46
from typing import Any, List, Optional, Tuple
@@ -808,6 +810,49 @@ def test_send_stream_message(
808810
req, self.controller
809811
)
810812

813+
@pytest.mark.parametrize(
814+
"file_name, upload_result, expected_result",
815+
[
816+
case(
817+
"existing_file.txt",
818+
{"result": "success", "uri": "http://example.com/success_uri"},
819+
"http://example.com/success_uri",
820+
id="exisiting_file_with_successful_response",
821+
),
822+
case(
823+
"existing_file.txt",
824+
{"result": "failure", "error_message": "Upload failed"},
825+
None,
826+
id="exisiting_file_with_unsuccessful_response",
827+
),
828+
case(
829+
"non_existing_file.txt",
830+
None,
831+
None,
832+
id="non_exisiting_file_with_no_response",
833+
),
834+
],
835+
)
836+
def test_get_file_upload_uri(
837+
self, mocker, model, file_name, upload_result, expected_result
838+
):
839+
self.client.upload_file = mocker.Mock(return_value=upload_result)
840+
with tempfile.TemporaryDirectory() as temp_dir:
841+
if upload_result is not None:
842+
temp_file_path = os.path.join(temp_dir, file_name)
843+
with open(temp_file_path, "w") as temp_file:
844+
temp_file.write("Test content")
845+
else:
846+
temp_file_path = f"random_path/{file_name}"
847+
848+
result = model.get_file_upload_uri(temp_file_path)
849+
850+
if upload_result is not None:
851+
self.client.upload_file.assert_called_once()
852+
else:
853+
self.client.upload_file.assert_not_called()
854+
assert result == expected_result
855+
811856
@pytest.mark.parametrize(
812857
"response, return_value",
813858
[

0 commit comments

Comments
 (0)