Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lpereiracgn committed Dec 9, 2024
1 parent 2400a67 commit 78d2d79
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 66 deletions.
62 changes: 47 additions & 15 deletions tests/tests_integration/test_api/test_simulators/seed/data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import time

resource_names = {
"simulator_external_id": "integration_tests_workflow",
"simulator_integration_external_id": "integration_tests_workflow_connector",
"simulator_model_external_id": "integration_tests_workflow_model_01",
"simulator_model_revision_external_id": "integration_tests_workflow_model_revision_1",
"simulator_model_file_external_id": "ShowerMixer_simulator_model_file_3",
"simulator_routine_external_id": "integration_tests_workflow_routine",
"simulator_routine_revision_external_id": "integration_tests_workflow_routine_revision",
"simulator_test_data_set_id": 97552494921583,
}

simulator = {
"name": "DWSIM",
"externalId": "integration_tests_workflow",
"name": resource_names["simulator_external_id"],
"externalId": resource_names["simulator_external_id"],
"fileExtensionTypes": ["dwxmz"],
"modelTypes": [{"name": "Steady State", "key": "SteadyState"}],
"stepFields": [
Expand Down Expand Up @@ -164,14 +177,34 @@
{"label": "F", "name": "F"},
],
},
{
"name": "volumetricFlow",
"label": "Volumetric Flow",
"units": [
{"label": "m3/h", "name": "m3/h"},
{"label": "cm3/s", "name": "cm3/s"},
{"label": "L/h", "name": "L/h"},
{"label": "L/min", "name": "L/min"},
{"label": "L/s", "name": "L/s"},
{"label": "ft3/h", "name": "ft3/h"},
{"label": "ft3/min", "name": "ft3/min"},
{"label": "ft3/s", "name": "ft3/s"},
{"label": "gal[US]/h", "name": "gal[US]/h"},
{"label": "gal[US]/min", "name": "gal[US]/min"},
{"label": "gal[US]/s", "name": "gal[US]/s"},
{"label": "gal[UK]/h", "name": "gal[UK]/h"},
{"label": "gal[UK]/min", "name": "gal[UK]/min"},
{"label": "gal[UK]/s", "name": "gal[UK]/s"},
],
},
],
}


simulator_integration = {
"externalId": "integration_tests_workflow_connector",
"simulatorExternalId": "integration_tests_workflow",
"heartbeat": 1706396950969,
"externalId": resource_names["simulator_integration_external_id"],
"simulatorExternalId": resource_names["simulator_external_id"],
"heartbeat": int(time.time() * 1000),
"dataSetId": 97552494921583,
"connectorVersion": "1.0.0",
"simulatorVersion": "1.0.0",
Expand All @@ -183,33 +216,32 @@


simulator_model = {
"externalId": "integration_tests_workflow_model",
"simulatorExternalId": "integration_tests_workflow",
"externalId": resource_names["simulator_model_external_id"],
"simulatorExternalId": resource_names["simulator_external_id"],
"name": "Test Simulator Model",
"description": "Test Simulator Model Desc",
"dataSetId": 97552494921583,
"labels": [{"externalId": "simconfig-labels-PROSPER"}],
"type": "SteadyState",
}

simulator_model_revision = {
"externalId": "integration_tests_workflow_model_revision",
"modelExternalId": "integration_tests_workflow_model",
"externalId": resource_names["simulator_model_revision_external_id"],
"modelExternalId": resource_names["simulator_model_external_id"],
"description": "test sim model revision description",
"fileId": 00000000000000,
}

simulator_routine = {
"externalId": "integration_tests_workflow_routine",
"modelExternalId": "integration_tests_workflow_model",
"simulatorIntegrationExternalId": "integration_tests_workflow_connector",
"externalId": resource_names["simulator_routine_external_id"],
"modelExternalId": resource_names["simulator_model_external_id"],
"simulatorIntegrationExternalId": resource_names["simulator_integration_external_id"],
"name": "Routine test",
"description": "test",
}

simulator_routine_revision = {
"externalId": "integration_tests_workflow_routine_revision",
"routineExternalId": "integration_tests_workflow_routine",
"externalId": resource_names["simulator_routine_revision_external_id"],
"routineExternalId": resource_names["simulator_routine_external_id"],
"configuration": {
"schedule": {"enabled": True, "cronExpression": "*/10 * * * *"},
"dataSampling": {"enabled": True, "validationWindow": None, "samplingWindow": 15, "granularity": 1},
Expand Down
160 changes: 109 additions & 51 deletions tests/tests_integration/test_api/test_simulators/test_simulators.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import pytest

from cognite.client import CogniteClient
from cognite.client.data_classes.simulators.filters import SimulatorIntegrationFilter
from cognite.client.data_classes.files import FileMetadata
from cognite.client.data_classes.simulators.filters import (
SimulatorIntegrationFilter,
SimulatorModelRevisionsFilter,
SimulatorModelsFilter,
)
from tests.tests_integration.test_api.test_simulators.seed.data import (
resource_names,
simulator,
simulator_integration,
simulator_model,
Expand All @@ -12,52 +18,97 @@
)


@pytest.fixture(scope="class")
def seed_resource_names() -> dict[str, str]:
return resource_names


@pytest.fixture
def add_simulator_resoures(cognite_client: CogniteClient) -> None:
simulator_external_id = "integration_tests_workflow"
simulator_model_file_external_id = "ShowerMixer_simulator_model_file"

file = cognite_client.files.upload(
path="tests/tests_integration/test_api/test_simulators/seed/data/ShowerMixer.dwxmz",
external_id=simulator_model_file_external_id,
name="ShowerMixer.dwxmz",
data_set_id=97552494921583,
def seed_file(cognite_client: CogniteClient, seed_resource_names) -> FileMetadata | None:
# check if file already exists
file = cognite_client.files.retrieve(external_id=seed_resource_names["simulator_model_file_external_id"])
if (file is None) or (file is False):
file = cognite_client.files.upload(
path="tests/tests_integration/test_api/test_simulators/seed/ShowerMixer.dwxmz",
external_id=seed_resource_names["simulator_model_file_external_id"],
name="ShowerMixer.dwxmz",
data_set_id=97552494921583,
)
yield file


@pytest.fixture
def seed_simulator(cognite_client: CogniteClient, seed_resource_names) -> None:
simulator_external_id = seed_resource_names["simulator_external_id"]
simulators = cognite_client.simulators.list()
for sim in simulators:
if sim.external_id == simulator_external_id:
return

cognite_client.post(
f"/api/v1/projects/{cognite_client.config.project}/simulators",
json={"items": [simulator]},
)

resources = [
{"url": f"/api/v1/projects/{cognite_client.config.project}/simulators", "seed": simulator},
{
"url": f"/api/v1/projects/{cognite_client.config.project}/simulators/integrations",
"seed": simulator_integration,
},
{"url": f"/api/v1/projects/{cognite_client.config.project}/simulators/models", "seed": simulator_model},
{
"url": f"/api/v1/projects/{cognite_client.config.project}/simulators/models/revisions",
"seed": {**simulator_model_revision, "fileId": file.id},
},
{"url": f"/api/v1/projects/{cognite_client.config.project}/simulators/routines", "seed": simulator_routine},
{
"url": f"/api/v1/projects/{cognite_client.config.project}/simulators/routines/revisions",
"seed": simulator_routine_revision,
},
]

for resource in resources:

@pytest.fixture
def seed_simulator_integration(cognite_client: CogniteClient, seed_simulator) -> None:
def create_integration():
cognite_client.post(
resource["url"],
json={"items": [resource["seed"]]},
headers={"cdf-version": "alpha"},
f"/api/v1/projects/{cognite_client.config.project}/simulators/integrations",
json={"items": [simulator_integration]},
)

yield None
try:
create_integration()
except Exception:
cognite_client.post(
f"/api/v1/projects/{cognite_client.config.project}/simulators/integrations/delete",
json={"items": [{"externalId": simulator_integration["externalId"]}]},
)
create_integration()
pass


@pytest.fixture
def seed_simulator_models(cognite_client: CogniteClient, seed_simulator_integration) -> None:
cognite_client.post(
f"/api/v1/projects/{cognite_client.config.project}/simulators/delete",
json={"items": [{"externalId": simulator_external_id}]},
headers={"cdf-version": "alpha"},
f"/api/v1/projects/{cognite_client.config.project}/simulators/models",
json={"items": [simulator_model]}, # Post actual simulator models here
)

cognite_client.files.delete(external_id=simulator_model_file_external_id)

@pytest.fixture
def seed_simulator_model_revisions(cognite_client: CogniteClient, seed_simulator_models, seed_file) -> None:
cognite_client.post(
f"/api/v1/projects/{cognite_client.config.project}/simulators/models/revisions",
json={"items": [{**simulator_model_revision, "fileId": seed_file.id}]}, # Post actual simulator models here
)


@pytest.fixture
def seed_simulator_routines(cognite_client: CogniteClient, seed_simulator_model_revisions) -> None:
cognite_client.post(
f"/api/v1/projects/{cognite_client.config.project}/simulators/routines",
json={"items": [simulator_routine]},
)


@pytest.fixture
def seed_simulator_routine_revisions(cognite_client: CogniteClient, seed_simulator_routines) -> None:
cognite_client.post(
f"/api/v1/projects/{cognite_client.config.project}/simulators/routines/revisions",
json={"items": [simulator_routine_revision]},
)


@pytest.fixture(scope="class")
def delete_simulator(cognite_client: CogniteClient, seed_resource_names) -> None:
yield
cognite_client.post(
f"/api/v1/projects/{cognite_client.config.project}/simulators/delete",
json={"items": [{"externalId": seed_resource_names["simulator_external_id"]}]},
)


class TestSimulators:
Expand All @@ -71,6 +122,7 @@ class TestSimulatorIntegrations:
# test list
# test filter
# test retrieve
@pytest.mark.usefixtures("seed_simulator_integration")
def test_list_integrations(self, cognite_client: CogniteClient) -> None:
integrations = cognite_client.simulators.integrations.list(limit=5)

Expand All @@ -91,24 +143,36 @@ def test_filter_integrations(self, cognite_client: CogniteClient) -> None:
assert len(all_integrations) != len(dwsim_integrations)


@pytest.mark.usefixtures("seed_resource_names", "seed_simulator", "delete_simulator")
class TestSimulatorModels:
def test_list_models(self, cognite_client: CogniteClient) -> None:
models = cognite_client.simulators.models.list(limit=5)
@pytest.mark.usefixtures("seed_simulator_models", "seed_simulator_model_revisions")
def test_list_models(self, cognite_client: CogniteClient, seed_resource_names) -> None:
models = cognite_client.simulators.models.list(
limit=5, filter=SimulatorModelsFilter(simulator_external_ids=[seed_resource_names["simulator_external_id"]])
)
assert len(models) > 0

def test_retrieve_model(self, cognite_client: CogniteClient) -> None:
def test_retrieve_model(self, cognite_client: CogniteClient, seed_resource_names) -> None:
model = cognite_client.simulators.models.retrieve(external_id="TEST_WORKFLOWS_SIMINT_INTEGRATION_MODEL")
assert model is not None
assert model.external_id == "TEST_WORKFLOWS_SIMINT_INTEGRATION_MODEL"

def test_list_model_revisions(self, cognite_client: CogniteClient) -> None:
revisions = cognite_client.simulators.models.revisions.list(limit=5)
def test_list_model_revisions(self, cognite_client: CogniteClient, seed_resource_names) -> None:
revisions = cognite_client.simulators.models.list(
limit=5,
filter=SimulatorModelRevisionsFilter(
model_external_ids=[seed_resource_names["simulator_model_external_id"]]
),
)
assert len(revisions) > 0

def test_retrieve_model_revision(self, cognite_client: CogniteClient) -> None:
model = cognite_client.simulators.models.revisions.retrieve(external_id="Shower_mixer-1")
def test_retrieve_model_revision(self, cognite_client: CogniteClient, seed_resource_names) -> None:
# TODO : this test is incorrect, it should retrieve model revisions instead of model
model = cognite_client.simulators.models.retrieve(
external_id=seed_resource_names["simulator_model_external_id"]
)
assert model is not None
assert model.external_id == "Shower_mixer-1"
assert model.external_id == seed_resource_names["simulator_model_external_id"]


class TestSimulatorRoutines:
Expand All @@ -126,9 +190,3 @@ def test_retrieve_routine_revision(self, cognite_client: CogniteClient) -> None:
revision = cognite_client.simulators.routines.revisions.retrieve(external_id="ShowerMixerForTests-1")
assert revision is not None
assert revision.external_id == "ShowerMixerForTests-1"


class TestSimulationRuns:
def test_list_runs(self, cognite_client: CogniteClient) -> None:
routines = cognite_client.simulators.runs.list(limit=5)
assert len(routines) > 0

0 comments on commit 78d2d79

Please sign in to comment.