Skip to content

Commit

Permalink
Request qualification data at the current date, not the future qualif…
Browse files Browse the repository at this point in the history
…ication date (#653)

* fixed qualifications with future dates

* rubocop

* Update registration_checker.rb

* Update competition_factory.rb

* Update stub_helper.rb

* changed time determination

* fixed dates to use utc timezone
  • Loading branch information
dunkOnIT authored Oct 1, 2024
1 parent 6eba143 commit 8b3f5d9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 15 deletions.
3 changes: 2 additions & 1 deletion app/services/registration_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ def existing_registration_in_series?
end

def competitor_qualifies_for_event?(event, qualification)
competitor_qualification_results = UserApi.qualifications(@requestee_user_id, qualification['whenDate'])
target_date = Date.parse(qualification['whenDate']) > Time.now.utc ? Time.now.utc.iso8601 : qualification['whenDate']
competitor_qualification_results = UserApi.qualifications(@requestee_user_id, target_date)
result_type = qualification['resultType']

competitor_pr = competitor_qualification_results.find { |result| result['eventId'] == event && result['type'] == result_type }
Expand Down
36 changes: 29 additions & 7 deletions spec/factories/competition_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@
end

trait :has_qualifications do
today = Time.zone.today.iso8601
today = Time.now.utc.iso8601

transient do
extra_qualifications { {} }
standard_qualifications {
{
'333' => { 'type' => 'attemptResult', 'resultType' => 'single', 'whenDate' => today, 'level' => 1000 },
'555' => { 'type' => 'attemptResult', 'resultType' => 'average', 'whenDate' => today, 'level' => 6000 },
'pyram' => { 'type' => 'ranking', 'resultType' => 'single', 'whenDate' => (Time.zone.today-2).iso8601, 'level' => 100 },
'pyram' => { 'type' => 'ranking', 'resultType' => 'single', 'whenDate' => (Time.now.utc-2).iso8601, 'level' => 100 },
'minx' => { 'type' => 'ranking', 'resultType' => 'average', 'whenDate' => today, 'level' => 200 },
'222' => { 'type' => 'anyResult', 'resultType' => 'single', 'whenDate' => today, 'level' => 0 },
'555bf' => { 'type' => 'anyResult', 'resultType' => 'average', 'whenDate' => today, 'level' => 0 },
Expand All @@ -69,19 +69,41 @@
allow_registration_without_qualification { false }
end

trait :has_future_qualifications do
tomorrow = (Time.now.utc+1).iso8601

transient do
extra_qualifications { {} }
standard_qualifications {
{
'333' => { 'type' => 'attemptResult', 'resultType' => 'single', 'whenDate' => tomorrow, 'level' => 1000 },
'555' => { 'type' => 'attemptResult', 'resultType' => 'average', 'whenDate' => tomorrow, 'level' => 6000 },
'pyram' => { 'type' => 'ranking', 'resultType' => 'single', 'whenDate' => tomorrow, 'level' => 100 },
'minx' => { 'type' => 'ranking', 'resultType' => 'average', 'whenDate' => tomorrow, 'level' => 200 },
'222' => { 'type' => 'anyResult', 'resultType' => 'single', 'whenDate' => tomorrow, 'level' => 0 },
'555bf' => { 'type' => 'anyResult', 'resultType' => 'average', 'whenDate' => tomorrow, 'level' => 0 },
}
}
end

qualifications { standard_qualifications.merge(extra_qualifications) }
qualification_results { true }
allow_registration_without_qualification { false }
end

trait :has_hard_qualifications do
today = Time.zone.today.iso8601
today = Time.now.utc.iso8601

transient do
extra_qualifications { {} }
standard_qualifications {
{
'333' => { 'type' => 'attemptResult', 'resultType' => 'single', 'whenDate' => today, 'level' => 10 },
'555' => { 'type' => 'attemptResult', 'resultType' => 'average', 'whenDate' => today, 'level' => 60 },
'pyram' => { 'type' => 'ranking', 'resultType' => 'single', 'whenDate' => (Time.zone.today-3).iso8601, 'level' => 10 },
'minx' => { 'type' => 'ranking', 'resultType' => 'average', 'whenDate' => (Time.zone.today-3).iso8601, 'level' => 20 },
'222' => { 'type' => 'anyResult', 'resultType' => 'single', 'whenDate' => (Time.zone.today-3).iso8601, 'level' => 0 },
'555bf' => { 'type' => 'anyResult', 'resultType' => 'average', 'whenDate' => (Time.zone.today-3).iso8601, 'level' => 0 },
'pyram' => { 'type' => 'ranking', 'resultType' => 'single', 'whenDate' => (Time.now.utc-3.days).iso8601, 'level' => 10 },
'minx' => { 'type' => 'ranking', 'resultType' => 'average', 'whenDate' => (Time.now.utc-3.days).iso8601, 'level' => 20 },
'222' => { 'type' => 'anyResult', 'resultType' => 'single', 'whenDate' => (Time.now.utc-3.days).iso8601, 'level' => 0 },
'555bf' => { 'type' => 'anyResult', 'resultType' => 'average', 'whenDate' => (Time.now.utc-3.days).iso8601, 'level' => 0 },
}
}
end
Expand Down
38 changes: 33 additions & 5 deletions spec/services/registration_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
end

it 'smoketest - all qualifications unmet' do
stub_qualifications(nil, (Time.zone.today-1).iso8601)
stub_qualifications(nil, (Time.now.utc-1).iso8601)

competition = FactoryBot.build(:competition, :has_hard_qualifications)
stub_json(CompetitionApi.url("#{competition['id']}/qualifications"), 200, competition['qualifications'])
Expand Down Expand Up @@ -301,11 +301,25 @@
RegistrationChecker.create_registration_allowed!(registration_request, competition_info, registration_request['submitted_by'])
}.not_to raise_error
end

it "succeeds given future qualification and #{description}" do
stub_qualifications

competition = FactoryBot.build(:competition, :has_future_qualifications)
stub_json(CompetitionApi.url("#{competition['id']}/qualifications"), 200, competition['qualifications'])
competition_info = CompetitionInfo.new(competition.except('qualifications'))

registration_request = FactoryBot.build(:registration_request, events: event_ids)

expect {
RegistrationChecker.create_registration_allowed!(registration_request, competition_info, registration_request['submitted_by'])
}.not_to raise_error
end
end

RSpec.shared_examples 'fail: qualification enforced' do |description, event_ids, extra_qualifications|
it "fails given #{description}" do
stub_qualifications(nil, (Time.zone.today-1).iso8601)
stub_qualifications(nil, (Time.now.utc-1).iso8601)

competition = FactoryBot.build(:competition, :has_qualifications, extra_qualifications: extra_qualifications)
stub_json(CompetitionApi.url("#{competition['id']}/qualifications"), 200, competition['qualifications'])
Expand Down Expand Up @@ -336,8 +350,8 @@
end

context 'fail: qualification enforced' do
today = Time.zone.today.iso8601
last_year = (Time.zone.today - 365).iso8601
today = Time.now.utc.iso8601
last_year = (Time.now.utc - 365.days).iso8601

it_behaves_like 'fail: qualification enforced', 'no qualifying result for attemptResult-single', ['666'], {
'666' => { 'type' => 'attemptResult', 'resultType' => 'single', 'whenDate' => today, 'level' => 10000 },
Expand Down Expand Up @@ -1463,7 +1477,7 @@
end

RSpec.shared_examples 'update succeed: qualification enforced' do |description, event_ids|
it "succeeds given given #{description}" do
it "succeeds given #{description}" do
competition = FactoryBot.build(:competition, :has_qualifications)
stub_json(CompetitionApi.url("#{competition['id']}/qualifications"), 200, competition['qualifications'])
competition_info = CompetitionInfo.new(competition.except('qualifications'))
Expand All @@ -1476,6 +1490,20 @@
RegistrationChecker.update_registration_allowed!(update_request, competition_info, update_request['submitted_by'])
}.not_to raise_error
end

it "succeeds given future qualification and #{description}" do
competition = FactoryBot.build(:competition, :has_future_qualifications)
stub_json(CompetitionApi.url("#{competition['id']}/qualifications"), 200, competition['qualifications'])
competition_info = CompetitionInfo.new(competition.except('qualifications'))

update_request = FactoryBot.build(:update_request, competing: { 'event_ids' => event_ids })

FactoryBot.create(:registration, user_id: update_request['user_id'])

expect {
RegistrationChecker.update_registration_allowed!(update_request, competition_info, update_request['submitted_by'])
}.not_to raise_error
end
end

RSpec.shared_examples 'update fail: qualification enforced' do |description, event_ids, extra_qualifications|
Expand Down
2 changes: 1 addition & 1 deletion spec/support/qualification_results_faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class QualificationResultsFaker
attr_accessor :qualification_results

def initialize(
date = (Time.zone.today-1).iso8601,
date = (Time.now.utc-1).iso8601,
results_inputs = [
['222', 'single', '200'],
['333', 'single', '900'],
Expand Down
4 changes: 3 additions & 1 deletion spec/support/stub_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def stub_qualifications(payload = nil, qualification_data_date = nil)
date = params['date']
payload_date = qualification_data_date || date

if !payload.nil? # Present doesnt work because [].present? == false
if Date.parse(date) > Time.now.utc
{ status: 200, body: { error: 'You cannot request qualification data for a future date.' }.to_json, headers: { 'Content-Type' => 'application/json' } }
elsif !payload.nil? # Present doesnt work because [].present? == false
{ status: 200, body: payload.to_json, headers: { 'Content-Type' => 'application/json' } }
elsif payload_date.present?
{ status: 200, body: QualificationResultsFaker.new(payload_date).qualification_results.to_json, headers: { 'Content-Type' => 'application/json' } }
Expand Down

0 comments on commit 8b3f5d9

Please sign in to comment.