Skip to content

Commit 77355ba

Browse files
committed
test: add observed var tests
1 parent c4ff53b commit 77355ba

File tree

4 files changed

+221
-112
lines changed

4 files changed

+221
-112
lines changed

tests/router/method/fixture.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ class MethodReferenceResponse:
2020

2121
@dataclass
2222
class MethodResponse:
23-
method_reference_response: Response
2423
method_response: Response
2524
device_response: Response | None = None
25+
method_reference_response: Response | None = None
2626

2727

2828
@dataclass
2929
class AllMethodFixtureResponse:
3030
day_to_anthesis: MethodResponse
3131
projected_shoot_area: MethodResponse
32+
zn_concentration: MethodResponse
3233
method_reference: MethodReferenceResponse
3334

3435

@@ -55,16 +56,20 @@ class AllMethodFixtureResponse:
5556
"comprising two side views and a view from above. The imaging data was prepared using the SET method "
5657
"check reference for the computations.",
5758
)
59+
ZN_CONCENTRATION_METHOD = Method(
60+
name="calculate zn concentration using Inductive Coupled plansma",
61+
description="concentration of zinc(2+) measured by Inductively Coupled Plasma method",
62+
)
5863

5964

6065
async def get_method_fixture(
6166
data: Method,
62-
reference: Response,
6367
test_client: AsyncTestClient,
6468
id: UUID | None = None,
6569
device: Response | None = None,
70+
reference: Response | None = None,
6671
) -> MethodResponse:
67-
method_reference_id = reference.json()["id"]
72+
method_reference_id = reference.json()["id"] if reference else None
6873
send_data = Method(method_reference_id=method_reference_id, **data.to_dict())
6974
if id is None:
7075
response = await post_fixture(PATH, send_data, test_client)
@@ -88,13 +93,30 @@ async def setup_method_reference(test_client: AsyncTestClient) -> AsyncGenerator
8893
async def setup_method(
8994
setup_method_reference: MethodReferenceResponse, setup_device: DeviceResponse, test_client: AsyncTestClient
9095
) -> AsyncGenerator[AllMethodFixtureResponse, None]:
91-
method = setup_method_reference
92-
day_to_anthesis = await get_method_fixture(DAY_TO_ANTHESIS_METHOD, method.day_to_anthesis, test_client)
96+
method_reference = setup_method_reference
97+
day_to_anthesis = await get_method_fixture(
98+
DAY_TO_ANTHESIS_METHOD,
99+
test_client,
100+
None,
101+
None,
102+
method_reference.day_to_anthesis,
103+
)
93104
projected_shoot_area = await get_method_fixture(
94-
PROJECTED_SHOOT_AREA_METHOD, method.projected_shoot_area, test_client, None, setup_device.device_response
105+
PROJECTED_SHOOT_AREA_METHOD,
106+
test_client,
107+
None,
108+
setup_device.device_response,
109+
method_reference.projected_shoot_area,
110+
)
111+
zn_concentration = await get_method_fixture(
112+
ZN_CONCENTRATION_METHOD,
113+
test_client,
95114
)
96115
yield AllMethodFixtureResponse(
97-
day_to_anthesis=day_to_anthesis, projected_shoot_area=projected_shoot_area, method_reference=method
116+
day_to_anthesis=day_to_anthesis,
117+
projected_shoot_area=projected_shoot_area,
118+
method_reference=method_reference,
119+
zn_concentration=zn_concentration,
98120
)
99121
await delete_fixture(PATH, day_to_anthesis.method_response.json()["id"], test_client)
100122
await delete_fixture(PATH, projected_shoot_area.method_response.json()["id"], test_client)

tests/router/method/test_method.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
DAY_TO_ANTHESIS_METHOD,
1111
PATH,
1212
PROJECTED_SHOOT_AREA_METHOD,
13+
ZN_CONCENTRATION_METHOD,
1314
AllMethodFixtureResponse,
1415
MethodResponse,
1516
)
@@ -20,13 +21,15 @@ class MethodFixture:
2021
id: UUID
2122
response: Response
2223
data: Method
23-
method_reference_id: UUID
24+
method_reference_id: UUID | None
2425
device_id: UUID | None = None
2526

2627

2728
def get_method_fixture(response: MethodResponse, data: Method) -> MethodFixture:
2829
method_response = response.method_response
29-
method_reference_id = response.method_reference_response.json()["id"]
30+
method_reference_id = (
31+
response.method_reference_response.json()["id"] if response.method_reference_response else None
32+
)
3033
device_id = response.device_response.json()["id"] if response.device_response else None
3134
fixture = Method(method_reference_id=method_reference_id, **data.to_dict())
3235
return MethodFixture(
@@ -44,3 +47,6 @@ async def test_methods_created(setup_method: AllMethodFixtureResponse, test_clie
4447

4548
fixture = get_method_fixture(setup_method.day_to_anthesis, DAY_TO_ANTHESIS_METHOD)
4649
await validate_post(PATH, fixture.data, test_client, fixture.response)
50+
51+
fixture = get_method_fixture(setup_method.zn_concentration, ZN_CONCENTRATION_METHOD)
52+
await validate_post(PATH, fixture.data, test_client, fixture.response)

tests/router/observed_variable/fixture.py

Lines changed: 138 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,18 @@
99
from src.model.observed_variable import ObservedVariable, ObservedVariableDataclass
1010
from src.model.vocabulary import Vocabulary
1111
from tests.helpers import delete_fixture, post_fixture, put_fixture
12+
from tests.router.method.fixture import AllMethodFixtureResponse
1213
from tests.router.study.fixture import AllStudyFixtureResponse
14+
from tests.router.unit.fixture import AllUnitFixtureResponse
1315

1416

1517
@dataclass
1618
class ObservedVariableResponse:
19+
trait_response: Response
1720
observed_variable_response: Response
1821
study_response: list[Response]
19-
20-
21-
@dataclass
22-
class AllObservedVariableFixtureResponse:
23-
projected_shoot_area: ObservedVariableResponse
24-
zn_concentration: ObservedVariableResponse
25-
26-
27-
@dataclass
28-
class VariableReferenceResponse:
29-
anthesis: Response
22+
method_response: Response
23+
unit_response: Response
3024

3125

3226
@dataclass
@@ -37,76 +31,159 @@ class VariableTraitResponse:
3731
zn_concentration: Response
3832

3933

40-
PATH = "observed_variable"
34+
@dataclass
35+
class AllObservedVariableFixtureResponse:
36+
anthesis: ObservedVariableResponse
37+
reproductive_growth_time: ObservedVariableResponse
38+
projected_shoot_area: ObservedVariableResponse
39+
zn_concentration: ObservedVariableResponse
40+
trait_response: VariableTraitResponse
41+
study_response: AllStudyFixtureResponse
42+
method_response: AllMethodFixtureResponse
43+
unit_response: AllUnitFixtureResponse
44+
45+
46+
PATH = "observedVariable"
4147
ANTHESIS_TRAIT = Vocabulary(title="Anthesis time", accession_number="CO_322:0000030")
4248
REPRODUCTIVE_GROWTH_TIME_TRAIT = Vocabulary(title="Reproductive growth time", accession_number="TO:0000366")
4349
PROJECTED_SHOOT_AREA_TRAIT = Vocabulary(title="Projected shoot area")
4450
ZN_CONCENTRATION_TRAIT = Vocabulary(title="Zn concentration", accession_number="CDNO_0200170")
4551

46-
ANTHESIS_VARIABLE = ObservedVariable(title="Ant_Cmp_Cday", description="Anthesis computed in growing degree days")
52+
ANTHESIS_VARIABLE = ObservedVariable(
53+
title="Ant_Cmp_Cday",
54+
description="Anthesis computed in growing degree days",
55+
time_interval="day",
56+
)
57+
REPRODUCTIVE_GROWTH_TIME_VARIABLE = ObservedVariable(
58+
title="Reproductive Growth Time",
59+
description="Reproductive growth time",
60+
time_interval="day",
61+
)
4762
PROJECTED_SHOOT_AREA_VARIABLE = ObservedVariable(title="PSA_img_kpixels")
4863
ZN_CONCENTRATION_VARIABLE = ObservedVariable(title="Zn_conc")
4964

5065

5166
async def get_observed_variable_fixture(
52-
data: ObservedVariable, studies: list[Response], test_client: AsyncTestClient, id: UUID | None = None
67+
data: ObservedVariable,
68+
studies: list[Response],
69+
method: Response,
70+
trait: Response,
71+
unit: Response,
72+
test_client: AsyncTestClient,
73+
id: UUID | None = None,
5374
) -> ObservedVariableResponse:
5475
study_id = [item.json()["id"] for item in studies]
55-
send_data = ObservedVariableDataclass(study_id=study_id, **data.to_dict())
76+
method_id = method.json()["id"]
77+
trait_reference_id = trait.json()["id"]
78+
unit_id = unit.json()["id"]
79+
send_data = ObservedVariableDataclass(
80+
method_id=method_id,
81+
trait_reference_id=trait_reference_id,
82+
study_id=study_id,
83+
unit_id=unit_id,
84+
**data.to_dict(),
85+
)
5686
if id is None:
5787
send_data.updated_at = None
5888
response = await post_fixture(PATH, send_data, test_client)
5989
else:
6090
response = await put_fixture(PATH, send_data, test_client, id)
61-
return ObservedVariableResponse(study_response=studies, observed_variable_response=response)
91+
return ObservedVariableResponse(
92+
study_response=studies,
93+
method_response=method,
94+
unit_response=unit,
95+
trait_response=trait,
96+
observed_variable_response=response,
97+
)
98+
99+
100+
@pytest.fixture(scope="function")
101+
async def setup_trait(test_client: AsyncTestClient) -> AsyncGenerator[VariableTraitResponse, None]:
102+
PATH = "vocabulary"
103+
anthesis_trait = await post_fixture(PATH, ANTHESIS_TRAIT, test_client)
104+
projected_shoot_area_trait = await post_fixture(PATH, PROJECTED_SHOOT_AREA_TRAIT, test_client)
105+
zn_concentration_trait = await post_fixture(PATH, ZN_CONCENTRATION_TRAIT, test_client)
106+
reproductive_growth_time_trait = await post_fixture(PATH, REPRODUCTIVE_GROWTH_TIME_TRAIT, test_client)
107+
yield VariableTraitResponse(
108+
anthesis=anthesis_trait,
109+
reproductive_growth_time=reproductive_growth_time_trait,
110+
projected_shoot_area=projected_shoot_area_trait,
111+
zn_concentration=zn_concentration_trait,
112+
)
113+
await delete_fixture(PATH, anthesis_trait.json()["id"], test_client)
114+
await delete_fixture(PATH, reproductive_growth_time_trait.json()["id"], test_client)
115+
await delete_fixture(PATH, projected_shoot_area_trait.json()["id"], test_client)
116+
await delete_fixture(PATH, zn_concentration_trait.json()["id"], test_client)
62117

63118

64119
@pytest.fixture(scope="function")
65120
async def setup_observed_variable(
66-
setup_study: AllStudyFixtureResponse, test_client: AsyncTestClient
121+
setup_study: AllStudyFixtureResponse,
122+
setup_method: AllMethodFixtureResponse,
123+
setup_unit: AllUnitFixtureResponse,
124+
setup_trait: VariableTraitResponse,
125+
test_client: AsyncTestClient,
67126
) -> AsyncGenerator[AllObservedVariableFixtureResponse, None]:
127+
first_study = setup_study.first.study_response
68128
maize_study = setup_study.maize.study_response
69-
barley_study = setup_study.barley.study_response
70-
maize_sowing_density = await get_observed_variable_fixture(
71-
MAIZE_SOWING_DENSITY, [maize_study, barley_study], test_client
129+
barley_study = setup_study.maize.study_response
130+
131+
projected_shoot_area_method = setup_method.projected_shoot_area.method_response
132+
zn_concentration_method = setup_method.zn_concentration.method_response
133+
day_to_anthesis_method = setup_method.day_to_anthesis.method_response
134+
135+
growing_degree_day = setup_unit.degree_day.unit_response
136+
kilopixel = setup_unit.kilo_pixel.unit_response
137+
microgram = setup_unit.microgram.unit_response
138+
139+
anthesis_trait = setup_trait.anthesis
140+
projected_shoot_area_trait = setup_trait.projected_shoot_area
141+
zn_concentration_trait = setup_trait.zn_concentration
142+
reproductive_growth_time_trait = setup_trait.reproductive_growth_time
143+
144+
anthesis_variable = await get_observed_variable_fixture(
145+
data=ANTHESIS_VARIABLE,
146+
studies=[first_study],
147+
method=day_to_anthesis_method,
148+
trait=anthesis_trait,
149+
unit=growing_degree_day,
150+
test_client=test_client,
151+
)
152+
reproductive_growth_time_variable = await get_observed_variable_fixture(
153+
data=REPRODUCTIVE_GROWTH_TIME_VARIABLE,
154+
studies=[maize_study],
155+
method=day_to_anthesis_method,
156+
trait=reproductive_growth_time_trait,
157+
unit=growing_degree_day,
158+
test_client=test_client,
72159
)
73-
maize_rooting_medium = await get_observed_variable_fixture(MAIZE_ROOTING_MEDIUM, [maize_study], test_client)
74-
maize_ph = await get_observed_variable_fixture(MAIZE_PH, [maize_study], test_client)
75-
barley_sowing_density = await get_observed_variable_fixture(BARLEY_SOWING_DENSITY, [barley_study], test_client)
76-
barley_rooting_medium = await get_observed_variable_fixture(BARLEY_ROOTING_MEDIUM, [barley_study], test_client)
77-
barley_ferterlizer = await get_observed_variable_fixture(BARLEY_FERTILIZER, [barley_study], test_client)
78-
barley_watering_exposure = await get_observed_variable_fixture(
79-
BARLEY_WATERING_EXPOSURE, [barley_study], test_client
160+
projected_shoot_area_variable = await get_observed_variable_fixture(
161+
data=PROJECTED_SHOOT_AREA_VARIABLE,
162+
studies=[barley_study],
163+
method=projected_shoot_area_method,
164+
trait=projected_shoot_area_trait,
165+
unit=kilopixel,
166+
test_client=test_client,
80167
)
81-
barley_light_intensity = await get_observed_variable_fixture(BARLEY_LIGHT_INTENSITY, [barley_study], test_client)
82-
barley_relative_humidity = await get_observed_variable_fixture(
83-
BARLEY_RELATIVE_HUMIDITY, [barley_study], test_client
168+
zn_concentration_variable = await get_observed_variable_fixture(
169+
data=ZN_CONCENTRATION_VARIABLE,
170+
studies=[barley_study],
171+
method=zn_concentration_method,
172+
trait=zn_concentration_trait,
173+
unit=microgram,
174+
test_client=test_client,
84175
)
85-
barley_temperature = await get_observed_variable_fixture(BARLEY_TEMPERATURE, [barley_study], test_client)
86176

87177
yield AllObservedVariableFixtureResponse(
88-
maize_sowing_density=maize_sowing_density,
89-
maize_rooting_medium=maize_rooting_medium,
90-
maize_ph=maize_ph,
91-
barley_sowing_density=barley_sowing_density,
92-
barley_rooting_medium=barley_rooting_medium,
93-
barley_fertilizer=barley_ferterlizer,
94-
barley_watering_exposure=barley_watering_exposure,
95-
barley_light_intensity=barley_light_intensity,
96-
barley_relative_humidity=barley_relative_humidity,
97-
barley_temperature=barley_temperature,
178+
reproductive_growth_time=reproductive_growth_time_variable,
179+
projected_shoot_area=projected_shoot_area_variable,
180+
zn_concentration=zn_concentration_variable,
181+
anthesis=anthesis_variable,
98182
study_response=setup_study,
183+
method_response=setup_method,
184+
trait_response=setup_trait,
185+
unit_response=setup_unit,
99186
)
100-
await delete_fixture(PATH, maize_sowing_density.observed_variable_response.json()["id"], test_client)
101-
await delete_fixture(PATH, maize_rooting_medium.observed_variable_response.json()["id"], test_client)
102-
await delete_fixture(PATH, maize_ph.observed_variable_response.json()["id"], test_client)
103-
await delete_fixture(PATH, barley_sowing_density.observed_variable_response.json()["id"], test_client)
104-
await delete_fixture(PATH, barley_rooting_medium.observed_variable_response.json()["id"], test_client)
105-
await delete_fixture(PATH, barley_ferterlizer.observed_variable_response.json()["id"], test_client)
106-
await delete_fixture(PATH, barley_watering_exposure.observed_variable_response.json()["id"], test_client)
107-
await delete_fixture(PATH, barley_light_intensity.observed_variable_response.json()["id"], test_client)
108-
await delete_fixture(PATH, barley_relative_humidity.observed_variable_response.json()["id"], test_client)
109-
await delete_fixture(PATH, barley_temperature.observed_variable_response.json()["id"], test_client)
110187

111188

112189
@pytest.fixture(scope="function")
@@ -115,9 +192,15 @@ async def update_observed_variable(
115192
) -> AsyncGenerator[AllObservedVariableFixtureResponse, None]:
116193
all_responses = setup_observed_variable
117194
maize_study = all_responses.study_response.maize.study_response
118-
maize_sowing_density = all_responses.maize_sowing_density
119-
maize_sowing_density_response = await get_observed_variable_fixture(
120-
MAIZE_SOWING_DENSITY, [maize_study], test_client, maize_sowing_density.observed_variable_response.json()["id"]
195+
anthesis = all_responses.anthesis
196+
anthesis_response = await get_observed_variable_fixture(
197+
data=ANTHESIS_VARIABLE,
198+
studies=[maize_study],
199+
test_client=test_client,
200+
id=anthesis.observed_variable_response.json()["id"],
201+
method=anthesis.method_response,
202+
trait=anthesis.trait_response,
203+
unit=anthesis.unit_response,
121204
)
122-
all_responses.maize_sowing_density = maize_sowing_density_response
205+
all_responses.anthesis = anthesis_response
123206
yield all_responses

0 commit comments

Comments
 (0)