Skip to content

Commit

Permalink
Jmm/resolve cog baseline bug (#2483)
Browse files Browse the repository at this point in the history
* Fix bug

* lint

* Move signals functionality to models.py in support. (#2616)

* rm refs to signals

---------

Co-authored-by: Tadhg O'Higgins <[email protected]>
  • Loading branch information
JeanMarie-PM and tadhg-ohiggins authored Nov 8, 2023
1 parent 6d384b3 commit dc650c2
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 50 deletions.
2 changes: 1 addition & 1 deletion backend/support/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class CognizantBaselineAdmin(SupportAdmin):
"source",
]
list_filter = [
"is_active",
"source",
"is_active",
"cognizant_agency",
]

Expand Down
3 changes: 0 additions & 3 deletions backend/support/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@
class SupportConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "support"

def ready(self):
from . import signals # noqa
47 changes: 47 additions & 0 deletions backend/support/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.apps import apps
from django.db import models
from django.db.models import Q
from django.utils import timezone


Expand Down Expand Up @@ -82,3 +84,48 @@ class CognizantAssignment(models.Model):
default=AssignmentTypeCode.COMPUTED,
verbose_name="Type",
)

def save(self, *args, **kwargs):
if self._state.adding:
sac_model = apps.get_model("audit.SingleAuditChecklist")
cognizant_agency = self.cognizant_agency
sac = sac_model.objects.get(report_id=self.report_id)
sac.cognizant_agency = cognizant_agency
sac.save()

uei, ein = sac.auditee_uei, sac.ein

reset_baseline(cognizant_agency, ein, uei)

try:
gen_model = apps.get_model("dissemination.General")
gen = gen_model.objects.get(report_id=sac.report_id)
gen.cognizant_agency = cognizant_agency
gen.save()
except gen_model.DoesNotExist:
# etl may not have been run yet
pass

super().save(*args, **kwargs)


def reset_baseline(cognizant_agency, ein, uei):
baselines = CognizantBaseline.objects.filter(
Q(is_active=True)
& ~Q(cognizant_agency=cognizant_agency)
& (Q(ein=ein) | Q(uei=uei))
)
for baseline in baselines:
baseline.is_active = False
baseline.save()
existing_baseline_count = CognizantBaseline.objects.filter(
Q(is_active=True)
& Q(cognizant_agency=cognizant_agency)
& (Q(ein=ein) | Q(uei=uei))
).count()
if existing_baseline_count == 0:
CognizantBaseline(
ein=ein,
uei=uei,
cognizant_agency=cognizant_agency,
).save()
36 changes: 0 additions & 36 deletions backend/support/signals.py

This file was deleted.

70 changes: 60 additions & 10 deletions backend/support/test_cog_over.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from audit.models import SingleAuditChecklist
from .models import CognizantBaseline, CognizantAssignment

from .cog_over import compute_cog_over
from .cog_over import compute_cog_over, record_cog_assignment

# Note: Fake data is generated for SingleAuditChecklist, CognizantBaseline.
# Using only the data fields that apply to cog / over assignment.
Expand Down Expand Up @@ -338,27 +338,77 @@ def test_over_assignment_with_hist(self):
self.assertEqual(over_agency, "15")

def test_cog_assignment_with_uei_in_baseline(self):
BASE_UEI = "UEI1"
BASE_EIN = "EIN1"
BASE_COG = "00"

sac = self._fake_sac()
sac.general_information["auditee_uei"] = UEI_WITH_BASELINE
sac.general_information["auditee_uei"] = BASE_UEI
sac.general_information["ein"] = BASE_EIN

baker.make(
CognizantBaseline,
uei=UEI_WITH_BASELINE,
cognizant_agency="17",
uei=BASE_UEI,
ein=BASE_EIN,
cognizant_agency=BASE_COG,
is_active=True,
)
cog_agency, over_agency = compute_cog_over(
sac.federal_awards, sac.submission_status, sac.ein, sac.auditee_uei
)
self.assertEqual(cog_agency, "10")
self.assertEqual(cog_agency, BASE_COG)
self.assertEqual(over_agency, None)

def test_cog_assignment_with_uei_in_baseline_and_overris(self):
BASE_UEI = "UEI1"
BASE_EIN = "EIN1"
BASE_COG = "00"

sac = self._fake_sac()
sac.general_information["auditee_uei"] = UEI_WITH_BASELINE
sac.general_information["auditee_uei"] = BASE_UEI
sac.general_information["ein"] = BASE_EIN
sac.save()

baker.make(
CognizantBaseline,
uei=UEI_WITH_BASELINE,
cognizant_agency="17",
uei=BASE_UEI,
ein=BASE_EIN,
cognizant_agency=BASE_COG,
)
sac.assign_cog_over()
cbs = CognizantBaseline.objects.all()
self.assertEquals(len(cbs), 1)

cog_agency, _ = compute_cog_over(
sac.federal_awards, sac.submission_status, sac.ein, sac.auditee_uei
)
record_cog_assignment(sac.report_id, sac.submitted_by, cog_agency)
cas = CognizantAssignment.objects.all()
self.assertEquals(len(cas), 1)
cbs = CognizantBaseline.objects.all()
self.assertEquals(len(cbs), 1)
sac = SingleAuditChecklist.objects.get(report_id=sac.report_id)
self.assertEquals(sac.cognizant_agency, cog_agency)

oberride_cog = "01"
CognizantAssignment(
report_id=sac.report_id,
cognizant_agency=oberride_cog,
assignor_email="test_cog_over @test.gov",
override_comment="test_cog_over",
).save()
cbs = CognizantBaseline.objects.all()
self.assertEquals(len(cbs), 2)
cas = CognizantAssignment.objects.all()
self.assertEquals(1, len(cas))
self.assertEquals(len(cas), 2)
sac = SingleAuditChecklist.objects.get(report_id=sac.report_id)
self.assertEquals(sac.cognizant_agency, oberride_cog)

# a re-run ahould create a third assignmenet
sac.cognizant_agency = None
sac.save()
cog_agency, _ = compute_cog_over(
sac.federal_awards, sac.submission_status, sac.ein, sac.auditee_uei
)
record_cog_assignment(sac.report_id, sac.submitted_by, cog_agency)
sac = SingleAuditChecklist.objects.get(report_id=sac.report_id)
self.assertEquals(sac.cognizant_agency, oberride_cog)

0 comments on commit dc650c2

Please sign in to comment.