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 test_matchPeople_errorCount(self): # Arrange schema_booking = schema.Booking( external_id=_EXTERNAL_ID, admission_date=_DATE_2, booking_id=_BOOKING_ID, custody_status=CustodyStatus.IN_CUSTODY.value, last_seen_time=_DATE, first_seen_time=_DATE) schema_booking_another = copy.deepcopy(schema_booking) schema_booking_another.booking_id = _BOOKING_ID_ANOTHER schema_person = schema.Person( person_id=_PERSON_ID, external_id=_EXTERNAL_ID, jurisdiction_id=_JURISDICTION_ID, full_name=_FULL_NAME, birthdate=_DATE, region=_REGION, bookings=[schema_booking, schema_booking_another]) schema_person_another = schema.Person(person_id=_PERSON_ID_ANOTHER, jurisdiction_id=_JURISDICTION_ID, region=_REGION, full_name=_NAME_2, external_id=_EXTERNAL_ID_ANOTHER) session = SessionFactory.for_schema_base(JailsBase) session.add(schema_person) session.add(schema_person_another) session.commit() ingested_booking = attr.evolve( converter.convert_schema_object_to_entity(schema_booking), booking_id=None, custody_status=CustodyStatus.RELEASED) ingested_person = attr.evolve( converter.convert_schema_object_to_entity(schema_person), person_id=None, bookings=[ingested_booking]) ingested_person_another = attr.evolve( converter.convert_schema_object_to_entity(schema_person_another), person_id=None ) # Act out = entity_matching.match( session, _REGION, [ingested_person, ingested_person_another]) # Assert expected_person = attr.evolve(ingested_person_another, person_id=schema_person_another.person_id) self.assertCountEqual( converter.convert_schema_objects_to_entity(out.people), [expected_person]) self.assertCountEqual( converter.convert_schema_objects_to_entity(out.orphaned_entities), []) self.assertEqual(out.error_count, 1)
def test_write_preexisting_person(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_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) 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', ) # Act persistence.write(ingest_info, metadata) # Assert 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) 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)))
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
def test_matchPeople(self): # Arrange 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, ) schema_person = schema.Person( person_id=_PERSON_ID, full_name=_FULL_NAME, birthdate=_DATE, jurisdiction_id=_JURISDICTION_ID, region=_REGION, bookings=[schema_booking], ) schema_booking_external_id = schema.Booking( admission_date=_DATE_2, booking_id=_BOOKING_ID_ANOTHER, release_date=_DATE, custody_status=CustodyStatus.RELEASED.value, last_seen_time=_DATE, first_seen_time=_DATE, ) schema_person_external_id = schema.Person( person_id=_PERSON_ID_ANOTHER, external_id=_EXTERNAL_ID, full_name=_FULL_NAME, birthdate=_DATE, jurisdiction_id=_JURISDICTION_ID, region=_REGION, bookings=[schema_booking_external_id], ) with SessionFactory.using_database(self.database_key, autocommit=False) as session: session.add(schema_person) session.add(schema_person_external_id) session.commit() ingested_booking = attr.evolve( converter.convert_schema_object_to_entity(schema_booking), booking_id=None, custody_status=CustodyStatus.RELEASED, ) ingested_person = attr.evolve( converter.convert_schema_object_to_entity(schema_person), person_id=None, bookings=[ingested_booking], ) ingested_booking_external_id = attr.evolve( converter.convert_schema_object_to_entity( schema_booking_external_id), booking_id=None, facility=_FACILITY, ) ingested_person_external_id = attr.evolve( converter.convert_schema_object_to_entity( schema_person_external_id), person_id=None, bookings=[ingested_booking_external_id], ) # Act out = entity_matching.match( session, _REGION, [ingested_person_external_id, ingested_person]) # Assert expected_booking = attr.evolve(ingested_booking, booking_id=_BOOKING_ID) expected_person = attr.evolve(ingested_person, person_id=_PERSON_ID, bookings=[expected_booking]) expected_booking_external_id = attr.evolve( ingested_booking_external_id, booking_id=_BOOKING_ID_ANOTHER) expected_person_external_id = attr.evolve( ingested_person_external_id, person_id=_PERSON_ID_ANOTHER, bookings=[expected_booking_external_id], ) self.assertCountEqual( converter.convert_schema_objects_to_entity(out.people), [expected_person_external_id, expected_person], ) self.assertCountEqual( converter.convert_schema_objects_to_entity(out.orphaned_entities), []) self.assertEqual(out.error_count, 0)