def testParseSentence_completionDateWithoutStatus_marksAsCompleted(self):
        # Arrange
        ingest_sentence = ingest_info_pb2.Sentence(
            sentence_id="SENTENCE_ID",
            completion_date="2018-1-1",
        )

        # Act
        sentence_builder = entities.Sentence.builder()
        sentence.copy_fields_to_builder(
            sentence_builder,
            ingest_sentence,
            FakeIngestMetadata.for_county(
                region="REGION",
                ingest_time=datetime.datetime(year=2018, month=6, day=8),
            ),
        )
        result = sentence_builder.build()

        # Assert
        expected_result = entities.Sentence.new_with_defaults(
            external_id="SENTENCE_ID",
            completion_date=datetime.date(year=2018, month=1, day=1),
            status=SentenceStatus.COMPLETED,
        )

        self.assertEqual(result, expected_result)
Exemple #2
0
    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)
Exemple #3
0
    def testParseBooking_releaseDateInFuture_setAsProjected(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(region="REGION",
                                                 ingest_time=_INGEST_TIME)

        ingest_booking = ingest_info_pb2.Booking(
            booking_id="BOOKING_ID",
            admission_date="2/3/1000",
            release_date="1/2/2020",
        )

        # Act
        booking.copy_fields_to_builder(self.subject, ingest_booking, metadata)
        result = self.subject.build()

        # Assert
        expected_result = entities.Booking.new_with_defaults(
            external_id="BOOKING_ID",
            admission_date=date(year=1000, month=2, day=3),
            admission_date_inferred=False,
            release_date=None,
            release_date_inferred=None,
            projected_release_date=date(year=2020, month=1, day=2),
            custody_status=CustodyStatus.PRESENT_WITHOUT_INFO,
            last_seen_time=_INGEST_TIME,
            first_seen_time=_INGEST_TIME,
        )

        self.assertEqual(result, expected_result)
Exemple #4
0
    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 = FakeIngestMetadata.for_county(
            region="REGION",
            jurisdiction_id="JURISDICTION_ID",
            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(
            region="REGION",
            jurisdiction_id="JURISDICTION_ID",
            ethnicity=Ethnicity.HISPANIC,
            race_raw_text="HISPANIC",
        )

        self.assertEqual(result, expected_result)
Exemple #5
0
    def testParseCharge_MapAcrossFields(self):
        # Arrange
        overrides_builder = EnumOverrides.Builder()
        overrides_builder.add("FELONY", ChargeClass.FELONY, ChargeDegree)
        overrides_builder.add("FIRST DEGREE", ChargeDegree.FIRST, ChargeClass)
        metadata = FakeIngestMetadata.for_county(
            region="REGION", enum_overrides=overrides_builder.build()
        )
        ingest_charge = ingest_info_pb2.Charge(
            charge_class="first degree", degree="felony"
        )

        # Act
        charge.copy_fields_to_builder(self.subject, ingest_charge, metadata)
        result = self.subject.build()

        # Assert
        expected_result = entities.Charge.new_with_defaults(
            degree=ChargeDegree.FIRST,
            degree_raw_text="FELONY",
            charge_class=ChargeClass.FELONY,
            class_raw_text="FIRST DEGREE",
            status=ChargeStatus.PRESENT_WITHOUT_INFO,
        )
        self.assertEqual(result, expected_result)
    def testParseSentence(self):
        # Arrange
        ingest_sentence = ingest_info_pb2.Sentence(
            sentence_id="SENTENCE_ID",
            min_length="1",
            date_imposed="2000-1-1",
            post_release_supervision_length="",
        )

        # Act
        sentence_builder = entities.Sentence.builder()
        sentence.copy_fields_to_builder(
            sentence_builder,
            ingest_sentence,
            FakeIngestMetadata.for_county(
                region="REGION",
                ingest_time=datetime.datetime(year=2018, month=6, day=8),
            ),
        )
        result = sentence_builder.build()

        # Assert
        expected_result = entities.Sentence.new_with_defaults(
            external_id="SENTENCE_ID",
            min_length_days=1,
            post_release_supervision_length_days=0,
            date_imposed=datetime.date(year=2000, month=1, day=1),
            status=SentenceStatus.PRESENT_WITHOUT_INFO,
        )

        self.assertEqual(result, expected_result)
    def testConvert_PersonInferredBooking(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(
            region="REGION",
            jurisdiction_id="JURISDICTION_ID",
            ingest_time=_INGEST_TIME)

        ingest_info = IngestInfo()
        ingest_info.people.add()

        # Act
        result = self._convert_and_throw_on_errors(ingest_info, metadata)

        # Assert
        expected_result = [
            Person.new_with_defaults(
                region="REGION",
                jurisdiction_id="JURISDICTION_ID",
                bookings=[
                    Booking.new_with_defaults(
                        admission_date_inferred=True,
                        first_seen_time=_INGEST_TIME,
                        last_seen_time=_INGEST_TIME,
                        admission_date=_INGEST_TIME.date(),
                        custody_status=CustodyStatus.PRESENT_WITHOUT_INFO,
                    )
                ],
            )
        ]

        self.assertEqual(result, expected_result)
Exemple #8
0
    def testParsesPerson(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(
            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)
Exemple #9
0
    def testParseBooking_MapAcrossFields(self):
        # Arrange
        overrides_builder = EnumOverrides.Builder()
        overrides_builder.add("WORK RELEASE", Classification.WORK_RELEASE,
                              AdmissionReason)
        overrides_builder.add("transfer", AdmissionReason.TRANSFER,
                              CustodyStatus)
        metadata = FakeIngestMetadata.for_county(
            region="REGION",
            ingest_time=_INGEST_TIME,
            enum_overrides=overrides_builder.build(),
        )
        ingest_booking = ingest_info_pb2.Booking(
            admission_reason="work release", custody_status="transfer")

        # Act
        booking.copy_fields_to_builder(self.subject, ingest_booking, metadata)
        result = self.subject.build()

        # Assert
        expected_result = entities.Booking.new_with_defaults(
            admission_date=_INGEST_TIME.date(),
            admission_reason=AdmissionReason.TRANSFER,
            admission_reason_raw_text="WORK RELEASE",
            admission_date_inferred=True,
            custody_status=CustodyStatus.PRESENT_WITHOUT_INFO,
            custody_status_raw_text="TRANSFER",
            classification=Classification.WORK_RELEASE,
            last_seen_time=_INGEST_TIME,
            first_seen_time=_INGEST_TIME,
        )
        self.assertEqual(result, expected_result)
Exemple #10
0
    def testParseBooking_facilityId(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(
            region="REGION",
            ingest_time=_INGEST_TIME,
        )

        ingest_booking = ingest_info_pb2.Booking(
            facility_id="1234567890123456")

        # Act
        booking.copy_fields_to_builder(self.subject, ingest_booking, metadata)
        result = self.subject.build()

        # Assert
        expected_result = entities.Booking.new_with_defaults(
            facility_id="1234567890123456",
            admission_date=_INGEST_TIME.date(),
            admission_date_inferred=True,
            last_seen_time=_INGEST_TIME,
            first_seen_time=_INGEST_TIME,
            release_date=None,
            release_date_inferred=None,
            custody_status=CustodyStatus.PRESENT_WITHOUT_INFO,
        )

        self.assertEqual(result, expected_result)
Exemple #11
0
    def testParseBooking_releaseDateWithoutStatus_marksAsReleased(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(region="REGION",
                                                 ingest_time=_INGEST_TIME)

        ingest_booking = ingest_info_pb2.Booking(
            booking_id="BOOKING_ID",
            release_date="2/1/2019",
        )

        # Act
        booking.copy_fields_to_builder(self.subject, ingest_booking, metadata)
        result = self.subject.build()

        # Assert
        expected_result = entities.Booking.new_with_defaults(
            external_id="BOOKING_ID",
            admission_date=_INGEST_TIME.date(),
            admission_date_inferred=True,
            release_date=date(year=2019, month=2, day=1),
            release_date_inferred=False,
            custody_status=CustodyStatus.RELEASED,
            last_seen_time=_INGEST_TIME,
            first_seen_time=_INGEST_TIME,
        )

        self.assertEqual(result, expected_result)
Exemple #12
0
    def testCompletionDateInFuture(self):
        # Arrange
        ingest_sentence = ingest_info_pb2.Sentence(
            sentence_id="SENTENCE_ID",
            completion_date="2020-1-1",
        )

        # Act
        sentence_builder = entities.Sentence.builder()
        sentence.copy_fields_to_builder(
            sentence_builder,
            ingest_sentence,
            FakeIngestMetadata.for_county(
                region="REGION",
                ingest_time=datetime.datetime(year=2018, month=6, day=8),
            ),
        )
        result = sentence_builder.build()

        # Assert
        expected_result = entities.Sentence.new_with_defaults(
            external_id="SENTENCE_ID",
            completion_date=None,
            projected_completion_date=datetime.date(year=2020, month=1, day=1),
            status=SentenceStatus.PRESENT_WITHOUT_INFO,
        )

        self.assertEqual(result, expected_result)
Exemple #13
0
    def testParsePerson_WithSurnameAndFullname_ThrowsException(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(region="REGION")
        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 testConvert_CannotConvertField_RaisesValueError(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(region="REGION")

        ingest_info = IngestInfo()
        ingest_info.people.add(birthdate="NOT_A_DATE")

        # Act + Assert
        with self.assertRaises(ValueError):
            self._convert_and_throw_on_errors(ingest_info, metadata)
Exemple #15
0
    def testParseBooking_facilityId_doesNotExist(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(
            region="REGION",
            ingest_time=_INGEST_TIME,
        )

        ingest_booking = ingest_info_pb2.Booking(
            facility_id="1234567890123456"  # invalid FID
        )

        with self.assertRaises(ValueError):
            booking.copy_fields_to_builder(self.subject, ingest_booking,
                                           metadata)
    def testConvert_TotalBondWithCharge_SetsTotalBondOnCharge(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(
            region="REGION",
            jurisdiction_id="JURISDICTION_ID",
            ingest_time=_INGEST_TIME)

        ingest_info = IngestInfo()
        ingest_info.people.add(booking_ids=["BOOKING_ID"])
        ingest_info.bookings.add(booking_id="BOOKING_ID",
                                 total_bond_amount="$100",
                                 charge_ids=["CHARGE_ID"])
        ingest_info.charges.add(charge_id="CHARGE_ID")

        # Act
        result = self._convert_and_throw_on_errors(ingest_info, metadata)

        # Assert
        expected_result = [
            Person.new_with_defaults(
                region="REGION",
                jurisdiction_id="JURISDICTION_ID",
                bookings=[
                    Booking.new_with_defaults(
                        external_id="BOOKING_ID",
                        admission_date=_INGEST_TIME.date(),
                        admission_date_inferred=True,
                        first_seen_time=_INGEST_TIME,
                        last_seen_time=_INGEST_TIME,
                        custody_status=CustodyStatus.PRESENT_WITHOUT_INFO,
                        charges=[
                            Charge.new_with_defaults(
                                external_id="CHARGE_ID_COUNT_1",
                                status=ChargeStatus.PRESENT_WITHOUT_INFO,
                                bond=Bond.new_with_defaults(
                                    amount_dollars=100,
                                    status=BondStatus.PRESENT_WITHOUT_INFO,
                                    bond_type=BondType.CASH,
                                ),
                            )
                        ],
                    )
                ],
            )
        ]

        self.assertEqual(result, expected_result)
Exemple #17
0
    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 testConvert_MultipleOpenBookings_RaisesValueError(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(
            region="REGION",
            jurisdiction_id="JURISDICTION_ID",
            ingest_time=_INGEST_TIME)

        ingest_info = IngestInfo()
        ingest_info.people.add(booking_ids=["BOOKING_ID1", "BOOKING_ID2"])
        ingest_info.bookings.add(booking_id="BOOKING_ID1",
                                 admission_date="3/14/2020")
        ingest_info.bookings.add(booking_id="BOOKING_ID2",
                                 admission_date="3/16/2020")

        # Act + Assert
        with self.assertRaises(ValueError):
            self._convert_and_throw_on_errors(ingest_info, metadata)
Exemple #19
0
    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)
Exemple #20
0
    def testParsePerson_ResidenceAndStatusCombined(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(
            region="us_ky_allen", jurisdiction_id="JURISDICTION_ID")
        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",
            jurisdiction_id="JURISDICTION_ID",
        )

        self.assertEqual(result, expected_result)
Exemple #21
0
    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)
Exemple #22
0
    def testParseBooking(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(region="REGION",
                                                 ingest_time=_INGEST_TIME)

        ingest_booking = ingest_info_pb2.Booking(
            booking_id="BOOKING_ID",
            admission_date="2/3/1000",
            admission_reason="New Commitment",
            release_date="1/2/2017",
            projected_release_date="5/20/2020",
            release_reason="Transfer",
            custody_status="Held Elsewhere",
            classification="Low",
        )

        # Act
        booking.copy_fields_to_builder(self.subject, ingest_booking, metadata)
        result = self.subject.build()

        # Assert
        expected_result = entities.Booking.new_with_defaults(
            external_id="BOOKING_ID",
            admission_date=date(year=1000, month=2, day=3),
            admission_reason=AdmissionReason.NEW_COMMITMENT,
            admission_reason_raw_text="NEW COMMITMENT",
            admission_date_inferred=False,
            release_date=date(year=2017, month=1, day=2),
            release_date_inferred=False,
            projected_release_date=date(year=2020, month=5, day=20),
            release_reason=ReleaseReason.TRANSFER,
            release_reason_raw_text="TRANSFER",
            custody_status=CustodyStatus.HELD_ELSEWHERE,
            custody_status_raw_text="HELD ELSEWHERE",
            classification=Classification.LOW,
            classification_raw_text="LOW",
            last_seen_time=_INGEST_TIME,
            first_seen_time=_INGEST_TIME,
        )

        self.assertEqual(result, expected_result)
Exemple #23
0
    def testParsePerson_NotResidentOfState(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(
            region="us_ky", jurisdiction_id="JURISDICTION_ID")
        # 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",
            jurisdiction_id="JURISDICTION_ID",
        )

        self.assertEqual(result, expected_result)
Exemple #24
0
    def testParsePerson_TakesLastZipCodeMatch(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(
            region="us_ky_allen", jurisdiction_id="JURISDICTION_ID")
        # 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",
            jurisdiction_id="JURISDICTION_ID",
        )

        self.assertEqual(result, expected_result)
    def testConvert_TotalBondWithMultipleBonds_ThrowsException(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(
            region="REGION",
            jurisdiction_id="JURISDICTION_ID",
            ingest_time=_INGEST_TIME)

        ingest_info = IngestInfo()
        ingest_info.people.add(booking_ids=["BOOKING_ID"])
        ingest_info.bookings.add(
            booking_id="BOOKING_ID",
            total_bond_amount="$100",
            charge_ids=["CHARGE_ID", "CHARGE_ID_2"],
        )
        ingest_info.charges.add(charge_id="CHARGE_ID", bond_id="BOND_ID")
        ingest_info.charges.add(charge_id="CHARGE_ID_2", bond_id="BOND_ID_2")
        ingest_info.bonds.add(bond_id="BOND_ID")
        ingest_info.bonds.add(bond_id="BOND_ID_2")

        # Act + Assert
        with self.assertRaises(ValueError):
            self._convert_and_throw_on_errors(ingest_info, metadata)
    def testConvert_ExternalId_ClearPII(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(
            region="REGION",
            jurisdiction_id=_JURISDICTION_ID,
            ingest_time=_INGEST_TIME)

        ingest_info = IngestInfo()
        ingest_info.people.add(person_id="PERSON_ID",
                               full_name="full_name",
                               booking_ids=["BOOKING_ID"])
        ingest_info.bookings.add(booking_id="BOOKING_ID",
                                 admission_date=str(_RELEASE_DATE))
        # Act
        result = self._convert_and_throw_on_errors(ingest_info, metadata)

        # Assert
        expected_result = [
            Person.new_with_defaults(
                external_id="PERSON_ID",
                region="REGION",
                jurisdiction_id="JURISDICTION_ID",
                bookings=[
                    Booking.new_with_defaults(
                        external_id="BOOKING_ID",
                        admission_date=_RELEASE_DATE,
                        admission_date_inferred=False,
                        custody_status=CustodyStatus.PRESENT_WITHOUT_INFO,
                        first_seen_time=_INGEST_TIME,
                        last_seen_time=_INGEST_TIME,
                    )
                ],
            )
        ]

        self.assertEqual(result, expected_result)
Exemple #27
0
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
# =============================================================================
"""Tests for converting bonds."""
import unittest

import attr

from recidiviz.common.constants.bond import BondType, BondStatus
from recidiviz.common.constants.enum_overrides import EnumOverrides
from recidiviz.ingest.models import ingest_info_pb2
from recidiviz.persistence.entity.county import entities
from recidiviz.persistence.ingest_info_converter.county.entity_helpers import bond
from recidiviz.tests.persistence.database.database_test_utils import FakeIngestMetadata

_EMPTY_METADATA = FakeIngestMetadata.for_county("us_xx_county")


class BondConverterTest(unittest.TestCase):
    """Tests for converting bonds."""
    def testParseBond(self):
        # Arrange
        ingest_bond = ingest_info_pb2.Bond(
            bond_id="BOND_ID",
            bond_type="CASH",
            amount="$125.00",
            status="ACTIVE",
            bond_agent="AGENT",
        )

        # Act
    def testConvert_FullIngestInfo(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(
            region="REGION",
            jurisdiction_id=_JURISDICTION_ID,
            ingest_time=_INGEST_TIME,
            facility_id=_FACILITY_ID,
        )

        ingest_info = IngestInfo()
        ingest_info.people.add(person_id="PERSON_ID",
                               booking_ids=["BOOKING_ID"])
        ingest_info.bookings.add(booking_id="BOOKING_ID",
                                 arrest_id="ARREST_ID",
                                 charge_ids=["CHARGE_ID"])
        ingest_info.arrests.add(arrest_id="ARREST_ID", agency="PD")
        ingest_info.charges.add(
            charge_id="CHARGE_ID",
            name="DUI",
            bond_id="BOND_ID",
            sentence_id="SENTENCE_ID",
        )
        ingest_info.bonds.add(bond_id="BOND_ID")
        ingest_info.sentences.add(sentence_id="SENTENCE_ID", is_life="True")

        # Act
        result = self._convert_and_throw_on_errors(ingest_info, metadata)

        # Assert
        expected_result = [
            Person.new_with_defaults(
                external_id="PERSON_ID",
                region="REGION",
                jurisdiction_id="JURISDICTION_ID",
                bookings=[
                    Booking.new_with_defaults(
                        external_id="BOOKING_ID",
                        facility_id=_FACILITY_ID,
                        admission_date=_INGEST_TIME.date(),
                        admission_date_inferred=True,
                        first_seen_time=_INGEST_TIME,
                        last_seen_time=_INGEST_TIME,
                        custody_status=CustodyStatus.PRESENT_WITHOUT_INFO,
                        arrest=Arrest.new_with_defaults(
                            external_id="ARREST_ID", agency="PD"),
                        charges=[
                            Charge.new_with_defaults(
                                external_id="CHARGE_ID_COUNT_1",
                                status=ChargeStatus.PRESENT_WITHOUT_INFO,
                                name="DUI",
                                bond=Bond.new_with_defaults(
                                    external_id="BOND_ID",
                                    status=BondStatus.PRESENT_WITHOUT_INFO,
                                ),
                                sentence=Sentence.new_with_defaults(
                                    status=SentenceStatus.PRESENT_WITHOUT_INFO,
                                    external_id="SENTENCE_ID",
                                    is_life=True,
                                ),
                            )
                        ],
                    )
                ],
            )
        ]

        self.assertEqual(result, expected_result)
    def testConvert_MultipleCountsOfCharge_CreatesDuplicateCharges(self):
        # Arrange
        metadata = FakeIngestMetadata.for_county(
            region="REGION",
            jurisdiction_id="JURISDICTION_ID",
            ingest_time=_INGEST_TIME)

        ingest_info = IngestInfo()
        ingest_info.people.add(booking_ids=["BOOKING_ID"])
        ingest_info.bookings.add(booking_id="BOOKING_ID",
                                 charge_ids=["CHARGE_ID"])
        ingest_info.charges.add(
            charge_id="CHARGE_ID",
            name="CHARGE_NAME",
            number_of_counts="3",
            bond_id="BOND_ID",
        )
        ingest_info.bonds.add(bond_id="BOND_ID")

        # Act
        result = self._convert_and_throw_on_errors(ingest_info, metadata)

        # Assert
        expected_duplicate_charge = Charge.new_with_defaults(
            external_id="CHARGE_ID_COUNT_1",
            status=ChargeStatus.PRESENT_WITHOUT_INFO,
            name="CHARGE_NAME",
        )
        expected_bond = Bond.new_with_defaults(
            external_id="BOND_ID", status=BondStatus.PRESENT_WITHOUT_INFO)
        expected_result = [
            Person.new_with_defaults(
                region="REGION",
                jurisdiction_id="JURISDICTION_ID",
                bookings=[
                    Booking.new_with_defaults(
                        external_id="BOOKING_ID",
                        last_seen_time=_INGEST_TIME,
                        first_seen_time=_INGEST_TIME,
                        admission_date_inferred=True,
                        admission_date=_INGEST_TIME.date(),
                        custody_status=CustodyStatus.PRESENT_WITHOUT_INFO,
                        charges=[
                            attr.evolve(
                                expected_duplicate_charge,
                                external_id="CHARGE_ID_COUNT_1",
                                bond=expected_bond,
                            ),
                            attr.evolve(
                                expected_duplicate_charge,
                                external_id="CHARGE_ID_COUNT_2",
                                bond=expected_bond,
                            ),
                            attr.evolve(
                                expected_duplicate_charge,
                                external_id="CHARGE_ID_COUNT_3",
                                bond=expected_bond,
                            ),
                        ],
                    )
                ],
            )
        ]

        self.assertEqual(result, expected_result)

        # Assert that the expanded charges, while containing duplicate
        # information, are actually different objects.

        # For some reason, pylint is having trouble identifying that you can
        # index into the result list even though the revealed type is a list.
        # pylint: disable=unsubscriptable-object
        result_expanded_charges = result[0].bookings[0].charges

        charges_grouped_by_id = list(
            itertools.groupby(result_expanded_charges, key=id))
        self.assertEqual(len(result_expanded_charges),
                         len(charges_grouped_by_id))

        # Assert that the bond belonging to each charge is the same object.
        bonds = [charge.bond for charge in result_expanded_charges]
        self.assertTrue(all(bond is bonds[0] for bond in bonds))
Exemple #30
0
    schema_entity_converter as converter, )
from recidiviz.persistence.database.schema_utils import SchemaType
from recidiviz.persistence.database.session_factory import SessionFactory
from recidiviz.persistence.database.sqlalchemy_database_key import SQLAlchemyDatabaseKey
from recidiviz.persistence.entity.county import entities
from recidiviz.tests.persistence.database.database_test_utils import FakeIngestMetadata
from recidiviz.tests.utils import fakes

_REGION = "region"
_REGION_ANOTHER = "wrong region"
_JURISDICTION_ID = "12345678"
_FULL_NAME = "full_name"
_EXTERNAL_ID = "external_id"
_BIRTHDATE = datetime.date(year=2012, month=1, day=2)
_FACILITY = "facility"
_DEFAULT_METADATA = FakeIngestMetadata.for_county(region="default_region")

DATE_SCRAPED = datetime.date(year=2019, month=1, day=1)


class TestDao(TestCase):
    """Test that the methods in dao.py correctly read from the SQL database."""
    def setUp(self) -> None:
        self.database_key = SQLAlchemyDatabaseKey.for_schema(SchemaType.JAILS)
        fakes.use_in_memory_sqlite_database(self.database_key)

    def tearDown(self) -> None:
        fakes.teardown_in_memory_sqlite_databases()

    def test_readPeopleWithOpenBookingsBeforeDate(self):
        # Arrange