mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-25 01:14:43 +00:00
Added mat assignment rules for the bout board and fixed the bug where a delegate making the tournamnet info public changes them to the owner
This commit is contained in:
203
test/controllers/mat_assignment_rules_controller_test.rb
Normal file
203
test/controllers/mat_assignment_rules_controller_test.rb
Normal file
@@ -0,0 +1,203 @@
|
||||
require 'test_helper'
|
||||
|
||||
class MatAssignmentRulesControllerTest < ActionController::TestCase
|
||||
include Devise::Test::ControllerHelpers
|
||||
|
||||
setup do
|
||||
@tournament = tournaments(:one) # Existing fixture
|
||||
@mat = mats(:one) # Existing fixture
|
||||
end
|
||||
|
||||
# Controller actions for testing
|
||||
def index
|
||||
get :index, params: { tournament_id: @tournament.id }
|
||||
end
|
||||
|
||||
def new
|
||||
get :new, params: { tournament_id: @tournament.id }
|
||||
end
|
||||
|
||||
def create_rule
|
||||
post :create, params: { tournament_id: @tournament.id, mat_assignment_rule: {
|
||||
mat_id: @mat.id, weight_classes: [1, 2, 3], bracket_positions: ['1/2'], rounds: [1, 2]
|
||||
} }
|
||||
end
|
||||
|
||||
def edit_rule(rule_id)
|
||||
get :edit, params: { tournament_id: @tournament.id, id: rule_id }
|
||||
end
|
||||
|
||||
def update_rule(rule_id)
|
||||
patch :update, params: { tournament_id: @tournament.id, id: rule_id, mat_assignment_rule: {
|
||||
weight_classes: [4, 5, 6]
|
||||
} }
|
||||
end
|
||||
|
||||
def destroy_rule(rule_id)
|
||||
delete :destroy, params: { tournament_id: @tournament.id, id: rule_id }
|
||||
end
|
||||
|
||||
# User sign-in helpers
|
||||
def sign_in_owner
|
||||
sign_in users(:one)
|
||||
end
|
||||
|
||||
def sign_in_non_owner
|
||||
sign_in users(:two)
|
||||
end
|
||||
|
||||
def sign_in_tournament_delegate
|
||||
sign_in users(:three)
|
||||
end
|
||||
|
||||
def sign_in_school_delegate
|
||||
sign_in users(:four)
|
||||
end
|
||||
|
||||
# Assertion helpers
|
||||
def success
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def redirect
|
||||
assert_redirected_to '/static_pages/not_allowed'
|
||||
end
|
||||
|
||||
# Tests
|
||||
test "logged in tournament owner should get index" do
|
||||
sign_in_owner
|
||||
index
|
||||
success
|
||||
end
|
||||
|
||||
test "logged in tournament delegate should get index" do
|
||||
sign_in_tournament_delegate
|
||||
index
|
||||
success
|
||||
end
|
||||
|
||||
test "logged in user should not get index if not owner or delegate" do
|
||||
sign_in_non_owner
|
||||
index
|
||||
redirect
|
||||
end
|
||||
|
||||
test "logged school delegate should not get index" do
|
||||
sign_in_school_delegate
|
||||
index
|
||||
redirect
|
||||
end
|
||||
|
||||
test "logged in tournament owner should create a new rule" do
|
||||
sign_in_owner
|
||||
new
|
||||
success
|
||||
assert_difference 'MatAssignmentRule.count', 1 do
|
||||
create_rule
|
||||
end
|
||||
assert_redirected_to tournament_mat_assignment_rules_path(@tournament)
|
||||
end
|
||||
|
||||
test "logged in tournament delegate should create a new rule" do
|
||||
sign_in_tournament_delegate
|
||||
new
|
||||
success
|
||||
assert_difference 'MatAssignmentRule.count', 1 do
|
||||
create_rule
|
||||
end
|
||||
assert_redirected_to tournament_mat_assignment_rules_path(@tournament)
|
||||
end
|
||||
|
||||
test "logged in user should not create a rule if not owner or delegate" do
|
||||
sign_in_non_owner
|
||||
new
|
||||
redirect
|
||||
assert_no_difference 'MatAssignmentRule.count' do
|
||||
create_rule
|
||||
end
|
||||
redirect
|
||||
end
|
||||
|
||||
test "logged school delegate should not create a rule" do
|
||||
sign_in_school_delegate
|
||||
new
|
||||
redirect
|
||||
assert_no_difference 'MatAssignmentRule.count' do
|
||||
create_rule
|
||||
end
|
||||
redirect
|
||||
end
|
||||
|
||||
test "logged in tournament owner should edit a rule" do
|
||||
rule = MatAssignmentRule.create!(mat_id: @mat.id, tournament_id: @tournament.id, weight_classes: [1, 2, 3])
|
||||
sign_in_owner
|
||||
edit_rule(rule.id)
|
||||
success
|
||||
end
|
||||
|
||||
test "logged in tournament delegate should edit a rule" do
|
||||
rule = MatAssignmentRule.create!(mat_id: @mat.id, tournament_id: @tournament.id, weight_classes: [1, 2, 3])
|
||||
sign_in_tournament_delegate
|
||||
edit_rule(rule.id)
|
||||
success
|
||||
end
|
||||
|
||||
test "logged in user should not edit a rule if not owner or delegate" do
|
||||
rule = MatAssignmentRule.create!(mat_id: @mat.id, tournament_id: @tournament.id, weight_classes: [1, 2, 3])
|
||||
sign_in_non_owner
|
||||
edit_rule(rule.id)
|
||||
redirect
|
||||
end
|
||||
|
||||
test "logged school delegate should not edit a rule" do
|
||||
rule = MatAssignmentRule.create!(mat_id: @mat.id, tournament_id: @tournament.id, weight_classes: [1, 2, 3])
|
||||
sign_in_school_delegate
|
||||
edit_rule(rule.id)
|
||||
redirect
|
||||
end
|
||||
|
||||
test "logged in tournament owner should update a rule" do
|
||||
rule = MatAssignmentRule.create!(mat_id: @mat.id, tournament_id: @tournament.id, weight_classes: [1, 2, 3])
|
||||
sign_in_owner
|
||||
update_rule(rule.id)
|
||||
assert_redirected_to tournament_mat_assignment_rules_path(@tournament)
|
||||
rule.reload
|
||||
assert_equal [4, 5, 6], rule.weight_classes
|
||||
end
|
||||
|
||||
test "logged in tournament delegate should update a rule" do
|
||||
rule = MatAssignmentRule.create!(mat_id: @mat.id, tournament_id: @tournament.id, weight_classes: [1, 2, 3])
|
||||
sign_in_tournament_delegate
|
||||
update_rule(rule.id)
|
||||
assert_redirected_to tournament_mat_assignment_rules_path(@tournament)
|
||||
rule.reload
|
||||
assert_equal [4, 5, 6], rule.weight_classes
|
||||
end
|
||||
|
||||
test "logged in tournament owner should destroy a rule" do
|
||||
rule = MatAssignmentRule.create!(mat_id: @mat.id, tournament_id: @tournament.id, weight_classes: [1, 2, 3])
|
||||
sign_in_owner
|
||||
assert_difference 'MatAssignmentRule.count', -1 do
|
||||
destroy_rule(rule.id)
|
||||
end
|
||||
assert_redirected_to tournament_mat_assignment_rules_path(@tournament)
|
||||
end
|
||||
|
||||
test "logged in tournament delegate should destroy a rule" do
|
||||
rule = MatAssignmentRule.create!(mat_id: @mat.id, tournament_id: @tournament.id, weight_classes: [1, 2, 3])
|
||||
sign_in_tournament_delegate
|
||||
assert_difference 'MatAssignmentRule.count', -1 do
|
||||
destroy_rule(rule.id)
|
||||
end
|
||||
assert_redirected_to tournament_mat_assignment_rules_path(@tournament)
|
||||
end
|
||||
|
||||
test "logged in user should not destroy a rule if not owner or delegate" do
|
||||
rule = MatAssignmentRule.create!(mat_id: @mat.id, tournament_id: @tournament.id, weight_classes: [1, 2, 3])
|
||||
sign_in_non_owner
|
||||
assert_no_difference 'MatAssignmentRule.count' do
|
||||
destroy_rule(rule.id)
|
||||
end
|
||||
redirect
|
||||
end
|
||||
end
|
||||
@@ -648,4 +648,18 @@ class TournamentsControllerTest < ActionController::TestCase
|
||||
post :calculate_team_scores, params: { id: 1 }
|
||||
redirect
|
||||
end
|
||||
|
||||
test "only owner can set user_id when creating a new tournament" do
|
||||
# Sign in as owner and create a new tournament
|
||||
sign_in_owner
|
||||
post :create, params: { tournament: { name: 'New Tournament', address: '123 Main St', director: 'Director', director_email: 'director@example.com', date: '2024-01-01', tournament_type: 'Pool to bracket', is_public: false } }
|
||||
new_tournament = Tournament.last
|
||||
assert_equal users(:one).id, new_tournament.user_id, "The owner should be assigned as the user_id on creation"
|
||||
|
||||
# Sign in as non-owner and try to update the tournament without changing user_id
|
||||
sign_in_non_owner
|
||||
patch :update, params: { id: new_tournament.id, tournament: { name: 'Updated Tournament', is_public: true } }
|
||||
updated_tournament = Tournament.find(new_tournament.id)
|
||||
assert_equal users(:one).id, updated_tournament.user_id, "The user_id should not change when the tournament is edited by a non-owner"
|
||||
end
|
||||
end
|
||||
|
||||
195
test/integration/mat_assignment_rules_test.rb
Normal file
195
test/integration/mat_assignment_rules_test.rb
Normal file
@@ -0,0 +1,195 @@
|
||||
require 'test_helper'
|
||||
|
||||
class MatAssignmentRules < ActionDispatch::IntegrationTest
|
||||
def setup
|
||||
create_double_elim_tournament_1_6_with_multiple_weights_and_multiple_mats(6, 5, 3) # 6 wrestlers, 5 weights, 3 mats
|
||||
end
|
||||
|
||||
test "Mat assignment works with no mat assignment rules" do
|
||||
@tournament.reset_and_fill_bout_board
|
||||
assert @tournament.mats.first.matches.first != nil
|
||||
end
|
||||
|
||||
test "Mat assignment only assigns matches for a certain weight" do
|
||||
assignment_weight_id = @tournament.weights.second.id
|
||||
mat = @tournament.mats.first
|
||||
|
||||
mat_assignment_rule = MatAssignmentRule.new(
|
||||
mat_id: mat.id,
|
||||
weight_classes: [assignment_weight_id],
|
||||
tournament_id: @tournament.id
|
||||
)
|
||||
assert mat_assignment_rule.save, "Mat assignment rule should be saved successfully"
|
||||
|
||||
@tournament.reload
|
||||
@tournament.reset_and_fill_bout_board
|
||||
|
||||
mat.reload
|
||||
assigned_matches = mat.matches.reload
|
||||
|
||||
assert_not_empty assigned_matches, "Matches should have been assigned to the mat"
|
||||
assert assigned_matches.all? { |match| match.weight_id == assignment_weight_id },
|
||||
"All matches assigned to the mat should belong to the specified weight class"
|
||||
end
|
||||
|
||||
test "Mat assignment only assigns matches for round 2" do
|
||||
mat = @tournament.mats.first
|
||||
|
||||
mat_assignment_rule = MatAssignmentRule.new(
|
||||
mat_id: mat.id,
|
||||
rounds: [2],
|
||||
tournament_id: @tournament.id
|
||||
)
|
||||
assert mat_assignment_rule.save, "Mat assignment rule should be saved successfully"
|
||||
|
||||
@tournament.reload
|
||||
@tournament.reset_and_fill_bout_board
|
||||
|
||||
mat.reload
|
||||
assigned_matches = mat.matches.reload
|
||||
|
||||
assert_not_empty assigned_matches, "Matches should have been assigned to the mat"
|
||||
assert assigned_matches.all? { |match| match.round == 2 },
|
||||
"All matches assigned to the mat should only be for round 2"
|
||||
end
|
||||
|
||||
test "Mat assignment only assigns matches for bracket position 1/2" do
|
||||
mat = @tournament.mats.first
|
||||
|
||||
mat_assignment_rule = MatAssignmentRule.new(
|
||||
mat_id: mat.id,
|
||||
bracket_positions: ['1/2'],
|
||||
tournament_id: @tournament.id
|
||||
)
|
||||
assert mat_assignment_rule.save, "Mat assignment rule should be saved successfully"
|
||||
|
||||
@tournament.reload
|
||||
@tournament.reset_and_fill_bout_board
|
||||
|
||||
mat.reload
|
||||
assigned_matches = mat.matches.reload
|
||||
|
||||
assert_not_empty assigned_matches, "Matches should have been assigned to the mat"
|
||||
assert assigned_matches.all? { |match| match.bracket_position == '1/2' },
|
||||
"All matches assigned to the mat should only be for bracket_position 1/2"
|
||||
end
|
||||
|
||||
test "Mat assignment only assigns matches for a certain weight, round, and bracket position" do
|
||||
assignment_weight_id = @tournament.weights.first.id # Use the first weight
|
||||
mat = @tournament.mats.first
|
||||
|
||||
last_round = @tournament.matches.maximum(:round) # Dynamically fetch the last round
|
||||
finals_bracket_position = '1/2' # Finals bracket position
|
||||
|
||||
# Verify that there are matches in the last round with the '1/2' bracket position
|
||||
relevant_matches = @tournament.matches.where(
|
||||
weight_id: assignment_weight_id,
|
||||
round: last_round,
|
||||
bracket_position: finals_bracket_position
|
||||
)
|
||||
assert_not_empty relevant_matches, "There should be matches in the last round with the '1/2' bracket position"
|
||||
|
||||
mat_assignment_rule = MatAssignmentRule.new(
|
||||
mat_id: mat.id,
|
||||
weight_classes: [assignment_weight_id],
|
||||
rounds: [last_round], # Use the last round dynamically
|
||||
bracket_positions: [finals_bracket_position], # Use '1/2' as the bracket position
|
||||
tournament_id: @tournament.id
|
||||
)
|
||||
assert mat_assignment_rule.save, "Mat assignment rule should be saved successfully"
|
||||
|
||||
@tournament.reload
|
||||
@tournament.reset_and_fill_bout_board
|
||||
|
||||
mat.reload
|
||||
assigned_matches = mat.matches.reload
|
||||
|
||||
assert_not_empty assigned_matches, "Matches should have been assigned to the mat"
|
||||
|
||||
assert(
|
||||
assigned_matches.all? do |match|
|
||||
match.weight_id == assignment_weight_id &&
|
||||
match.round == last_round &&
|
||||
match.bracket_position == finals_bracket_position
|
||||
end,
|
||||
"All matches assigned to the mat should satisfy all conditions (weight, round, and bracket position)"
|
||||
)
|
||||
end
|
||||
|
||||
test "No matches assigned when no matches meet rule criteria" do
|
||||
mat = @tournament.mats.first
|
||||
|
||||
mat_assignment_rule = MatAssignmentRule.new(
|
||||
mat_id: mat.id,
|
||||
weight_classes: [-1], # Nonexistent weight ID
|
||||
tournament_id: @tournament.id
|
||||
)
|
||||
assert mat_assignment_rule.save, "Mat assignment rule should be saved successfully"
|
||||
|
||||
@tournament.reload
|
||||
@tournament.reset_and_fill_bout_board
|
||||
|
||||
mat.reload
|
||||
assigned_matches = mat.matches.reload
|
||||
|
||||
assert_empty assigned_matches, "No matches should have been assigned to the mat"
|
||||
end
|
||||
|
||||
test "Multiple mats follow their respective rules" do
|
||||
mat1 = @tournament.mats.first
|
||||
mat2 = @tournament.mats.second
|
||||
|
||||
mat1_rule = MatAssignmentRule.new(
|
||||
mat_id: mat1.id,
|
||||
weight_classes: [@tournament.weights.first.id],
|
||||
tournament_id: @tournament.id
|
||||
)
|
||||
assert mat1_rule.save, "Mat 1 assignment rule should be saved successfully"
|
||||
|
||||
mat2_rule = MatAssignmentRule.new(
|
||||
mat_id: mat2.id,
|
||||
rounds: [3],
|
||||
tournament_id: @tournament.id
|
||||
)
|
||||
assert mat2_rule.save, "Mat 2 assignment rule should be saved successfully"
|
||||
|
||||
@tournament.reload
|
||||
@tournament.reset_and_fill_bout_board
|
||||
|
||||
mat1.reload
|
||||
mat2.reload
|
||||
|
||||
mat1_matches = mat1.matches.reload
|
||||
mat2_matches = mat2.matches.reload
|
||||
|
||||
assert_not_empty mat1_matches, "Matches should have been assigned to Mat 1"
|
||||
assert_not_empty mat2_matches, "Matches should have been assigned to Mat 2"
|
||||
|
||||
assert mat1_matches.all? { |match| match.weight_id == @tournament.weights.first.id },
|
||||
"All matches assigned to Mat 1 should be for the specified weight class"
|
||||
|
||||
assert mat2_matches.all? { |match| match.round == 3 },
|
||||
"All matches assigned to Mat 2 should be for the specified round"
|
||||
end
|
||||
|
||||
test "No matches assigned in an empty tournament" do
|
||||
# Use the tournament created in the setup
|
||||
@tournament.matches.destroy_all # Remove all matches to simulate an empty tournament
|
||||
|
||||
mat = @tournament.mats.first
|
||||
|
||||
mat_assignment_rule = MatAssignmentRule.new(
|
||||
mat_id: mat.id,
|
||||
rounds: [1], # Arbitrary round; no matches should exist anyway
|
||||
tournament_id: @tournament.id
|
||||
)
|
||||
assert mat_assignment_rule.save, "Mat assignment rule should be saved successfully"
|
||||
|
||||
@tournament.reset_and_fill_bout_board
|
||||
|
||||
mat.reload
|
||||
assigned_matches = mat.matches.reload
|
||||
|
||||
assert_empty assigned_matches, "No matches should have been assigned for an empty tournament"
|
||||
end
|
||||
end
|
||||
@@ -104,16 +104,53 @@ class ActiveSupport::TestCase
|
||||
return @tournament
|
||||
end
|
||||
|
||||
def create_double_elim_tournament_1_6_with_multiple_weights_and_multiple_mats(number_of_wrestlers, number_of_weights, number_of_mats)
|
||||
@tournament = Tournament.new
|
||||
@tournament.name = "Test Tournament"
|
||||
@tournament.address = "some place"
|
||||
@tournament.director = "some guy"
|
||||
@tournament.director_email = "test@test.com"
|
||||
@tournament.tournament_type = "Regular Double Elimination 1-6"
|
||||
@tournament.date = "2015-12-30"
|
||||
@tournament.is_public = true
|
||||
@tournament.save
|
||||
|
||||
@school = School.new
|
||||
@school.name = "Test"
|
||||
@school.tournament_id = @tournament.id
|
||||
@school.save
|
||||
|
||||
@mats = []
|
||||
(1..number_of_mats).each do |mat_number|
|
||||
mat = Mat.new
|
||||
mat.name = "Mat #{mat_number}"
|
||||
mat.tournament_id = @tournament.id
|
||||
mat.save
|
||||
@mats << mat
|
||||
end
|
||||
|
||||
(1..number_of_weights).each do |weight_number|
|
||||
weight = Weight.new
|
||||
weight.max = 100 + weight_number
|
||||
weight.tournament_id = @tournament.id
|
||||
weight.save
|
||||
create_wrestlers_for_weight_for_double_elim(weight, @school, number_of_wrestlers, 1)
|
||||
end
|
||||
|
||||
GenerateTournamentMatches.new(@tournament).generate
|
||||
return @tournament
|
||||
end
|
||||
|
||||
def create_wrestlers_for_weight_for_double_elim(weight, school, number_of_wrestlers, naming_start_number)
|
||||
naming_number = naming_start_number
|
||||
for number in (1..number_of_wrestlers) do
|
||||
(1..number_of_wrestlers).each do |number|
|
||||
wrestler = Wrestler.new
|
||||
wrestler.name = "Test#{naming_number}"
|
||||
wrestler.school_id = school.id
|
||||
wrestler.weight_id = weight.id
|
||||
wrestler.original_seed = naming_number
|
||||
wrestler.save
|
||||
naming_number = naming_number + 1
|
||||
naming_number += 1
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user