PageRenderTime 25ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/support/criterion.rb

https://github.com/tobioboye/Markus
Ruby | 272 lines | 215 code | 46 blank | 11 comment | 0 complexity | 4606c8e72b0be6db77a578987f27d016 MD5 | raw file
  1. shared_examples 'a criterion' do
  2. describe 'assigning and unassigning TAs' do
  3. let(:assignment) { create(assignment_factory_name) }
  4. let(:criteria) do
  5. Array.new(2) { create(criterion_factory_name, assignment: assignment) }
  6. end
  7. let(:tas) { Array.new(2) { create(:ta) } }
  8. let(:criterion_ids) { criteria.map(&:id) }
  9. let(:ta_ids) { tas.map(&:id) }
  10. describe '.randomly_assign_tas' do
  11. it 'can randomly bulk assign no TAs to no criteria' do
  12. Criterion.randomly_assign_tas([], [], assignment)
  13. end
  14. it 'can randomly bulk assign TAs to no criteria' do
  15. Criterion.randomly_assign_tas([], ta_ids, assignment)
  16. end
  17. it 'can randomly bulk assign no TAs to all criteria' do
  18. Criterion.randomly_assign_tas(criterion_ids, [], assignment)
  19. end
  20. it 'can randomly bulk assign TAs to all criteria' do
  21. Criterion.randomly_assign_tas(criterion_ids, ta_ids, assignment)
  22. criteria.each do |criterion|
  23. criterion.reload
  24. expect(criterion.tas.size).to eq 1
  25. expect(tas).to include criterion.tas.first
  26. end
  27. end
  28. it 'can randomly bulk assign duplicated TAs to criteria' do
  29. # The probability of assigning no duplicated TAs after (tas.size + 1)
  30. # trials is 0.
  31. (tas.size + 1).times do
  32. Criterion.randomly_assign_tas(criterion_ids, ta_ids, assignment)
  33. end
  34. ta_set = tas.to_set
  35. criteria.each do |criterion|
  36. criterion.reload
  37. expect(criterion.tas.size).to be_between(1, 2).inclusive
  38. expect(criterion.tas.to_set).to be_subset(ta_set)
  39. end
  40. end
  41. it 'updates criteria coverage counts after randomly bulk assign TAs' do
  42. expect(Grouping).to receive(:update_criteria_coverage_counts)
  43. .with(assignment)
  44. Criterion.randomly_assign_tas(criterion_ids, ta_ids, assignment)
  45. end
  46. it 'updates assigned groups counts after randomly bulk assign TAs' do
  47. expect(Criterion).to receive(:update_assigned_groups_counts)
  48. .with(assignment, match_array(criterion_ids))
  49. Criterion.randomly_assign_tas(criterion_ids, ta_ids, assignment)
  50. end
  51. end
  52. describe '.assign_all_tas' do
  53. it 'can bulk assign no TAs to no criteria' do
  54. Criterion.assign_all_tas([], [], assignment)
  55. end
  56. it 'can bulk assign all TAs to no criteria' do
  57. Criterion.assign_all_tas([], ta_ids, assignment)
  58. end
  59. it 'can bulk assign no TAs to all criteria' do
  60. Criterion.assign_all_tas(criterion_ids, [], assignment)
  61. end
  62. it 'can bulk assign all TAs to all criteria' do
  63. Criterion.assign_all_tas(criterion_ids, ta_ids, assignment)
  64. criteria.each do |criterion|
  65. criterion.reload
  66. expect(criterion.tas).to match_array(tas)
  67. end
  68. end
  69. it 'can bulk assign duplicated TAs to criteria' do
  70. Criterion.assign_all_tas(criterion_ids.first, ta_ids, assignment)
  71. Criterion.assign_all_tas(criterion_ids, ta_ids.first, assignment)
  72. # First criterion gets all the TAs.
  73. criterion = criteria.shift
  74. criterion.reload
  75. expect(criterion.tas).to match_array(tas)
  76. # The rest of the criteria gets only the first TA.
  77. criteria.each do |criterion|
  78. criterion.reload
  79. expect(criterion.tas).to eq [tas.first]
  80. end
  81. end
  82. it 'updates criteria coverage counts after bulk assign all TAs' do
  83. expect(Grouping).to receive(:update_criteria_coverage_counts)
  84. .with(assignment)
  85. Criterion.assign_all_tas(criterion_ids, ta_ids, assignment)
  86. end
  87. it 'updates assigned groups counts after bulk assign all TAs' do
  88. expect(Criterion).to receive(:update_assigned_groups_counts)
  89. .with(assignment, match_array(criterion_ids))
  90. Criterion.assign_all_tas(criterion_ids, ta_ids, assignment)
  91. end
  92. end
  93. describe '.unassign_tas' do
  94. it 'can bulk unassign no TAs' do
  95. Criterion.unassign_tas([], [], assignment)
  96. end
  97. it 'can bulk unassign TAs' do
  98. Criterion.assign_all_tas(criterion_ids, ta_ids, assignment)
  99. criterion_ta_ids = criteria
  100. .map { |criterion| criterion.criterion_ta_associations.pluck(:id) }
  101. .reduce(:+)
  102. Criterion.unassign_tas(criterion_ta_ids, criterion_ids, assignment)
  103. criteria.each do |criterion|
  104. criterion.reload
  105. expect(criterion.tas).to be_empty
  106. end
  107. end
  108. it 'updates criteria coverage counts after bulk unassign TAs' do
  109. expect(Grouping).to receive(:update_criteria_coverage_counts)
  110. .with(assignment)
  111. Criterion.unassign_tas([], criterion_ids, assignment)
  112. end
  113. it 'updates assigned groups counts after bulk unassign TAs' do
  114. expect(Criterion).to receive(:update_assigned_groups_counts)
  115. .with(assignment, match_array(criterion_ids))
  116. Criterion.unassign_tas([], criterion_ids, assignment)
  117. end
  118. end
  119. end
  120. describe '.update_assigned_groups_counts' do
  121. let(:criterion) { create(criterion_factory_name) }
  122. let(:assignment) { criterion.assignment }
  123. context 'when no criterion IDs are specified' do
  124. # Verifies the assigned groups count of +criterion+ is equal to
  125. # +expected_count+ after updating all the counts.
  126. def expect_updated_assigned_groups_count_to_eq(expected_count)
  127. Criterion.update_assigned_groups_counts(assignment)
  128. criterion.reload
  129. expect(criterion.assigned_groups_count).to eq expected_count
  130. end
  131. context 'with no assigned TAs' do
  132. it 'updates assigned groups count to 0' do
  133. expect_updated_assigned_groups_count_to_eq 0
  134. end
  135. end
  136. context 'with assigned TAs' do
  137. let!(:tas) { Array.new(2) { create(:ta) } }
  138. before :each do
  139. criterion.add_tas(tas)
  140. end
  141. context 'with no assigned groups' do
  142. it 'updates assigned groups count to 0' do
  143. expect_updated_assigned_groups_count_to_eq 0
  144. end
  145. end
  146. context 'with assigned groups' do
  147. let!(:groupings) do
  148. # Create more groupings than TAs to verify that irrelevant
  149. # groupings are not counted. Only `tas.size` number of groupings
  150. # are assigned TAs.
  151. Array.new(tas.size + 1) do
  152. create(:grouping, assignment: assignment)
  153. end
  154. end
  155. it 'updates assigned groups count to 0' do
  156. expect_updated_assigned_groups_count_to_eq 0
  157. end
  158. context 'when only one is assigned a TA' do
  159. before(:each) { create_ta_memberships(groupings[0], tas[0]) }
  160. it 'updates assigned groups count to 1' do
  161. expect_updated_assigned_groups_count_to_eq 1
  162. end
  163. end
  164. context 'when only one is assigned multiple TAs' do
  165. before(:each) { create_ta_memberships(groupings[0], tas) }
  166. it 'updates assigned groups count to 1' do
  167. expect_updated_assigned_groups_count_to_eq 1
  168. end
  169. end
  170. context 'when `tas.size` are assigned unique TAs' do
  171. before :each do
  172. tas.size.times { |i| create_ta_memberships(groupings[i], tas[i]) }
  173. end
  174. it 'updates assigned groups count to `tas.size`' do
  175. expect_updated_assigned_groups_count_to_eq tas.size
  176. end
  177. end
  178. context 'when `tas.size` are assigned non-unique TAs' do
  179. before(:each) do
  180. tas.size.times { |i| create_ta_memberships(groupings[i], tas) }
  181. end
  182. it 'updates assigned groups count to `tas.size`' do
  183. expect_updated_assigned_groups_count_to_eq tas.size
  184. end
  185. context 'when TAs are also assigned to groups of another ' +
  186. 'assignment' do
  187. before :each do
  188. # Creating a new criterion also creates a new assignment.
  189. criterion = create(criterion_factory_name)
  190. grouping = create(:grouping, assignment: criterion.assignment)
  191. criterion.add_tas(tas)
  192. create_ta_memberships(grouping, tas)
  193. end
  194. it 'updates assigned groups count to `tas.size`' do
  195. expect_updated_assigned_groups_count_to_eq tas.size
  196. end
  197. end
  198. end
  199. end
  200. end
  201. end
  202. context 'with specified criterion IDs' do
  203. let(:another_criterion) do
  204. create(criterion_factory_name, assignment: assignment)
  205. end
  206. let(:ta) { create(:ta) }
  207. let(:grouping) { create(:grouping, assignment: assignment) }
  208. let(:another_grouping) { create(:grouping, assignment: assignment) }
  209. before :each do
  210. criterion.add_tas(ta)
  211. another_criterion.add_tas(ta)
  212. create_ta_memberships([grouping, another_grouping], ta)
  213. # Update only `criterion` not `another_criterion`.
  214. Criterion.update_assigned_groups_counts(assignment, criterion.id)
  215. end
  216. it 'updates the count for the specified criterion' do
  217. criterion.reload
  218. expect(criterion.assigned_groups_count).to eq 2
  219. end
  220. it 'does not update the count for the unspecified criterion' do
  221. another_criterion.reload
  222. expect(another_criterion.assigned_groups_count).to eq 0
  223. end
  224. end
  225. end
  226. end