def convert_entities_to_schema( entities: Sequence[Entity], populate_back_edges: bool = True) -> List[DatabaseEntity]: def _is_county_entity(obj: Any) -> bool: return _is_obj_in_module(obj, county_entities) and issubclass( obj.__class__, Entity) def _is_state_entity(obj: Any) -> bool: return _is_obj_in_module(obj, state_entities) and issubclass( obj.__class__, Entity) def _is_operations_entity(obj: Any) -> bool: return _is_obj_in_module(obj, operations_entities) and issubclass( obj.__class__, Entity) if all(_is_county_entity(obj) for obj in entities): return list(CountyEntityToSchemaConverter().convert_all( entities, populate_back_edges)) if all(_is_state_entity(obj) for obj in entities): return StateEntityToSchemaConverter().convert_all( entities, populate_back_edges) if all(_is_operations_entity(obj) for obj in entities): return OperationsEntityToSchemaConverter().convert_all( entities, populate_back_edges) raise ValueError( f"Expected all types to belong to the same schema, one of " f"[{county_schema.__name__}] or [{state_schema.__name__}]")
def test_convert_person(self): schema_person = CountyEntityToSchemaConverter().convert(_PERSON) session = SessionFactory.for_schema_base(JailsBase) session.add(schema_person) session.commit() people = session.query(schema.Person).all() self.assertEqual(len(people), 1) self.assertEqual( CountySchemaToEntityConverter().convert((one(people))), _PERSON)
def test_convert_person(self): schema_person = CountyEntityToSchemaConverter().convert(_PERSON) with SessionFactory.using_database(self.database_key, autocommit=False) as session: session.add(schema_person) session.commit() people = session.query(schema.Person).all() self.assertEqual(len(people), 1) self.assertEqual( CountySchemaToEntityConverter().convert((one(people))), _PERSON)