Skip to content

Commit

Permalink
fix ci_measurements add endpoint; added api test for this endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-mm committed Sep 19, 2023
1 parent 436004c commit 6e1d30e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
10 changes: 1 addition & 9 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,14 +621,6 @@ class CI_Measurement(BaseModel):
async def post_ci_measurement_add(measurement: CI_Measurement):
for key, value in measurement.model_dump().items():
match key:
case 'run_id':
if value is None or value.strip() == '':
measurement.run_id = None
continue
if not is_valid_uuid(value.strip()):
return ORJSONResponse({'success': False, 'err': f"run_id '{value}' is not a valid uuid"}, status_code=400)
continue

case 'unit':
if value is None or value.strip() == '':
return ORJSONResponse({'success': False, 'err': f"{key} is empty"}, status_code=400)
Expand All @@ -651,7 +643,7 @@ async def post_ci_measurement_add(measurement: CI_Measurement):
query = """
INSERT INTO
ci_measurements (energy_value, energy_unit, repo, branch, workflow, run_id, label, source, cpu, commit_hash, duration, cpu_util_avg)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
params = (measurement.energy_value, measurement.energy_unit, measurement.repo, measurement.branch,
measurement.workflow, measurement.run_id,
Expand Down
25 changes: 25 additions & 0 deletions test/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

# pylint: disable=no-name-in-module
from api import Software
from api import CI_Measurement

@pytest.fixture(autouse=True, name="register_machine")
def register_machine_fixture():
Expand Down Expand Up @@ -49,6 +50,30 @@ def test_post_run_add():
job_id = get_job_id(run_name)
assert job_id is not None

def test_ci_measurement_add():
measurement = CI_Measurement(energy_value=123,
energy_unit='mJ',
repo='testRepo',
branch='testBranch',
cpu='testCPU',
cpu_util_avg=50,
commit_hash='1234asdf',
workflow='testWorkflow',
run_id='testRunID',
source='testSource',
label='testLabel',
duration=20)
response = requests.post(f"{API_URL}/v1/ci/measurement/add", json=measurement.model_dump(), timeout=15)
assert response.status_code == 201, Tests.assertion_info('success', response.text)
query = """
SELECT * FROM ci_measurements WHERE run_id = %s
"""
data = DB().fetch_one(query, (measurement.run_id, ), row_factory=psycopg.rows.dict_row)
assert data is not None
for key in measurement.model_dump().keys():
assert data[key] == measurement.model_dump()[key], Tests.assertion_info(f"{key}: {data[key]}", measurement.model_dump()[key])


def todo_test_get_runs():
run_name = 'test_' + utils.randomword(12)
uri = os.path.abspath(os.path.join(
Expand Down

0 comments on commit 6e1d30e

Please sign in to comment.