PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/models/section_spec.rb

https://github.com/benjaminvialle/Markus
Ruby | 106 lines | 97 code | 9 blank | 0 comment | 0 complexity | c73a4f6f62ce258cd8fcb9ff6e6aa25e MD5 | raw file
  1. describe Section do
  2. it { is_expected.to validate_presence_of(:name) }
  3. it { is_expected.to validate_uniqueness_of(:name) }
  4. it { is_expected.to have_many(:students) }
  5. it { is_expected.to have_many(:section_due_dates) }
  6. it { is_expected.not_to allow_value('A!a.sa').for(:name) }
  7. it { is_expected.not_to allow_value('<abc').for(:name) }
  8. it { is_expected.to allow_value('abc 234').for(:name) }
  9. it { is_expected.to allow_value('Ads_-hb').for(:name) }
  10. it { is_expected.to allow_value('-22125-k1lj42_').for(:name) }
  11. describe '.has_students?' do
  12. context 'A section with students associated to' do
  13. it 'return true to has_students?' do
  14. section_1 = Section.create!(name: "Shrek")
  15. section_1.students.create!(user_name: 'exist_student', first_name: 'Nelle', last_name: 'Varoquaux')
  16. expect(section_1.has_students?).to be true
  17. end
  18. end
  19. context 'A section with no student associated to' do
  20. it 'return false to has_students?' do
  21. section_2 = Section.create!(name: "Shrek")
  22. expect(section_2.has_students?).to be false
  23. end
  24. end
  25. end
  26. describe '.count_students' do
  27. context 'A section with students associated to' do
  28. it 'return 1 to students associated to' do
  29. section_3 = Section.create!(name: "Shrek")
  30. section_3.students.create!(user_name: 'exist_student', first_name: 'Shrek', last_name: 'Varoquaux')
  31. expect(section_3.count_students).to eq(1)
  32. end
  33. end
  34. context 'A section with no student associated to' do
  35. it 'return 0 to count_student' do
  36. section_4 = Section.create!(name: "Shrek")
  37. expect(section_4.count_students).to eq(0)
  38. end
  39. end
  40. end
  41. describe '.section_due_date_for' do
  42. context 'A section with students associated to' do
  43. context 'With a section due date for an assignment' do
  44. it 'return the section due date for an assignment' do
  45. section_5 = Section.create!(name: "Shrek")
  46. assignment = create(:assignment,
  47. due_date: 2.days.from_now,
  48. assignment_properties_attributes: { section_due_dates_type: false })
  49. section_due_date = SectionDueDate.create!(section: section_5, assignment: assignment)
  50. expect(section_due_date).to eq(section_5.section_due_date_for(assignment))
  51. end
  52. end
  53. end
  54. end
  55. describe '#starter_file_group_for' do
  56. let(:assignment) { create :assignment }
  57. let(:sections) { create_list :section, 2 }
  58. let!(:starter_file_groups) { create_list :starter_file_group_with_entries, 2, assignment: assignment }
  59. let!(:ssfg) do
  60. create :section_starter_file_group, starter_file_group: starter_file_groups.second, section: sections.second
  61. end
  62. it 'should return the assignment default for a section without a section starter file group' do
  63. expect(sections.first.starter_file_group_for(assignment)).to eq starter_file_groups.first
  64. end
  65. it 'should return the assigned starter file group for a section with a section starter file group' do
  66. expect(sections.second.starter_file_group_for(assignment)).to eq starter_file_groups.second
  67. end
  68. end
  69. describe '#update_starter_file_group' do
  70. let(:assignment) { create :assignment }
  71. let(:sections) { create_list :section, 2 }
  72. let!(:starter_file_groups) { create_list :starter_file_group_with_entries, 2, assignment: assignment }
  73. let!(:ssfg) do
  74. create :section_starter_file_group, starter_file_group: starter_file_groups.second, section: sections.second
  75. end
  76. context 'when a starter file group is not already assigned' do
  77. let(:section) { sections.first }
  78. before { section.update_starter_file_group(assignment.id, starter_file_groups.first.id) }
  79. it 'should assign the new starter file group' do
  80. ids = section.reload.section_starter_file_groups.pluck(:starter_file_group_id)
  81. expect(ids).to include starter_file_groups.first.id
  82. end
  83. end
  84. context 'when a starter file group is already assigned' do
  85. let(:section) { sections.second }
  86. before { section.update_starter_file_group(assignment.id, starter_file_groups.first.id) }
  87. it 'should assign the new starter file group' do
  88. ids = section.reload.section_starter_file_groups.pluck(:starter_file_group_id)
  89. expect(ids).to include starter_file_groups.first.id
  90. end
  91. it 'should remove the old starter file group' do
  92. ids = section.reload.section_starter_file_groups.pluck(:starter_file_group_id)
  93. expect(ids).not_to include starter_file_groups.second.id
  94. end
  95. end
  96. end
  97. end