Coverage for e2xgrader/preprocessors/validateextracells.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.4.2, created at 2024-03-14 13:22 +0100

1import traceback 

2 

3from nbgrader.nbgraderformat import ValidationError 

4from nbgrader.preprocessors import NbGraderPreprocessor 

5 

6from ..utils.extra_cells import is_singlechoice 

7 

8 

9class ExtraCellValidator: 

10 def validate_cell(self, cell): 

11 if "nbgrader" not in cell.metadata: 

12 return 

13 

14 # check if there is a single choice cell without a solution 

15 if is_singlechoice(cell): 

16 extended_metadata = cell.metadata.extended_cell 

17 if ("choice" not in extended_metadata) or ( 

18 len(extended_metadata.choice) < 1 

19 ): 

20 raise ValidationError( 

21 "single choice nbgrader cell {} does not have a solution".format( 

22 cell.metadata.nbgrader.grade_id 

23 ) 

24 ) 

25 

26 def validate_nb(self, nb): 

27 for cell in nb.cells: 

28 self.validate_cell(cell) 

29 

30 

31class ValidateExtraCells(NbGraderPreprocessor): 

32 """A preprocessor for checking that choice cells have valid solutions.""" 

33 

34 def preprocess(self, nb, resources): 

35 try: 

36 ExtraCellValidator().validate_nb(nb) 

37 except ValidationError: 

38 self.log.error(traceback.format_exc()) 

39 msg = "Some choice cells seem to miss a solution. Please check them again." 

40 self.log.error(msg) 

41 raise ValidationError(msg) 

42 

43 return nb, resources