From 4576a407e0cf16b92e18f1a3654d0c2f190e36c6 Mon Sep 17 00:00:00 2001 From: Kyle Sferrazza Date: Fri, 8 Oct 2021 07:02:01 -0400 Subject: [PATCH 1/3] registrations are unique for examversion,user pairs --- app/models/registration.rb | 2 ++ test/models/user_test.rb | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/app/models/registration.rb b/app/models/registration.rb index 3ede8051b..30c9d0df8 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -24,6 +24,8 @@ class Registration < ApplicationRecord delegate :course, to: :exam delegate :term, to: :course + validates :user, uniqueness: { scope: :exam_version } + def room_version_same_exam return unless room diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 83cbe5453..05ea8df11 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -10,4 +10,12 @@ class UserTest < ActiveSupport::TestCase test 'admin factory builds admins' do assert build(:admin).admin? end + + test 'cannot create two registrations for the same ExamVersion-User pair' do + r1 = create(:registration) + r2 = build(:registration, user: r1.user, exam_version: r1.exam_version) + assert_not r2.valid? + assert_match(/has already been taken/, r2.errors.full_messages.to_sentence) + assert_not r2.save + end end From e7bd0cab5f5cc8c3c0b33074f946a85e1ba378ce Mon Sep 17 00:00:00 2001 From: Kyle Sferrazza Date: Fri, 8 Oct 2021 07:54:25 -0400 Subject: [PATCH 2/3] registrations are unique for exam,user pairs --- app/models/registration.rb | 11 +++++++++++ test/models/user_test.rb | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/app/models/registration.rb b/app/models/registration.rb index 30c9d0df8..e7c882f8f 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -26,6 +26,17 @@ class Registration < ApplicationRecord validates :user, uniqueness: { scope: :exam_version } + validate :user_exam_uniqueness + def user_exam_uniqueness + other_reg_exists = + user + .registrations + .where(exam_version: exam.exam_versions) + .where.not(exam_version: exam_version) + .any? + errors.add(:user, 'already has a registration for another version of that exam') if other_reg_exists + end + def room_version_same_exam return unless room diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 05ea8df11..8421daa6e 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -18,4 +18,13 @@ class UserTest < ActiveSupport::TestCase assert_match(/has already been taken/, r2.errors.full_messages.to_sentence) assert_not r2.save end + + test 'cannot create two registrations for the same Exam-User pair' do + r1 = create(:registration) + second_version = create(:exam_version, exam: r1.exam) + r2 = build(:registration, user: r1.user, exam_version: second_version) + assert_not r2.valid? + assert_match(/has a registration for another version/, r2.errors.full_messages.to_sentence) + assert_not r2.save + end end From d0c7b429816bea5430e041dcfbfd702660bf196e Mon Sep 17 00:00:00 2001 From: Kyle Sferrazza <6677292+kylesferrazza@users.noreply.github.com> Date: Fri, 8 Oct 2021 13:09:36 -0400 Subject: [PATCH 3/3] Add note to uniqueness checks in registration --- app/models/registration.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/registration.rb b/app/models/registration.rb index e7c882f8f..6f6d2d310 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -26,6 +26,8 @@ class Registration < ApplicationRecord validates :user, uniqueness: { scope: :exam_version } + # note: we are not sure whether the current reg will show up in the association, + # so this validation remains separate from the previous uniqueness check validate :user_exam_uniqueness def user_exam_uniqueness other_reg_exists =