44import time
55from collections .abc import Iterator
66from pathlib import Path
7- from typing import Any
87
98import pytest
109
1110from cognite .client ._cognite_client import CogniteClient
1211from cognite .client .data_classes .data_sets import DataSetWrite
1312from cognite .client .data_classes .files import FileMetadata
1413from cognite .client .data_classes .simulators import SimulatorModelWrite
15- from cognite .client .data_classes .simulators .routine_revisions import SimulatorRoutineRevisionWrite
16- from cognite .client .data_classes .simulators .routines import SimulatorRoutineWrite
14+ from cognite .client .data_classes .simulators .models import SimulatorModel
15+ from cognite .client .data_classes .simulators .routine_revisions import (
16+ SimulatorRoutineRevision ,
17+ SimulatorRoutineRevisionWrite ,
18+ )
19+ from cognite .client .data_classes .simulators .routines import (
20+ SimulatorRoutine ,
21+ SimulatorRoutineList ,
22+ SimulatorRoutineWrite ,
23+ )
24+ from cognite .client .utils ._text import to_snake_case
1725from tests .tests_integration .test_api .test_simulators .seed .data import (
1826 RESOURCES ,
1927 SIMULATOR ,
2028 SIMULATOR_INTEGRATION ,
2129 SIMULATOR_MODEL ,
2230 SIMULATOR_ROUTINE ,
23- SIMULATOR_ROUTINE_REVISION_OBJ ,
31+ SIMULATOR_ROUTINE_REVISION ,
2432 ResourceNames ,
2533)
2634from tests .tests_integration .test_api .test_simulators .utils import update_logs
@@ -44,12 +52,14 @@ def seed_resource_names(cognite_client: CogniteClient) -> ResourceNames:
4452def upload_file (cognite_client : CogniteClient , filename : str , external_id : str , data_set_id : int ) -> FileMetadata :
4553 file = cognite_client .files .retrieve (external_id = external_id )
4654 if not file :
47- return cognite_client .files .upload (
48- path = SEED_DIR / filename ,
55+ uploaded_file = cognite_client .files .upload (
56+ path = str ( SEED_DIR / filename ) ,
4957 external_id = external_id ,
5058 name = filename ,
5159 data_set_id = data_set_id ,
5260 )
61+ assert isinstance (uploaded_file , FileMetadata )
62+ return uploaded_file
5363
5464 return file
5565
@@ -70,46 +80,47 @@ def seed_model_revision_file(
7080
7181
7282@pytest .fixture (scope = "session" )
73- def seed_simulator (cognite_client : CogniteClient , seed_resource_names : ResourceNames ) -> Iterator [ None ] :
83+ def seed_simulator (cognite_client : CogniteClient , seed_resource_names : ResourceNames ) -> None :
7484 simulator_external_id = seed_resource_names .simulator_external_id
7585 simulators = cognite_client .simulators .list (limit = None )
7686 seeded_simulator = simulators .get (external_id = simulator_external_id )
7787 fields_to_compare = ["fileExtensionTypes" , "modelTypes" , "modelDependencies" , "stepFields" , "unitQuantities" ]
78- seeded_simulator_dump = seeded_simulator .dump () if seeded_simulator else None
7988
8089 if not seeded_simulator :
8190 cognite_client .simulators ._post ("/simulators" , json = {"items" : [SIMULATOR ]})
8291 # if any field in simulator is different from the current seeded simulator, update it
83- elif any (seeded_simulator_dump . get ( field ) != SIMULATOR [ field ] for field in fields_to_compare if field in SIMULATOR ):
92+ elif any (getattr ( seeded_simulator , to_snake_case ( field )) != SIMULATOR . get ( field ) for field in fields_to_compare ):
8493 simulator_update = {
8594 "id" : seeded_simulator .id ,
86- "update" : {field : {"set" : SIMULATOR [ field ] } for field in fields_to_compare },
95+ "update" : {field : {"set" : SIMULATOR . get ( field ) } for field in fields_to_compare },
8796 }
8897 cognite_client .simulators ._post ("/simulators/update" , json = {"items" : [simulator_update ]})
8998
9099
91100@pytest .fixture (scope = "session" )
92101def seed_simulator_integration (
93102 cognite_client : CogniteClient , seed_simulator : None , seed_resource_names : ResourceNames
94- ) -> Iterator [ None ] :
103+ ) -> None :
95104 log_id = None
96105 timestamp = int (time .time () * 1000 )
97106 simulator_integrations = cognite_client .simulators .integrations .list (limit = None )
98- if not simulator_integrations .get (external_id = SIMULATOR_INTEGRATION ["externalId" ]):
99- SIMULATOR_INTEGRATION ["heartbeat" ] = timestamp
100- SIMULATOR_INTEGRATION ["dataSetId" ] = seed_resource_names .simulator_test_data_set_id
107+ existing_integration = simulator_integrations .get (external_id = seed_resource_names .simulator_integration_external_id )
108+ if not existing_integration :
109+ new_integration = {
110+ ** SIMULATOR_INTEGRATION ,
111+ "heartbeat" : timestamp ,
112+ "dataSetId" : seed_resource_names .simulator_test_data_set_id ,
113+ }
101114 res = cognite_client .simulators ._post (
102115 "/simulators/integrations" ,
103- json = {"items" : [SIMULATOR_INTEGRATION ]},
116+ json = {"items" : [new_integration ]},
104117 )
105118 log_id = res .json ()["items" ][0 ]["logId" ]
106119 else :
107- integration = simulator_integrations .get (external_id = SIMULATOR_INTEGRATION ["externalId" ])
108- if integration is not None :
109- log_id = integration .log_id
120+ log_id = existing_integration .log_id
110121 cognite_client .simulators .integrations ._post (
111122 "/simulators/integrations/update" ,
112- json = {"items" : [{"id" : integration .id , "update" : {"heartbeat" : {"set" : timestamp }}}]},
123+ json = {"items" : [{"id" : existing_integration .id , "update" : {"heartbeat" : {"set" : timestamp }}}]},
113124 )
114125
115126 if log_id :
@@ -123,35 +134,33 @@ def seed_simulator_integration(
123134@pytest .fixture (scope = "session" )
124135def seed_simulator_models (
125136 cognite_client : CogniteClient , seed_simulator_integration : None , seed_resource_names : ResourceNames
126- ) -> Iterator [dict [ str , Any ] ]:
137+ ) -> Iterator [SimulatorModel ]:
127138 model_unique_external_id = seed_resource_names .simulator_model_external_id
128139 models = cognite_client .simulators .models .list (limit = None )
129- model_exists = models .get (external_id = model_unique_external_id )
130-
131- if not model_exists :
132- SIMULATOR_MODEL ["dataSetId" ] = seed_resource_names .simulator_test_data_set_id
133- SIMULATOR_MODEL ["externalId" ] = model_unique_external_id
140+ model = models .get (external_id = model_unique_external_id )
134141
135- model = SimulatorModelWrite ._load (
142+ if not model :
143+ new_model = SimulatorModelWrite ._load (
136144 {
137- "externalId" : SIMULATOR_MODEL ["externalId" ],
138- "simulatorExternalId" : SIMULATOR_MODEL ["simulatorExternalId" ],
139- "dataSetId" : SIMULATOR_MODEL ["dataSetId" ],
140- "name" : SIMULATOR_MODEL ["name" ],
141- "type" : SIMULATOR_MODEL ["type" ],
142- "description" : SIMULATOR_MODEL ["description" ],
145+ ** SIMULATOR_MODEL .dump (),
146+ "dataSetId" : seed_resource_names .simulator_test_data_set_id ,
147+ "externalId" : model_unique_external_id ,
143148 }
144149 )
145- cognite_client .simulators .models .create (model )
146-
147- yield SIMULATOR_MODEL
150+ res = cognite_client .simulators .models .create (new_model )
151+ yield res
152+ else :
153+ yield model
148154
149155 cognite_client .simulators .models .delete (external_ids = model_unique_external_id )
150156
151157
152158@pytest .fixture (scope = "session" )
153159def seed_simulator_model_revisions (
154- cognite_client : CogniteClient , seed_simulator_models , seed_model_revision_file , seed_resource_names : ResourceNames
160+ cognite_client : CogniteClient ,
161+ seed_simulator_models : SimulatorModel ,
162+ seed_model_revision_file : FileMetadata ,
163+ seed_resource_names : ResourceNames ,
155164) -> None :
156165 model_unique_external_id = seed_resource_names .simulator_model_external_id
157166 model_revision_unique_external_id = seed_resource_names .simulator_model_revision_external_id
@@ -179,22 +188,24 @@ def seed_simulator_model_revisions(
179188
180189
181190@pytest .fixture (scope = "session" )
182- def seed_simulator_routines (cognite_client : CogniteClient , seed_simulator_model_revisions ):
191+ def seed_simulator_routines (
192+ cognite_client : CogniteClient , seed_simulator_model_revisions : None
193+ ) -> Iterator [SimulatorRoutineList ]:
183194 model_unique_external_id = RESOURCES .simulator_model_external_id
184195 simulator_routine_unique_external_id = RESOURCES .simulator_routine_external_id
185196
186197 routines = cognite_client .simulators .routines .create (
187198 [
188199 SimulatorRoutineWrite .load (
189200 {
190- ** SIMULATOR_ROUTINE ,
201+ ** SIMULATOR_ROUTINE . dump () ,
191202 "modelExternalId" : model_unique_external_id ,
192203 "externalId" : simulator_routine_unique_external_id ,
193204 }
194205 ),
195206 SimulatorRoutineWrite .load (
196207 {
197- ** SIMULATOR_ROUTINE ,
208+ ** SIMULATOR_ROUTINE . dump () ,
198209 "modelExternalId" : model_unique_external_id ,
199210 "externalId" : simulator_routine_unique_external_id + "_1" ,
200211 }
@@ -211,21 +222,24 @@ def seed_simulator_routines(cognite_client: CogniteClient, seed_simulator_model_
211222
212223def seed_simulator_routine_revision (
213224 cognite_client : CogniteClient , routine_external_id : str , version : str
214- ) -> dict [ str , Any ] :
225+ ) -> SimulatorRoutineRevision :
215226 routine_revs = cognite_client .simulators .routines .revisions .list (routine_external_ids = [routine_external_id ])
216227 rev_external_id = f"{ routine_external_id } _{ version } "
217- routine_rev_exists = routine_revs .get (external_id = rev_external_id )
228+ revision = routine_revs .get (external_id = rev_external_id )
218229
219- revision = {** SIMULATOR_ROUTINE_REVISION_OBJ , "externalId" : rev_external_id }
220-
221- if not routine_rev_exists :
222- cognite_client .simulators .routines .revisions .create (SimulatorRoutineRevisionWrite .load (revision ))
230+ if not revision :
231+ revision_write = SimulatorRoutineRevisionWrite .load (
232+ {** SIMULATOR_ROUTINE_REVISION .dump (), "externalId" : rev_external_id }
233+ )
234+ return cognite_client .simulators .routines .revisions .create (revision_write )
223235
224236 return revision
225237
226238
227239@pytest .fixture (scope = "session" )
228- def seed_simulator_routine_revisions (cognite_client : CogniteClient , seed_simulator_routines ):
240+ def seed_simulator_routine_revisions (
241+ cognite_client : CogniteClient , seed_simulator_routines : list [SimulatorRoutine ]
242+ ) -> Iterator [tuple [SimulatorRoutineRevision , SimulatorRoutineRevision ]]:
229243 simulator_routine_external_id = RESOURCES .simulator_routine_external_id
230244
231245 rev1 = seed_simulator_routine_revision (cognite_client , simulator_routine_external_id , "v1" )
0 commit comments