@@ -311,6 +311,8 @@ defmodule Cadet.AssessmentsTest do
311311 closed_voting_assessment = insert ( :assessment , % { course: course } )
312312 open_voting_assessment = insert ( :assessment , % { course: course } )
313313 compiled_voting_assessment = insert ( :assessment , % { course: course } )
314+ # voting assessment that references an invalid contest number
315+ invalid_voting_assessment = insert ( :assessment , % { course: course } )
314316
315317 closed_question =
316318 insert ( :voting_question , % {
@@ -333,6 +335,12 @@ defmodule Cadet.AssessmentsTest do
333335 build ( :voting_question_content , contest_number: compiled_contest_assessment . number )
334336 } )
335337
338+ invalid_question =
339+ insert ( :voting_question , % {
340+ assessment: invalid_voting_assessment ,
341+ question: build ( :voting_question_content , contest_number: "test_invalid" )
342+ } )
343+
336344 students =
337345 insert_list ( 10 , :course_registration , % {
338346 role: :student ,
@@ -375,6 +383,9 @@ defmodule Cadet.AssessmentsTest do
375383 |> Repo . all ( )
376384 |> length ( ) == 4 * 3 + 6 * 4
377385
386+ assert SubmissionVotes |> where ( question_id: ^ invalid_question . id ) |> Repo . all ( ) |> length ( ) ==
387+ 0
388+
378389 Enum . map ( students , fn student ->
379390 submission =
380391 insert ( :submission ,
@@ -420,6 +431,15 @@ defmodule Cadet.AssessmentsTest do
420431 )
421432 end )
422433
434+ # fetching all unassigned voting questions should only yield open and closed questions
435+ unassigned_voting_questions = Assessments . fetch_unassigned_voting_questions ( )
436+ assert Enum . count ( unassigned_voting_questions ) == 2
437+
438+ assert Enum . map ( unassigned_voting_questions , fn q -> q . question_id end ) == [
439+ closed_question . id ,
440+ open_question . id
441+ ]
442+
423443 Assessments . update_final_contest_entries ( )
424444
425445 # only the closed_contest should have been updated
@@ -433,6 +453,9 @@ defmodule Cadet.AssessmentsTest do
433453 |> where ( question_id: ^ compiled_question . id )
434454 |> Repo . all ( )
435455 |> length ( ) == 4 * 3 + 6 * 4
456+
457+ assert SubmissionVotes |> where ( question_id: ^ invalid_question . id ) |> Repo . all ( ) |> length ( ) ==
458+ 0
436459 end
437460
438461 test "create voting parameters with invalid contest number" do
@@ -493,6 +516,65 @@ defmodule Cadet.AssessmentsTest do
493516 Assessments . delete_assessment ( voting_assessment . id )
494517 refute Repo . exists? ( SubmissionVotes , question_id: question . id )
495518 end
519+
520+ test "does not delete contest assessment if referencing voting assessment is present" do
521+ course = insert ( :course )
522+ config = insert ( :assessment_config )
523+
524+ contest_assessment =
525+ insert ( :assessment ,
526+ is_published: true ,
527+ open_at: Timex . shift ( Timex . now ( ) , days: - 5 ) ,
528+ close_at: Timex . shift ( Timex . now ( ) , hours: - 1 ) ,
529+ course: course ,
530+ config: config ,
531+ number: "test"
532+ )
533+
534+ voting_assessment = insert ( :assessment , % { course: course , config: config } )
535+
536+ # insert voting question that references the contest assessment
537+ _voting_question =
538+ insert ( :voting_question , % {
539+ assessment: voting_assessment ,
540+ question: build ( :voting_question_content , contest_number: contest_assessment . number )
541+ } )
542+
543+ error_message = { :bad_request , "Contest voting for this contest is still up" }
544+
545+ assert { :error , ^ error_message } = Assessments . delete_assessment ( contest_assessment . id )
546+ # deletion should fail
547+ assert Assessment |> where ( id: ^ contest_assessment . id ) |> Repo . exists? ( )
548+ end
549+
550+ test "deletes contest assessment if voting assessment references same number but different course" do
551+ course_1 = insert ( :course )
552+ course_2 = insert ( :course )
553+ config = insert ( :assessment_config )
554+
555+ contest_assessment =
556+ insert ( :assessment ,
557+ is_published: true ,
558+ open_at: Timex . shift ( Timex . now ( ) , days: - 5 ) ,
559+ close_at: Timex . shift ( Timex . now ( ) , hours: - 1 ) ,
560+ course: course_1 ,
561+ config: config ,
562+ number: "test"
563+ )
564+
565+ voting_assessment = insert ( :assessment , % { course: course_2 , config: config } )
566+
567+ # insert voting question from a different course that references the same contest number
568+ _voting_question =
569+ insert ( :voting_question , % {
570+ assessment: voting_assessment ,
571+ question: build ( :voting_question_content , contest_number: contest_assessment . number )
572+ } )
573+
574+ assert { :ok , _ } = Assessments . delete_assessment ( contest_assessment . id )
575+ # deletion should succeed
576+ refute Assessment |> where ( id: ^ contest_assessment . id ) |> Repo . exists? ( )
577+ end
496578 end
497579
498580 describe "contest voting leaderboard utility functions" do
0 commit comments