Coverage for crateweb/nlp_classification/tests/forms_tests.py: 100%

34 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-08-27 10:34 -0500

1""" 

2crate_anon/crateweb/nlp_classification/tests/forms_tests.py 

3 

4=============================================================================== 

5 

6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry. 

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk). 

8 

9 This file is part of CRATE. 

10 

11 CRATE is free software: you can redistribute it and/or modify 

12 it under the terms of the GNU General Public License as published by 

13 the Free Software Foundation, either version 3 of the License, or 

14 (at your option) any later version. 

15 

16 CRATE is distributed in the hope that it will be useful, 

17 but WITHOUT ANY WARRANTY; without even the implied warranty of 

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

19 GNU General Public License for more details. 

20 

21 You should have received a copy of the GNU General Public License 

22 along with CRATE. If not, see <https://www.gnu.org/licenses/>. 

23 

24=============================================================================== 

25 

26Tests for CRATE NLP classification forms. 

27 

28""" 

29 

30from django.test import TestCase 

31 

32from crate_anon.crateweb.core.constants import ( 

33 NLP_DB_CONNECTION_NAME, 

34 RESEARCH_DB_CONNECTION_NAME, 

35) 

36from crate_anon.crateweb.nlp_classification.forms import ( 

37 WizardSelectQuestionForm, 

38 WizardSelectSourceTableDefinitionForm, 

39 WizardSelectSourceTableForm, 

40) 

41from crate_anon.crateweb.nlp_classification.tests.factories import ( 

42 QuestionFactory, 

43 TableDefinitionFactory, 

44 TaskFactory, 

45) 

46 

47 

48class WizardSelectQuestionFormTests(TestCase): 

49 def test_not_filtered_by_task_by_default(self) -> None: 

50 task1 = TaskFactory() 

51 task2 = TaskFactory() 

52 

53 q1_1 = QuestionFactory(task=task1) 

54 q1_2 = QuestionFactory(task=task1) 

55 

56 q2_1 = QuestionFactory(task=task2) 

57 q2_2 = QuestionFactory(task=task2) 

58 

59 form = WizardSelectQuestionForm() 

60 

61 self.assertQuerySetEqual( 

62 form.fields["question"].queryset, 

63 [q1_1, q1_2, q2_1, q2_2], 

64 ordered=False, 

65 ) 

66 

67 def test_filtered_by_task(self) -> None: 

68 task1 = TaskFactory() 

69 task2 = TaskFactory() 

70 

71 q1_1 = QuestionFactory(task=task1) 

72 q1_2 = QuestionFactory(task=task1) 

73 

74 QuestionFactory(task=task2) 

75 QuestionFactory(task=task2) 

76 

77 form = WizardSelectQuestionForm(task=task1) 

78 

79 self.assertQuerySetEqual( 

80 form.fields["question"].queryset, [q1_1, q1_2], ordered=False 

81 ) 

82 

83 

84class WizardSelectSourceTableDefinitionFormTests(TestCase): 

85 def test_filtered_by_source_db_connection(self) -> None: 

86 source_td_1 = TableDefinitionFactory( 

87 db_connection_name=RESEARCH_DB_CONNECTION_NAME 

88 ) 

89 source_td_2 = TableDefinitionFactory( 

90 db_connection_name=RESEARCH_DB_CONNECTION_NAME 

91 ) 

92 TableDefinitionFactory(db_connection_name=NLP_DB_CONNECTION_NAME) 

93 

94 form = WizardSelectSourceTableDefinitionForm() 

95 

96 self.assertQuerySetEqual( 

97 form.fields["table_definition"].queryset, 

98 [source_td_1, source_td_2], 

99 ordered=False, 

100 ) 

101 

102 

103class WizardSelectSourceTableFormTests(TestCase): 

104 def test_names_are_source_tables(self) -> None: 

105 form = WizardSelectSourceTableForm() 

106 

107 self.assertEquals( 

108 form.fields["table_name"].choices, 

109 [ 

110 ("table_1", "table_1"), 

111 ("table_2", "table_2"), 

112 ("table_3", "table_3"), 

113 ] 

114 )