def test_download_one_file_fails(self, mock_file_download_response_one_fails):
     with TemporaryDirectory() as dir:
         with pytest.raises(CogniteAPIError) as e:
             FILES_API.download(directory=dir, id=[1], external_id="fail")
         assert [FileMetadata(id=1, name="file1", external_id="success")] == e.value.successful
         assert [FileMetadata(id=2, name="file2", external_id="fail")] == e.value.failed
         assert os.path.isfile(os.path.join(dir, "file1"))
 def test_upload_from_directory(self, mock_file_upload_response):
     path = os.path.join(os.path.dirname(__file__), "files_for_test_upload")
     res = FILES_API.upload(path=path)
     response_body = mock_file_upload_response.calls[0].response.json()
     del response_body["uploadUrl"]
     assert FileMetadataList([FileMetadata._load(response_body), FileMetadata._load(response_body)]) == res
     assert 4 == len(mock_file_upload_response.calls)
     for call in mock_file_upload_response.calls:
         payload = call.request.body
         if isinstance(payload, BufferedReader):
             continue
         elif jsgz_load(payload)["name"] in ["file_for_test_upload_1.txt", "file_for_test_upload_2.txt"]:
             continue
         else:
             raise AssertionError("incorrect payload: {}".format(payload))
Example #3
0
 def test_update_multiple(self, mock_files_response):
     res = FILES_API.update([
         FileMetadataUpdate(id=1).source.set(None),
         FileMetadata(external_id="2", source="bla")
     ])
     assert isinstance(res, FileMetadataList)
     assert {
         "items": [
             {
                 "id": 1,
                 "update": {
                     "source": {
                         "setNull": True
                     }
                 }
             },
             {
                 "externalId": "2",
                 "update": {
                     "source": {
                         "set": "bla"
                     }
                 }
             },
         ]
     } == jsgz_load(mock_files_response.calls[0].request.body)
 def test_upload_from_memory(self, mock_file_upload_response):
     res = FILES_API.upload_bytes(content=b"content", name="bla")
     response_body = mock_file_upload_response.calls[0].response.json()
     del response_body["uploadUrl"]
     assert FileMetadata._load(response_body) == res
     assert "https://upload.here/" == mock_file_upload_response.calls[1].request.url
     assert {"name": "bla"} == jsgz_load(mock_file_upload_response.calls[0].request.body)
     assert b"content" == mock_file_upload_response.calls[1].request.body
 def test_upload(self, mock_file_upload_response):
     path = os.path.join(os.path.dirname(__file__), "files_for_test_upload", "file_for_test_upload_1.txt")
     res = FILES_API.upload(path, name="bla")
     response_body = mock_file_upload_response.calls[0].response.json()
     del response_body["uploadUrl"]
     assert FileMetadata._load(response_body) == res
     assert "https://upload.here/" == mock_file_upload_response.calls[1].request.url
     assert {"name": "bla"} == jsgz_load(mock_file_upload_response.calls[0].request.body)
     assert isinstance(mock_file_upload_response.calls[1].request.body, BufferedReader)
 def test_upload_using_file_handle(self, mock_file_upload_response):
     path = os.path.join(os.path.dirname(__file__), "files_for_test_upload", "file_for_test_upload_1.txt")
     with open(path) as fh:
         res = FILES_API.upload_bytes(fh, name="bla")
     response_body = mock_file_upload_response.calls[0].response.json()
     del response_body["uploadUrl"]
     assert FileMetadata._load(response_body) == res
     assert "https://upload.here/" == mock_file_upload_response.calls[1].request.url
     assert {"name": "bla"} == jsgz_load(mock_file_upload_response.calls[0].request.body)
     assert isinstance(mock_file_upload_response.calls[1].request.body, TextIOWrapper)
Example #7
0
 def test_upload_from_directory(self, mock_file_upload_response):
     path = os.path.join(os.path.dirname(__file__), "files_for_test_upload")
     res = FILES_API.upload(path=path, asset_ids=[1, 2])
     response_body = mock_file_upload_response.calls[0].response.json()
     del response_body["uploadUrl"]
     assert FileMetadataList([
         FileMetadata._load(response_body),
         FileMetadata._load(response_body)
     ]) == res
     assert 4 == len(mock_file_upload_response.calls)
     for call in mock_file_upload_response.calls:
         payload = call.request.body
         if isinstance(payload, BufferedReader):
             continue
         else:
             json = jsgz_load(payload)
             assert [1, 2] == json["assetIds"]
             assert json["name"] in [
                 "file_for_test_upload_1.txt", "file_for_test_upload_2.txt"
             ]
Example #8
0
 def test_update_with_resource_class(self, mock_files_response):
     res = FILES_API.update(FileMetadata(id=1, source="bla"))
     assert isinstance(res, FileMetadata)
     assert {
         "items": [{
             "id": 1,
             "update": {
                 "source": {
                     "set": "bla"
                 }
             }
         }]
     } == jsgz_load(mock_files_response.calls[0].request.body)
 def test_create(self, mock_file_create_response):
     file_metadata = FileMetadata(name="bla")
     returned_file_metadata, upload_url = FILES_API.create(file_metadata)
     response_body = mock_file_create_response.calls[0].response.json()
     assert FileMetadata._load(response_body) == returned_file_metadata
     assert response_body["uploadUrl"] == upload_url