Coverage for postrfp/shared/fetch/view_scoring.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-22 21:34 +0000

1from typing import Dict, List, Optional, Union 

2from sqlalchemy import and_ 

3from sqlalchemy.orm import Session 

4from postrfp.model import Section 

5from postrfp.model.questionnaire.score_views import ( 

6 QuestionScoreComponent, 

7 SectionScoreComponent, 

8) 

9 

10 

11def get_child_scores( 

12 session: Session, 

13 section: Section, 

14 target_types: List[str], 

15 scoreset_id: str = "", 

16 weighting_set_id: Optional[int] = None, 

17 scoring_model: str = "Unweighted", 

18) -> Dict[str, List[Union[QuestionScoreComponent, SectionScoreComponent]]]: 

19 """ 

20 Get scores using SQLAlchemy view models with proper type support. 

21 """ 

22 if weighting_set_id is None: 

23 weighting_set_id = section.project.get_or_create_default_weighting_set_id() 

24 

25 results: Dict[str, List[Union[QuestionScoreComponent, SectionScoreComponent]]] = { 

26 "question": [], 

27 "section": [], 

28 } 

29 

30 # Get question scores if requested 

31 if "question" in target_types: 

32 question_query = session.query(QuestionScoreComponent).filter( 

33 QuestionScoreComponent.section_id == section.id, 

34 QuestionScoreComponent.scoreset_id == scoreset_id, 

35 ) 

36 

37 # Add weighting filter conditionally 

38 if weighting_set_id: 

39 question_query = question_query.filter( 

40 and_( 

41 QuestionScoreComponent.weighting_set_id == weighting_set_id, 

42 QuestionScoreComponent.weighting_set_id.isnot(None), 

43 ) 

44 ) 

45 

46 results["question"] = list(question_query) 

47 

48 # Get section scores if requested 

49 if "section" in target_types: 

50 section_query = session.query(SectionScoreComponent).filter( 

51 SectionScoreComponent.parent_id == section.id, 

52 SectionScoreComponent.scoreset_id == scoreset_id, 

53 ) 

54 

55 # Add weighting filter conditionally (same as first function) 

56 if weighting_set_id: 

57 section_query = section_query.filter( 

58 and_( 

59 SectionScoreComponent.weighting_set_id == weighting_set_id, 

60 SectionScoreComponent.weighting_set_id.isnot(None), 

61 ) 

62 ) 

63 

64 results["section"] = list(section_query) 

65 

66 return results