Esempio n. 1
0
    def test_matchPerson_updateStatusOnOrphanedEntities(self):
        # Arrange
        schema_bond = schema.Bond(
            bond_id=_BOND_ID, status=BondStatus.PENDING.value,
            booking_id=_BOOKING_ID)
        schema_charge = schema.Charge(
            charge_id=_CHARGE_ID, status=ChargeStatus.PENDING.value,
            bond=schema_bond)
        schema_booking = schema.Booking(
            admission_date=_DATE_2, booking_id=_BOOKING_ID,
            custody_status=CustodyStatus.IN_CUSTODY.value, last_seen_time=_DATE,
            first_seen_time=_DATE, charges=[schema_charge])

        schema_person = schema.Person(
            person_id=_PERSON_ID, full_name=_FULL_NAME, birthdate=_DATE,
            jurisdiction_id=_JURISDICTION_ID, region=_REGION,
            bookings=[schema_booking])

        session = SessionFactory.for_schema_base(JailsBase)
        session.add(schema_person)
        session.commit()

        ingested_charge_no_bond = attr.evolve(
            converter.convert_schema_object_to_entity(schema_charge),
            charge_id=None,
            bond=None)
        ingested_booking = attr.evolve(
            converter.convert_schema_object_to_entity(schema_booking),
            booking_id=None,
            custody_status=CustodyStatus.RELEASED,
            charges=[ingested_charge_no_bond])
        ingested_person = attr.evolve(
            converter.convert_schema_object_to_entity(schema_person),
            person_id=None,
            bookings=[ingested_booking])

        # Act
        out = entity_matching.match(session, _REGION, [ingested_person])

        # Assert
        expected_orphaned_bond = attr.evolve(
            converter.convert_schema_object_to_entity(schema_bond),
            status=BondStatus.REMOVED_WITHOUT_INFO)
        expected_charge = attr.evolve(
            ingested_charge_no_bond, charge_id=schema_charge.charge_id)
        expected_booking = attr.evolve(
            ingested_booking, booking_id=schema_booking.booking_id,
            charges=[expected_charge])
        expected_person = attr.evolve(
            ingested_person, person_id=schema_person.person_id,
            bookings=[expected_booking])

        self.assertCountEqual(
            converter.convert_schema_objects_to_entity(out.people),
            [expected_person])
        self.assertCountEqual(
            converter.convert_schema_objects_to_entity(out.orphaned_entities),
            [expected_orphaned_bond])
        self.assertEqual(out.error_count, 0)
    def generate_schema_county_person_obj_tree(
            self) -> county_schema.Person:
        """Test util for generating a Person schema object that has at least one
         child of each possible schema object type defined on county/schema.py.

        Returns:
            A test instance of a Person schema object.
        """
        person_id = 143
        booking_id = 938
        hold_id = 9945
        arrest_id = 861
        charge_id = 11111
        bond_id = 22222
        sentence_id = 12345

        person = county_schema.Person(
            person_id=person_id,
            full_name='name',
            birthdate=datetime.date(1980, 1, 5),
            birthdate_inferred_from_age=False,
            external_id='some_id',
            gender=Gender.EXTERNAL_UNKNOWN.value,
            gender_raw_text='Unknown',
            race=Race.OTHER.value,
            race_raw_text='Other',
            ethnicity=Ethnicity.EXTERNAL_UNKNOWN.value,
            ethnicity_raw_text='Unknown',
            residency_status=ResidencyStatus.TRANSIENT.value,
            resident_of_region=False,
            region='somewhere',
            jurisdiction_id='12345678',
        )
        booking = county_schema.Booking(
            booking_id=booking_id,
            person_id=person_id,
            external_id='booking_id',
            admission_date=datetime.date(2018, 7, 12),
            admission_date_inferred=True,
            admission_reason=AdmissionReason.TRANSFER.value,
            admission_reason_raw_text='Transferred',
            release_date=datetime.date(2018, 7, 30),
            release_date_inferred=False,
            projected_release_date=datetime.date(2018, 7, 25),
            release_reason=ReleaseReason.ACQUITTAL.value,
            release_reason_raw_text='Acquitted',
            custody_status=CustodyStatus.RELEASED.value,
            custody_status_raw_text='Released',
            facility='some facility',
            classification=Classification.MEDIUM.value,
            classification_raw_text='M',
            last_seen_time=datetime.datetime(2018, 7, 30),
            first_seen_time=datetime.datetime(2018, 7, 12),
        )
        person.bookings.append(booking)
        hold = county_schema.Hold(
            hold_id=hold_id,
            booking_id=booking_id,
            external_id='hold_id',
            jurisdiction_name='some jurisdiction',
            status=HoldStatus.INFERRED_DROPPED.value,
            status_raw_text=None,
        )
        booking.holds.append(hold)
        arrest = county_schema.Arrest(
            arrest_id=arrest_id,
            booking_id=booking_id,
            external_id='arrest_id',
            location='somewhere',
            agency='some agency',
            officer_name='some officer',
            officer_id='some officer ID',
        )
        booking.arrest = arrest
        charge = county_schema.Charge(
            charge_id=charge_id,
            booking_id=booking_id,
            bond_id=bond_id,
            sentence_id=sentence_id,
            external_id='charge_id',
            offense_date=datetime.date(2018, 7, 1),
            statute='some statute',
            name='charge name',
            attempted=False,
            degree=ChargeDegree.SECOND.value,
            degree_raw_text='2nd',
            charge_class=ChargeClass.CIVIL.value,
            class_raw_text='Civil',
            level='some level',
            fee_dollars=200,
            charging_entity='some entity',
            status=ChargeStatus.ACQUITTED.value,
            status_raw_text='Acquitted',
            court_type='court type',
            case_number='case_number',
            next_court_date=datetime.date(2018, 7, 14),
            judge_name='some name',
            charge_notes='some notes',
        )
        booking.charges.append(charge)
        bond = county_schema.Bond(
            bond_id=bond_id,
            booking_id=booking_id,
            external_id='bond_id',
            amount_dollars=2000,
            bond_type=BondType.CASH.value,
            bond_type_raw_text='Cash bond',
            status=BondStatus.POSTED.value,
            status_raw_text='Posted',
            bond_agent='some bond agent',
        )
        charge.bond = bond
        sentence = county_schema.Sentence(
            sentence_id=sentence_id,
            booking_id=booking_id,
            external_id='sentence_id',
            status=SentenceStatus.COMMUTED.value,
            status_raw_text='Commuted',
            sentencing_region='some region',
            min_length_days=90,
            max_length_days=180,
            date_imposed=datetime.date(2018, 7, 14),
            completion_date=datetime.date(2018, 7, 30),
            projected_completion_date=datetime.date(2018, 10, 1),
            is_life=False,
            is_probation=False,
            is_suspended=False,
            fine_dollars=500,
            parole_possible=True,
            post_release_supervision_length_days=60,
        )
        charge.sentence = sentence

        return person