Beispiel #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
Beispiel #3
0
    def test_write_preexisting_person_duplicate_charges(self):
        # Arrange
        most_recent_scrape_time = (SCRAPER_START_DATETIME + timedelta(days=1))
        metadata = IngestMetadata.new_with_defaults(
            region=REGION_1,
            jurisdiction_id=JURISDICTION_ID,
            ingest_time=most_recent_scrape_time)

        schema_charge = schema.Charge(
            charge_id=ID,
            external_id=EXTERNAL_ID + '_COUNT_1',
            status=ChargeStatus.PRESENT_WITHOUT_INFO.value)
        schema_charge_another = schema.Charge(
            charge_id=ID_2,
            external_id=EXTERNAL_ID + '_COUNT_2',
            status=ChargeStatus.PRESENT_WITHOUT_INFO.value)
        schema_booking = schema.Booking(
            booking_id=BOOKING_ID,
            external_id=EXTERNAL_BOOKING_ID,
            admission_date_inferred=True,
            custody_status=CustodyStatus.IN_CUSTODY.value,
            last_seen_time=SCRAPER_START_DATETIME,
            first_seen_time=SCRAPER_START_DATETIME,
            charges=[schema_charge, schema_charge_another])
        schema_person = schema.Person(person_id=PERSON_ID,
                                      jurisdiction_id=JURISDICTION_ID,
                                      external_id=EXTERNAL_PERSON_ID,
                                      region=REGION_1,
                                      bookings=[schema_booking])

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

        ingest_info = IngestInfo()
        ingest_info.people.add(full_name=FULL_NAME_1,
                               person_id=EXTERNAL_PERSON_ID,
                               booking_ids=[EXTERNAL_BOOKING_ID])
        ingest_info.bookings.add(
            booking_id=EXTERNAL_BOOKING_ID,
            custody_status='IN CUSTODY',
            charge_ids=[EXTERNAL_ID],
        )
        ingest_info.charges.add(charge_id=EXTERNAL_ID, number_of_counts='2')

        # Act
        persistence.write(ingest_info, metadata)

        # Assert
        expected_charge = county_entities.Charge.new_with_defaults(
            charge_id=ID,
            external_id=EXTERNAL_ID + '_COUNT_1',
            status=ChargeStatus.PRESENT_WITHOUT_INFO)
        expected_charge_another = attr.evolve(expected_charge,
                                              charge_id=ID_2,
                                              external_id=EXTERNAL_ID +
                                              '_COUNT_2')
        expected_booking = county_entities.Booking.new_with_defaults(
            booking_id=BOOKING_ID,
            external_id=EXTERNAL_BOOKING_ID,
            admission_date_inferred=True,
            custody_status=CustodyStatus.IN_CUSTODY,
            custody_status_raw_text=BOOKING_CUSTODY_STATUS.upper(),
            last_seen_time=most_recent_scrape_time,
            first_seen_time=SCRAPER_START_DATETIME,
            charges=[expected_charge, expected_charge_another])
        expected_person = county_entities.Person.new_with_defaults(
            person_id=PERSON_ID,
            external_id=EXTERNAL_PERSON_ID,
            region=REGION_1,
            jurisdiction_id=JURISDICTION_ID,
            bookings=[expected_booking])
        self.assertEqual([expected_person],
                         county_dao.read_people(
                             SessionFactory.for_schema_base(JailsBase)))