Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add method read_study_api #22

Merged
merged 42 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
bcdf961
added read_thermal_clusters/storages/renewables methods and realized …
mehdiwahada Nov 26, 2024
d31bba1
added read_thermal_clusters/storages/renewables methods and realized …
mehdiwahada Nov 26, 2024
e3c5030
added read_thermal_clusters/storages/renewables methods and realized …
mehdiwahada Nov 27, 2024
e06f118
added read_thermal_clusters/storages/renewables methods and realized …
mehdiwahada Nov 27, 2024
d581ddb
added read_thermal_clusters/storages/renewables methods and realized …
mehdiwahada Nov 27, 2024
2ab7da7
added read_thermal_clusters/storages/renewables methods and realized …
mehdiwahada Nov 27, 2024
6906b17
feat(gh): add release gh action (#14)
MartinBelthle Nov 26, 2024
603cfe7
feat(ci): use tox (#12)
MartinBelthle Nov 26, 2024
ab74da8
chore(release): prepare first release (#15)
MartinBelthle Nov 26, 2024
5b8d269
v0.1.1 (#16)
MartinBelthle Nov 26, 2024
877f53a
integrated integration tests for the reading clusters methods
mehdiwahada Nov 27, 2024
838ae57
Merge branch 'main' into features/add_clusters
mehdiwahada Nov 27, 2024
bb9c9e4
integrated integration tests for the reading clusters methods
mehdiwahada Nov 28, 2024
2dd6b7a
integrated integration tests for the reading clusters methods
mehdiwahada Nov 28, 2024
483e4d6
integrated integration tests for the reading clusters methods
mehdiwahada Nov 28, 2024
d2f4fd9
integrated integration tests for the reading clusters methods
mehdiwahada Nov 28, 2024
6ef393c
adding hydro data (unit and integration testing)
mehdiwahada Nov 28, 2024
ed5aa63
adding hydro data (unit and integration testing)
mehdiwahada Nov 28, 2024
4fcd430
Merge branch 'main' into features/add_hydro_data
mehdiwahada Nov 28, 2024
0e16576
adding hydro data (unit and integration testing)
mehdiwahada Nov 28, 2024
8238314
adding hydro data (unit and integration testing)
mehdiwahada Nov 28, 2024
41821a5
fix(api): correcting import problems
mehdiwahada Nov 29, 2024
e402e5b
fix(api): correcting import problems
mehdiwahada Nov 29, 2024
671336f
fix(api): correcting import problems
mehdiwahada Nov 29, 2024
b56994a
fix(api): correcting import problems
mehdiwahada Nov 29, 2024
d0b8052
feat(api): adding read_study method and unit testing
mehdiwahada Dec 2, 2024
af39424
feat(api): adding read_study method and unit testing
mehdiwahada Dec 2, 2024
0fd3de8
Merge branch 'main' into feat/read_study_api
mehdiwahada Dec 2, 2024
e21278d
feat(api): adding read_study method, unit testing and integration tes…
mehdiwahada Dec 2, 2024
2bdb548
Merge branch 'main' into feat/read_study_api
MartinBelthle Dec 2, 2024
a7e28fe
Merge remote-tracking branch 'origin/feat/read_study_api' into feat/r…
mehdiwahada Dec 2, 2024
91d01da
feat(api): adding read_study method, unit testing and integration tes…
mehdiwahada Dec 3, 2024
4c2d927
Merge branch 'main' into feat/read_study_api
MartinBelthle Dec 3, 2024
a729713
feat(api): adding read_study method, unit testing and integration tes…
mehdiwahada Dec 3, 2024
c4767cf
Merge remote-tracking branch 'origin/feat/read_study_api' into feat/r…
mehdiwahada Dec 3, 2024
7d35ba7
feat(api): adding read_study method, unit testing and integration tes…
mehdiwahada Dec 3, 2024
e605d28
Merge branch 'main' into feat/read_study_api
MartinBelthle Dec 3, 2024
4b4edd4
feat(api): adding read_study method, unit testing and integration tes…
mehdiwahada Dec 4, 2024
f6f1a61
Merge remote-tracking branch 'origin/feat/read_study_api' into feat/r…
mehdiwahada Dec 4, 2024
e140264
feat(api): adding read_study method, unit testing and integration tes…
mehdiwahada Dec 4, 2024
95b08fc
feat(api): adding read_study method, unit testing and integration tes…
mehdiwahada Dec 4, 2024
78c93c1
feat(api): adding read_study method, unit testing and integration tes…
mehdiwahada Dec 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/antares/model/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ def _directory_not_exists(local_path: Path) -> None:
)


def read_study_api(api_config: APIconf, study_id: str) -> "Study":
session = api_config.set_up_api_conf()
wrapper = RequestWrapper(session)
base_url = f"{api_config.get_host()}/api/v1"
json_study = wrapper.get(f"{base_url}/studies/{study_id}").json()

study_name = json_study.pop("name")
study_version = str(json_study.pop("version"))

study_settings = _returns_study_settings(base_url, study_id, wrapper, False, None)
study = Study(study_name, study_version, ServiceFactory(api_config, study_id, study_name), study_settings)

study.read_areas()

return study


class Study:
def __init__(
self,
Expand All @@ -194,7 +211,13 @@ def service(self) -> BaseStudyService:
return self._study_service

def read_areas(self) -> list[Area]:
return self._area_service.read_areas()
"""
Synchronize the internal study object with the actual object written in an antares study
Returns: the synchronized area list
"""
area_list = self._area_service.read_areas()
MartinBelthle marked this conversation as resolved.
Show resolved Hide resolved
self._areas = {area.id: area for area in area_list}
return area_list

def get_areas(self) -> MappingProxyType[str, Area]:
return MappingProxyType(dict(sorted(self._areas.items())))
Expand Down
1 change: 0 additions & 1 deletion src/antares/service/api_services/area_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ def read_areas(self) -> List[Area]:
base_api_url = f"{self._base_url}/studies/{self.study_id}/areas"
ui_url = "ui=true"
url_properties_form = "properties/form"

json_resp = self._wrapper.get(base_api_url + "?" + ui_url).json()
for area in json_resp:
area_url = base_api_url + "/" + f"{area}/"
Expand Down
102 changes: 101 additions & 1 deletion tests/antares/services/api_services/test_study_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from antares.model.link import Link, LinkProperties, LinkUi
from antares.model.settings.general import GeneralParameters
from antares.model.settings.study_settings import StudySettings
from antares.model.study import Study, create_study_api
from antares.model.study import Study, create_study_api, read_study_api
from antares.service.service_factory import ServiceFactory


Expand Down Expand Up @@ -199,3 +199,103 @@ def test_create_binding_constraint_fails(self):
match=f"Could not create the binding constraint {constraint_name}: {self.antares_web_description_msg}",
):
self.study.create_binding_constraint(name=constraint_name)

def test_read_study_api(self):
json_study = {
MartinBelthle marked this conversation as resolved.
Show resolved Hide resolved
"id": "22c52f44-4c2a-407b-862b-490887f93dd8",
"name": "test_read_areas",
"version": "880",
}

json_ui = {
"zone": {
"ui": {"x": 0, "y": 0, "color_r": 230, "color_g": 108, "color_b": 44, "layers": "0"},
"layerX": {"0": 0},
"layerY": {"0": 0},
"layerColor": {"0": "230, 108, 44"},
}
}

json_thermal = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To simplify your test you can just replace the json you introduce by:

json_thermal = []
json_renewable = []
json_st_storage = []
json_properties = {}

and your test will still pass.
You can even remove these variables and simply pur the [] or {} inside the .get() to make it even simpler

{
"id": "therm_un",
"group": "Gas",
"name": "therm_un",
"enabled": "true",
"unitCount": 1,
"nominalCapacity": 0,
}
]

json_renewable = [
{
"id": "test_renouvelable",
"group": "Solar Thermal",
"name": "test_renouvelable",
"enabled": "true",
"unitCount": 1,
"nominalCapacity": 0,
"tsInterpretation": "power-generation",
}
]

json_st_storage = [
{
"id": "test_storage",
"group": "Pondage",
"name": "test_storage",
"injectionNominalCapacity": 0,
"withdrawalNominalCapacity": 0,
"reservoirCapacity": 0,
"efficiency": 1,
"initialLevel": 0.5,
"initialLevelOptim": "false",
"enabled": "true",
}
]

json_properties = {
"energyCostUnsupplied": 0,
"energyCostSpilled": 0,
"nonDispatchPower": "true",
"dispatchHydroPower": "true",
"otherDispatchPower": "true",
"filterSynthesis": ["weekly", "daily", "hourly", "monthly", "annual"],
"filterByYear": ["weekly", "daily", "hourly", "monthly", "annual"],
"adequacyPatchMode": "outside",
}

config_urls = re.compile(f"https://antares.com/api/v1/studies/{self.study_id}/config/.*")

base_url = "https://antares.com/api/v1"
url = f"{base_url}/studies/{self.study_id}"
area_url = f"{url}/areas"
area_props_url = f"{area_url}/zone/properties/form"
thermal_url = f"{area_url}/zone/clusters/thermal"
renewable_url = f"{area_url}/zone/clusters/renewable"
storage_url = f"{area_url}/zone/storages"

with requests_mock.Mocker() as mocker:
mocker.get(url, json=json_study)
mocker.get(config_urls, json={})
mocker.get(area_url, json=json_ui)
mocker.get(area_props_url, json=json_properties)
mocker.get(renewable_url, json=json_renewable)
mocker.get(thermal_url, json=json_thermal)
mocker.get(storage_url, json=json_st_storage)
actual_study = read_study_api(self.api, self.study_id)

expected_study_name = json_study.pop("name")
expected_study_id = json_study.pop("id")
expected_study_version = json_study.pop("version")

expected_study = Study(
expected_study_name,
expected_study_version,
ServiceFactory(self.api, expected_study_id, expected_study_name),
None,
)

assert actual_study.name == expected_study.name
assert actual_study.version == expected_study.version
assert actual_study.service.study_id == expected_study.service.study_id
mehdiwahada marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 21 additions & 1 deletion tests/integration/test_web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from antares.model.settings.general import GeneralParameters, Mode
from antares.model.settings.study_settings import PlaylistParameters, StudySettings
from antares.model.st_storage import STStorageGroup, STStorageMatrixName, STStorageProperties
from antares.model.study import create_study_api
from antares.model.study import create_study_api, read_study_api
from antares.model.thermal import ThermalClusterGroup, ThermalClusterProperties

from tests.integration.antares_web_desktop import AntaresWebDesktop
Expand Down Expand Up @@ -219,6 +219,26 @@ def test_creation_lifecycle(self, antares_web: AntaresWebDesktop):
assert actual_hydro.area_id == area_fr.id
assert actual_hydro.properties == area_fr.hydro.properties

# tests study reading method and comparing ids, name, areas and settings
actual_study = read_study_api(api_config, study.service.study_id)

assert study.service.study_id == actual_study.service.study_id
assert study.name == actual_study.name
assert study.version == actual_study.version
assert list(study.get_areas().keys()) == list(actual_study.get_areas().keys())
assert (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better. You can introduce
actual_area_fr = actual_study.get_areas()["fr"] and expected_area_fr = study.get_areas()["fr"] to avoid calling get_areas multiple times.
Also I would prefer you do the same check as the ones for areas (line 228) for thermal, storages and renewables as it would check more things.
Last thing, I believe you can also check the settings

study.get_areas()["fr"].get_thermals().get("cluster_test").id
== actual_study.get_areas()["fr"].get_thermals().get("cluster_test").id
)
assert (
study.get_areas()["fr"].get_renewables().get("cluster_test").id
== actual_study.get_areas()["fr"].get_renewables().get("cluster_test").id
)
assert (
study.get_areas()["fr"].get_st_storages().get("cluster_test").id
== actual_study.get_areas()["fr"].get_st_storages().get("cluster_test").id
)

# test short term storage creation with properties
st_storage_name = "wind_onshore"
storage_properties = STStorageProperties(reservoir_capacity=0.5)
Expand Down