From 79f05e3d996cb5d00b3f21ef54f940f2cbe633e6 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 18 Dec 2024 18:34:10 -0500 Subject: [PATCH] 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