From 79bafec6935dadf625bc160fc9d2a4ecb7d06253 Mon Sep 17 00:00:00 2001 From: Malaber Date: Sun, 9 Mar 2025 21:05:41 +0100 Subject: [PATCH 1/6] Add advancing teams into tournaments show endpoint --- app/controllers/tournaments_controller.rb | 2 +- app/models/tournament.rb | 12 ++++++++++++ spec/controllers/tournaments_controller_spec.rb | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index a40c6bf..5751202 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -29,7 +29,7 @@ class TournamentsController < ApplicationController if show_params.fetch(:simple, 'false') == 'true' render json: @tournament, serializer: SimpleTournamentSerializer else - render json: @tournament, include: '**' + render json: @tournament.as_json(methods: :teams_advancing_from_group_stage) end end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index d91ec72..1f02015 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -20,6 +20,18 @@ class Tournament < ApplicationRecord [stages.map(&:matches), stages.map { |s| s.groups.map(&:matches) }].flatten end + def teams_advancing_from_group_stage + group_stage = self.group_stage + return [] if group_stage.nil? + + GroupStageService.get_advancing_teams(group_stage) + end + + def group_stage + # get the stage with level -1 (group stage) + stages.find_by(level: -1) + end + private def generate_code diff --git a/spec/controllers/tournaments_controller_spec.rb b/spec/controllers/tournaments_controller_spec.rb index ae8f469..9dad030 100644 --- a/spec/controllers/tournaments_controller_spec.rb +++ b/spec/controllers/tournaments_controller_spec.rb @@ -109,6 +109,20 @@ RSpec.describe TournamentsController, type: :controller do body = deserialize_response response expect(body[:stages]).to be_nil expect(body[:teams]).to be_nil + expect(body[:advancing_teams]).to be_nil + end + end + context 'on a group stage tournament' do + before do + @group_stage_tournament = create(:group_stage_tournament) + end + + it 'includes advancing teams' do + get :show, params: { id: @group_stage_tournament.to_param } + body = deserialize_response response + advancing_teams = body[:teams_advancing_from_group_stage] + expected_advancing_teams = GroupStageService.get_advancing_teams(@group_stage_tournament.group_stage) + expect(advancing_teams).to match_array(expected_advancing_teams) end end end From 4449314cf675cd82f7d61c2c795509bc63b55574 Mon Sep 17 00:00:00 2001 From: Malaber Date: Sun, 9 Mar 2025 21:07:36 +0100 Subject: [PATCH 2/6] Improve "normal" controller tests --- spec/controllers/tournaments_controller_spec.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/controllers/tournaments_controller_spec.rb b/spec/controllers/tournaments_controller_spec.rb index 9dad030..43d43bf 100644 --- a/spec/controllers/tournaments_controller_spec.rb +++ b/spec/controllers/tournaments_controller_spec.rb @@ -100,7 +100,12 @@ RSpec.describe TournamentsController, type: :controller do it 'returns the requested tournament' do get :show, params: { id: @tournament.to_param } - expect(deserialize_response(response)[:id].to_i).to eq(@tournament.id) + json = deserialize_response(response) + expect(json[:id].to_i).to eq(@tournament.id) + expect(json[:name]).to eq(@tournament.name) + expect(json[:description]).to eq(@tournament.description) + expect(json[:public]).to eq(@tournament.public) + expect(json[:teams_advancing_from_group_stage]).to eq([]) end context 'with simple=true parameter' do From d1cbe8a3547908d4c0c39f8b4a68eb9e8ccf20e2 Mon Sep 17 00:00:00 2001 From: Malaber Date: Thu, 13 Mar 2025 12:52:12 +0100 Subject: [PATCH 3/6] Fix tests --- spec/controllers/tournaments_controller_spec.rb | 10 +++++++++- spec/services/group_stage_service_spec.rb | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/spec/controllers/tournaments_controller_spec.rb b/spec/controllers/tournaments_controller_spec.rb index 43d43bf..1663157 100644 --- a/spec/controllers/tournaments_controller_spec.rb +++ b/spec/controllers/tournaments_controller_spec.rb @@ -123,10 +123,18 @@ RSpec.describe TournamentsController, type: :controller do end it 'includes advancing teams' do + # mock GroupStageService.get_advancing_teams(@group_stage_tournament.group_stage) to include 4 teams + expected_advancing_teams = create_list(:team, 4) + allow(GroupStageService).to receive(:get_advancing_teams).and_return(expected_advancing_teams) + get :show, params: { id: @group_stage_tournament.to_param } body = deserialize_response response advancing_teams = body[:teams_advancing_from_group_stage] - expected_advancing_teams = GroupStageService.get_advancing_teams(@group_stage_tournament.group_stage) + # check array is not empty + expect(advancing_teams).not_to be_empty + + # cast json to teams and compare + advancing_teams = advancing_teams.map { |team| Team.find(team[:id]) } expect(advancing_teams).to match_array(expected_advancing_teams) end end diff --git a/spec/services/group_stage_service_spec.rb b/spec/services/group_stage_service_spec.rb index 8073ef2..95c800e 100644 --- a/spec/services/group_stage_service_spec.rb +++ b/spec/services/group_stage_service_spec.rb @@ -52,6 +52,8 @@ RSpec.describe GroupStageService do end end + # TODO test get_advancing_teams when test data for running group stage is ready + describe '#get_group_object_from' do it 'returns a group' do group = GroupStageService.get_group_object_from(@teams1) From dd520089ec92844077543ee5c1c29dd6f6a3f9fe Mon Sep 17 00:00:00 2001 From: Malaber Date: Fri, 14 Mar 2025 13:22:52 +0100 Subject: [PATCH 4/6] Fix missing stages and teams in tournaments show --- app/controllers/tournaments_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 5751202..a40c6bf 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -29,7 +29,7 @@ class TournamentsController < ApplicationController if show_params.fetch(:simple, 'false') == 'true' render json: @tournament, serializer: SimpleTournamentSerializer else - render json: @tournament.as_json(methods: :teams_advancing_from_group_stage) + render json: @tournament, include: '**' end end From 963812cb0cd2661ca7a64d060fc77f108bfed6d4 Mon Sep 17 00:00:00 2001 From: Malaber Date: Fri, 14 Mar 2025 13:23:10 +0100 Subject: [PATCH 5/6] Annotate advancing teams that advance from group stage --- app/serializers/tournament_serializer.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/serializers/tournament_serializer.rb b/app/serializers/tournament_serializer.rb index 25582e1..df02e38 100644 --- a/app/serializers/tournament_serializer.rb +++ b/app/serializers/tournament_serializer.rb @@ -4,9 +4,19 @@ class TournamentSerializer < SimpleTournamentSerializer attributes :description, :playoff_teams_amount, :instant_finalists_amount, :intermediate_round_participants_amount has_many :stages - has_many :teams attribute :owner_username do object.owner.username end + + attribute :teams do + adv_teams = object.group_stage ? GroupStageService.get_advancing_teams(object.group_stage) : [] + object.teams.map do |team| + { + id: team.id, + name: team.name, + advancing_from_group_stage: adv_teams.include?(team) + } + end + end end From 4d62f6777a44984870fcfbcfd4db7886928ff3d8 Mon Sep 17 00:00:00 2001 From: Malaber Date: Fri, 14 Mar 2025 13:32:15 +0100 Subject: [PATCH 6/6] Adjust tests to match new version of advancing teams --- spec/controllers/tournaments_controller_spec.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/spec/controllers/tournaments_controller_spec.rb b/spec/controllers/tournaments_controller_spec.rb index 918c39d..b7dfc87 100644 --- a/spec/controllers/tournaments_controller_spec.rb +++ b/spec/controllers/tournaments_controller_spec.rb @@ -108,7 +108,6 @@ RSpec.describe TournamentsController, type: :controller do expect(json[:name]).to eq(@tournament.name) expect(json[:description]).to eq(@tournament.description) expect(json[:public]).to eq(@tournament.public) - expect(json[:teams_advancing_from_group_stage]).to eq([]) end context 'with simple=true parameter' do @@ -117,7 +116,6 @@ RSpec.describe TournamentsController, type: :controller do body = deserialize_response response expect(body[:stages]).to be_nil expect(body[:teams]).to be_nil - expect(body[:advancing_teams]).to be_nil end end context 'on a group stage tournament' do @@ -126,13 +124,14 @@ RSpec.describe TournamentsController, type: :controller do end it 'includes advancing teams' do - # mock GroupStageService.get_advancing_teams(@group_stage_tournament.group_stage) to include 4 teams - expected_advancing_teams = create_list(:team, 4) + # take 4 of the teams in the tournament and set them as advancing teams + expected_advancing_teams = @group_stage_tournament.teams.take(4) allow(GroupStageService).to receive(:get_advancing_teams).and_return(expected_advancing_teams) get :show, params: { id: @group_stage_tournament.to_param } body = deserialize_response response - advancing_teams = body[:teams_advancing_from_group_stage] + # get the advancing teams from the teams array in the response ( they have advancing_from_group_stage = true ) + advancing_teams = body[:teams].select { |team| team[:advancing_from_group_stage] } # check array is not empty expect(advancing_teams).not_to be_empty