Coverage for rfpy/api/fix/orphan_questions.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-29 13:25 +0000

1from sqlalchemy.orm import Session 

2 

3from rfpy.model.questionnaire import QuestionDefinition, QuestionInstance 

4 

5 

6def delete_orphan_questions(session: Session, dry_run=True): 

7 """ 

8 Find, and optionally delete if dry_run is False, question definitions that have no 

9 associated question instances - orphans. 

10 

11 If dry_run is False question definitions and child question_elements are deleted. 

12 """ 

13 q = ( 

14 session.query(QuestionDefinition) 

15 .outerjoin(QuestionInstance) 

16 .filter(QuestionInstance.id == None) # noqa E711 

17 ) # noqa E711 

18 orphans = [] 

19 for qd in q: 

20 if not dry_run: 

21 session.delete(qd) 

22 orphans.append({"question_def_id": qd.id, "refcount": qd.refcount}) 

23 

24 return orphans