Skip to content

Commit d6d8b90

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 5002e8d commit d6d8b90

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,5 +1,7 @@
11
import copy
22
import json
3+
import os
4+
import tempfile
35
from collections import OrderedDict
46
from copy import deepcopy
57
from typing import Any, List, Optional, Tuple
@@ -810,6 +812,49 @@ def test_send_stream_message(
810812
req, self.controller
811813
)
812814

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

0 commit comments

Comments
 (0)