|
29 | 29 | FileMetadataWrite, |
30 | 30 | ) |
31 | 31 | from cognite.client.data_classes.labels import Label, LabelFilter |
32 | | -from cognite.client.exceptions import CogniteAPIError, CogniteAuthorizationError |
| 32 | +from cognite.client.exceptions import CogniteAPIError, CogniteAuthorizationError, CogniteFileUploadError |
33 | 33 | from tests.tests_unit.conftest import DefaultResourceGenerator |
34 | 34 | from tests.utils import get_or_raise, get_url, jsgz_load |
35 | 35 |
|
@@ -836,6 +836,57 @@ def test_upload_path_does_not_exist(self, cognite_client: CogniteClient) -> None |
836 | 836 | with pytest.raises(FileNotFoundError): |
837 | 837 | cognite_client.files.upload(path=Path("/no/such/path")) |
838 | 838 |
|
| 839 | + def test_upload_bytes_400_raises_file_upload_error( |
| 840 | + self, |
| 841 | + cognite_client: CogniteClient, |
| 842 | + example_file: dict[str, Any], |
| 843 | + async_client: AsyncCogniteClient, |
| 844 | + httpx_mock: HTTPXMock, |
| 845 | + ) -> None: |
| 846 | + """Bug in 8.0.0 to 8.6.0: a 400 from the blob storage PUT raised CogniteHTTPStatusError instead of CogniteFileUploadError.""" |
| 847 | + httpx_mock.add_response( |
| 848 | + method="POST", |
| 849 | + url=get_url(async_client.files) + "/files?overwrite=false", |
| 850 | + status_code=200, |
| 851 | + json=example_file, |
| 852 | + ) |
| 853 | + httpx_mock.add_response(method="PUT", url="https://upload.here", status_code=400, text="Bad Request") |
| 854 | + |
| 855 | + with pytest.raises(CogniteFileUploadError) as exc_info: |
| 856 | + cognite_client.files.upload_bytes(content=b"content", name="bla") |
| 857 | + |
| 858 | + assert exc_info.value.code == 400 |
| 859 | + |
| 860 | + def test_upload_content_part_400_raises_file_upload_error( |
| 861 | + self, |
| 862 | + cognite_client: CogniteClient, |
| 863 | + example_file: dict[str, Any], |
| 864 | + async_client: AsyncCogniteClient, |
| 865 | + httpx_mock: HTTPXMock, |
| 866 | + tmp_path: Path, |
| 867 | + ) -> None: |
| 868 | + """Bug in 8.0.0 to 8.6.0: a 400 from the blob storage PUT during multipart upload raised CogniteHTTPStatusError |
| 869 | + instead of CogniteFileUploadError. |
| 870 | + """ |
| 871 | + multipart_response = { |
| 872 | + "items": [{**example_file, "uploadUrls": ["https://upload.here/part0"], "uploadId": "test-id"}] |
| 873 | + } |
| 874 | + httpx_mock.add_response( |
| 875 | + method="POST", |
| 876 | + url=re.compile(re.escape(get_url(async_client.files) + "/files/multiuploadlink") + r"\?.*"), |
| 877 | + status_code=200, |
| 878 | + json=multipart_response, |
| 879 | + ) |
| 880 | + httpx_mock.add_response(method="PUT", url="https://upload.here/part0", status_code=400, text="Bad Request") |
| 881 | + |
| 882 | + test_file = tmp_path / "test.bin" |
| 883 | + test_file.write_bytes(b"x") |
| 884 | + |
| 885 | + with pytest.raises(CogniteFileUploadError) as exc_info: |
| 886 | + cognite_client.files.upload_content(path=test_file, instance_id=NodeId("test-space", "test-0001")) |
| 887 | + |
| 888 | + assert exc_info.value.code == 400 |
| 889 | + |
839 | 890 | @pytest.mark.parametrize( |
840 | 891 | "file_size, expected_parts", |
841 | 892 | [ |
|
0 commit comments