Skip to content

Commit

Permalink
Merge branch 'master' into add-avenger-backlog-notification
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-henz authored May 22, 2023
2 parents b93cdbd + 6837435 commit 67a56f8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 67 deletions.
8 changes: 4 additions & 4 deletions lib/cadet/assessments/assessments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,8 @@ defmodule Cadet.Assessments do
{:error, :race_condition} ->
{:error, {:internal_server_error, "Please try again later."}}

{:error, :vote_not_unique} ->
{:error, {:bad_request, "Invalid vote or vote is not unique! Vote is not saved."}}
{:error, :invalid_vote} ->
{:error, {:bad_request, "Invalid vote! Vote is not saved."}}

_ ->
{:error, {:bad_request, "Missing or invalid parameter(s)"}}
Expand Down Expand Up @@ -1025,7 +1025,7 @@ defmodule Cadet.Assessments do
Computes rolling leaderboard for contest votes that are still open.
"""
def update_rolling_contest_leaderboards do
# 115 = 2 hours - 5 minutes
# 115 = 2 hours - 5 minutes is default.
if Log.log_execution("update_rolling_contest_leaderboards", Timex.Duration.from_minutes(115)) do
Logger.info("Started update_rolling_contest_leaderboards")

Expand Down Expand Up @@ -1650,7 +1650,7 @@ defmodule Cadet.Assessments do
|> Repo.transaction()
|> case do
{:ok, _result} -> {:ok, nil}
{:error, _name, _changset, _error} -> {:error, :vote_not_unique}
{:error, _name, _changeset, _error} -> {:error, :invalid_vote}
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/cadet/assessments/submission_votes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule Cadet.Assessments.SubmissionVotes do
@required_fields ~w(voter_id submission_id question_id)a
@optional_fields ~w(score)a

# There is no unique constraint for contest vote scores.
def changeset(submission_vote, params) do
submission_vote
|> cast(params, @required_fields ++ @optional_fields)
Expand All @@ -25,6 +26,5 @@ defmodule Cadet.Assessments.SubmissionVotes do
|> foreign_key_constraint(:voter_id)
|> foreign_key_constraint(:submission_id)
|> foreign_key_constraint(:question_id)
|> unique_constraint(:vote_not_unique, name: :unique_score)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Cadet.Repo.Migrations.RemoveUniqueScoreConstraint do
use Ecto.Migration

def up do
drop(unique_index(:submission_votes, [:user_id, :question_id, :score], name: :unique_score))
end

def down do
create(unique_index(:submission_votes, [:user_id, :question_id, :score], name: :unique_score))
end
end
17 changes: 1 addition & 16 deletions test/cadet/assessments/submission_votes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,6 @@ defmodule Cadet.Assessments.SubmissionVotesTest do
|> assert_changeset_db(:invalid)
end

test "invalid changeset unique constraint", %{
valid_params: params
} do
params = Map.put(params, :score, 2)
first_entry = SubmissionVotes.changeset(%SubmissionVotes{}, params)
{:ok, _} = Repo.insert(first_entry)
new_submission = insert(:submission)

second_entry =
first_entry
|> Map.delete(:submission_id)
|> Map.put(:submission_id, new_submission.id)

{:error, changeset} = Repo.insert(second_entry)
refute changeset.valid?
end
# There is no constraint for unique vote score.
end
end
47 changes: 1 addition & 46 deletions test/cadet_web/controllers/answer_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -259,52 +259,7 @@ defmodule CadetWeb.AnswerControllerTest do
})

assert response(voting_conn, 400) ==
"Invalid vote or vote is not unique! Vote is not saved."
end

@tag authenticate: role
test "update duplicate score in submission_votes is unsuccessful", %{
conn: conn,
voting_question: voting_question
} do
course_reg = conn.assigns.test_cr
course_id = conn.assigns.course_id

first_contest_submission = insert(:submission)
second_contest_submission = insert(:submission)

Repo.insert(%SubmissionVotes{
voter_id: course_reg.id,
question_id: voting_question.id,
submission_id: first_contest_submission.id,
score: 1
})

Repo.insert(%SubmissionVotes{
voter_id: course_reg.id,
question_id: voting_question.id,
submission_id: second_contest_submission.id,
score: 2
})

voting_conn =
post(conn, build_url(course_id, voting_question.id), %{
answer: [
%{
"answer" => "hello world",
"submission_id" => first_contest_submission.id,
"score" => 3
},
%{
"answer" => "hello world",
"submission_id" => second_contest_submission.id,
"score" => 3
}
]
})

assert response(voting_conn, 400) ==
"Invalid vote or vote is not unique! Vote is not saved."
"Invalid vote! Vote is not saved."
end
end
end
Expand Down

0 comments on commit 67a56f8

Please sign in to comment.