From 86f9c039916641597f4f460540da702d5da2a2f4 Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Fri, 23 Jan 2026 17:35:16 -0500 Subject: [PATCH] Fixed double elim match generation errors and added tests --- app/models/tournament.rb | 6 +- test/models/tournament_test.rb | 127 ++++++++++++++++++--------------- 2 files changed, 74 insertions(+), 59 deletions(-) diff --git a/app/models/tournament.rb b/app/models/tournament.rb index f426fd6..eb3bc68 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -156,14 +156,14 @@ class Tournament < ApplicationRecord def double_elim_number_of_wrestlers_error error_string = "" - if self.tournament_type == "Double Elimination 1-6" or self.tournament_type == "Double Elimination 1-8" + if self.tournament_type == "Regular Double Elimination 1-6" or self.tournament_type == "Regular Double Elimination 1-8" weights_with_too_many_wrestlers = weights.select{|w| w.wrestlers.size > 64} - weight_with_too_few_wrestlers = weights.select{|w| w.wrestlers.size < 4} + weight_with_too_few_wrestlers = weights.select{|w| w.wrestlers.size < 2} weights_with_too_many_wrestlers.each do |weight| error_string = error_string + " The weight class #{weight.max} has more than 64 wrestlers." end weight_with_too_few_wrestlers.each do |weight| - error_string = error_string + " The weight class #{weight.max} has less than 4 wrestlers." + error_string = error_string + " The weight class #{weight.max} has less than 2 wrestlers." end end return error_string diff --git a/test/models/tournament_test.rb b/test/models/tournament_test.rb index 64c7613..2bb4b47 100644 --- a/test/models/tournament_test.rb +++ b/test/models/tournament_test.rb @@ -14,79 +14,94 @@ class TournamentTest < ActiveSupport::TestCase # Pool to bracket match_generation_error test "Tournament pool to bracket match generation errors with less than two wrestlers" do - number_of_wrestlers=1 - create_a_tournament_with_single_weight("Pool to bracket", number_of_wrestlers) - assert @tournament.match_generation_error != nil + create_a_tournament_with_single_weight("Pool to bracket", 1) + assert_match("There is a tournament error.", @tournament.match_generation_error) end - + test "Tournament pool to bracket match generation errors with more than 24 wrestlers" do - number_of_wrestlers=25 - create_a_tournament_with_single_weight("Pool to bracket", number_of_wrestlers) - assert @tournament.match_generation_error != nil + create_a_tournament_with_single_weight("Pool to bracket", 25) + assert_match("There is a tournament error.", @tournament.match_generation_error) end - + test "Tournament pool to bracket no match generation errors with 24 wrestlers" do - number_of_wrestlers=24 - create_a_tournament_with_single_weight("Pool to bracket", number_of_wrestlers) - assert @tournament.match_generation_error == nil + create_a_tournament_with_single_weight("Pool to bracket", 24) + assert_nil @tournament.match_generation_error end - + test "Tournament pool to bracket no match generation errors with 2 wrestlers" do - number_of_wrestlers=2 - create_a_tournament_with_single_weight("Pool to bracket", number_of_wrestlers) - assert @tournament.match_generation_error == nil + create_a_tournament_with_single_weight("Pool to bracket", 2) + assert_nil @tournament.match_generation_error end - + # Modified 16 Man Double Elimination match_generation_error - test "TournamentModified 16 Man Double Elimination match generation errors with less than 12 wrestlers" do - number_of_wrestlers=11 - create_a_tournament_with_single_weight("Modified 16 Man Double Elimination", number_of_wrestlers) - assert @tournament.match_generation_error != nil + test "Tournament modified 16 man double elimination match generation errors with less than 12 wrestlers" do + create_a_tournament_with_single_weight("Modified 16 Man Double Elimination", 11) + assert_match("There is a tournament error.", @tournament.match_generation_error) end - - test "Tournament Modified 16 Man Double Elimination match generation errors with more than 16 wrestlers" do - number_of_wrestlers=17 - create_a_tournament_with_single_weight("Modified 16 Man Double Elimination", number_of_wrestlers) - assert @tournament.match_generation_error != nil + + test "Tournament modified 16 man double elimination match generation errors with more than 16 wrestlers" do + create_a_tournament_with_single_weight("Modified 16 Man Double Elimination", 17) + assert_match("There is a tournament error.", @tournament.match_generation_error) end - - test "Tournament Modified 16 Man Double Elimination no match generation errors with 16 wrestlers" do - number_of_wrestlers=16 - create_a_tournament_with_single_weight("Modified 16 Man Double Elimination", number_of_wrestlers) - assert @tournament.match_generation_error == nil + + test "Tournament modified 16 man double elimination no match generation errors with 16 wrestlers" do + create_a_tournament_with_single_weight("Modified 16 Man Double Elimination", 16) + assert_nil @tournament.match_generation_error end - - test "Tournament Modified 16 Man Double Elimination no match generation errors with 12 wrestlers" do - number_of_wrestlers=12 - create_a_tournament_with_single_weight("Modified 16 Man Double Elimination", number_of_wrestlers) - assert @tournament.match_generation_error == nil + + test "Tournament modified 16 man double elimination no match generation errors with 12 wrestlers" do + create_a_tournament_with_single_weight("Modified 16 Man Double Elimination", 12) + assert_nil @tournament.match_generation_error end - + # Double Elimination match_generation_error - test "Tournament Double Elimination 1-8 match generation errors with less than 4 wrestlers" do - number_of_wrestlers=3 - create_a_tournament_with_single_weight("Double Elimination 1-8", number_of_wrestlers) - assert @tournament.match_generation_error != nil + test "Tournament regular double elimination 1-8 match generation errors with less than 2 wrestlers" do + create_a_tournament_with_single_weight("Regular Double Elimination 1-8", 1) + assert_match("There is a tournament error.", @tournament.match_generation_error) end - - test "Tournament Double Elimination 1-8 match generation errors with more than 64 wrestlers" do - number_of_wrestlers=65 - create_a_tournament_with_single_weight("Double Elimination 1-8", number_of_wrestlers) - assert @tournament.match_generation_error != nil + + test "Tournament regular double elimination 1-8 match generation errors with more than 64 wrestlers" do + create_a_tournament_with_single_weight("Regular Double Elimination 1-8", 65) + assert_match("There is a tournament error.", @tournament.match_generation_error) end - - test "Tournament Double Elimination 1-8 no match generation errors with 16 wrestlers" do - number_of_wrestlers=16 - create_a_tournament_with_single_weight("Double Elimination 1-8", number_of_wrestlers) - assert @tournament.match_generation_error == nil + + test "Tournament regular double elimination 1-8 no match generation errors with 64 wrestlers" do + create_a_tournament_with_single_weight("Regular Double Elimination 1-8", 64) + assert_nil @tournament.match_generation_error end - - test "Tournament Double Elimination 1-8 no match generation errors with 4 wrestlers" do - number_of_wrestlers=4 - create_a_tournament_with_single_weight("Double Elimination 1-8", number_of_wrestlers) - assert @tournament.match_generation_error == nil + + test "Tournament regular double elimination 1-8 no match generation errors with 2 wrestlers" do + create_a_tournament_with_single_weight("Regular Double Elimination 1-8", 2) + assert_nil @tournament.match_generation_error end - + + test "Tournament match generation errors when a wrestler seed exceeds bracket size" do + create_a_tournament_with_single_weight("Pool to bracket", 4) + @tournament.weights.first.wrestlers.first.update!(original_seed: 8) + assert_match("There is a tournament error.", @tournament.match_generation_error) + assert_match("greater than the amount of wrestlers", @tournament.match_generation_error) + end + + test "Tournament match generation errors when duplicate original seeds are present" do + create_a_tournament_with_single_weight("Pool to bracket", 4) + wrestlers = @tournament.weights.first.wrestlers.order(:id) + wrestlers.first.update!(original_seed: 2) + wrestlers.second.update!(original_seed: 2) + assert_match("There is a tournament error.", @tournament.match_generation_error) + assert_match("seeded 2", @tournament.match_generation_error) + end + + test "Tournament match generation errors when original seeds are out of order" do + create_a_tournament_with_single_weight("Pool to bracket", 4) + wrestlers = @tournament.weights.first.wrestlers.order(:id) + wrestlers.first.update!(original_seed: 1) + wrestlers.second.update!(original_seed: 3) + wrestlers.third.update!(original_seed: nil) + wrestlers.fourth.update!(original_seed: nil) + assert_match("There is a tournament error.", @tournament.match_generation_error) + assert_match("out-of-order seeds", @tournament.match_generation_error) + end + ## End match_generation_error tests test "Tournament create_pre_defined_weights High School Boys Weights" do