Example #1
0
    def test_add_fine_conflicting_external_id_same_session(self) -> None:
        # Arrange
        db_person = schema.StatePerson(full_name=self.FULL_NAME,
                                       state_code=self.state_code)
        db_fine = schema.StateFine(
            person=db_person,
            status=StateFineStatus.EXTERNAL_UNKNOWN.value,
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_1,
            county_code=self.COUNTY_CODE,
        )
        db_sentence_group = schema.StateSentenceGroup(
            status=StateSentenceStatus.EXTERNAL_UNKNOWN.value,
            external_id=self.EXTERNAL_ID_1,
            state_code=self.state_code,
            county_code=self.COUNTY_CODE,
            fines=[db_fine],
        )
        db_external_id = schema.StatePersonExternalId(
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_1,
            id_type=self.ID_TYPE_1,
        )
        db_person.sentence_groups = [db_sentence_group]
        db_person.external_ids = [db_external_id]

        db_person_dupe = schema.StatePerson(full_name=self.FULL_NAME,
                                            state_code=self.state_code)
        db_fine_dupe = schema.StateFine(
            person=db_person_dupe,
            status=StateFineStatus.EXTERNAL_UNKNOWN.value,
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_1,
            county_code=self.COUNTY_CODE,
        )
        db_sentence_group_dupe = schema.StateSentenceGroup(
            status=StateSentenceStatus.EXTERNAL_UNKNOWN.value,
            external_id=self.EXTERNAL_ID_2,
            state_code=self.state_code,
            county_code=self.COUNTY_CODE,
            fines=[db_fine_dupe],
        )
        db_external_id_dupe = schema.StatePersonExternalId(
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_2,
            id_type=self.ID_TYPE_1,
        )
        db_person_dupe.sentence_groups = [db_sentence_group_dupe]
        db_person_dupe.external_ids = [db_external_id_dupe]

        # Act
        session = SessionFactory.for_schema_base(StateBase)

        session.add(db_fine)
        session.add(db_fine_dupe)
        session.flush()

        with self.assertRaises(sqlalchemy.exc.IntegrityError):
            session.commit()
    def test_removeChildFromEntity(self) -> None:
        fine = schema.StateFine(state_code=_STATE_CODE, fine_id=_ID)
        fine_another = schema.StateFine(state_code=_STATE_CODE, fine_id=_ID_2)
        sentence_group = schema.StateSentenceGroup(
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            state_code=_STATE_CODE,
            fines=[fine, fine_another],
            sentence_group_id=_ID,
        )

        remove_child_from_entity(
            entity=sentence_group,
            child_field_name="fines",
            child_to_remove=fine_another,
        )
        self.assertEqual(sentence_group.fines, [fine])
def generate_fine(person, **kwargs) -> schema.StateFine:
    args = {
        "status": StateFineStatus.PRESENT_WITHOUT_INFO.value,
        "state_code": _STATE_CODE,
    }
    args.update(kwargs)
    return schema.StateFine(person=person, **args)
 def test_getEntityRelationshipFieldNames_foreignKeys(self):
     entity = schema.StateSentenceGroup(fines=[schema.StateFine()],
                                        person=schema.StatePerson(),
                                        person_id=_ID,
                                        sentence_group_id=_ID)
     self.assertEqual({'person_id'},
                      get_set_entity_field_names(
                          entity, EntityFieldType.FOREIGN_KEYS))
 def test_getEntityRelationshipFieldNames_backedges(self):
     entity = schema.StateSentenceGroup(fines=[schema.StateFine()],
                                        person=schema.StatePerson(),
                                        person_id=_ID,
                                        sentence_group_id=_ID)
     self.assertEqual({'person'},
                      get_set_entity_field_names(entity,
                                                 EntityFieldType.BACK_EDGE))
 def test_getDbEntityRelationshipFieldNames_children(self):
     entity = schema.StateSentenceGroup(fines=[schema.StateFine()],
                                        person=schema.StatePerson(),
                                        person_id=_ID,
                                        sentence_group_id=_ID)
     self.assertEqual({'fines'},
                      get_set_entity_field_names(
                          entity, EntityFieldType.FORWARD_EDGE))
 def test_getEntityRelationshipFieldNames_all(self):
     entity = schema.StateSentenceGroup(fines=[schema.StateFine()],
                                        person=schema.StatePerson(),
                                        person_id=_ID,
                                        sentence_group_id=_ID)
     self.assertEqual({'fines', 'person', 'person_id', 'sentence_group_id'},
                      get_set_entity_field_names(entity,
                                                 EntityFieldType.ALL))
def generate_test_fine(person_id) -> state_schema.StateFine:
    instance = state_schema.StateFine(
        fine_id=3333,
        status=StateFineStatus.PAID.value,
        state_code='us_ca',
        person_id=person_id,
    )

    return instance
 def test_isPlaceholder(self):
     entity = schema.StateSentenceGroup(
         status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value,
         state_code=_STATE_CODE,
         fines=[schema.StateFine()],
         person=schema.StatePerson(),
         sentence_group_id=_ID)
     self.assertTrue(is_placeholder(entity))
     entity.county_code = 'county_code'
     self.assertFalse(is_placeholder(entity))
Example #10
0
 def test_getEntityRelationshipFieldNames_backedges(self) -> None:
     entity = schema.StateSentenceGroup(
         state_code="US_XX",
         fines=[schema.StateFine()],
         person=schema.StatePerson(),
         person_id=_ID,
         sentence_group_id=_ID,
     )
     self.assertEqual(
         {"person"}, get_set_entity_field_names(entity, EntityFieldType.BACK_EDGE)
     )
Example #11
0
 def test_getDbEntityRelationshipFieldNames_children(self) -> None:
     entity = schema.StateSentenceGroup(
         state_code="US_XX",
         fines=[schema.StateFine()],
         person=schema.StatePerson(),
         person_id=_ID,
         sentence_group_id=_ID,
     )
     self.assertEqual(
         {"fines"}, get_set_entity_field_names(entity, EntityFieldType.FORWARD_EDGE)
     )
Example #12
0
 def test_getEntityRelationshipFieldNames_foreignKeys(self) -> None:
     entity = schema.StateSentenceGroup(
         state_code="US_XX",
         fines=[schema.StateFine()],
         person=schema.StatePerson(),
         person_id=_ID,
         sentence_group_id=_ID,
     )
     self.assertEqual(
         {"person_id"},
         get_set_entity_field_names(entity, EntityFieldType.FOREIGN_KEYS),
     )
Example #13
0
 def test_getEntityRelationshipFieldNames_all(self) -> None:
     entity = schema.StateSentenceGroup(
         state_code="US_XX",
         fines=[schema.StateFine()],
         person=schema.StatePerson(),
         person_id=_ID,
         sentence_group_id=_ID,
     )
     self.assertEqual(
         {"state_code", "fines", "person", "person_id", "sentence_group_id"},
         get_set_entity_field_names(entity, EntityFieldType.ALL),
     )
    def test_addChildToEntity(self) -> None:
        fine = schema.StateFine(state_code=_STATE_CODE, fine_id=_ID)
        sentence_group = schema.StateSentenceGroup(
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            state_code=_STATE_CODE,
            fines=[],
            sentence_group_id=_ID,
        )

        add_child_to_entity(entity=sentence_group,
                            child_field_name="fines",
                            child_to_add=fine)
        self.assertEqual(sentence_group.fines, [fine])
    def test_generateChildEntitiesWithAncestorChain(self) -> None:
        fine = schema.StateFine(state_code=_STATE_CODE, fine_id=_ID)
        fine_another = schema.StateFine(state_code=_STATE_CODE, fine_id=_ID_2)
        person = schema.StatePerson(state_code=_STATE_CODE, person_id=_ID)
        sentence_group = schema.StateSentenceGroup(
            status=StateSentenceStatus.PRESENT_WITHOUT_INFO,
            state_code=_STATE_CODE,
            fines=[fine, fine_another],
            person=person,
            sentence_group_id=_ID,
        )
        sentence_group_tree = EntityTree(entity=sentence_group,
                                         ancestor_chain=[person])

        expected_child_trees = [
            EntityTree(entity=fine, ancestor_chain=[person, sentence_group]),
            EntityTree(entity=fine_another,
                       ancestor_chain=[person, sentence_group]),
        ]

        self.assertEqual(
            expected_child_trees,
            generate_child_entity_trees("fines", [sentence_group_tree]),
        )
Example #16
0
    def test_add_fine_different_external_id_same_state(self) -> None:
        # Arrange
        arrange_session = SessionFactory.for_schema_base(StateBase)
        db_person = schema.StatePerson(person_id=self._ID_1,
                                       full_name=self.FULL_NAME,
                                       state_code=self.state_code)
        db_fine = schema.StateFine(
            person=db_person,
            status=StateFineStatus.EXTERNAL_UNKNOWN.value,
            fine_id=self._ID_1,
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_1,
            county_code=self.COUNTY_CODE,
        )
        db_sentence_group = schema.StateSentenceGroup(
            sentence_group_id=self._ID_1,
            status=StateSentenceStatus.EXTERNAL_UNKNOWN.value,
            external_id=self.EXTERNAL_ID_1,
            state_code=self.state_code,
            county_code=self.COUNTY_CODE,
            fines=[db_fine],
        )
        db_external_id = schema.StatePersonExternalId(
            person_external_id_id=self._ID_1,
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_1,
            id_type=self.ID_TYPE_1,
        )
        db_person.sentence_groups = [db_sentence_group]
        db_person.external_ids = [db_external_id]

        arrange_session.add(db_fine)
        arrange_session.commit()

        db_person_dupe = schema.StatePerson(person_id=self._ID_2,
                                            full_name=self.FULL_NAME,
                                            state_code=self.state_code)
        db_fine_dupe = schema.StateFine(
            person=db_person_dupe,
            status=StateFineStatus.EXTERNAL_UNKNOWN.value,
            fine_id=self._ID_2,
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_2,
            county_code=self.COUNTY_CODE,
        )
        db_sentence_group_dupe = schema.StateSentenceGroup(
            sentence_group_id=self._ID_2,
            status=StateSentenceStatus.EXTERNAL_UNKNOWN.value,
            external_id=self.EXTERNAL_ID_2,
            state_code=self.state_code,
            county_code=self.COUNTY_CODE,
            fines=[db_fine_dupe],
        )
        db_external_id_dupe = schema.StatePersonExternalId(
            person_external_id_id=self._ID_2,
            state_code=self.state_code,
            external_id=self.EXTERNAL_ID_2,
            id_type=self.ID_TYPE_1,
        )
        db_person_dupe.sentence_groups = [db_sentence_group_dupe]
        db_person_dupe.external_ids = [db_external_id_dupe]

        # Act
        session = SessionFactory.for_schema_base(StateBase)

        session.add(db_fine_dupe)
        session.flush()
        session.commit()
Example #17
0
    def test_add_fine_conflicting_external_id_different_state(self) -> None:
        # Arrange
        with SessionFactory.using_database(
                self.database_key) as arrange_session:
            db_person = schema.StatePerson(
                person_id=self._ID_1,
                full_name=self.FULL_NAME,
                state_code=self.state_code,
            )
            db_fine = schema.StateFine(
                person=db_person,
                status=StateFineStatus.EXTERNAL_UNKNOWN.value,
                fine_id=self._ID_1,
                state_code=self.state_code,
                external_id=self.EXTERNAL_ID_1,
                county_code=self.COUNTY_CODE,
            )
            db_sentence_group = schema.StateSentenceGroup(
                sentence_group_id=self._ID_1,
                status=StateSentenceStatus.EXTERNAL_UNKNOWN.value,
                external_id=self.EXTERNAL_ID_1,
                state_code=self.state_code,
                county_code=self.COUNTY_CODE,
                fines=[db_fine],
            )
            db_external_id = schema.StatePersonExternalId(
                person_external_id_id=self._ID_1,
                state_code=self.state_code,
                external_id=self.EXTERNAL_ID_1,
                id_type=self.ID_TYPE_1,
            )
            db_person.sentence_groups = [db_sentence_group]
            db_person.external_ids = [db_external_id]

            arrange_session.add(db_fine)

        db_person_dupe = schema.StatePerson(
            person_id=self._ID_2,
            full_name=self.FULL_NAME,
            state_code=self.OTHER_STATE_CODE,
        )
        db_fine_dupe = schema.StateFine(
            person=db_person_dupe,
            status=StateFineStatus.EXTERNAL_UNKNOWN.value,
            fine_id=self._ID_2,
            state_code=self.OTHER_STATE_CODE,
            external_id=self.EXTERNAL_ID_1,
            county_code=self.COUNTY_CODE,
        )
        db_sentence_group_dupe = schema.StateSentenceGroup(
            sentence_group_id=self._ID_2,
            status=StateSentenceStatus.EXTERNAL_UNKNOWN.value,
            external_id=self.EXTERNAL_ID_2,
            state_code=self.OTHER_STATE_CODE,
            county_code=self.COUNTY_CODE,
            fines=[db_fine_dupe],
        )
        db_external_id_dupe = schema.StatePersonExternalId(
            person_external_id_id=self._ID_2,
            state_code=self.OTHER_STATE_CODE,
            external_id=self.EXTERNAL_ID_2,
            id_type=self.ID_TYPE_1,
        )
        db_person_dupe.sentence_groups = [db_sentence_group_dupe]
        db_person_dupe.external_ids = [db_external_id_dupe]

        # Act
        with SessionFactory.using_database(self.database_key) as session:
            session.add(db_fine_dupe)
            session.flush()