From 25bb447679f939e5a13c6c34e3ea73226c25aeb1 Mon Sep 17 00:00:00 2001 From: Simen Fivelstad Smaaberg <66635118+simensma-fresh@users.noreply.github.com> Date: Mon, 16 Dec 2024 10:06:35 -0800 Subject: [PATCH] [FIX] Fix flaky permit condition requirement test (#3349) * [FIX] Fix reports resource test flakiness * Remove unused import * [FIX] Permit condition test flakiness * Updated permit conditions --- .../models/permit_conditions.py | 21 +++++++++++++++++++ ...test_create_permit_conditions_from_task.py | 8 ++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/services/core-api/app/api/mines/permits/permit_conditions/models/permit_conditions.py b/services/core-api/app/api/mines/permits/permit_conditions/models/permit_conditions.py index bd5c41366c..123a9513d6 100644 --- a/services/core-api/app/api/mines/permits/permit_conditions/models/permit_conditions.py +++ b/services/core-api/app/api/mines/permits/permit_conditions/models/permit_conditions.py @@ -193,3 +193,24 @@ def find_by_condition_category_code(cls, condition_category_code): return cls.query.filter_by( condition_category_code=condition_category_code, deleted_ind=False ).all() + + @classmethod + def find_by_permit_amendment_id_ordered(cls, permit_amendment_id): + # Returns a list of root conditions ordered by display_order + # within each parent condition, subconditions are ordered by display_order + + def get_all_conditions(condition): + conditions = [condition] + for sub_condition in condition.all_sub_conditions: + if not sub_condition.deleted_ind: + conditions.extend(get_all_conditions(sub_condition)) + return conditions + + all_conditions = [] + root_conditions = cls.query\ + .filter_by(parent_permit_condition_id=None, deleted_ind=False, permit_amendment_id=permit_amendment_id)\ + .order_by(cls.display_order)\ + .all() + for root_condition in root_conditions: + all_conditions.extend(get_all_conditions(root_condition)) + return all_conditions diff --git a/services/core-api/tests/permits/permit_extraction/test_create_permit_conditions_from_task.py b/services/core-api/tests/permits/permit_extraction/test_create_permit_conditions_from_task.py index 5652a8a6d5..090cf1df47 100644 --- a/services/core-api/tests/permits/permit_extraction/test_create_permit_conditions_from_task.py +++ b/services/core-api/tests/permits/permit_extraction/test_create_permit_conditions_from_task.py @@ -20,12 +20,13 @@ def permit_amendment(test_client, db_session): mine, permit = create_mine_and_permit() permit_amendment = permit.permit_amendments[0] PermitConditions.query.delete() + db_session.flush() yield permit_amendment @pytest.fixture(scope="function") -def permit_conditions(permit_amendment): +def permit_conditions(permit_amendment, db_session): task = PermitExtractionTask( task_result={ "conditions": [ @@ -128,15 +129,16 @@ def permit_conditions(permit_amendment): create_permit_conditions_from_task(task) # Retrieve the created permit conditions from the database - permit_conditions = PermitConditions.query.all() + permit_conditions = PermitConditions.find_by_permit_amendment_id_ordered(permit_amendment.permit_amendment_id) return permit_conditions def test_create_permit_conditions_from_task( permit_conditions, permit_amendment, db_session -): +): + assert len(permit_conditions) == 7 ### General Section gen_cat = permit_conditions[0]