Coverage for src/mesh/tests/models/test_submission_container_model.py: 100%

100 statements  

« prev     ^ index     » next       coverage.py v7.9.0, created at 2026-02-04 09:42 +0000

1from ...models.factories import SubmissionFactory 

2from ...models.journal_models import JournalSection 

3from ...models.submission_models import Submission 

4from ...views.components.tree_node import build_tree_recursive 

5from ..base_test_case import BaseTestCase 

6 

7 

8class JournalSectionTestCase(BaseTestCase): 

9 used_models = [Submission, JournalSection] 

10 

11 def sort_journal_section_list( 

12 self, journal_section_list: list[JournalSection] 

13 ) -> list[JournalSection]: 

14 """ 

15 Sort a list of submission journal_section by primary key. 

16 """ 

17 return sorted(journal_section_list, key=lambda c: c.pk) 

18 

19 def setUp(self): 

20 """ 

21 JournalSection tree: 

22 Science 

23 Maths 

24 Physics 

25 Relativity 

26 Laser 

27 Arts 

28 """ 

29 self.journal_section_1 = JournalSection.objects.create(id=1, name="Science") 

30 self.journal_section_2 = JournalSection.objects.create(id=2, name="Arts") 

31 self.journal_section_3 = JournalSection.objects.create( 

32 id=3, name="Physics", parent=self.journal_section_1 

33 ) 

34 self.journal_section_4 = JournalSection.objects.create( 

35 id=4, name="Relativity", parent=self.journal_section_3 

36 ) 

37 self.journal_section_5 = JournalSection.objects.create( 

38 id=5, name="Maths", parent=self.journal_section_1 

39 ) 

40 self.journal_section_6 = JournalSection.objects.create( 

41 id=6, name="Laser", parent=self.journal_section_3 

42 ) 

43 

44 def test_all_journal_sections(self): 

45 journal_sections = JournalSection.objects.all_journal_sections() 

46 self.assertEqual(len(journal_sections), 6) 

47 for ind, c in enumerate(journal_sections): 

48 self.assertEqual(c.pk, ind + 1) 

49 

50 def test_all_journal_sections_children(self): 

51 journal_sections_dict = JournalSection.objects.all_journal_sections_children() 

52 expected = { 

53 None: [self.journal_section_1, self.journal_section_2], 

54 1: [self.journal_section_3, self.journal_section_5], 

55 2: [], 

56 3: [self.journal_section_4, self.journal_section_6], 

57 4: [], 

58 5: [], 

59 6: [], 

60 } 

61 self.assertDictEqual(journal_sections_dict, expected) 

62 

63 self.journal_section_3.delete() 

64 journal_sections_dict = JournalSection.objects.all_journal_sections_children() 

65 expected = { 

66 None: [self.journal_section_1, self.journal_section_2], 

67 1: [self.journal_section_4, self.journal_section_5, self.journal_section_6], 

68 2: [], 

69 4: [], 

70 5: [], 

71 6: [], 

72 } 

73 

74 def test_all_journal_sections_parent(self): 

75 journal_sections_dict = JournalSection.objects.all_journal_sections_parents() 

76 expected = { 

77 1: None, 

78 2: None, 

79 3: self.journal_section_1, 

80 4: self.journal_section_3, 

81 5: self.journal_section_1, 

82 6: self.journal_section_3, 

83 } 

84 self.assertDictEqual(journal_sections_dict, expected) 

85 

86 self.journal_section_3.delete() 

87 journal_sections_dict = JournalSection.objects.all_journal_sections_parents() 

88 expected = { 

89 1: None, 

90 2: None, 

91 4: self.journal_section_1, 

92 5: self.journal_section_1, 

93 6: self.journal_section_1, 

94 } 

95 self.assertDictEqual(journal_sections_dict, expected) 

96 

97 def test_get_children_recursive(self): 

98 children = JournalSection.objects.get_children_recursive(None) 

99 expected = [ 

100 self.journal_section_1, 

101 self.journal_section_2, 

102 self.journal_section_3, 

103 self.journal_section_4, 

104 self.journal_section_5, 

105 self.journal_section_6, 

106 ] 

107 self.assertListEqual( 

108 self.sort_journal_section_list(children), self.sort_journal_section_list(expected) 

109 ) 

110 

111 children = JournalSection.objects.get_children_recursive(self.journal_section_1) 

112 expected = [ 

113 self.journal_section_3, 

114 self.journal_section_4, 

115 self.journal_section_5, 

116 self.journal_section_6, 

117 ] 

118 self.assertListEqual( 

119 self.sort_journal_section_list(children), self.sort_journal_section_list(expected) 

120 ) 

121 

122 def test_get_parents_recursive(self): 

123 parents = JournalSection.objects.get_parents_recursive(None) 

124 expected = [] 

125 self.assertListEqual( 

126 self.sort_journal_section_list(parents), self.sort_journal_section_list(expected) 

127 ) 

128 

129 parents = JournalSection.objects.get_parents_recursive(self.journal_section_6) 

130 expected = [self.journal_section_1, self.journal_section_3] 

131 self.assertListEqual( 

132 self.sort_journal_section_list(parents), self.sort_journal_section_list(expected) 

133 ) 

134 

135 def test_build_tree_recursive(self): 

136 # Full tree 

137 tree = build_tree_recursive(JournalSection.objects) 

138 self.assertIsNone(tree.item, None) 

139 self.assertEqual(len(tree.children), 2) 

140 

141 self.assertEqual(tree.children[0].item, self.journal_section_1) 

142 self.assertEqual(tree.children[1].item, self.journal_section_2) 

143 

144 science_tree = tree.children[0] 

145 self.assertEqual(len(science_tree.children), 2) 

146 self.assertEqual(science_tree.children[0].item, self.journal_section_3) 

147 self.assertEqual(science_tree.children[1].item, self.journal_section_5) 

148 

149 physics_tree = science_tree.children[0] 

150 self.assertEqual(len(physics_tree.children), 2) 

151 self.assertEqual(physics_tree.children[0].item, self.journal_section_4) 

152 self.assertEqual(physics_tree.children[1].item, self.journal_section_6) 

153 

154 # Partial tree 

155 physics_tree = build_tree_recursive(JournalSection.objects, self.journal_section_3) 

156 self.assertEqual(len(physics_tree.children), 2) 

157 self.assertEqual(physics_tree.children[0].item, self.journal_section_4) 

158 self.assertEqual(physics_tree.children[1].item, self.journal_section_6) 

159 

160 def test_clean_cache(self): 

161 _ = JournalSection.objects.all_journal_sections() 

162 _ = JournalSection.objects.all_journal_sections_children() 

163 _ = JournalSection.objects.all_journal_sections_parents() 

164 

165 self.assertIsNotNone(JournalSection.objects._all_journal_sections) 

166 self.assertIsNotNone(JournalSection.objects._all_journal_sections_children) 

167 self.assertIsNotNone(JournalSection.objects._all_journal_sections_parents) 

168 

169 JournalSection.objects.clean_cache() 

170 self.assertIsNone(JournalSection.objects._all_journal_sections) 

171 self.assertIsNone(JournalSection.objects._all_journal_sections_children) 

172 self.assertIsNone(JournalSection.objects._all_journal_sections_parents) 

173 

174 def test_update_submission_journal_section(self): 

175 _ = JournalSection.objects.all_journal_sections() 

176 _ = JournalSection.objects.all_journal_sections_children() 

177 _ = JournalSection.objects.all_journal_sections_parents() 

178 

179 self.journal_section_3.parent = None 

180 self.journal_section_3.save() 

181 

182 # Test clean cache 

183 self.assertIsNone(JournalSection.objects._all_journal_sections) 

184 self.assertIsNone(JournalSection.objects._all_journal_sections_children) 

185 self.assertIsNone(JournalSection.objects._all_journal_sections_parents) 

186 

187 self.journal_section_3.parent = self.journal_section_3 

188 self.assertRaises(ValueError, self.journal_section_3.save) 

189 

190 self.journal_section_3.parent = self.journal_section_4 

191 self.assertRaises(ValueError, self.journal_section_3.save) 

192 

193 self.journal_section_3.parent = self.journal_section_2 

194 self.journal_section_3.save() 

195 

196 def test_delete_submission_journal_section(self): 

197 submission = SubmissionFactory(journal_section=self.journal_section_3) 

198 

199 self.assertEqual(submission.journal_section, self.journal_section_3) # type:ignore 

200 

201 self.journal_section_3.delete() 

202 submission = Submission.objects.get(pk=submission.pk) # type:ignore 

203 self.assertEqual(submission.journal_section, self.journal_section_1)