Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/bug report infractions #451

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions app/domain/regulation_computations.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def get_regulatory_alerts(user_id, start_date=None, end_date=None):


def get_regulatory_computations(user_id, start_date=None, end_date=None):
return RegulationComputation.query.filter(
RegulationComputation.user_id == user_id,
RegulationComputation.day.between(start_date, end_date),
).all()
return (
RegulationComputation.query.filter(
RegulationComputation.user_id == user_id,
RegulationComputation.day.between(start_date, end_date),
)
.order_by(RegulationComputation.creation_time)
.all()
)
17 changes: 10 additions & 7 deletions app/models/controller_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,30 @@ def report_infractions(self):
)
observed_infractions = []
for regulatory_alert in regulatory_alerts:

extra = regulatory_alert.extra
if not extra or not "sanction_code" in extra:
continue

regulation_computations_for_the_day = [
rc
for rc in regulation_computations
if rc.day == regulatory_alert.day
]
if len(regulation_computations_for_the_day) == 0:
nb_rc_for_the_day = len(regulation_computations_for_the_day)
if nb_rc_for_the_day == 0:
continue
if (
len(regulation_computations_for_the_day) == 2
nb_rc_for_the_day == 2
and regulatory_alert.submitter_type != SubmitterType.ADMIN
):
continue
if (
regulation_computations_for_the_day[0].submitter_type
nb_rc_for_the_day == 1
and regulation_computations_for_the_day[0].submitter_type
!= regulatory_alert.submitter_type
):
continue

extra = regulatory_alert.extra
if not extra or not "sanction_code" in extra:
continue
sanction_code = extra.get("sanction_code")
is_reportable = "NATINF" in sanction_code
check_type = regulatory_alert.regulation_check.type
Expand Down
54 changes: 54 additions & 0 deletions app/tests/regulations/test_report_infractions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import datetime

from app import db
from app.helpers.submitter_type import SubmitterType
from app.models import RegulationComputation, RegulatoryAlert, RegulationCheck
from app.models.controller_control import ControllerControl, ControlType
from app.seed import ControllerUserFactory
from app.seed.helpers import get_date
from app.tests.regulations import RegulationsTest


class TestReportInfractions(RegulationsTest):
def test_report_infractions_take_correct_alerts(self):
controller_user = ControllerUserFactory.create()
work_day = get_date(how_many_days_ago=2)
random_regulation_check = RegulationCheck.query.first()

# order is important - this order gave a bug when the test was written
for submitter_type in [SubmitterType.EMPLOYEE, SubmitterType.ADMIN]:
db.session.add(
RegulationComputation(
day=work_day,
submitter_type=submitter_type,
user=self.employee,
)
)

db.session.add(
RegulatoryAlert(
day=work_day,
submitter_type=SubmitterType.ADMIN,
user=self.employee,
extra={"sanction_code": "Code"},
regulation_check=random_regulation_check,
)
)

new_control = ControllerControl(
qr_code_generation_time=datetime.datetime.now(),
user_id=self.employee.id,
user_first_name=self.employee.first_name,
user_last_name=self.employee.last_name,
control_type=ControlType.mobilic,
controller_id=controller_user.id,
company_name=self.company.name,
vehicle_registration_number="AAA 11 BBB",
nb_controlled_days=28,
)
db.session.add(new_control)
db.session.commit()

new_control.report_infractions()

self.assertEqual(len(new_control.observed_infractions), 1)