def test_mergeFlatFields_twoDbEntities(self) -> None: to_entity = schema.StateSentenceGroup( state_code=_STATE_CODE, sentence_group_id=_ID, county_code="county_code", status=StateSentenceStatus.SERVING, ) from_entity = schema.StateSentenceGroup( state_code=_STATE_CODE, sentence_group_id=_ID_2, county_code="county_code-updated", max_length_days=10, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, ) expected_entity = schema.StateSentenceGroup( state_code=_STATE_CODE, sentence_group_id=_ID, county_code="county_code-updated", max_length_days=10, status=StateSentenceStatus.SERVING.value, ) merged_entity = default_merge_flat_fields(new_entity=from_entity, old_entity=to_entity) self.assert_schema_objects_equal(expected_entity, merged_entity)
def test_readPersons_ndSpecific(self): schema_person = schema.StatePerson(person_id=1) schema_sentence_group = schema.StateSentenceGroup( sentence_group_id=1, external_id=_EXTERNAL_ID, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code='US_ND') schema_sentence_group_2 = schema.StateSentenceGroup( sentence_group_id=2, external_id=_EXTERNAL_ID_2, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code='US_ND') schema_person.sentence_groups = [ schema_sentence_group, schema_sentence_group_2 ] schema_person_2 = schema.StatePerson(person_id=2) session = SessionFactory.for_schema_base(StateBase) session.add(schema_person) session.add(schema_person_2) session.commit() ingested_sentence_group = StateSentenceGroup.new_with_defaults( state_code='us_nd', external_id=_EXTERNAL_ID) ingested_person = StatePerson.new_with_defaults( sentence_groups=[ingested_sentence_group]) expected_people = StateSchemaToEntityConverter().convert_all( [schema_person], populate_back_edges=False) people = read_persons(session, 'us_nd', [ingested_person]) self.assertCountEqual(expected_people, people)
def test_getExternalIdsOfCls(self): supervision_sentence = schema.StateSupervisionSentence( external_id=_EXTERNAL_ID) supervision_sentence_2 = schema.StateSupervisionSentence( external_id=_EXTERNAL_ID_2) supervision_sentence_3 = schema.StateSupervisionSentence( external_id=_EXTERNAL_ID_3) sentence_group = schema.StateSentenceGroup(external_id=_EXTERNAL_ID, supervision_sentences=[ supervision_sentence, supervision_sentence_2 ]) sentence_group_2 = schema.StateSentenceGroup( external_id=_EXTERNAL_ID_2, supervision_sentences=[ supervision_sentence_2, supervision_sentence_3 ]) external_id = schema.StatePersonExternalId(external_id=_EXTERNAL_ID) person = schema.StatePerson( external_ids=[external_id], sentence_groups=[sentence_group, sentence_group_2]) self.assertCountEqual([_EXTERNAL_ID, _EXTERNAL_ID_2, _EXTERNAL_ID_3], get_external_ids_of_cls( [person], schema.StateSupervisionSentence)) self.assertCountEqual([_EXTERNAL_ID, _EXTERNAL_ID_2], get_external_ids_of_cls( [person], schema.StateSentenceGroup)) self.assertCountEqual([_EXTERNAL_ID], get_external_ids_of_cls([person], schema.StatePerson))
def test_readPeopleByRootExternalIds_SentenceGroupExternalId(self) -> None: # Arrange person = schema.StatePerson(person_id=1, state_code=_STATE_CODE) sentence_group = schema.StateSentenceGroup( sentence_group_id=1, external_id=_EXTERNAL_ID, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person, ) sentence_group_2 = schema.StateSentenceGroup( sentence_group_id=2, external_id=_EXTERNAL_ID2, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person, ) person.sentence_groups = [sentence_group, sentence_group_2] session = SessionFactory.for_schema_base(StateBase) session.add(person) session.commit() # Act people = dao.read_people_by_cls_external_ids(session, _STATE_CODE, schema.StateSentenceGroup, [_EXTERNAL_ID]) # Assert expected_people = [person] self.assertCountEqual(people, expected_people)
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_readPersonsByRootEntityCls(self) -> None: schema_person_with_root_entity = schema.StatePerson( person_id=1, state_code=_STATE_CODE) schema_sentence_group = schema.StateSentenceGroup( sentence_group_id=_ID, external_id=_EXTERNAL_ID, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, ) schema_sentence_group_2 = schema.StateSentenceGroup( sentence_group_id=_ID_2, external_id=_EXTERNAL_ID_2, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, ) schema_person_with_root_entity.sentence_groups = [ schema_sentence_group, schema_sentence_group_2, ] placeholder_schema_person = schema.StatePerson(person_id=_ID_2, state_code=_STATE_CODE) schema_person_other_state = schema.StatePerson(person_id=_ID_3, state_code=_STATE_CODE) schema_external_id_other_state = schema.StatePersonExternalId( person_external_id_id=_ID_2, external_id=_ID, id_type=_ID_TYPE, state_code=_STATE_CODE, ) schema_person_other_state.external_ids = [ schema_external_id_other_state ] with SessionFactory.using_database(self.database_key, autocommit=False) as session: session.add(schema_person_with_root_entity) session.add(placeholder_schema_person) session.add(schema_person_other_state) session.commit() ingested_sentence_group = schema.StateSentenceGroup( state_code=_STATE_CODE, external_id=_EXTERNAL_ID) ingested_person = schema.StatePerson( sentence_groups=[ingested_sentence_group]) expected_people = [ schema_person_with_root_entity, placeholder_schema_person, ] people = read_persons_by_root_entity_cls( session, _STATE_CODE, [ingested_person], allowed_root_entity_classes=[schema.StateSentenceGroup], ) self.assert_schema_object_lists_equal(expected_people, people)
def test_readDbEntitiesOfClsToMerge(self): person_1 = schema.StatePerson(person_id=1, state_code=_STATE_CODE) sentence_group_1 = schema.StateSentenceGroup( sentence_group_id=1, external_id=_EXTERNAL_ID, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person_1) person_1.sentence_groups = [sentence_group_1] person_2 = schema.StatePerson(person_id=2, state_code=_STATE_CODE) sentence_group_1_dup = schema.StateSentenceGroup( sentence_group_id=2, external_id=_EXTERNAL_ID, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person_2) sentence_group_2 = schema.StateSentenceGroup( sentence_group_id=3, external_id=_EXTERNAL_ID_2, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person_2) placeholder_sentence_group = schema.StateSentenceGroup( sentence_group_id=4, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person_2) person_2.sentence_groups = [ sentence_group_1_dup, sentence_group_2, placeholder_sentence_group ] session = SessionFactory.for_schema_base(StateBase) session.add(person_1) session.add(person_2) session.commit() # Act trees_to_merge = read_db_entity_trees_of_cls_to_merge( session, _STATE_CODE, schema.StateSentenceGroup) sentence_group_trees_to_merge = one(trees_to_merge) self.assertEqual(len(sentence_group_trees_to_merge), 2) for entity_tree in sentence_group_trees_to_merge: self.assertIsInstance(entity_tree, EntityTree) self.assertIsInstance(entity_tree.entity, schema.StateSentenceGroup) self.assertEqual(entity_tree.entity.external_id, _EXTERNAL_ID) self.assertEqual( { entity_tree.entity.sentence_group_id for entity_tree in sentence_group_trees_to_merge }, {1, 2})
def test_getExternalIdsOfCls_emptyExternalId_raises(self): sentence_group = schema.StateSentenceGroup(external_id=_EXTERNAL_ID) sentence_group_2 = schema.StateSentenceGroup() external_id = schema.StatePersonExternalId(external_id=_EXTERNAL_ID) person = schema.StatePerson( external_ids=[external_id], sentence_groups=[sentence_group, sentence_group_2]) with pytest.raises(EntityMatchingError): get_external_ids_of_cls([person], schema.StateSentenceGroup)
def test_getAllEntityTreesOfCls(self): sentence_group = schema.StateSentenceGroup(sentence_group_id=_ID) sentence_group_2 = schema.StateSentenceGroup(sentence_group_id=_ID_2) person = schema.StatePerson( person_id=_ID, sentence_groups=[sentence_group, sentence_group_2]) self.assertEqual([ EntityTree(entity=sentence_group, ancestor_chain=[person]), EntityTree(entity=sentence_group_2, ancestor_chain=[person]) ], get_all_entity_trees_of_cls([person], schema.StateSentenceGroup)) self.assertEqual([EntityTree(entity=person, ancestor_chain=[])], get_all_entity_trees_of_cls([person], schema.StatePerson))
def test_readObjsWithExternalIdMatch(self) -> None: person_1 = schema.StatePerson(person_id=1, state_code=_STATE_CODE) sentence_group_1 = schema.StateSentenceGroup( sentence_group_id=1, external_id=_EXTERNAL_ID, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person_1, ) person_1.sentence_groups = [sentence_group_1] person_2 = schema.StatePerson(person_id=2, state_code=_STATE_CODE) sentence_group_1_dup = schema.StateSentenceGroup( sentence_group_id=2, external_id=_EXTERNAL_ID, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person_2, ) sentence_group_2 = schema.StateSentenceGroup( sentence_group_id=3, external_id=_EXTERNAL_ID2, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person_2, ) placeholder_sentence_group = schema.StateSentenceGroup( sentence_group_id=4, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person_2, ) person_2.sentence_groups = [ sentence_group_1_dup, sentence_group_2, placeholder_sentence_group, ] with SessionFactory.using_database(self.database_key, autocommit=False) as session: session.add(person_1) session.add(person_2) session.flush() # Act external_ids = dao.read_external_ids_of_cls_with_external_id_match( session, _STATE_CODE, schema.StateSentenceGroup) # Assert self.assertEqual(external_ids, [_EXTERNAL_ID])
def test_readPersonsByRootEntityCls(self): schema_person_with_root_entity = schema.StatePerson( person_id=1, state_code=_STATE_CODE) schema_sentence_group = schema.StateSentenceGroup( sentence_group_id=_ID, external_id=_EXTERNAL_ID, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code='US_ND') schema_sentence_group_2 = schema.StateSentenceGroup( sentence_group_id=_ID_2, external_id=_EXTERNAL_ID_2, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code='US_ND') schema_person_with_root_entity.sentence_groups = [ schema_sentence_group, schema_sentence_group_2 ] placeholder_schema_person = schema.StatePerson(person_id=_ID_2, state_code=_STATE_CODE) schema_person_other_state = schema.StatePerson(person_id=_ID_3, state_code=_STATE_CODE) schema_external_id_other_state = schema.StatePersonExternalId( person_external_id_id=_ID_2, external_id=_ID, id_type=_ID_TYPE, state_code=_STATE_CODE) schema_person_other_state.external_ids = [ schema_external_id_other_state ] session = SessionFactory.for_schema_base(StateBase) session.add(schema_person_with_root_entity) session.add(placeholder_schema_person) session.add(schema_person_other_state) session.commit() ingested_sentence_group = schema.StateSentenceGroup( state_code='us_nd', external_id=_EXTERNAL_ID) ingested_person = schema.StatePerson( sentence_groups=[ingested_sentence_group]) expected_people = [ schema_person_with_root_entity, placeholder_schema_person ] people = read_persons_by_root_entity_cls( session, 'us_nd', [ingested_person], allowed_root_entity_classes=[schema.StateSentenceGroup]) self.assert_schema_object_lists_equal(expected_people, people)
def generate_sentence_group(**kwargs) -> schema.StateSentenceGroup: args = { "status": StateSentenceStatus.PRESENT_WITHOUT_INFO.value, "state_code": _STATE_CODE, } args.update(kwargs) return schema.StateSentenceGroup(**args)
def test_mergeFlatFields(self): ing_entity = schema.StateSentenceGroup( county_code='county_code-updated', max_length_days=10, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value) db_entity = schema.StateSentenceGroup( sentence_group_id=_ID, county_code='county_code', status=StateSentenceStatus.SERVING) expected_entity = schema.StateSentenceGroup( sentence_group_id=_ID, county_code='county_code-updated', max_length_days=10, status=StateSentenceStatus.SERVING.value) merged_entity = default_merge_flat_fields( new_entity=ing_entity, old_entity=db_entity) self.assert_schema_objects_equal(expected_entity, merged_entity)
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 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_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_removeSeosFromViolationIds_unexpectedFormat(self): with pytest.raises(ValueError): sv = schema.StateSupervisionViolation(external_id='bad_id') sp = schema.StateSupervisionPeriod( supervision_violation_entries=[sv]) ss = schema.StateSupervisionSentence(supervision_periods=[sp]) sg = schema.StateSentenceGroup(supervision_sentences=[ss]) p = schema.StatePerson(sentence_groups=[sg]) remove_suffix_from_violation_ids([p])
def test_readObjsWithExternalIdMatch(self): person_1 = schema.StatePerson(person_id=1) sentence_group_1 = schema.StateSentenceGroup( sentence_group_id=1, external_id=_EXTERNAL_ID, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person_1) person_1.sentence_groups = [sentence_group_1] person_2 = schema.StatePerson(person_id=2) sentence_group_1_dup = schema.StateSentenceGroup( sentence_group_id=2, external_id=_EXTERNAL_ID, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person_2) sentence_group_2 = schema.StateSentenceGroup( sentence_group_id=3, external_id=_EXTERNAL_ID2, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person_2) placeholder_sentence_group = schema.StateSentenceGroup( sentence_group_id=4, status=StateSentenceStatus.PRESENT_WITHOUT_INFO.value, state_code=_STATE_CODE, person=person_2) person_2.sentence_groups = [ sentence_group_1_dup, sentence_group_2, placeholder_sentence_group ] session = SessionFactory.for_schema_base(StateBase) session.add(person_1) session.add(person_2) session.commit() # Act external_ids = dao.read_external_ids_of_cls_with_external_id_match( session, _STATE_CODE, schema.StateSentenceGroup) # Assert self.assertEqual(external_ids, [_EXTERNAL_ID])
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))
def test_getRootEntity_allPlaceholders_raises(self) -> None: placeholder_incarceration_period = schema.StateIncarcerationPeriod() placeholder_incarceration_sentence = schema.StateIncarcerationSentence( incarceration_periods=[placeholder_incarceration_period]) placeholder_sentence_group = schema.StateSentenceGroup( incarceration_sentences=[placeholder_incarceration_sentence]) placeholder_person = schema.StatePerson( sentence_groups=[placeholder_sentence_group]) with pytest.raises(EntityMatchingError): get_root_entity_cls([placeholder_person])
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) )
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) )
def test_getTotalEntitiesOfCls(self): supervision_sentence = schema.StateSupervisionSentence() supervision_sentence_2 = schema.StateSupervisionSentence() supervision_sentence_3 = schema.StateSupervisionSentence() sentence_group = schema.StateSentenceGroup(supervision_sentences=[ supervision_sentence, supervision_sentence_2 ]) sentence_group_2 = schema.StateSentenceGroup(supervision_sentences=[ supervision_sentence_2, supervision_sentence_3 ]) person = schema.StatePerson( sentence_groups=[sentence_group, sentence_group_2]) self.assertEqual( 3, get_total_entities_of_cls([person], schema.StateSupervisionSentence)) self.assertEqual( 2, get_total_entities_of_cls([person], schema.StateSentenceGroup)) self.assertEqual( 1, get_total_entities_of_cls([person], schema.StatePerson))
def test_isPlaceholder_personWithExternalId(self) -> None: sentence_group = schema.StateSentenceGroup( state_code=_STATE_CODE, status=StateSentenceStatus.PRESENT_WITHOUT_INFO) person = schema.StatePerson(state_code=_STATE_CODE, sentence_groups=[sentence_group]) self.assertTrue(is_placeholder(person)) person.external_ids.append( schema.StatePersonExternalId(state_code=_STATE_CODE, external_id=_EXTERNAL_ID, id_type=_ID_TYPE)) self.assertFalse(is_placeholder(person))
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), )
def generate_test_sentence_group(person_id, supervision_sentences, incarceration_sentences, fines) -> state_schema.StateSentenceGroup: instance = state_schema.StateSentenceGroup( sentence_group_id=567, status=StateSentenceStatus.SUSPENDED.value, state_code='us_ca', supervision_sentences=supervision_sentences, incarceration_sentences=incarceration_sentences, fines=fines, person_id=person_id) return instance
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_readPersons_unexpectedRoot_raises(self): ingested_supervision_sentence = \ schema.StateSupervisionSentence( external_id=_EXTERNAL_ID) ingested_sentence_group = schema.StateSentenceGroup( supervision_sentences=[ingested_supervision_sentence]) ingested_person = schema.StatePerson( sentence_groups=[ingested_sentence_group]) with pytest.raises(ValueError): session = SessionFactory.for_schema_base(StateBase) read_persons_by_root_entity_cls( session, 'us_nd', [ingested_person], allowed_root_entity_classes=[schema.StateSentenceGroup])