-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
lib/dl_api_lib/dl_api_lib_tests/db/data_api/result/test_parameters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from __future__ import annotations | ||
|
||
from http import HTTPStatus | ||
|
||
import pytest | ||
|
||
from dl_api_client.dsmaker.primitives import ( | ||
IntegerParameterValue, | ||
RangeParameterValueConstraint, | ||
) | ||
from dl_api_client.dsmaker.shortcuts.dataset import ( | ||
add_formulas_to_dataset, | ||
add_parameters_to_dataset, | ||
) | ||
from dl_api_client.dsmaker.shortcuts.result_data import get_data_rows | ||
from dl_api_lib_tests.db.base import DefaultApiTestBase | ||
from dl_constants.enums import UserDataType | ||
|
||
|
||
class TestParameters(DefaultApiTestBase): | ||
@pytest.mark.parametrize( | ||
("multiplier", "expected_status_code"), | ||
( | ||
(None, HTTPStatus.OK), | ||
(2, HTTPStatus.OK), | ||
(5, HTTPStatus.OK), | ||
(-1, HTTPStatus.BAD_REQUEST), | ||
), | ||
) | ||
def test_parameter_in_formula(self, control_api, data_api, saved_dataset, multiplier, expected_status_code): | ||
default_multiplier = 1 | ||
ds = add_parameters_to_dataset( | ||
api_v1=control_api, | ||
dataset_id=saved_dataset.id, | ||
parameters={ | ||
"Multiplier": ( | ||
IntegerParameterValue(default_multiplier), | ||
RangeParameterValueConstraint(min=IntegerParameterValue(default_multiplier)), | ||
), | ||
}, | ||
) | ||
|
||
integer_field = next(field for field in saved_dataset.result_schema if field.data_type == UserDataType.integer) | ||
ds = add_formulas_to_dataset( | ||
api_v1=control_api, | ||
dataset=ds, | ||
formulas={ | ||
"Multiplied Field": f"[{integer_field.title}] * [Multiplier]", | ||
}, | ||
) | ||
|
||
result_resp = data_api.get_result( | ||
dataset=ds, | ||
fields=[ | ||
integer_field, | ||
ds.find_field(title="Multiplier"), | ||
ds.find_field(title="Multiplied Field"), | ||
], | ||
parameters=[ | ||
ds.find_field(title="Multiplier").parameter_value(multiplier), | ||
], | ||
fail_ok=True, | ||
) | ||
assert result_resp.status_code == expected_status_code, result_resp.json | ||
|
||
if expected_status_code == HTTPStatus.OK: | ||
data_rows = get_data_rows(result_resp) | ||
assert data_rows | ||
for row in data_rows: | ||
assert int(row[1]) == (multiplier or default_multiplier) | ||
assert int(row[0]) * int(row[1]) == int(row[2]) | ||
|
||
def test_parameter_no_constraint(self, control_api, data_api, dataset_id): | ||
ds = add_parameters_to_dataset( | ||
api_v1=control_api, | ||
dataset_id=dataset_id, | ||
parameters={ | ||
"Param": (IntegerParameterValue(0), None), | ||
}, | ||
) | ||
ds = add_formulas_to_dataset( | ||
api_v1=control_api, | ||
dataset=ds, | ||
formulas={ | ||
"Value": "[Param]", | ||
}, | ||
) | ||
|
||
result_resp = data_api.get_result( | ||
dataset=ds, | ||
fields=[ | ||
ds.find_field(title="Value"), | ||
], | ||
parameters=[ | ||
ds.find_field(title="Param").parameter_value(1), | ||
], | ||
limit=1, | ||
) | ||
assert result_resp.status_code == HTTPStatus.OK, result_resp.json | ||
assert int(get_data_rows(result_resp)[0][0]) == 1 |