From 818f7090abaf664fabb5ec2393ecaa490f31f3b5 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Sat, 14 Dec 2024 17:59:24 -0500 Subject: [PATCH 01/10] add new 48in24 badges --- app/models/badges/michael_phelps.rb | 15 +++++++++++++++ app/models/badges/paavo_nurmi.rb | 16 ++++++++++++++++ .../badges/participant_in_48_in_24_badge.rb | 15 +++++++++++++++ app/models/badges/usain_bolt.rb | 15 +++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 app/models/badges/michael_phelps.rb create mode 100644 app/models/badges/paavo_nurmi.rb create mode 100644 app/models/badges/participant_in_48_in_24_badge.rb create mode 100644 app/models/badges/usain_bolt.rb diff --git a/app/models/badges/michael_phelps.rb b/app/models/badges/michael_phelps.rb new file mode 100644 index 0000000000..0032ca37eb --- /dev/null +++ b/app/models/badges/michael_phelps.rb @@ -0,0 +1,15 @@ +module Badges + class MichaelPhelpsBadge < Badge + seed "Michael Phelps", + :rare, + 'michael-phelps', + 'Earned 48 medals in the #48in24 challenge' + + def award_to?(user) + def exercises = User::Challenges::FeaturedExercisesProgress48In24.(user) + exercises.none? {|e| e.status == :in_progress} + end + + def send_email_on_acquisition? = true + end +end diff --git a/app/models/badges/paavo_nurmi.rb b/app/models/badges/paavo_nurmi.rb new file mode 100644 index 0000000000..20c261b930 --- /dev/null +++ b/app/models/badges/paavo_nurmi.rb @@ -0,0 +1,16 @@ +module Badges + class PaavoNurmiBadge < Badge + seed "Paavo Nurmi", + :ultimate, + 'paavo-nurmi', + 'Earned 48 gold or silver medals in the #48in24 challenge' + + def award_to?(user) + def exercises = User::Challenges::FeaturedExercisesProgress48In24.(user) + exercises.none? {|e| e.status == :in_progress} && + exercises.none? {|e| e.status == :bronze} + end + + def send_email_on_acquisition? = true + end +end diff --git a/app/models/badges/participant_in_48_in_24_badge.rb b/app/models/badges/participant_in_48_in_24_badge.rb new file mode 100644 index 0000000000..ee9cb99ece --- /dev/null +++ b/app/models/badges/participant_in_48_in_24_badge.rb @@ -0,0 +1,15 @@ +module Badges + class ParticipantIn48In24Badge < Badge + seed "#48in24 Participant", + :common, + '48in24', + 'Participated in the #48in24 challenge and achieved a medal' + + def award_to?(user) + def exercises = User::Challenges::FeaturedExercisesProgress48In24.(user) + exercises.any? {|e| e.status != :in_progress} + end + + def send_email_on_acquisition? = true + end +end diff --git a/app/models/badges/usain_bolt.rb b/app/models/badges/usain_bolt.rb new file mode 100644 index 0000000000..a96dcbb955 --- /dev/null +++ b/app/models/badges/usain_bolt.rb @@ -0,0 +1,15 @@ +module Badges + class UsainBoltBadge < Badge + seed "Usain Bolt", + :legendary, + 'usain-bolt', + 'Earned 48 gold medals in the #48in24 challenge' + + def award_to?(user) + def exercises = User::Challenges::FeaturedExercisesProgress48In24.(user) + exercises.all? {|e| e.status == :gold} + end + + def send_email_on_acquisition? = true + end +end From b28f804f091672626f8d8059d3cee88ce45726b3 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 18 Dec 2024 17:49:00 -0500 Subject: [PATCH 02/10] allow timezone leeway I forget if the database is MySQL: is this the right "date_format" function? --- .../user/challenges/featured_exercises_progress_48_in_24.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/commands/user/challenges/featured_exercises_progress_48_in_24.rb b/app/commands/user/challenges/featured_exercises_progress_48_in_24.rb index 1e7062a66d..370f1cbe6d 100644 --- a/app/commands/user/challenges/featured_exercises_progress_48_in_24.rb +++ b/app/commands/user/challenges/featured_exercises_progress_48_in_24.rb @@ -73,7 +73,7 @@ def status(exercise) completed_exercises = completions[exercise[:slug]].to_a return :in_progress if completed_exercises.blank? - num_completions_in_2024 = completed_exercises.count { |(_, year)| year == 2024 } + num_completions_in_2024 = completed_exercises.count { |(_, date)| date[0..3] == '2024' || date == '2025-01-01' || date == '2023-12-31' } return :in_progress if num_completions_in_2024.zero? return :bronze if num_completions_in_2024 < 3 @@ -86,7 +86,7 @@ def completions user.solutions.completed. joins(exercise: :track). where(exercise: { slug: EXERCISES.pluck(:slug) }). - pluck('exercise.slug', 'tracks.slug', 'YEAR(completed_at)'). + pluck('exercise.slug', 'tracks.slug', "DATE_FORMAT(completed_at, '%Y-%m-%d')"). group_by(&:first). transform_values { |entries| entries.map { |entry| entry[1..] } } end From 3c5ad138fb411511ea5663142546408f18b95abc Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 18 Dec 2024 17:50:36 -0500 Subject: [PATCH 03/10] tests for Michael Phelps badge --- .../badges/michael_phelps_badge_test.rb | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 test/models/badges/michael_phelps_badge_test.rb diff --git a/test/models/badges/michael_phelps_badge_test.rb b/test/models/badges/michael_phelps_badge_test.rb new file mode 100644 index 0000000000..a8647c83c9 --- /dev/null +++ b/test/models/badges/michael_phelps_badge_test.rb @@ -0,0 +1,100 @@ +require "test_helper" + +class Badges::MichaelPhelpsBadgeTest < ActiveSupport::TestCase + test "attributes" do + badge = create :michael_phelps_badge + assert_equal "Michael Phelps", badge.name + assert_equal :rare, badge.rarity + assert_equal :'michael-phelps', badge.icon + assert_equal 'Earned 48 medals in the #48in24 challenge', badge.description + assert badge.send_email_on_acquisition? + assert_nil badge.notification_key + end + + test "award_to?" do + badge = create :michael_phelps_badge + exercises = User::Challenges::FeaturedExercisesProgress48In24.EXERCISES + week1 = exercises.select {|e| e[:week] == 1} + tracks = exercises.map {|e| e[:featured_tracks]}.flatten.uniq + map {|track_slug| [track_slug.to_sym, create(:track, slug: track_slug)]} + .to_h + user = create :user + + # No solutions + refute badge.award_to?(user.reload) + + create :user_challenge, user:, challenge_id: '48in24' + refute badge.award_to?(user.reload) + + # One bronze all year + exercise = create(:practice_exercise, track: track[:csharp], slug: week1[:slug]) + create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + + refute badge.award_to?(user.reload) + + # One silver medal + [:tcl, :wren].each do |track_slug| + exercise = create(:practice_exercise, track: track[track_slug], slug: week1[:slug]) + create(:practice_solution, :published, user:, track: track[track_slug], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + + refute badge.award_to?(user.reload) + + # One gold medal + week1[:featured_tracks].each do |track_slug| + create(:practice_solution, :published, user:, track: track[track_slug.to_sym], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + + refute badge.award_to?(user.reload) + + # in addition to week 1 gold, add 46 bronze medals: 47 medals does not qualify + exercises.select {|e| !(e[:week] == 1 || e[:week] == 48)}.each do |e| + exercise = create(:practice_exercise, track: track[:csharp], slug: e[:slug]) + create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + + refute badge.award_to?(user.reload) + + # add week 48 solution, not in 2024 + week48 = exercises.select {|e| e[:week] == 48} + exercise = create(:practice_exercise, track: track[:csharp], slug: week48[:slug]) + solution = create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + published_at: Time.utc(2023, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + + refute badge.award_to?(user.reload) + + # add an iteration to week 48 to qualify + iteration = create(:iteration, solution:) + iteration.update(created_at: Time.utc(2024, 1, 1, 0, 0, 0) + + assert badge.award_to?(user.reload) + + # 48 gold or silver medals + exercises.select {|e| !(e[:week] == 1}.each do |e| + [:tcl, :wren].each do |t| + exercise = create(:practice_exercise, track: track[t], slug: e[:slug]) + create(:practice_solution, :published, user:, track: track[t], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + end + + assert badge.award_to?(user.reload) + + # 48 gold medals + exercises.select {|e| !(e[:week] == 1}.each do |e| + e[:featured_tracks].map(&:to_sym).each do |t| + next if [:csharp, :tcl, :wren].contains(t) # already have these + + exercise = create(:practice_exercise, track: track[t], slug: e[:slug]) + create(:practice_solution, :published, user:, track: track[t], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + end + + assert badge.award_to?(user.reload) + end +end From 0cb82c6de984e8e3a819d2c551ff2ef14f74e7a9 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 18 Dec 2024 18:13:42 -0500 Subject: [PATCH 04/10] add tests for Paavo Nurmi and Usain Bolt badges --- .../badges/michael_phelps_badge_test.rb | 2 +- test/models/badges/paavo_nurmi_badge_test.rb | 100 ++++++++++++++++++ test/models/badges/usain_bolt_badge_test.rb | 100 ++++++++++++++++++ 3 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 test/models/badges/paavo_nurmi_badge_test.rb create mode 100644 test/models/badges/usain_bolt_badge_test.rb diff --git a/test/models/badges/michael_phelps_badge_test.rb b/test/models/badges/michael_phelps_badge_test.rb index a8647c83c9..3503399adf 100644 --- a/test/models/badges/michael_phelps_badge_test.rb +++ b/test/models/badges/michael_phelps_badge_test.rb @@ -33,7 +33,7 @@ class Badges::MichaelPhelpsBadgeTest < ActiveSupport::TestCase refute badge.award_to?(user.reload) - # One silver medal + # One silver medal: csharp+tcl+wren are never the featured tracks for an exercise [:tcl, :wren].each do |track_slug| exercise = create(:practice_exercise, track: track[track_slug], slug: week1[:slug]) create(:practice_solution, :published, user:, track: track[track_slug], exercise:, diff --git a/test/models/badges/paavo_nurmi_badge_test.rb b/test/models/badges/paavo_nurmi_badge_test.rb new file mode 100644 index 0000000000..eb8fc842ba --- /dev/null +++ b/test/models/badges/paavo_nurmi_badge_test.rb @@ -0,0 +1,100 @@ +require "test_helper" + +class Badges::PaavoNurmiBadgeTest < ActiveSupport::TestCase + test "attributes" do + badge = create :paavo_nurmi_badge + assert_equal "Paavo Nurmi", badge.name + assert_equal :ultimate, badge.rarity + assert_equal :'paavo-nurmi', badge.icon + assert_equal 'Earned 48 gold or silver medals in the #48in24 challenge', badge.description + assert badge.send_email_on_acquisition? + assert_nil badge.notification_key + end + + test "award_to?" do + badge = create :paavo_nurmi_badge + exercises = User::Challenges::FeaturedExercisesProgress48In24.EXERCISES + week1 = exercises.select {|e| e[:week] == 1} + tracks = exercises.map {|e| e[:featured_tracks]}.flatten.uniq + map {|track_slug| [track_slug.to_sym, create(:track, slug: track_slug)]} + .to_h + user = create :user + + # No solutions + refute badge.award_to?(user.reload) + + create :user_challenge, user:, challenge_id: '48in24' + refute badge.award_to?(user.reload) + + # One bronze all year + exercise = create(:practice_exercise, track: track[:csharp], slug: week1[:slug]) + create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + + refute badge.award_to?(user.reload) + + # One silver medal: csharp+tcl+wren are never the featured tracks for an exercise + [:tcl, :wren].each do |track_slug| + exercise = create(:practice_exercise, track: track[track_slug], slug: week1[:slug]) + create(:practice_solution, :published, user:, track: track[track_slug], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + + refute badge.award_to?(user.reload) + + # One gold medal + week1[:featured_tracks].each do |track_slug| + create(:practice_solution, :published, user:, track: track[track_slug.to_sym], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + + refute badge.award_to?(user.reload) + + # in addition to week 1 gold, add 46 bronze medals: 47 medals does not qualify + exercises.select {|e| !(e[:week] == 1 || e[:week] == 48)}.each do |e| + exercise = create(:practice_exercise, track: track[:csharp], slug: e[:slug]) + create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + + refute badge.award_to?(user.reload) + + # add week 48 solution, not in 2024 + week48 = exercises.select {|e| e[:week] == 48} + exercise = create(:practice_exercise, track: track[:csharp], slug: week48[:slug]) + solution = create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + published_at: Time.utc(2023, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + + refute badge.award_to?(user.reload) + + # add an iteration to week 48 + iteration = create(:iteration, solution:) + iteration.update(created_at: Time.utc(2024, 1, 1, 0, 0, 0) + + refute badge.award_to?(user.reload) + + # 48 gold or silver medals + exercises.select {|e| !(e[:week] == 1}.each do |e| + [:tcl, :wren].each do |t| + exercise = create(:practice_exercise, track: track[t], slug: e[:slug]) + create(:practice_solution, :published, user:, track: track[t], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + end + + assert badge.award_to?(user.reload) + + # 48 gold medals + exercises.select {|e| !(e[:week] == 1}.each do |e| + e[:featured_tracks].map(&:to_sym).each do |t| + next if [:csharp, :tcl, :wren].contains(t) # already have these + + exercise = create(:practice_exercise, track: track[t], slug: e[:slug]) + create(:practice_solution, :published, user:, track: track[t], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + end + + assert badge.award_to?(user.reload) + end +end diff --git a/test/models/badges/usain_bolt_badge_test.rb b/test/models/badges/usain_bolt_badge_test.rb new file mode 100644 index 0000000000..eb54987cce --- /dev/null +++ b/test/models/badges/usain_bolt_badge_test.rb @@ -0,0 +1,100 @@ +require "test_helper" + +class Badges::UsainBoltBadgeTest < ActiveSupport::TestCase + test "attributes" do + badge = create :usain_bolt_badge + assert_equal "Usain Bolt", badge.name + assert_equal :legendary, badge.rarity + assert_equal :'usain-bolt', badge.icon + assert_equal 'Earned 48 gold medals in the #48in24 challenge', badge.description + assert badge.send_email_on_acquisition? + assert_nil badge.notification_key + end + + test "award_to?" do + badge = create :usain_bolt_badge + exercises = User::Challenges::FeaturedExercisesProgress48In24.EXERCISES + week1 = exercises.select {|e| e[:week] == 1} + tracks = exercises.map {|e| e[:featured_tracks]}.flatten.uniq + map {|track_slug| [track_slug.to_sym, create(:track, slug: track_slug)]} + .to_h + user = create :user + + # No solutions + refute badge.award_to?(user.reload) + + create :user_challenge, user:, challenge_id: '48in24' + refute badge.award_to?(user.reload) + + # One bronze all year + exercise = create(:practice_exercise, track: track[:csharp], slug: week1[:slug]) + create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + + refute badge.award_to?(user.reload) + + # One silver medal: csharp+tcl+wren are never the featured tracks for an exercise + [:tcl, :wren].each do |track_slug| + exercise = create(:practice_exercise, track: track[track_slug], slug: week1[:slug]) + create(:practice_solution, :published, user:, track: track[track_slug], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + + refute badge.award_to?(user.reload) + + # One gold medal + week1[:featured_tracks].each do |track_slug| + create(:practice_solution, :published, user:, track: track[track_slug.to_sym], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + + refute badge.award_to?(user.reload) + + # in addition to week 1 gold, add 46 bronze medals: 47 medals does not qualify + exercises.select {|e| !(e[:week] == 1 || e[:week] == 48)}.each do |e| + exercise = create(:practice_exercise, track: track[:csharp], slug: e[:slug]) + create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + + refute badge.award_to?(user.reload) + + # add week 48 solution, not in 2024 + week48 = exercises.select {|e| e[:week] == 48} + exercise = create(:practice_exercise, track: track[:csharp], slug: week48[:slug]) + solution = create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + published_at: Time.utc(2023, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + + refute badge.award_to?(user.reload) + + # add an iteration to week 48 to qualify + iteration = create(:iteration, solution:) + iteration.update(created_at: Time.utc(2024, 1, 1, 0, 0, 0) + + assert badge.award_to?(user.reload) + + # 48 gold or silver medals + exercises.select {|e| !(e[:week] == 1}.each do |e| + [:tcl, :wren].each do |t| + exercise = create(:practice_exercise, track: track[t], slug: e[:slug]) + create(:practice_solution, :published, user:, track: track[t], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + end + + refute badge.award_to?(user.reload) + + # 48 gold medals + exercises.select {|e| !(e[:week] == 1}.each do |e| + e[:featured_tracks].map(&:to_sym).each do |t| + next if [:csharp, :tcl, :wren].contains(t) # already have these + + exercise = create(:practice_exercise, track: track[t], slug: e[:slug]) + create(:practice_solution, :published, user:, track: track[t], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + end + end + + assert badge.award_to?(user.reload) + end +end From f084322988154757f0834e351807484186996730 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 18 Dec 2024 18:17:58 -0500 Subject: [PATCH 05/10] add tests for 48in24 participant badge --- .../participant_in_48_in_24_badge_test.rb | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 test/models/badges/participant_in_48_in_24_badge_test.rb diff --git a/test/models/badges/participant_in_48_in_24_badge_test.rb b/test/models/badges/participant_in_48_in_24_badge_test.rb new file mode 100644 index 0000000000..2ed3d5f5f2 --- /dev/null +++ b/test/models/badges/participant_in_48_in_24_badge_test.rb @@ -0,0 +1,66 @@ +require "test_helper" + +class Badge::ParticipantIn12In23BadgeTest < ActiveSupport::TestCase + test "attributes" do + badge = create :participant_in_48_in_24_badge + assert_equal "#48in24 Participant", badge.name + assert_equal :common, badge.rarity + assert_equal :'48in24', badge.icon + assert_equal 'Participated in the #48in24 challenge and achieved a medal', badge.description + assert badge.send_email_on_acquisition? + assert_nil badge.notification_key + end + + test "award_to?" do + badge = create :participant_in_48_in_24_badge + exercises = User::Challenges::FeaturedExercisesProgress48In24.EXERCISES + week1_slug = 'leap' + user = create :user + + # No solutions + refute badge.award_to?(user.reload) + + create :user_challenge, user:, challenge_id: '48in24' + refute badge.award_to?(user.reload) + + # One exercise before 2024 does not qualify + exercise = create(:practice_exercise, track: track[:csharp], slug: week1_slug) + create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + published_at: Time.utc(2023, 12, 30)) + refute badge.award_to?(user.reload) + + # One exercise after 2024 does not qualify + exercise = create(:practice_exercise, track: track[:tcl], slug: week1_slug) + create(:practice_solution, :published, user:, track: track[:tcl], exercise:, + published_at: Time.utc(2025, 1, 2)) + refute badge.award_to?(user.reload) + + # One exercise in 2024 for a non-featured exercise does not qualify + exercise = create(:practice_exercise, track: track[:wren], slug: 'hello-world') + create(:practice_solution, :published, user:, track: track[:wren], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + refute badge.award_to?(user.reload) + + # One exercise in 2024 + exercise = create(:practice_exercise, track: track[:wren], slug: week1_slug) + create(:practice_solution, :published, user:, track: track[:wren], exercise:, + published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + assert badge.award_to?(user.reload) + + # One exercise on Dec 31, 2023 + user2 = create :user + create :user_challenge, user: user2, challenge_id: '48in24' + exercise = create(:practice_exercise, track: track[:csharp], slug: week1_slug) + create(:practice_solution, :published, user: user2, track: track[:csharp], exercise:, + published_at: Time.utc(2023, 12, 31)) + assert badge.award_to?(user2.reload) + + # One exercise on Jan 1, 2025 + user3 = create :user + create :user_challenge, user: user3, challenge_id: '48in24' + exercise = create(:practice_exercise, track: track[:csharp], slug: week1_slug) + create(:practice_solution, :published, user: user3, track: track[:csharp], exercise:, + published_at: Time.utc(2025, 1, 1)) + assert badge.award_to?(user3.reload) + end +end From 36618476923c16d88a9470a9df5e4edc6bd52b63 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 18 Dec 2024 18:22:50 -0500 Subject: [PATCH 06/10] rename Michael Phelps badge to Larisa Latynina --- .../badges/{michael_phelps.rb => larisa_latynina.rb} | 6 +++--- ...lps_badge_test.rb => larisa_latynina_badge_test.rb} | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) rename app/models/badges/{michael_phelps.rb => larisa_latynina.rb} (76%) rename test/models/badges/{michael_phelps_badge_test.rb => larisa_latynina_badge_test.rb} (94%) diff --git a/app/models/badges/michael_phelps.rb b/app/models/badges/larisa_latynina.rb similarity index 76% rename from app/models/badges/michael_phelps.rb rename to app/models/badges/larisa_latynina.rb index 0032ca37eb..015393c07e 100644 --- a/app/models/badges/michael_phelps.rb +++ b/app/models/badges/larisa_latynina.rb @@ -1,8 +1,8 @@ module Badges - class MichaelPhelpsBadge < Badge - seed "Michael Phelps", + class LarisaLatyninaBadge < Badge + seed "Larisa Latynina", :rare, - 'michael-phelps', + 'larisa-latynina', 'Earned 48 medals in the #48in24 challenge' def award_to?(user) diff --git a/test/models/badges/michael_phelps_badge_test.rb b/test/models/badges/larisa_latynina_badge_test.rb similarity index 94% rename from test/models/badges/michael_phelps_badge_test.rb rename to test/models/badges/larisa_latynina_badge_test.rb index 3503399adf..ed3948234d 100644 --- a/test/models/badges/michael_phelps_badge_test.rb +++ b/test/models/badges/larisa_latynina_badge_test.rb @@ -1,18 +1,18 @@ require "test_helper" -class Badges::MichaelPhelpsBadgeTest < ActiveSupport::TestCase +class Badges::LarisaLatyninaBadgeTest < ActiveSupport::TestCase test "attributes" do - badge = create :michael_phelps_badge - assert_equal "Michael Phelps", badge.name + badge = create :larisa_latynina_badge + assert_equal "Larisa Latynina", badge.name assert_equal :rare, badge.rarity - assert_equal :'michael-phelps', badge.icon + assert_equal :'larisa-latynina', badge.icon assert_equal 'Earned 48 medals in the #48in24 challenge', badge.description assert badge.send_email_on_acquisition? assert_nil badge.notification_key end test "award_to?" do - badge = create :michael_phelps_badge + badge = create :larisa_latynina_badge exercises = User::Challenges::FeaturedExercisesProgress48In24.EXERCISES week1 = exercises.select {|e| e[:week] == 1} tracks = exercises.map {|e| e[:featured_tracks]}.flatten.uniq From 79f05e3d996cb5d00b3f21ef54f940f2cbe633e6 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 18 Dec 2024 18:34:10 -0500 Subject: [PATCH 07/10] errors and corrections --- .../featured_exercises_progress_48_in_24.rb | 4 +- ...a_latynina.rb => larisa_latynina_badge.rb} | 4 +- .../{paavo_nurmi.rb => paavo_nurmi_badge.rb} | 6 +- .../badges/participant_in_48_in_24_badge.rb | 6 +- .../{usain_bolt.rb => usain_bolt_badge.rb} | 4 +- ...atured_exercises_progress_48_in_24_test.rb | 48 ++++++------- test/factories/badges.rb | 1 + .../badges/larisa_latynina_badge_test.rb | 65 ++++++++---------- test/models/badges/paavo_nurmi_badge_test.rb | 65 ++++++++---------- .../participant_in_48_in_24_badge_test.rb | 50 ++++++++------ test/models/badges/usain_bolt_badge_test.rb | 67 +++++++++---------- 11 files changed, 153 insertions(+), 167 deletions(-) rename app/models/badges/{larisa_latynina.rb => larisa_latynina_badge.rb} (65%) rename app/models/badges/{paavo_nurmi.rb => paavo_nurmi_badge.rb} (58%) rename app/models/badges/{usain_bolt.rb => usain_bolt_badge.rb} (66%) diff --git a/app/commands/user/challenges/featured_exercises_progress_48_in_24.rb b/app/commands/user/challenges/featured_exercises_progress_48_in_24.rb index 370f1cbe6d..06fb059520 100644 --- a/app/commands/user/challenges/featured_exercises_progress_48_in_24.rb +++ b/app/commands/user/challenges/featured_exercises_progress_48_in_24.rb @@ -73,7 +73,7 @@ def status(exercise) completed_exercises = completions[exercise[:slug]].to_a return :in_progress if completed_exercises.blank? - num_completions_in_2024 = completed_exercises.count { |(_, date)| date[0..3] == '2024' || date == '2025-01-01' || date == '2023-12-31' } + num_completions_in_2024 = completed_exercises.count { |(_, date)| date >= '2023-12-31' && date <= '2025-01-01' } return :in_progress if num_completions_in_2024.zero? return :bronze if num_completions_in_2024 < 3 @@ -86,7 +86,7 @@ def completions user.solutions.completed. joins(exercise: :track). where(exercise: { slug: EXERCISES.pluck(:slug) }). - pluck('exercise.slug', 'tracks.slug', "DATE_FORMAT(completed_at, '%Y-%m-%d')"). + pluck('exercise.slug', 'tracks.slug', Arel.sql("DATE_FORMAT(completed_at, '%Y-%m-%d')")). group_by(&:first). transform_values { |entries| entries.map { |entry| entry[1..] } } end diff --git a/app/models/badges/larisa_latynina.rb b/app/models/badges/larisa_latynina_badge.rb similarity index 65% rename from app/models/badges/larisa_latynina.rb rename to app/models/badges/larisa_latynina_badge.rb index 015393c07e..3fea8d48a3 100644 --- a/app/models/badges/larisa_latynina.rb +++ b/app/models/badges/larisa_latynina_badge.rb @@ -6,8 +6,8 @@ class LarisaLatyninaBadge < Badge 'Earned 48 medals in the #48in24 challenge' def award_to?(user) - def exercises = User::Challenges::FeaturedExercisesProgress48In24.(user) - exercises.none? {|e| e.status == :in_progress} + exercises = User::Challenges::FeaturedExercisesProgress48In24.(user) + exercises.none? { |e| e.status == :in_progress } end def send_email_on_acquisition? = true diff --git a/app/models/badges/paavo_nurmi.rb b/app/models/badges/paavo_nurmi_badge.rb similarity index 58% rename from app/models/badges/paavo_nurmi.rb rename to app/models/badges/paavo_nurmi_badge.rb index 20c261b930..297f6446cd 100644 --- a/app/models/badges/paavo_nurmi.rb +++ b/app/models/badges/paavo_nurmi_badge.rb @@ -6,9 +6,9 @@ class PaavoNurmiBadge < Badge 'Earned 48 gold or silver medals in the #48in24 challenge' def award_to?(user) - def exercises = User::Challenges::FeaturedExercisesProgress48In24.(user) - exercises.none? {|e| e.status == :in_progress} && - exercises.none? {|e| e.status == :bronze} + exercises = User::Challenges::FeaturedExercisesProgress48In24.(user) + exercises.none? { |e| e.status == :in_progress } && + exercises.none? { |e| e.status == :bronze } end def send_email_on_acquisition? = true diff --git a/app/models/badges/participant_in_48_in_24_badge.rb b/app/models/badges/participant_in_48_in_24_badge.rb index ee9cb99ece..e7e31e2903 100644 --- a/app/models/badges/participant_in_48_in_24_badge.rb +++ b/app/models/badges/participant_in_48_in_24_badge.rb @@ -2,12 +2,12 @@ module Badges class ParticipantIn48In24Badge < Badge seed "#48in24 Participant", :common, - '48in24', + '48_in_24', 'Participated in the #48in24 challenge and achieved a medal' def award_to?(user) - def exercises = User::Challenges::FeaturedExercisesProgress48In24.(user) - exercises.any? {|e| e.status != :in_progress} + exercises = User::Challenges::FeaturedExercisesProgress48In24.(user) + exercises.any? { |e| e.status != :in_progress } end def send_email_on_acquisition? = true diff --git a/app/models/badges/usain_bolt.rb b/app/models/badges/usain_bolt_badge.rb similarity index 66% rename from app/models/badges/usain_bolt.rb rename to app/models/badges/usain_bolt_badge.rb index a96dcbb955..d813208202 100644 --- a/app/models/badges/usain_bolt.rb +++ b/app/models/badges/usain_bolt_badge.rb @@ -6,8 +6,8 @@ class UsainBoltBadge < Badge 'Earned 48 gold medals in the #48in24 challenge' def award_to?(user) - def exercises = User::Challenges::FeaturedExercisesProgress48In24.(user) - exercises.all? {|e| e.status == :gold} + exercises = User::Challenges::FeaturedExercisesProgress48In24.(user) + exercises.all? { |e| e.status == :gold } end def send_email_on_acquisition? = true diff --git a/test/commands/user/challenges/featured_exercises_progress_48_in_24_test.rb b/test/commands/user/challenges/featured_exercises_progress_48_in_24_test.rb index 54149648d8..d100f8a22f 100644 --- a/test/commands/user/challenges/featured_exercises_progress_48_in_24_test.rb +++ b/test/commands/user/challenges/featured_exercises_progress_48_in_24_test.rb @@ -23,23 +23,23 @@ class User::Challenges::FeaturedExercisesProgress48In24Test < ActiveSupport::Tes test "returns completed tracks" do user = create :user - create_completed_solution(user, 2022, 'reverse-string', 'cpp') - create_completed_solution(user, 2023, 'reverse-string', 'nim') - create_completed_solution(user, 2024, 'leap', 'elixir') + create_completed_solution(user, 2022, 2, 3, 'reverse-string', 'cpp') + create_completed_solution(user, 2023, 4, 5, 'reverse-string', 'nim') + create_completed_solution(user, 2024, 6, 7, 'leap', 'elixir') progress = User::Challenges::FeaturedExercisesProgress48In24.(user.reload) exercise_progress = progress_by_exercise(progress) - assert_equal ({ "cpp" => 2022, "nim" => 2023 }), exercise_progress["reverse-string"].completed_tracks - assert_equal ({ "elixir" => 2024 }), exercise_progress["leap"].completed_tracks + assert_equal ({ "cpp" => '2022-02-03', "nim" => '2023-04-05' }), exercise_progress["reverse-string"].completed_tracks + assert_equal ({ "elixir" => '2024-06-07' }), exercise_progress["leap"].completed_tracks end test "gold status when user has completed all three featured tracks in 2024" do user = create :user - create_completed_solution(user, 2024, 'reverse-string', 'cpp') - create_completed_solution(user, 2024, 'reverse-string', 'nim') - create_completed_solution(user, 2024, 'reverse-string', 'javascript') + create_completed_solution(user, 2024, 11, 12, 'reverse-string', 'cpp') + create_completed_solution(user, 2024, 10, 11, 'reverse-string', 'nim') + create_completed_solution(user, 2024, 12, 13, 'reverse-string', 'javascript') progress = User::Challenges::FeaturedExercisesProgress48In24.(user.reload) @@ -50,9 +50,9 @@ class User::Challenges::FeaturedExercisesProgress48In24Test < ActiveSupport::Tes test "gold status when completed all featured tracks and at least three iterations in 2024" do user = create :user - create_completed_solution(user, 2021, 'reverse-string', 'cpp') - create_completed_solution(user, 2022, 'reverse-string', 'csharp') - create_completed_solution(user, 2023, 'reverse-string', 'javascript') + create_completed_solution(user, 2021, 4, 5, 'reverse-string', 'cpp') + create_completed_solution(user, 2022, 6, 7, 'reverse-string', 'csharp') + create_completed_solution(user, 2023, 8, 9, 'reverse-string', 'javascript') progress = User::Challenges::FeaturedExercisesProgress48In24.(user.reload) @@ -60,14 +60,14 @@ class User::Challenges::FeaturedExercisesProgress48In24Test < ActiveSupport::Tes assert_equal :in_progress, exercise_progress["reverse-string"].status # Create two iterations in 2024 - create_completed_solution(user, 2024, 'reverse-string', 'zig') - create_completed_solution(user, 2024, 'reverse-string', 'nim') + create_completed_solution(user, 2024, 1, 10, 'reverse-string', 'zig') + create_completed_solution(user, 2024, 2, 20, 'reverse-string', 'nim') exercise_progress = progress_by_exercise(progress) assert_equal :in_progress, exercise_progress["reverse-string"].status # Ensure that there are now three iterations in 2024 - create_completed_solution(user, 2024, 'reverse-string', 'racket') + create_completed_solution(user, 2024, 3, 30, 'reverse-string', 'racket') progress = User::Challenges::FeaturedExercisesProgress48In24.(user.reload) exercise_progress = progress_by_exercise(progress) @@ -77,9 +77,9 @@ class User::Challenges::FeaturedExercisesProgress48In24Test < ActiveSupport::Tes test "silver status when user has completed at least three tracks in 2024 (but not the three featured ones)" do user = create :user - create_completed_solution(user, 2024, 'reverse-string', 'zig') - create_completed_solution(user, 2024, 'reverse-string', 'csharp') - create_completed_solution(user, 2024, 'reverse-string', 'nim') + create_completed_solution(user, 2024, 1, 1, 'reverse-string', 'zig') + create_completed_solution(user, 2024, 2, 2, 'reverse-string', 'csharp') + create_completed_solution(user, 2024, 3, 3, 'reverse-string', 'nim') progress = User::Challenges::FeaturedExercisesProgress48In24.(user.reload) @@ -87,8 +87,8 @@ class User::Challenges::FeaturedExercisesProgress48In24Test < ActiveSupport::Tes assert_equal :silver, exercise_progress["reverse-string"].status # Even if the user has started all three featured tracks, they don't count if they're not completed - create_non_completed_solution(user, 2024, 'reverse-string', 'python') - create_non_completed_solution(user, 2024, 'reverse-string', 'javascript') + create_non_completed_solution(user, 2024, 4, 4, 'reverse-string', 'python') + create_non_completed_solution(user, 2024, 5, 5, 'reverse-string', 'javascript') progress = User::Challenges::FeaturedExercisesProgress48In24.(user.reload) @@ -98,7 +98,7 @@ class User::Challenges::FeaturedExercisesProgress48In24Test < ActiveSupport::Tes test "bronze status when user has completed at least one track in 2024" do user = create :user - create_completed_solution(user, 2024, 'reverse-string', 'kotlin') + create_completed_solution(user, 2024, 6, 6, 'reverse-string', 'kotlin') progress = User::Challenges::FeaturedExercisesProgress48In24.(user.reload) @@ -109,16 +109,16 @@ class User::Challenges::FeaturedExercisesProgress48In24Test < ActiveSupport::Tes private def progress_by_exercise(progress) = progress.index_by(&:slug) - def create_completed_solution(user, year, exercise_slug, track_slug) - travel_to Time.utc(year, SecureRandom.random_number(1..12), SecureRandom.random_number(1..28)) do + def create_completed_solution(user, year, month, day, exercise_slug, track_slug) + travel_to Time.utc(year, month, day) do track = create(:track, slug: track_slug) exercise = create(:practice_exercise, slug: exercise_slug, track:) create(:practice_solution, :completed, user:, exercise:) end end - def create_non_completed_solution(user, year, exercise_slug, track_slug) - travel_to Time.utc(year, SecureRandom.random_number(1..12), SecureRandom.random_number(1..28)) do + def create_non_completed_solution(user, year, month, day, exercise_slug, track_slug) + travel_to Time.utc(year, month, day) do track = create(:track, slug: track_slug) exercise = create(:practice_exercise, slug: exercise_slug, track:) create(:practice_solution, user:, exercise:) diff --git a/test/factories/badges.rb b/test/factories/badges.rb index bb9e9a57d7..3138c4263b 100644 --- a/test/factories/badges.rb +++ b/test/factories/badges.rb @@ -16,6 +16,7 @@ summer_of_sexps jurassic_july apps_august slimline_september object_oriented_october nibbly_november december_diversions completed_12_in_23 polyglot + participant_in_48_in_24 larisa_latynina paavo_nurmi usain_bolt ].each do |type| factory "#{type}_badge", class: "Badges::#{type.to_s.camelize}Badge" do end diff --git a/test/models/badges/larisa_latynina_badge_test.rb b/test/models/badges/larisa_latynina_badge_test.rb index ed3948234d..df97308aa6 100644 --- a/test/models/badges/larisa_latynina_badge_test.rb +++ b/test/models/badges/larisa_latynina_badge_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class Badges::LarisaLatyninaBadgeTest < ActiveSupport::TestCase +class Badge::LarisaLatyninaBadgeTest < ActiveSupport::TestCase test "attributes" do badge = create :larisa_latynina_badge assert_equal "Larisa Latynina", badge.name @@ -13,11 +13,11 @@ class Badges::LarisaLatyninaBadgeTest < ActiveSupport::TestCase test "award_to?" do badge = create :larisa_latynina_badge - exercises = User::Challenges::FeaturedExercisesProgress48In24.EXERCISES - week1 = exercises.select {|e| e[:week] == 1} - tracks = exercises.map {|e| e[:featured_tracks]}.flatten.uniq - map {|track_slug| [track_slug.to_sym, create(:track, slug: track_slug)]} - .to_h + exercises = User::Challenges::FeaturedExercisesProgress48In24::EXERCISES + week_1 = exercises.find { |e| e[:week] == 1 } + tracks = exercises.map { |e| e[:featured_tracks] }.flatten.uniq. + map { |track_slug| [track_slug.to_sym, create(:track, slug: track_slug)] }. + to_h user = create :user # No solutions @@ -27,74 +27,67 @@ class Badges::LarisaLatyninaBadgeTest < ActiveSupport::TestCase refute badge.award_to?(user.reload) # One bronze all year - exercise = create(:practice_exercise, track: track[:csharp], slug: week1[:slug]) - create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_1[:slug]) + create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) - refute badge.award_to?(user.reload) # One silver medal: csharp+tcl+wren are never the featured tracks for an exercise - [:tcl, :wren].each do |track_slug| - exercise = create(:practice_exercise, track: track[track_slug], slug: week1[:slug]) - create(:practice_solution, :published, user:, track: track[track_slug], exercise:, + %i[tcl wren].each do |track_slug| + exercise = create(:practice_exercise, track: tracks[track_slug], slug: week_1[:slug]) + create(:practice_solution, :published, user:, track: tracks[track_slug], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end - refute badge.award_to?(user.reload) # One gold medal - week1[:featured_tracks].each do |track_slug| - create(:practice_solution, :published, user:, track: track[track_slug.to_sym], exercise:, + week_1[:featured_tracks].each do |track_slug| + exercise = create(:practice_exercise, track: tracks[track_slug.to_sym], slug: week_1[:slug]) + create(:practice_solution, :published, user:, track: tracks[track_slug.to_sym], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end - refute badge.award_to?(user.reload) # in addition to week 1 gold, add 46 bronze medals: 47 medals does not qualify - exercises.select {|e| !(e[:week] == 1 || e[:week] == 48)}.each do |e| - exercise = create(:practice_exercise, track: track[:csharp], slug: e[:slug]) - create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + exercises.reject { |e| e[:week] == 1 || e[:week] == 48 }.each do |e| + exercise = create(:practice_exercise, track: tracks[:csharp], slug: e[:slug]) + create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end - refute badge.award_to?(user.reload) # add week 48 solution, not in 2024 - week48 = exercises.select {|e| e[:week] == 48} - exercise = create(:practice_exercise, track: track[:csharp], slug: week48[:slug]) - solution = create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + week_48 = exercises.find { |e| e[:week] == 48 } + exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_48[:slug]) + solution = create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, published_at: Time.utc(2023, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) - refute badge.award_to?(user.reload) # add an iteration to week 48 to qualify iteration = create(:iteration, solution:) - iteration.update(created_at: Time.utc(2024, 1, 1, 0, 0, 0) - + iteration.update(created_at: Time.utc(2024, 1, 1, 0, 0, 0)) assert badge.award_to?(user.reload) # 48 gold or silver medals - exercises.select {|e| !(e[:week] == 1}.each do |e| - [:tcl, :wren].each do |t| - exercise = create(:practice_exercise, track: track[t], slug: e[:slug]) - create(:practice_solution, :published, user:, track: track[t], exercise:, + exercises.reject { |e| e[:week] == 1 }.each do |e| + %i[tcl wren].each do |t| + exercise = create(:practice_exercise, track: tracks[t], slug: e[:slug]) + create(:practice_solution, :published, user:, track: tracks[t], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end - assert badge.award_to?(user.reload) # 48 gold medals - exercises.select {|e| !(e[:week] == 1}.each do |e| + exercises.reject { |e| e[:week] == 1 }.each do |e| e[:featured_tracks].map(&:to_sym).each do |t| - next if [:csharp, :tcl, :wren].contains(t) # already have these + next if %i[csharp tcl wren].include?(t) # already have these - exercise = create(:practice_exercise, track: track[t], slug: e[:slug]) - create(:practice_solution, :published, user:, track: track[t], exercise:, + exercise = create(:practice_exercise, track: tracks[t], slug: e[:slug]) + create(:practice_solution, :published, user:, track: tracks[t], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end - assert badge.award_to?(user.reload) end end diff --git a/test/models/badges/paavo_nurmi_badge_test.rb b/test/models/badges/paavo_nurmi_badge_test.rb index eb8fc842ba..62b1cea7ff 100644 --- a/test/models/badges/paavo_nurmi_badge_test.rb +++ b/test/models/badges/paavo_nurmi_badge_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class Badges::PaavoNurmiBadgeTest < ActiveSupport::TestCase +class Badge::PaavoNurmiBadgeTest < ActiveSupport::TestCase test "attributes" do badge = create :paavo_nurmi_badge assert_equal "Paavo Nurmi", badge.name @@ -13,11 +13,11 @@ class Badges::PaavoNurmiBadgeTest < ActiveSupport::TestCase test "award_to?" do badge = create :paavo_nurmi_badge - exercises = User::Challenges::FeaturedExercisesProgress48In24.EXERCISES - week1 = exercises.select {|e| e[:week] == 1} - tracks = exercises.map {|e| e[:featured_tracks]}.flatten.uniq - map {|track_slug| [track_slug.to_sym, create(:track, slug: track_slug)]} - .to_h + exercises = User::Challenges::FeaturedExercisesProgress48In24::EXERCISES + week_1 = exercises.find { |e| e[:week] == 1 } + tracks = exercises.map { |e| e[:featured_tracks] }.flatten.uniq. + map { |track_slug| [track_slug.to_sym, create(:track, slug: track_slug)] }. + to_h user = create :user # No solutions @@ -27,74 +27,67 @@ class Badges::PaavoNurmiBadgeTest < ActiveSupport::TestCase refute badge.award_to?(user.reload) # One bronze all year - exercise = create(:practice_exercise, track: track[:csharp], slug: week1[:slug]) - create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_1[:slug]) + create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) - refute badge.award_to?(user.reload) # One silver medal: csharp+tcl+wren are never the featured tracks for an exercise - [:tcl, :wren].each do |track_slug| - exercise = create(:practice_exercise, track: track[track_slug], slug: week1[:slug]) - create(:practice_solution, :published, user:, track: track[track_slug], exercise:, + %i[tcl wren].each do |track_slug| + exercise = create(:practice_exercise, track: tracks[track_slug], slug: week_1[:slug]) + create(:practice_solution, :published, user:, track: tracks[track_slug], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end - refute badge.award_to?(user.reload) # One gold medal - week1[:featured_tracks].each do |track_slug| - create(:practice_solution, :published, user:, track: track[track_slug.to_sym], exercise:, + week_1[:featured_tracks].each do |track_slug| + exercise = create(:practice_exercise, track: tracks[track_slug.to_sym], slug: week_1[:slug]) + create(:practice_solution, :published, user:, track: tracks[track_slug.to_sym], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end - refute badge.award_to?(user.reload) # in addition to week 1 gold, add 46 bronze medals: 47 medals does not qualify - exercises.select {|e| !(e[:week] == 1 || e[:week] == 48)}.each do |e| - exercise = create(:practice_exercise, track: track[:csharp], slug: e[:slug]) - create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + exercises.reject { |e| e[:week] == 1 || e[:week] == 48 }.each do |e| + exercise = create(:practice_exercise, track: tracks[:csharp], slug: e[:slug]) + create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end - refute badge.award_to?(user.reload) # add week 48 solution, not in 2024 - week48 = exercises.select {|e| e[:week] == 48} - exercise = create(:practice_exercise, track: track[:csharp], slug: week48[:slug]) - solution = create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + week_48 = exercises.find { |e| e[:week] == 48 } + exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_48[:slug]) + solution = create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, published_at: Time.utc(2023, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) - refute badge.award_to?(user.reload) # add an iteration to week 48 iteration = create(:iteration, solution:) - iteration.update(created_at: Time.utc(2024, 1, 1, 0, 0, 0) - + iteration.update(created_at: Time.utc(2024, 1, 1, 0, 0, 0)) refute badge.award_to?(user.reload) # 48 gold or silver medals - exercises.select {|e| !(e[:week] == 1}.each do |e| - [:tcl, :wren].each do |t| - exercise = create(:practice_exercise, track: track[t], slug: e[:slug]) - create(:practice_solution, :published, user:, track: track[t], exercise:, + exercises.reject { |e| e[:week] == 1 }.each do |e| + %i[tcl wren].each do |t| + exercise = create(:practice_exercise, track: tracks[t], slug: e[:slug]) + create(:practice_solution, :published, user:, track: tracks[t], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end - assert badge.award_to?(user.reload) # 48 gold medals - exercises.select {|e| !(e[:week] == 1}.each do |e| + exercises.reject { |e| e[:week] == 1 }.each do |e| e[:featured_tracks].map(&:to_sym).each do |t| - next if [:csharp, :tcl, :wren].contains(t) # already have these + next if %i[csharp tcl wren].include?(t) # already have these - exercise = create(:practice_exercise, track: track[t], slug: e[:slug]) - create(:practice_solution, :published, user:, track: track[t], exercise:, + exercise = create(:practice_exercise, track: tracks[t], slug: e[:slug]) + create(:practice_solution, :published, user:, track: tracks[t], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end - assert badge.award_to?(user.reload) end end diff --git a/test/models/badges/participant_in_48_in_24_badge_test.rb b/test/models/badges/participant_in_48_in_24_badge_test.rb index 2ed3d5f5f2..b56d21253f 100644 --- a/test/models/badges/participant_in_48_in_24_badge_test.rb +++ b/test/models/badges/participant_in_48_in_24_badge_test.rb @@ -1,11 +1,11 @@ require "test_helper" -class Badge::ParticipantIn12In23BadgeTest < ActiveSupport::TestCase +class Badge::ParticipantIn48In24BadgeTest < ActiveSupport::TestCase test "attributes" do badge = create :participant_in_48_in_24_badge assert_equal "#48in24 Participant", badge.name assert_equal :common, badge.rarity - assert_equal :'48in24', badge.icon + assert_equal :'48_in_24', badge.icon assert_equal 'Participated in the #48in24 challenge and achieved a medal', badge.description assert badge.send_email_on_acquisition? assert_nil badge.notification_key @@ -13,8 +13,13 @@ class Badge::ParticipantIn12In23BadgeTest < ActiveSupport::TestCase test "award_to?" do badge = create :participant_in_48_in_24_badge - exercises = User::Challenges::FeaturedExercisesProgress48In24.EXERCISES - week1_slug = 'leap' + tracks = {} + leap = {} + + %i[csharp tcl wren].each do |t| + tracks[t] = create(:track, slug: t.to_s) + leap[t] = create(:practice_exercise, track: tracks[t], slug: 'leap') + end user = create :user # No solutions @@ -24,43 +29,44 @@ class Badge::ParticipantIn12In23BadgeTest < ActiveSupport::TestCase refute badge.award_to?(user.reload) # One exercise before 2024 does not qualify - exercise = create(:practice_exercise, track: track[:csharp], slug: week1_slug) - create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + create(:practice_solution, :published, user:, + track: tracks[:csharp], exercise: leap[:csharp], published_at: Time.utc(2023, 12, 30)) refute badge.award_to?(user.reload) # One exercise after 2024 does not qualify - exercise = create(:practice_exercise, track: track[:tcl], slug: week1_slug) - create(:practice_solution, :published, user:, track: track[:tcl], exercise:, + create(:practice_solution, :published, user:, + track: tracks[:tcl], exercise: leap[:tcl], published_at: Time.utc(2025, 1, 2)) refute badge.award_to?(user.reload) # One exercise in 2024 for a non-featured exercise does not qualify - exercise = create(:practice_exercise, track: track[:wren], slug: 'hello-world') - create(:practice_solution, :published, user:, track: track[:wren], exercise:, + create(:practice_solution, :published, user:, + track: tracks[:wren], + exercise: create(:practice_exercise, track: tracks[:wren], slug: 'hello-world'), published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) refute badge.award_to?(user.reload) # One exercise in 2024 - exercise = create(:practice_exercise, track: track[:wren], slug: week1_slug) - create(:practice_solution, :published, user:, track: track[:wren], exercise:, + create(:practice_solution, :published, user:, + track: tracks[:wren], exercise: leap[:wren], published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) assert badge.award_to?(user.reload) # One exercise on Dec 31, 2023 - user2 = create :user - create :user_challenge, user: user2, challenge_id: '48in24' - exercise = create(:practice_exercise, track: track[:csharp], slug: week1_slug) - create(:practice_solution, :published, user: user2, track: track[:csharp], exercise:, + user_2 = create :user + create :user_challenge, user: user_2, challenge_id: '48in24' + create(:practice_solution, :published, user: user_2, + track: tracks[:csharp], exercise: leap[:csharp], published_at: Time.utc(2023, 12, 31)) - assert badge.award_to?(user2.reload) + assert badge.award_to?(user_2.reload) # One exercise on Jan 1, 2025 - user3 = create :user - create :user_challenge, user: user3, challenge_id: '48in24' - exercise = create(:practice_exercise, track: track[:csharp], slug: week1_slug) - create(:practice_solution, :published, user: user3, track: track[:csharp], exercise:, + user_3 = create :user + create :user_challenge, user: user_3, challenge_id: '48in24' + create(:practice_solution, :published, user: user_3, + track: tracks[:csharp], exercise: leap[:csharp], published_at: Time.utc(2025, 1, 1)) - assert badge.award_to?(user3.reload) + assert badge.award_to?(user_3.reload) end end diff --git a/test/models/badges/usain_bolt_badge_test.rb b/test/models/badges/usain_bolt_badge_test.rb index eb54987cce..3c61cb7855 100644 --- a/test/models/badges/usain_bolt_badge_test.rb +++ b/test/models/badges/usain_bolt_badge_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class Badges::UsainBoltBadgeTest < ActiveSupport::TestCase +class Badge::UsainBoltBadgeTest < ActiveSupport::TestCase test "attributes" do badge = create :usain_bolt_badge assert_equal "Usain Bolt", badge.name @@ -13,11 +13,11 @@ class Badges::UsainBoltBadgeTest < ActiveSupport::TestCase test "award_to?" do badge = create :usain_bolt_badge - exercises = User::Challenges::FeaturedExercisesProgress48In24.EXERCISES - week1 = exercises.select {|e| e[:week] == 1} - tracks = exercises.map {|e| e[:featured_tracks]}.flatten.uniq - map {|track_slug| [track_slug.to_sym, create(:track, slug: track_slug)]} - .to_h + exercises = User::Challenges::FeaturedExercisesProgress48In24::EXERCISES + week_1 = exercises.find { |e| e[:week] == 1 } + tracks = exercises.map { |e| e[:featured_tracks] }.flatten.uniq. + map { |track_slug| [track_slug.to_sym, create(:track, slug: track_slug)] }. + to_h user = create :user # No solutions @@ -27,74 +27,67 @@ class Badges::UsainBoltBadgeTest < ActiveSupport::TestCase refute badge.award_to?(user.reload) # One bronze all year - exercise = create(:practice_exercise, track: track[:csharp], slug: week1[:slug]) - create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_1[:slug]) + create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) - refute badge.award_to?(user.reload) # One silver medal: csharp+tcl+wren are never the featured tracks for an exercise - [:tcl, :wren].each do |track_slug| - exercise = create(:practice_exercise, track: track[track_slug], slug: week1[:slug]) - create(:practice_solution, :published, user:, track: track[track_slug], exercise:, + %i[tcl wren].each do |track_slug| + exercise = create(:practice_exercise, track: tracks[track_slug], slug: week_1[:slug]) + create(:practice_solution, :published, user:, track: tracks[track_slug], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end - refute badge.award_to?(user.reload) # One gold medal - week1[:featured_tracks].each do |track_slug| - create(:practice_solution, :published, user:, track: track[track_slug.to_sym], exercise:, + week_1[:featured_tracks].each do |track_slug| + exercise = create(:practice_exercise, track: tracks[track_slug.to_sym], slug: week_1[:slug]) + create(:practice_solution, :published, user:, track: tracks[track_slug.to_sym], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end - refute badge.award_to?(user.reload) # in addition to week 1 gold, add 46 bronze medals: 47 medals does not qualify - exercises.select {|e| !(e[:week] == 1 || e[:week] == 48)}.each do |e| - exercise = create(:practice_exercise, track: track[:csharp], slug: e[:slug]) - create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + exercises.reject { |e| e[:week] == 1 || e[:week] == 48 }.each do |e| + exercise = create(:practice_exercise, track: tracks[:csharp], slug: e[:slug]) + create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end - refute badge.award_to?(user.reload) # add week 48 solution, not in 2024 - week48 = exercises.select {|e| e[:week] == 48} - exercise = create(:practice_exercise, track: track[:csharp], slug: week48[:slug]) - solution = create(:practice_solution, :published, user:, track: track[:csharp], exercise:, + week_48 = exercises.find { |e| e[:week] == 48 } + exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_48[:slug]) + solution = create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, published_at: Time.utc(2023, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) - refute badge.award_to?(user.reload) # add an iteration to week 48 to qualify iteration = create(:iteration, solution:) - iteration.update(created_at: Time.utc(2024, 1, 1, 0, 0, 0) - - assert badge.award_to?(user.reload) + iteration.update(created_at: Time.utc(2024, 1, 1, 0, 0, 0)) + refute badge.award_to?(user.reload) # 48 gold or silver medals - exercises.select {|e| !(e[:week] == 1}.each do |e| - [:tcl, :wren].each do |t| - exercise = create(:practice_exercise, track: track[t], slug: e[:slug]) - create(:practice_solution, :published, user:, track: track[t], exercise:, + exercises.reject { |e| e[:week] == 1 }.each do |e| + %i[tcl wren].each do |t| + exercise = create(:practice_exercise, track: tracks[t], slug: e[:slug]) + create(:practice_solution, :published, user:, track: tracks[t], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end - refute badge.award_to?(user.reload) # 48 gold medals - exercises.select {|e| !(e[:week] == 1}.each do |e| + exercises.reject { |e| e[:week] == 1 }.each do |e| e[:featured_tracks].map(&:to_sym).each do |t| - next if [:csharp, :tcl, :wren].contains(t) # already have these + next if %i[csharp tcl wren].include?(t) # already have these - exercise = create(:practice_exercise, track: track[t], slug: e[:slug]) - create(:practice_solution, :published, user:, track: track[t], exercise:, + exercise = create(:practice_exercise, track: tracks[t], slug: e[:slug]) + create(:practice_solution, :published, user:, track: tracks[t], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end - assert badge.award_to?(user.reload) end end From 329662e2c093548f1820d7c73c43c726eea84e6f Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 18 Dec 2024 22:37:38 -0500 Subject: [PATCH 08/10] add assertion messages --- .../badges/larisa_latynina_badge_test.rb | 22 +++++++++---------- .../participant_in_48_in_24_badge_test.rb | 16 ++++++-------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/test/models/badges/larisa_latynina_badge_test.rb b/test/models/badges/larisa_latynina_badge_test.rb index df97308aa6..dff6507362 100644 --- a/test/models/badges/larisa_latynina_badge_test.rb +++ b/test/models/badges/larisa_latynina_badge_test.rb @@ -21,16 +21,14 @@ class Badge::LarisaLatyninaBadgeTest < ActiveSupport::TestCase user = create :user # No solutions - refute badge.award_to?(user.reload) - create :user_challenge, user:, challenge_id: '48in24' - refute badge.award_to?(user.reload) + refute badge.award_to?(user.reload), "new user has no medals" # One bronze all year exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_1[:slug]) create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) - refute badge.award_to?(user.reload) + refute badge.award_to?(user.reload), "one bronze does not qualify" # One silver medal: csharp+tcl+wren are never the featured tracks for an exercise %i[tcl wren].each do |track_slug| @@ -38,7 +36,7 @@ class Badge::LarisaLatyninaBadgeTest < ActiveSupport::TestCase create(:practice_solution, :published, user:, track: tracks[track_slug], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end - refute badge.award_to?(user.reload) + refute badge.award_to?(user.reload), "one silver does not qualify" # One gold medal week_1[:featured_tracks].each do |track_slug| @@ -46,7 +44,7 @@ class Badge::LarisaLatyninaBadgeTest < ActiveSupport::TestCase create(:practice_solution, :published, user:, track: tracks[track_slug.to_sym], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end - refute badge.award_to?(user.reload) + refute badge.award_to?(user.reload), "one gold does not qualify" # in addition to week 1 gold, add 46 bronze medals: 47 medals does not qualify exercises.reject { |e| e[:week] == 1 || e[:week] == 48 }.each do |e| @@ -54,19 +52,19 @@ class Badge::LarisaLatyninaBadgeTest < ActiveSupport::TestCase create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end - refute badge.award_to?(user.reload) + refute badge.award_to?(user.reload), "47 medals does not qualify" # add week 48 solution, not in 2024 week_48 = exercises.find { |e| e[:week] == 48 } exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_48[:slug]) solution = create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, - published_at: Time.utc(2023, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) - refute badge.award_to?(user.reload) + published_at: Time.utc(2023, SecureRandom.rand(1..11), SecureRandom.rand(1..28))) + refute badge.award_to?(user.reload), "47 medals plus one solution in 2023 does not qualify" # add an iteration to week 48 to qualify iteration = create(:iteration, solution:) iteration.update(created_at: Time.utc(2024, 1, 1, 0, 0, 0)) - assert badge.award_to?(user.reload) + assert badge.award_to?(user.reload), "48 bronzes qualifies" # 48 gold or silver medals exercises.reject { |e| e[:week] == 1 }.each do |e| @@ -76,7 +74,7 @@ class Badge::LarisaLatyninaBadgeTest < ActiveSupport::TestCase published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end - assert badge.award_to?(user.reload) + assert badge.award_to?(user.reload), "48 golds&silvers qualifies" # 48 gold medals exercises.reject { |e| e[:week] == 1 }.each do |e| @@ -88,6 +86,6 @@ class Badge::LarisaLatyninaBadgeTest < ActiveSupport::TestCase published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end - assert badge.award_to?(user.reload) + assert badge.award_to?(user.reload), "48 golds qualifies" end end diff --git a/test/models/badges/participant_in_48_in_24_badge_test.rb b/test/models/badges/participant_in_48_in_24_badge_test.rb index b56d21253f..47bca71c94 100644 --- a/test/models/badges/participant_in_48_in_24_badge_test.rb +++ b/test/models/badges/participant_in_48_in_24_badge_test.rb @@ -23,35 +23,33 @@ class Badge::ParticipantIn48In24BadgeTest < ActiveSupport::TestCase user = create :user # No solutions - refute badge.award_to?(user.reload) - create :user_challenge, user:, challenge_id: '48in24' - refute badge.award_to?(user.reload) + refute badge.award_to?(user.reload), "new user does not qualify" # One exercise before 2024 does not qualify create(:practice_solution, :published, user:, track: tracks[:csharp], exercise: leap[:csharp], published_at: Time.utc(2023, 12, 30)) - refute badge.award_to?(user.reload) + refute badge.award_to?(user.reload), "a 2023 solution does not qualify" # One exercise after 2024 does not qualify create(:practice_solution, :published, user:, track: tracks[:tcl], exercise: leap[:tcl], published_at: Time.utc(2025, 1, 2)) - refute badge.award_to?(user.reload) + refute badge.award_to?(user.reload), "a 2025 solution does not qualify" # One exercise in 2024 for a non-featured exercise does not qualify create(:practice_solution, :published, user:, track: tracks[:wren], exercise: create(:practice_exercise, track: tracks[:wren], slug: 'hello-world'), published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) - refute badge.award_to?(user.reload) + refute badge.award_to?(user.reload), "a 2024 non-featured solution does not qualify" # One exercise in 2024 create(:practice_solution, :published, user:, track: tracks[:wren], exercise: leap[:wren], published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) - assert badge.award_to?(user.reload) + assert badge.award_to?(user.reload), "one solution in 2024 qualifies" # One exercise on Dec 31, 2023 user_2 = create :user @@ -59,7 +57,7 @@ class Badge::ParticipantIn48In24BadgeTest < ActiveSupport::TestCase create(:practice_solution, :published, user: user_2, track: tracks[:csharp], exercise: leap[:csharp], published_at: Time.utc(2023, 12, 31)) - assert badge.award_to?(user_2.reload) + assert badge.award_to?(user_2.reload), "one solution on 2023-12-31 qualifies" # One exercise on Jan 1, 2025 user_3 = create :user @@ -67,6 +65,6 @@ class Badge::ParticipantIn48In24BadgeTest < ActiveSupport::TestCase create(:practice_solution, :published, user: user_3, track: tracks[:csharp], exercise: leap[:csharp], published_at: Time.utc(2025, 1, 1)) - assert badge.award_to?(user_3.reload) + assert badge.award_to?(user_3.reload), "one solution on 2025-01-01 qualifies" end end From 9b04d6a48164aaf4d9e80a27c054149b6035ba48 Mon Sep 17 00:00:00 2001 From: Jeremy Walker Date: Thu, 19 Dec 2024 15:43:24 +0900 Subject: [PATCH 09/10] Fix tests --- test/models/badges/larisa_latynina_badge_test.rb | 7 +++---- .../badges/participant_in_48_in_24_badge_test.rb | 12 ++++++------ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/test/models/badges/larisa_latynina_badge_test.rb b/test/models/badges/larisa_latynina_badge_test.rb index dff6507362..4fe7e5bfd6 100644 --- a/test/models/badges/larisa_latynina_badge_test.rb +++ b/test/models/badges/larisa_latynina_badge_test.rb @@ -58,12 +58,11 @@ class Badge::LarisaLatyninaBadgeTest < ActiveSupport::TestCase week_48 = exercises.find { |e| e[:week] == 48 } exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_48[:slug]) solution = create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, - published_at: Time.utc(2023, SecureRandom.rand(1..11), SecureRandom.rand(1..28))) + completed_at: Time.utc(2023, SecureRandom.rand(1..11), SecureRandom.rand(1..28))) refute badge.award_to?(user.reload), "47 medals plus one solution in 2023 does not qualify" - # add an iteration to week 48 to qualify - iteration = create(:iteration, solution:) - iteration.update(created_at: Time.utc(2024, 1, 1, 0, 0, 0)) + # change completion date to 2024 to qualify + solution.update(completed_at: Time.utc(2024, 1, 1, 0, 0, 0)) assert badge.award_to?(user.reload), "48 bronzes qualifies" # 48 gold or silver medals diff --git a/test/models/badges/participant_in_48_in_24_badge_test.rb b/test/models/badges/participant_in_48_in_24_badge_test.rb index 47bca71c94..9576bdb003 100644 --- a/test/models/badges/participant_in_48_in_24_badge_test.rb +++ b/test/models/badges/participant_in_48_in_24_badge_test.rb @@ -29,26 +29,26 @@ class Badge::ParticipantIn48In24BadgeTest < ActiveSupport::TestCase # One exercise before 2024 does not qualify create(:practice_solution, :published, user:, track: tracks[:csharp], exercise: leap[:csharp], - published_at: Time.utc(2023, 12, 30)) + completed_at: Time.utc(2023, 12, 30)) refute badge.award_to?(user.reload), "a 2023 solution does not qualify" # One exercise after 2024 does not qualify create(:practice_solution, :published, user:, track: tracks[:tcl], exercise: leap[:tcl], - published_at: Time.utc(2025, 1, 2)) + completed_at: Time.utc(2025, 1, 2)) refute badge.award_to?(user.reload), "a 2025 solution does not qualify" # One exercise in 2024 for a non-featured exercise does not qualify create(:practice_solution, :published, user:, track: tracks[:wren], exercise: create(:practice_exercise, track: tracks[:wren], slug: 'hello-world'), - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) refute badge.award_to?(user.reload), "a 2024 non-featured solution does not qualify" # One exercise in 2024 create(:practice_solution, :published, user:, track: tracks[:wren], exercise: leap[:wren], - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) assert badge.award_to?(user.reload), "one solution in 2024 qualifies" # One exercise on Dec 31, 2023 @@ -56,7 +56,7 @@ class Badge::ParticipantIn48In24BadgeTest < ActiveSupport::TestCase create :user_challenge, user: user_2, challenge_id: '48in24' create(:practice_solution, :published, user: user_2, track: tracks[:csharp], exercise: leap[:csharp], - published_at: Time.utc(2023, 12, 31)) + completed_at: Time.utc(2023, 12, 31)) assert badge.award_to?(user_2.reload), "one solution on 2023-12-31 qualifies" # One exercise on Jan 1, 2025 @@ -64,7 +64,7 @@ class Badge::ParticipantIn48In24BadgeTest < ActiveSupport::TestCase create :user_challenge, user: user_3, challenge_id: '48in24' create(:practice_solution, :published, user: user_3, track: tracks[:csharp], exercise: leap[:csharp], - published_at: Time.utc(2025, 1, 1)) + completed_at: Time.utc(2025, 1, 1)) assert badge.award_to?(user_3.reload), "one solution on 2025-01-01 qualifies" end end From 5cc2e97d11383e4b8586db6e50afd8209fa2122a Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Thu, 19 Dec 2024 09:12:08 -0500 Subject: [PATCH 10/10] apply corrections completely --- .../badges/larisa_latynina_badge_test.rb | 12 ++++++------ test/models/badges/paavo_nurmi_badge_test.rb | 19 +++++++++---------- test/models/badges/usain_bolt_badge_test.rb | 19 +++++++++---------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/test/models/badges/larisa_latynina_badge_test.rb b/test/models/badges/larisa_latynina_badge_test.rb index 4fe7e5bfd6..e6d917918b 100644 --- a/test/models/badges/larisa_latynina_badge_test.rb +++ b/test/models/badges/larisa_latynina_badge_test.rb @@ -27,14 +27,14 @@ class Badge::LarisaLatyninaBadgeTest < ActiveSupport::TestCase # One bronze all year exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_1[:slug]) create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) refute badge.award_to?(user.reload), "one bronze does not qualify" # One silver medal: csharp+tcl+wren are never the featured tracks for an exercise %i[tcl wren].each do |track_slug| exercise = create(:practice_exercise, track: tracks[track_slug], slug: week_1[:slug]) create(:practice_solution, :published, user:, track: tracks[track_slug], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end refute badge.award_to?(user.reload), "one silver does not qualify" @@ -42,7 +42,7 @@ class Badge::LarisaLatyninaBadgeTest < ActiveSupport::TestCase week_1[:featured_tracks].each do |track_slug| exercise = create(:practice_exercise, track: tracks[track_slug.to_sym], slug: week_1[:slug]) create(:practice_solution, :published, user:, track: tracks[track_slug.to_sym], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end refute badge.award_to?(user.reload), "one gold does not qualify" @@ -50,7 +50,7 @@ class Badge::LarisaLatyninaBadgeTest < ActiveSupport::TestCase exercises.reject { |e| e[:week] == 1 || e[:week] == 48 }.each do |e| exercise = create(:practice_exercise, track: tracks[:csharp], slug: e[:slug]) create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end refute badge.award_to?(user.reload), "47 medals does not qualify" @@ -70,7 +70,7 @@ class Badge::LarisaLatyninaBadgeTest < ActiveSupport::TestCase %i[tcl wren].each do |t| exercise = create(:practice_exercise, track: tracks[t], slug: e[:slug]) create(:practice_solution, :published, user:, track: tracks[t], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end assert badge.award_to?(user.reload), "48 golds&silvers qualifies" @@ -82,7 +82,7 @@ class Badge::LarisaLatyninaBadgeTest < ActiveSupport::TestCase exercise = create(:practice_exercise, track: tracks[t], slug: e[:slug]) create(:practice_solution, :published, user:, track: tracks[t], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end assert badge.award_to?(user.reload), "48 golds qualifies" diff --git a/test/models/badges/paavo_nurmi_badge_test.rb b/test/models/badges/paavo_nurmi_badge_test.rb index 62b1cea7ff..72ccafda65 100644 --- a/test/models/badges/paavo_nurmi_badge_test.rb +++ b/test/models/badges/paavo_nurmi_badge_test.rb @@ -29,14 +29,14 @@ class Badge::PaavoNurmiBadgeTest < ActiveSupport::TestCase # One bronze all year exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_1[:slug]) create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) refute badge.award_to?(user.reload) # One silver medal: csharp+tcl+wren are never the featured tracks for an exercise %i[tcl wren].each do |track_slug| exercise = create(:practice_exercise, track: tracks[track_slug], slug: week_1[:slug]) create(:practice_solution, :published, user:, track: tracks[track_slug], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end refute badge.award_to?(user.reload) @@ -44,7 +44,7 @@ class Badge::PaavoNurmiBadgeTest < ActiveSupport::TestCase week_1[:featured_tracks].each do |track_slug| exercise = create(:practice_exercise, track: tracks[track_slug.to_sym], slug: week_1[:slug]) create(:practice_solution, :published, user:, track: tracks[track_slug.to_sym], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end refute badge.award_to?(user.reload) @@ -52,7 +52,7 @@ class Badge::PaavoNurmiBadgeTest < ActiveSupport::TestCase exercises.reject { |e| e[:week] == 1 || e[:week] == 48 }.each do |e| exercise = create(:practice_exercise, track: tracks[:csharp], slug: e[:slug]) create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end refute badge.award_to?(user.reload) @@ -60,12 +60,11 @@ class Badge::PaavoNurmiBadgeTest < ActiveSupport::TestCase week_48 = exercises.find { |e| e[:week] == 48 } exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_48[:slug]) solution = create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, - published_at: Time.utc(2023, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2023, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) refute badge.award_to?(user.reload) - # add an iteration to week 48 - iteration = create(:iteration, solution:) - iteration.update(created_at: Time.utc(2024, 1, 1, 0, 0, 0)) + # change completion date to 2024, still does not qualify + solution.update(completed_at: Time.utc(2024, 1, 1, 0, 0, 0)) refute badge.award_to?(user.reload) # 48 gold or silver medals @@ -73,7 +72,7 @@ class Badge::PaavoNurmiBadgeTest < ActiveSupport::TestCase %i[tcl wren].each do |t| exercise = create(:practice_exercise, track: tracks[t], slug: e[:slug]) create(:practice_solution, :published, user:, track: tracks[t], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end assert badge.award_to?(user.reload) @@ -85,7 +84,7 @@ class Badge::PaavoNurmiBadgeTest < ActiveSupport::TestCase exercise = create(:practice_exercise, track: tracks[t], slug: e[:slug]) create(:practice_solution, :published, user:, track: tracks[t], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end assert badge.award_to?(user.reload) diff --git a/test/models/badges/usain_bolt_badge_test.rb b/test/models/badges/usain_bolt_badge_test.rb index 3c61cb7855..78f442ab18 100644 --- a/test/models/badges/usain_bolt_badge_test.rb +++ b/test/models/badges/usain_bolt_badge_test.rb @@ -29,14 +29,14 @@ class Badge::UsainBoltBadgeTest < ActiveSupport::TestCase # One bronze all year exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_1[:slug]) create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) refute badge.award_to?(user.reload) # One silver medal: csharp+tcl+wren are never the featured tracks for an exercise %i[tcl wren].each do |track_slug| exercise = create(:practice_exercise, track: tracks[track_slug], slug: week_1[:slug]) create(:practice_solution, :published, user:, track: tracks[track_slug], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end refute badge.award_to?(user.reload) @@ -44,7 +44,7 @@ class Badge::UsainBoltBadgeTest < ActiveSupport::TestCase week_1[:featured_tracks].each do |track_slug| exercise = create(:practice_exercise, track: tracks[track_slug.to_sym], slug: week_1[:slug]) create(:practice_solution, :published, user:, track: tracks[track_slug.to_sym], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end refute badge.award_to?(user.reload) @@ -52,7 +52,7 @@ class Badge::UsainBoltBadgeTest < ActiveSupport::TestCase exercises.reject { |e| e[:week] == 1 || e[:week] == 48 }.each do |e| exercise = create(:practice_exercise, track: tracks[:csharp], slug: e[:slug]) create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end refute badge.award_to?(user.reload) @@ -60,12 +60,11 @@ class Badge::UsainBoltBadgeTest < ActiveSupport::TestCase week_48 = exercises.find { |e| e[:week] == 48 } exercise = create(:practice_exercise, track: tracks[:csharp], slug: week_48[:slug]) solution = create(:practice_solution, :published, user:, track: tracks[:csharp], exercise:, - published_at: Time.utc(2023, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2023, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) refute badge.award_to?(user.reload) - # add an iteration to week 48 to qualify - iteration = create(:iteration, solution:) - iteration.update(created_at: Time.utc(2024, 1, 1, 0, 0, 0)) + # change completion date to 2024, still does not qualify + solution.update(completed_at: Time.utc(2024, 1, 1, 0, 0, 0)) refute badge.award_to?(user.reload) # 48 gold or silver medals @@ -73,7 +72,7 @@ class Badge::UsainBoltBadgeTest < ActiveSupport::TestCase %i[tcl wren].each do |t| exercise = create(:practice_exercise, track: tracks[t], slug: e[:slug]) create(:practice_solution, :published, user:, track: tracks[t], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end refute badge.award_to?(user.reload) @@ -85,7 +84,7 @@ class Badge::UsainBoltBadgeTest < ActiveSupport::TestCase exercise = create(:practice_exercise, track: tracks[t], slug: e[:slug]) create(:practice_solution, :published, user:, track: tracks[t], exercise:, - published_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) + completed_at: Time.utc(2024, SecureRandom.rand(1..12), SecureRandom.rand(1..28))) end end assert badge.award_to?(user.reload)