77import pytest
88import responses
99import respx
10- from cognite .client .data_classes import Asset , AssetList , Annotation , AnnotationList
10+ from cognite .client .data_classes import Annotation , AnnotationList , Asset , AssetList
1111from cognite .client .data_classes .data_modeling import (
1212 ContainerId ,
1313 DataModel ,
1414 DataModelList ,
15- EdgeApply ,
1615 DirectRelation ,
1716 MappedProperty ,
1817 NodeApply ,
3332 MODEL_ID ,
3433 RESOURCE_VIEW_MAPPING_VIEW_ID ,
3534)
36- from cognite_toolkit ._cdf_tk .commands ._migrate .default_mappings import ASSET_ID , create_default_mappings , FILE_ANNOTATIONS_ID
35+ from cognite_toolkit ._cdf_tk .commands ._migrate .default_mappings import (
36+ ASSET_ID ,
37+ FILE_ANNOTATIONS_ID ,
38+ create_default_mappings ,
39+ )
3740from cognite_toolkit ._cdf_tk .commands ._migrate .migration_io import AssetCentricMigrationIO
3841from cognite_toolkit ._cdf_tk .commands ._migrate .selectors import MigrationCSVFileSelector
3942from cognite_toolkit ._cdf_tk .exceptions import ToolkitMigrationError , ToolkitValueError
@@ -46,21 +49,23 @@ def cognite_migration_model(
4649) -> Iterator [responses .RequestsMock ]:
4750 config = toolkit_config
4851 mapping_by_id = {mapping .external_id : mapping for mapping in create_default_mappings ()}
49- asset_mapping = mapping_by_id [ASSET_ID ]
50- # Lookup of the mapping in the Migration Model
51- mapping_node_response = asset_mapping .dump (context = "api" )
52- mapping_node_response .update ({"createdTime" : 0 , "lastUpdatedTime" : 0 , "version" : 1 })
53- sources = mapping_node_response .pop ("sources" , [])
54- if sources :
55- mapping_view_id = asset_mapping .sources [0 ].source
56- mapping_node_response ["properties" ] = {
57- mapping_view_id .space : {
58- f"{ mapping_view_id .external_id } /{ mapping_view_id .version } " : sources [0 ]["properties" ]
52+ node_items : list [dict ] = []
53+ for mapping in mapping_by_id .values ():
54+ # Lookup of the mapping in the Migration Model
55+ mapping_node_response = mapping .dump (context = "api" )
56+ mapping_node_response .update ({"createdTime" : 0 , "lastUpdatedTime" : 0 , "version" : 1 })
57+ sources = mapping_node_response .pop ("sources" , [])
58+ if sources :
59+ mapping_view_id = mapping .sources [0 ].source
60+ mapping_node_response ["properties" ] = {
61+ mapping_view_id .space : {
62+ f"{ mapping_view_id .external_id } /{ mapping_view_id .version } " : sources [0 ]["properties" ]
63+ }
5964 }
60- }
65+ node_items . append ( mapping_node_response )
6166 rsps .post (
6267 config .create_api_url ("models/instances/byids" ),
63- json = {"items" : [ mapping_node_response ] },
68+ json = {"items" : node_items },
6469 status = 200 ,
6570 )
6671
@@ -296,7 +301,8 @@ def test_migrate_annotations(
296301 annotated_resource_type = "file" ,
297302 annotated_resource_id = 3000 + i ,
298303 data = {
299-
304+ "assetRef" : {"id" : 4000 + i },
305+ "textRegion" : {"xMin" : 10 , "xMax" : 100 , "yMin" : 20 , "yMax" : 200 },
300306 },
301307 status = "approved" ,
302308 creating_user = "doctrino" ,
@@ -311,10 +317,9 @@ def test_migrate_annotations(
311317 csv_content = "id,space,externalId,ingestionView\n " + "\n " .join (
312318 f"{ 2000 + i } ,{ space } ,annotation_{ i } ,{ FILE_ANNOTATIONS_ID } " for i in range (len (annotations ))
313319 )
314-
315320 # Annotation retrieve ids
316321 rsps .post (
317- config .create_api_url ("/annotations/byids " ),
322+ config .create_api_url ("/annotations/list " ),
318323 json = {"items" : [annotation .dump () for annotation in annotations ]},
319324 status = 200 ,
320325 )
@@ -347,45 +352,22 @@ def test_migrate_annotations(
347352 command = MigrationCommand (silent = True )
348353
349354 result = command .migrate (
350- selected = MigrationCSVFileSelector (datafile = csv_file , kind = "Annotations " ),
355+ selected = MigrationCSVFileSelector (datafile = csv_file , kind = "FileAnnotations " ),
351356 data = AssetCentricMigrationIO (client ),
352357 mapper = AssetCentricMapper (client ),
353358 log_dir = tmp_path / "logs" ,
354359 dry_run = False ,
355360 verbose = False ,
356361 )
362+ actual_results = [result .get_progress (f"fileAnnotation_{ annotation .id } " ) for annotation in annotations ]
363+ expected_results = [{"download" : "success" , "convert" : "success" , "upload" : "success" } for _ in annotations ]
364+ assert actual_results == expected_results
357365
358366 # Check that the annotations were uploaded
359367 last_call = respx_mock .calls [- 1 ]
360368 assert last_call .request .url == config .create_api_url ("/models/instances" )
361369 assert last_call .request .method == "POST"
362370 actual_instances = json .loads (last_call .request .content )["items" ]
363- expected_instance = [
364- EdgeApply (
365- space = space ,
366- external_id = annotation .external_id ,
367- sources = [
368- NodeOrEdgeData (
369- source = ViewId ("cdf_cdm" , "CogniteAsset" , "v1" ),
370- properties = {
371- "name" : annotation .name ,
372- "description" : annotation .description ,
373- },
374- ),
375- NodeOrEdgeData (
376- source = INSTANCE_SOURCE_VIEW_ID ,
377- properties = {
378- "id" : annotation .id ,
379- "resourceType" : "annotation" ,
380- "dataSetId" : None ,
381- "classicExternalId" : annotation .external_id ,
382- },
383- ),
384- ],
385- )
386-
387-
388-
389371
390372
391373class TestMigrateCommandHelpers :
0 commit comments