def testParsePerson_RaceIsEthnicity(self): class ScraperWithDefaultOverrides(BaseScraper): """Class created so BaseScraper's enum_overrides can be used.""" def __init__(self): # pylint: disable=super-init-not-called self.region = Mock() def populate_data(self, _, __, ___): pass # Arrange metadata = IngestMetadata.new_with_defaults( enum_overrides=ScraperWithDefaultOverrides().get_enum_overrides() ) ingest_person = ingest_info_pb2.Person(race="HISPANIC") # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.new_with_defaults( ethnicity=Ethnicity.HISPANIC, race_raw_text="HISPANIC", ) self.assertEqual(result, expected_result)
def testParsesPerson(self): # Arrange metadata = IngestMetadata.new_with_defaults( region="REGION", jurisdiction_id="JURISDICTION_ID" ) ingest_person = ingest_info_pb2.Person( full_name="FULL_NAME", birthdate="12-31-1999", gender="MALE", race="WHITE", ethnicity="HISPANIC", place_of_residence="NNN\n STREET \t ZIP", ) # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.new_with_defaults( full_name='{{"full_name": "{}"}}'.format("FULL_NAME"), birthdate=date(year=1999, month=12, day=31), birthdate_inferred_from_age=False, gender=Gender.MALE, gender_raw_text="MALE", race=Race.WHITE, race_raw_text="WHITE", ethnicity=Ethnicity.HISPANIC, ethnicity_raw_text="HISPANIC", residency_status=ResidencyStatus.PERMANENT, region="REGION", jurisdiction_id="JURISDICTION_ID", ) self.assertEqual(result, expected_result)
def testParsePerson_WithSurnameAndGivenNames_UsesFullNameAsJson(self): # Arrange metadata = FakeIngestMetadata.for_county( region="REGION", jurisdiction_id="JURISDICTION_ID") ingest_person = ingest_info_pb2.Person( surname='UNESCAPED,SURNAME"WITH-CHARS"', given_names="GIVEN_NAMES", middle_names="MIDDLE_NAMES", ) # Act 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.Person.new_with_defaults( region="REGION", jurisdiction_id="JURISDICTION_ID", full_name=expected_full_name, ) self.assertEqual(result, expected_result)
def _convert_person(self, ingest_person) -> entities.Person: """Converts an ingest_info proto Person to a persistence entity.""" person_builder = entities.Person.builder() person.copy_fields_to_builder(person_builder, ingest_person, self.metadata) converted_bookings = [ self._convert_booking(self.bookings[booking_id]) for booking_id in ingest_person.booking_ids ] if len([b for b in converted_bookings if not b.release_date]) > 1: raise ValueError( f"Multiple open bookings for person with person_id" f" [{ingest_person}]") # If no bookings were ingested, create booking to house inferred data. if not converted_bookings: inferred_booking = self._convert_booking(ingest_info_pb2.Booking()) converted_bookings = [inferred_booking] person_builder.bookings = converted_bookings converted_person = person_builder.build() # Scrub PII if the person either has an external id or has no open # bookings. if converted_person.external_id \ or not persistence_utils.has_active_booking(converted_person): persistence_utils.remove_pii_for_person(converted_person) return converted_person
def testParsePerson_WithSurnameAndFullname_ThrowsException(self): # Arrange metadata = IngestMetadata.new_with_defaults() ingest_person = ingest_info_pb2.Person(full_name="LAST,FIRST", surname="LAST") # Arrange + Act with self.assertRaises(ValueError): person.copy_fields_to_builder(self.subject, ingest_person, metadata)
def testParsePerson_NoNames_FullNameIsNone(self): # Arrange metadata = IngestMetadata.new_with_defaults() ingest_person = ingest_info_pb2.Person(person_id="1234") # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.new_with_defaults(external_id="1234") self.assertEqual(result, expected_result)
def testParsePerson_NoiseInPlaceOfResidence_ParsesResidencyStatus(self): # Arrange metadata = IngestMetadata.new_with_defaults(region='us_ky_allen') ingest_person = ingest_info_pb2.Person( place_of_residence='transient moves around') # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.new_with_defaults( residency_status=ResidencyStatus.TRANSIENT, region='us_ky_allen') self.assertEqual(result, expected_result)
def testParsePerson_InfersBirthdateFromAge(self, mock_datetime): # Arrange mock_datetime.now.return_value = _NOW metadata = IngestMetadata.new_with_defaults() ingest_person = ingest_info_pb2.Person(age='27') # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.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 testParsePerson_NoNames_FullNameIsNone(self): # Arrange metadata = FakeIngestMetadata.for_county( region="REGION", jurisdiction_id="JURISDICTION_ID") ingest_person = ingest_info_pb2.Person(person_id="1234") # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.new_with_defaults( region="REGION", jurisdiction_id="JURISDICTION_ID", external_id="1234") self.assertEqual(result, expected_result)
def testParsePerson_ResidenceAndStatusCombined(self): # Arrange metadata = IngestMetadata.new_with_defaults(region="us_ky_allen") ingest_person = ingest_info_pb2.Person(place_of_residence="42164 homeless") # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.new_with_defaults( resident_of_region=True, residency_status=ResidencyStatus.HOMELESS, region="us_ky_allen", ) self.assertEqual(result, expected_result)
def testParsePerson_TakesLastZipCodeMatch(self): # Arrange metadata = IngestMetadata.new_with_defaults(region="us_ky_allen") # 5-digit address could be mistaken for a zip code ingest_person = ingest_info_pb2.Person(place_of_residence="12345 Main 42164") # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.new_with_defaults( resident_of_region=True, residency_status=ResidencyStatus.PERMANENT, region="us_ky_allen", ) self.assertEqual(result, expected_result)
def testParsePerson_NotResidentOfState(self): # Arrange metadata = IngestMetadata.new_with_defaults(region="us_ky") # 10011 is in New York ingest_person = ingest_info_pb2.Person(place_of_residence="123 Main 10011") # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.new_with_defaults( resident_of_region=False, residency_status=ResidencyStatus.PERMANENT, region="us_ky", ) self.assertEqual(result, expected_result)
def testParsePerson_ResidentOfCounty(self): # Arrange metadata = IngestMetadata.new_with_defaults(region="us_ky_allen") # 42164 is in Allen ingest_person = ingest_info_pb2.Person(place_of_residence="123 Main 42164") # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.new_with_defaults( resident_of_region=True, residency_status=ResidencyStatus.PERMANENT, region="us_ky_allen", ) self.assertEqual(result, expected_result)
def testParsePerson_NotResidentOfCounty(self): # Arrange metadata = IngestMetadata.new_with_defaults(region='us_ky_allen') # 40601 is in Frankfort ingest_person = ingest_info_pb2.Person( place_of_residence='123 Main 40601') # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.new_with_defaults( resident_of_region=False, residency_status=ResidencyStatus.PERMANENT, region='us_ky_allen') self.assertEqual(result, expected_result)
def testParsePerson_NoiseInPlaceOfResidence_ParsesResidencyStatus(self): # Arrange metadata = FakeIngestMetadata.for_county( region="us_ky_allen", jurisdiction_id="JURISDICTION_ID") ingest_person = ingest_info_pb2.Person( place_of_residence="transient moves around") # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.new_with_defaults( residency_status=ResidencyStatus.TRANSIENT, region="us_ky_allen", jurisdiction_id="JURISDICTION_ID", ) self.assertEqual(result, expected_result)
def testParsePerson_InfersBirthdateFromAge(self, mock_datetime): # Arrange mock_datetime.now.return_value = _NOW metadata = FakeIngestMetadata.for_county( region="REGION", jurisdiction_id="JURISDICTION_ID") ingest_person = ingest_info_pb2.Person(age="27") # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.new_with_defaults( region="REGION", jurisdiction_id="JURISDICTION_ID", birthdate=datetime(year=_NOW.year - 27, month=1, day=1).date(), birthdate_inferred_from_age=True, ) self.assertEqual(result, expected_result)
def testParsePerson_WithSurnameAndGivenNames_UsesFullNameAsJson(self): # Arrange metadata = IngestMetadata.new_with_defaults() ingest_person = ingest_info_pb2.Person( surname='UNESCAPED,SURNAME"WITH-CHARS"', given_names='GIVEN_NAMES', middle_names='MIDDLE_NAMES') # Act 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.Person.new_with_defaults( full_name=expected_full_name) self.assertEqual(result, expected_result)
def testParsePerson_NotResidentOfCounty(self): # Arrange metadata = FakeIngestMetadata.for_county( region="us_ky_allen", jurisdiction_id="JURISDICTION_ID") # 40601 is in Frankfort ingest_person = ingest_info_pb2.Person( place_of_residence="123 Main 40601") # Act person.copy_fields_to_builder(self.subject, ingest_person, metadata) result = self.subject.build() # Assert expected_result = entities.Person.new_with_defaults( resident_of_region=False, residency_status=ResidencyStatus.PERMANENT, region="us_ky_allen", jurisdiction_id="JURISDICTION_ID", ) self.assertEqual(result, expected_result)