def test_project_recreate_resets_object_ids(projects_api, deliverables_api): project = create_project() projects_api.put_project(project) deliverable = deliverables_api.create_deliverable(project.project_id, create_deliverable()) deliverables_api.delete_deliverable(deliverable.object_id) projects_api.delete_project(project.project_id) projects_api.put_project(project) deliverable = deliverables_api.create_deliverable(project.project_id, create_deliverable()) assert deliverable.object_id == EntityId.from_parts(project.project_id, 1)
def get_new_id(self, project_id: EntityId) -> EntityId: query = select([IDS_COUNTER_TABLE.c.next_id], for_update=True).where( IDS_COUNTER_TABLE.c.project_id == project_id.project_id) result = self._session.execute(query).fetchone() if result: current_id = result["next_id"] query = update(IDS_COUNTER_TABLE).values( next_id=current_id + 1).where( IDS_COUNTER_TABLE.c.project_id == project_id.project_id) else: current_id = 1 query = insert(IDS_COUNTER_TABLE).values( project_id=project_id.project_id, next_id=current_id + 1) self._session.execute(query) mark_changed(self._session) return EntityId.from_parts(project_id.project_id, current_id)
def test_object_id_from_parts(): assert EntityId.from_parts("UUU", 44) == EntityId("UUU-44")
def test_init_counter(ids_counter_model): object_id = ids_counter_model.get_new_id(PROJECT_ID1) assert object_id == EntityId.from_parts(PROJECT_ID1.project_id, 1)
def test_drop_project(ids_counter_model): ids_counter_model.get_new_id(PROJECT_ID1) ids_counter_model.drop_project(PROJECT_ID1) object_id = ids_counter_model.get_new_id(PROJECT_ID1) assert object_id == EntityId.from_parts(PROJECT_ID1.project_id, 1)
def test_incr_different_counter(ids_counter_model): ids_counter_model.get_new_id(PROJECT_ID1) ids_counter_model.get_new_id(PROJECT_ID2) object_id = ids_counter_model.get_new_id(PROJECT_ID2) assert object_id == EntityId.from_parts(PROJECT_ID2.project_id, 2)