def testParsesStatePerson(self): # Arrange metadata = IngestMetadata.new_with_defaults( region='us_nd', jurisdiction_id='JURISDICTION_ID') ingest_person = ingest_info_pb2.StatePerson( gender='MALE', full_name='FULL_NAME', birthdate='12-31-1999', current_address='NNN\n STREET \t ZIP') # Act state_person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.StatePerson.new_with_defaults( gender=Gender.MALE, gender_raw_text='MALE', full_name='{"full_name": "FULL_NAME"}', birthdate=date(year=1999, month=12, day=31), birthdate_inferred_from_age=False, current_address='NNN STREET ZIP', residency_status=ResidencyStatus.PERMANENT, ) self.assertEqual(result, expected_result)
def testParseStatePerson_WithSurnameAndGivenNames_UsesFullNameAsJson(self): # Arrange metadata = FakeIngestMetadata.for_state(region="us_xx") ingest_person = ingest_info_pb2.StatePerson( state_code="us_xx", surname='UNESCAPED,SURNAME"WITH-CHARS"', given_names="GIVEN_NAMES", middle_names="MIDDLE_NAMES", ) # Act state_person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build(StatePersonFactory.deserialize) # Assert expected_full_name = ( '{{"given_names": "{}", "middle_names": "{}", "surname": "{}"}}'.format( "GIVEN_NAMES", "MIDDLE_NAMES", 'UNESCAPED,SURNAME\\"WITH-CHARS\\"' ) ) expected_result = entities.StatePerson.new_with_defaults( state_code="US_XX", full_name=expected_full_name ) self.assertEqual(result, expected_result)
def testParsesStatePerson(self): # Arrange metadata = FakeIngestMetadata.for_state(region="us_nd") ingest_person = ingest_info_pb2.StatePerson( gender="MALE", full_name="FULL_NAME", birthdate="12-31-1999", current_address="NNN\n STREET \t ZIP", ) # Act state_person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build(StatePersonFactory.deserialize) # Assert expected_result = entities.StatePerson.new_with_defaults( gender=Gender.MALE, gender_raw_text="MALE", full_name='{"full_name": "FULL_NAME"}', birthdate=date(year=1999, month=12, day=31), birthdate_inferred_from_age=False, current_address="NNN STREET ZIP", residency_status=ResidencyStatus.PERMANENT, state_code="US_ND", ) self.assertEqual(result, expected_result)
def testParseStatePerson_WithSurnameAndFullname_ThrowsException(self): # Arrange metadata = FakeIngestMetadata.for_state(region="us_xx") ingest_person = ingest_info_pb2.StatePerson( full_name="LAST,FIRST", surname="LAST" ) # Arrange + Act with self.assertRaises(ValueError): state_person.copy_fields_to_builder(self.subject, ingest_person, metadata)
def testParseStatePerson_WithSurnameAndFullname_ThrowsException(self): # Arrange metadata = IngestMetadata.new_with_defaults() ingest_person = ingest_info_pb2.StatePerson(full_name='LAST,FIRST', surname='LAST') # Arrange + Act with self.assertRaises(ValueError): state_person.copy_fields_to_builder(self.subject, ingest_person, metadata)
def _convert_state_person(self, ingest_state_person: StatePerson) \ -> entities.StatePerson: """Converts an ingest_info proto StatePerson to a persistence entity.""" state_person_builder = entities.StatePerson.builder() state_person.copy_fields_to_builder( state_person_builder, ingest_state_person, self.metadata) converted_aliases = [state_alias.convert(self.aliases[alias_id], self.metadata) for alias_id in ingest_state_person.state_alias_ids] state_person_builder.aliases = converted_aliases converted_races = [ state_person_race.convert(self.person_races[race_id], self.metadata) for race_id in ingest_state_person.state_person_race_ids ] state_person_builder.races = converted_races converted_ethnicities = [ state_person_ethnicity.convert( self.person_ethnicities[ethnicity_id], self.metadata) for ethnicity_id in ingest_state_person.state_person_ethnicity_ids ] state_person_builder.ethnicities = converted_ethnicities converted_assessments = [ self._convert_assessment(self.assessments[assessment_id]) for assessment_id in ingest_state_person.state_assessment_ids ] state_person_builder.assessments = converted_assessments converted_external_ids = [ state_person_external_id.convert( self.person_external_ids[external_id], self.metadata) for external_id in ingest_state_person.state_person_external_ids_ids ] state_person_builder.external_ids = converted_external_ids converted_sentence_groups = [ self._convert_sentence_group( self.sentence_groups[sentence_group_id]) for sentence_group_id in ingest_state_person.state_sentence_group_ids ] state_person_builder.sentence_groups = converted_sentence_groups return state_person_builder.build()
def testParseStatePerson_NoiseInPlaceOfResidence_ParsesResidency(self): # Arrange metadata = IngestMetadata.new_with_defaults(region='us_ky_allen') ingest_person = ingest_info_pb2.StatePerson( current_address='transient moves around') # Act state_person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.StatePerson.new_with_defaults( current_address='TRANSIENT MOVES AROUND', residency_status=ResidencyStatus.TRANSIENT) self.assertEqual(result, expected_result)
def testParseStatePerson_InfersBirthdateFromAge(self, mock_datetime): # Arrange mock_datetime.now.return_value = _NOW metadata = IngestMetadata.new_with_defaults() ingest_person = ingest_info_pb2.StatePerson(age='27') # Act state_person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.StatePerson.new_with_defaults( birthdate=datetime(year=_NOW.year - 27, month=1, day=1).date(), birthdate_inferred_from_age=True) self.assertEqual(result, expected_result)
def testParseStatePerson_TakesLastZipCodeMatch(self): # Arrange metadata = FakeIngestMetadata.for_state(region="us_nd") # 5-digit address could be mistaken for a zip code ingest_person = ingest_info_pb2.StatePerson(current_address="12345 Main 58503") # Act state_person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build(StatePersonFactory.deserialize) # Assert expected_result = entities.StatePerson.new_with_defaults( current_address="12345 MAIN 58503", residency_status=ResidencyStatus.PERMANENT, state_code="US_ND", ) self.assertEqual(result, expected_result)
def testParseStatePerson_TakesLastZipCodeMatch(self): # Arrange metadata = IngestMetadata.new_with_defaults(region='us_nd') # 5-digit address could be mistaken for a zip code ingest_person = ingest_info_pb2.StatePerson( current_address='12345 Main 58503') # Act state_person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.StatePerson.new_with_defaults( current_address='12345 MAIN 58503', residency_status=ResidencyStatus.PERMANENT) self.assertEqual(result, expected_result)
def testParseStatePerson_NoiseInPlaceOfResidence_ParsesResidency(self): # Arrange metadata = FakeIngestMetadata.for_state(region="us_xx") ingest_person = ingest_info_pb2.StatePerson( current_address="transient moves around" ) # Act state_person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build(StatePersonFactory.deserialize) # Assert expected_result = entities.StatePerson.new_with_defaults( current_address="TRANSIENT MOVES AROUND", residency_status=ResidencyStatus.TRANSIENT, state_code="US_XX", ) self.assertEqual(result, expected_result)
def testParseStatePerson_WithSurnameAndGivenNames_UsesFullNameAsJson(self): # Arrange metadata = IngestMetadata.new_with_defaults() ingest_person = ingest_info_pb2.StatePerson( surname='UNESCAPED,SURNAME"WITH-CHARS"', given_names='GIVEN_NAMES', middle_names='MIDDLE_NAMES') # Act state_person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_full_name = \ '{{"given_names": "{}", "middle_names": "{}", "surname": "{}"}}'\ .format('GIVEN_NAMES', 'MIDDLE_NAMES', 'UNESCAPED,SURNAME\\"WITH-CHARS\\"') expected_result = entities.StatePerson.new_with_defaults( full_name=expected_full_name) self.assertEqual(result, expected_result)
def _convert_person(self, ingest_person: StatePerson) -> entities.StatePerson: """Converts an ingest_info proto StatePerson to a persistence entity.""" state_person_builder = entities.StatePerson.builder() state_person.copy_fields_to_builder(state_person_builder, ingest_person, self.metadata) converted_aliases = [ state_alias.convert(self.aliases[alias_id], self.metadata) for alias_id in ingest_person.state_alias_ids ] state_person_builder.aliases = converted_aliases converted_races = [ state_person_race.convert(self.person_races[race_id], self.metadata) for race_id in ingest_person.state_person_race_ids ] state_person_builder.races = converted_races converted_ethnicities = [ state_person_ethnicity.convert( self.person_ethnicities[ethnicity_id], self.metadata) for ethnicity_id in ingest_person.state_person_ethnicity_ids ] state_person_builder.ethnicities = converted_ethnicities converted_assessments = [ self._convert_assessment(self.assessments[assessment_id]) for assessment_id in ingest_person.state_assessment_ids ] state_person_builder.assessments = converted_assessments converted_program_assignments = [ self._convert_program_assignment( self.program_assignments[assignment_id]) for assignment_id in ingest_person.state_program_assignment_ids ] state_person_builder.program_assignments = converted_program_assignments converted_external_ids = [ state_person_external_id.convert( self.person_external_ids[external_id], self.metadata) for external_id in ingest_person.state_person_external_ids_ids ] state_person_builder.external_ids = converted_external_ids converted_sentence_groups = [ self._convert_sentence_group( self.sentence_groups[sentence_group_id]) for sentence_group_id in ingest_person.state_sentence_group_ids ] state_person_builder.sentence_groups = converted_sentence_groups if ingest_person.supervising_officer_id: converted_supervising_officer = state_agent.convert( self.agents[ingest_person.supervising_officer_id], self.metadata) state_person_builder.supervising_officer = converted_supervising_officer return state_person_builder.build(StatePersonFactory.deserialize)