Skip to content

Commit a469b13

Browse files
committed
Standalone tests progress for client.py
1 parent 88e5620 commit a469b13

1 file changed

Lines changed: 150 additions & 104 deletions

File tree

meorg_client/tests/test_client.py

Lines changed: 150 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from meorg_client.client import Client
66
import meorg_client.utilities as mu
77
from conftest import store
8+
9+
# from requests import RequestException
810
import tempfile as tf
911
import time
1012

@@ -37,20 +39,6 @@ def _get_authenticated_client() -> Client:
3739
return client
3840

3941

40-
@pytest.fixture
41-
def model_output_id() -> str:
42-
"""Get the model output ID.
43-
44-
Returns
45-
-------
46-
str
47-
Model output ID.
48-
"""
49-
# response = client.model_output_create(model_profile_id, model_output_name)
50-
# if client failure then use from env
51-
return os.environ.get("MEORG_MODEL_OUTPUT_ID")
52-
53-
5442
@pytest.fixture
5543
def client() -> Client:
5644
"""Get an authenticated client.
@@ -95,89 +83,131 @@ def test_list_endpoints(client: Client):
9583
assert isinstance(response, dict)
9684

9785

98-
def test_create_model_output(
99-
client: Client, model_profile_id: str, model_output_name: str
100-
):
101-
"""Test Creation of Model output."""
102-
response = client.model_output_create(model_profile_id, model_output_name)
103-
assert client.success()
86+
@pytest.fixture
87+
def model_output_generator(client: Client, model_profile_id):
88+
model_output_ids = []
10489

105-
model_output_id = response.get("data").get("modeloutput")
106-
assert model_output_id is not None
90+
def _make_model_output(model_output_name):
91+
response = client.model_output_create(model_profile_id, model_output_name)
92+
model_output_id = response.get("data").get("modeloutput")
93+
model_output_ids.append(model_output_id)
94+
return model_output_id
10795

108-
store.set("model_output_id", model_output_id)
109-
test_model_output_query(client, model_output_id)
96+
yield _make_model_output
11097

98+
# TODO: Make tests more tight
99+
# try:
100+
# for model_output_id in model_output_ids:
101+
# client.model_output_delete(model_output_id)
102+
# except RequestException:
103+
# pass
111104

112-
def test_model_output_query(client: Client):
113-
"""Test Existing Model output."""
114-
response = client.model_output_query(store.get("model_output_id"))
115-
assert client.success()
116105

117-
response_model_output_data = response.get("data").get("modeloutput")
118-
assert response_model_output_data.get("id") == model_output_id
106+
@pytest.fixture
107+
def model_output_id(client: Client, model_output_generator: str):
108+
return model_output_generator("model_output_test")
119109

120110

121-
def test_model_output_update(
122-
client: Client,
123-
model_profile_id: str,
124-
):
125-
"""Test updation of model output."""
126-
127-
update_data = {
128-
"name": "updated_mo_name",
129-
"model": model_profile_id,
130-
"state_selection": "default model initialisation",
131-
"parameter_selection": "automated calibration",
132-
"comments": "updated model output pytest",
133-
"is_bundle": False,
134-
}
135-
_ = client.model_output_update(store.get("model_output_id"), update_data)
136-
assert client.success()
111+
class ModelOutput:
137112

113+
def test_create_model_output(
114+
self, client: Client, model_profile_id: str, model_output_name: str
115+
):
116+
"""Test Creation of Model output."""
117+
response = client.model_output_create(model_profile_id, model_output_name)
118+
assert client.success()
138119

139-
class TestBenchmark:
140-
pass
141-
# Create temporary new model output and have it benchmarked against that
120+
model_output_id = response.get("data").get("modeloutput")
121+
assert model_output_id is not None
142122

123+
store.set("model_output_id", model_output_id)
143124

144-
def test_model_output_benchmarks_list(client: Client, experiment_id: str):
145-
test_model_output_experiments_extend(client, experiment_id)
146-
response = client.model_output_benchmarks_list(
147-
store.get("model_output_id"), experiment_id
148-
)
125+
self.test_model_output_query(client, model_output_id)
149126

150-
benchmarks_list = response.get("data").get("benchmarks")
127+
def test_model_output_query(self, client: Client, model_output_id: str):
128+
"""Test Existing Model output."""
129+
# response = client.model_output_query(store.get("model_output_id"))
130+
response = client.model_output_query(model_output_id)
131+
assert client.success()
151132

152-
assert type(benchmarks_list) is list
153-
assert len(benchmarks_list) > 0
133+
response_model_output_data = response.get("data").get("modeloutput")
134+
assert response_model_output_data.get("id") == model_output_id
154135

155-
store.set("sample_benchmark_id", benchmarks_list[0]["id"])
136+
def test_model_output_update(
137+
self,
138+
client: Client,
139+
model_profile_id: str,
140+
):
141+
"""Test updation of model output."""
156142

157-
assert client.success()
143+
update_data = {
144+
"name": "updated_mo_name",
145+
"model": model_profile_id,
146+
"state_selection": "default model initialisation",
147+
"parameter_selection": "automated calibration",
148+
"comments": "updated model output pytest",
149+
"is_bundle": False,
150+
}
151+
_ = client.model_output_update(store.get("model_output_id"), update_data)
152+
assert client.success()
158153

154+
def test_model_output_delete(client: Client, model_output_id: str):
155+
_ = client.model_output_delete(model_output_id)
156+
assert client.success()
159157

160-
def test_model_output_benchmarks_replace(client: Client, experiment_id: str):
161-
client.model_output_benchmarks_replace(
162-
store.get("model_output_id"), experiment_id, [store.get("sample_benchmark_id")]
163-
)
164-
assert client.success()
165158

159+
class TestBenchmark:
166160

167-
def test_model_output_experiments_extend(client: Client, experiment_id: str):
168-
client.model_output_experiments_extend(
169-
store.get("model_output_id"), [experiment_id]
170-
)
171-
assert client.success()
161+
# This model_output_id will always have multiple benchmarks
162+
@pytest.fixture
163+
def model_output_id(
164+
self, client: Client, model_output_generator, experiment_id: str
165+
):
166+
id1 = model_output_generator("model_output_benchmark1")
167+
id2 = model_output_generator("model_output_benchmark2")
168+
client.model_output_experiments_extend(id1, [experiment_id])
169+
client.model_output_experiments_extend(id2, [experiment_id])
170+
return id1
171+
172+
def test_model_output_benchmarks_list(
173+
self, client: Client, model_output_id: str, experiment_id: str
174+
):
175+
176+
response = client.model_output_benchmarks_list(model_output_id, experiment_id)
172177

178+
benchmarks_list = response.get("data").get("benchmarks")
173179

174-
def test_model_output_experiment_delete(client: Client, experiment_id: str):
175-
client.model_output_experiment_delete(store.get("model_output_id"), experiment_id)
180+
assert type(benchmarks_list) is list
181+
assert len(benchmarks_list) > 0
182+
183+
store.set("sample_benchmark_id", benchmarks_list[0]["id"])
184+
185+
assert client.success()
186+
187+
def test_model_output_benchmarks_replace(
188+
self, client: Client, model_output_id: str, experiment_id: str
189+
):
190+
client.model_output_benchmarks_replace(
191+
model_output_id,
192+
experiment_id,
193+
[store.get("sample_benchmark_id")],
194+
)
195+
assert client.success()
196+
197+
198+
def test_model_output_experiments_extend(
199+
client: Client, model_output_id: str, experiment_id: str
200+
):
201+
client.model_output_experiments_extend(model_output_id, [experiment_id])
176202
assert client.success()
177203

178204

179-
def test_model_output_delete(client: Client):
180-
_ = client.model_output_delete(store.get("model_output_id"))
205+
def test_model_output_experiment_delete(
206+
client: Client, model_output_id: str, experiment_id: str
207+
):
208+
# For now, since fresh model output id
209+
client.model_output_experiments_extend(model_output_id, [experiment_id])
210+
client.model_output_experiment_delete(model_output_id, experiment_id)
181211
assert client.success()
182212

183213

@@ -243,37 +273,53 @@ def test_file_list(client: Client, model_output_id: str):
243273
store.set("file_list", response)
244274

245275

246-
def test_start_analysis(client: Client, model_output_id: str, experiment_id: str):
247-
"""Test starting an analysis.
248-
249-
Parameters
250-
----------
251-
client : Client
252-
Client.
253-
model_output_id : str
254-
Model output ID.
255-
"""
256-
# Wait 5s for data to move from cache to store (otherwise analysis will fail, still might)
257-
time.sleep(5)
258-
response = client.start_analysis(model_output_id, experiment_id)
259-
assert client.success()
260-
261-
# Store result for status check below
262-
store.set("start_analysis", response)
263-
264-
265-
def test_get_analysis_status(client: Client):
266-
"""Test getting the analysis status.
267-
268-
Parameters
269-
----------
270-
client : Client
271-
Client.
272-
"""
273-
# Get the analysis id from the store
274-
analysis_id = store.get("start_analysis").get("data").get("analysisId")
275-
_ = client.get_analysis_status(analysis_id)
276-
assert client.success()
276+
class TestAnalysis:
277+
278+
@pytest.fixture
279+
def model_output_id_analysis(
280+
self,
281+
client: Client,
282+
test_filepath: str,
283+
experiment_id: str,
284+
model_output_generator,
285+
):
286+
model_output_id = model_output_generator("model_output_analysis_test")
287+
client.model_output_experiments_extend(model_output_id, [experiment_id])
288+
client.upload_files([test_filepath, test_filepath], model_output_id)
289+
return model_output_id
290+
291+
def test_start_analysis(
292+
self, client: Client, model_output_id_analysis: str, experiment_id: str
293+
):
294+
"""Test starting an analysis.
295+
296+
Parameters
297+
----------
298+
client : Client
299+
Client.
300+
model_output_id : str
301+
Model output ID.
302+
"""
303+
# Wait 5s for data to move from cache to store (otherwise analysis will fail, still might)
304+
time.sleep(5)
305+
response = client.start_analysis(model_output_id_analysis, experiment_id)
306+
assert client.success()
307+
308+
# Store result for status check below
309+
store.set("start_analysis", response)
310+
311+
def test_get_analysis_status(self, client: Client):
312+
"""Test getting the analysis status.
313+
314+
Parameters
315+
----------
316+
client : Client
317+
Client.
318+
"""
319+
# Get the analysis id from the store
320+
analysis_id = store.get("start_analysis").get("data").get("analysisId")
321+
_ = client.get_analysis_status(analysis_id)
322+
assert client.success()
277323

278324

279325
@pytest.mark.xfail(strict=False)

0 commit comments

Comments
 (0)