|
1 | 1 | import copy
|
2 | 2 | import json
|
| 3 | +import os |
| 4 | +import tempfile |
3 | 5 | from collections import OrderedDict
|
4 | 6 | from copy import deepcopy
|
5 | 7 | from typing import Any, List, Optional, Tuple
|
@@ -810,6 +812,49 @@ def test_send_stream_message(
|
810 | 812 | req, self.controller
|
811 | 813 | )
|
812 | 814 |
|
| 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 | + |
813 | 858 | @pytest.mark.parametrize(
|
814 | 859 | "response, return_value",
|
815 | 860 | [
|
|
0 commit comments