diff options
author | Marius Peter <wtt@marius-peter.com> | 2025-01-02 19:11:34 +0100 |
---|---|---|
committer | Marius Peter <wtt@marius-peter.com> | 2025-01-02 19:11:34 +0100 |
commit | b34d2b51174b511d59fa324d9f42abc75a1fa09a (patch) | |
tree | 7fa57e3e2e33f43bc7ae787cfbba414cc66ac8df | |
parent | 175111f9d84354dce00503525649197e9acb6382 (diff) |
Never too late for all tests to pass...
-rw-r--r-- | app/controllers/scores_controller.rb | 2 | ||||
-rw-r--r-- | app/services/tartiflette_scoring_service.rb | 10 | ||||
-rw-r--r-- | test/services/tartiflette_scoring_service_test.rb (renamed from test/services/tartiflette_scoring_service.rb) | 66 |
3 files changed, 41 insertions, 37 deletions
diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index 16e0e44..039f5df 100644 --- a/app/controllers/scores_controller.rb +++ b/app/controllers/scores_controller.rb @@ -1,7 +1,7 @@ class ScoresController < ApplicationController allow_unauthenticated_access before_action :set_tartiflette, only: [ :new, :create, :edit_all, :update_all ] - before_action :ensure_session_id, only: [:edit_all, :update_all] + before_action :ensure_session_id def new end diff --git a/app/services/tartiflette_scoring_service.rb b/app/services/tartiflette_scoring_service.rb index 6df2f0c..4e4bc76 100644 --- a/app/services/tartiflette_scoring_service.rb +++ b/app/services/tartiflette_scoring_service.rb @@ -10,11 +10,11 @@ class TartifletteScoringService end def self.submit_scores(tartiflette, scores, session) - scores.each do |criterium_id, score_params| + scores.each do |scoring_criterium_id, score_params| Score.create!( - session_id: session.id, + session_id: session[:id], tartiflette: tartiflette, - scoring_criterium_id: criterium_id, + scoring_criterium_id: scoring_criterium_id, value: score_params[:value] ) end @@ -32,6 +32,8 @@ class TartifletteScoringService raise StandardError, "Failed to update score: #{e.message}" end + private + def self.average_score(tartiflette) tartiflette.scores.average(:value).to_f end @@ -50,7 +52,7 @@ class TartifletteScoringService .scores .group_by { |score| score.scoring_criterium.category } .transform_values do |scores| - (scores.sum(&:value).to_f / scores.size).round(2) + scores.sum(&:value).to_f.round(2) end end diff --git a/test/services/tartiflette_scoring_service.rb b/test/services/tartiflette_scoring_service_test.rb index 4988b0a..2004da0 100644 --- a/test/services/tartiflette_scoring_service.rb +++ b/test/services/tartiflette_scoring_service_test.rb @@ -3,54 +3,59 @@ require "test_helper" class TartifletteScoringServiceTest < ActiveSupport::TestCase setup do @tartiflette = tartiflettes(:one) - @existing_score = scores(:one) - @session = { id: @existing_score.session_id } end test "should check if tartiflette is already scored" do - @session[:scored_tartiflettes] = [ @tartiflette.id ] - assert TartifletteScoringService.scored?(@tartiflette, @session) + session = { scored_tartiflettes: [ @tartiflette.id ] } + assert TartifletteScoringService.scored?(@tartiflette, session) end test "should mark tartiflette as scored" do - TartifletteScoringService.mark_as_scored(@tartiflette, @session) - assert_includes @session[:scored_tartiflettes], @tartiflette.id + session = { id: SecureRandom.uuid } + TartifletteScoringService.mark_as_scored(@tartiflette, session) + assert_includes session[:scored_tartiflettes], @tartiflette.id end test "should submit new scores for tartiflette" do - scores = { - "1" => { value: 4, id: nil } # New score for criterium_id 1 - } + scoring_criterium_id = scoring_criteria(:one).id + new_session = { id: SecureRandom.uuid } + scores = { scoring_criterium_id => { value: 4 } } assert_nothing_raised do - TartifletteScoringService.submit_scores(@tartiflette, scores, @session) + TartifletteScoringService.submit_scores(@tartiflette, scores, new_session) end - # Assertions for new score - new_score = Score.find_by(scoring_criterium_id: 1, tartiflette: @tartiflette) + new_score = Score.find_by(scoring_criterium_id: scoring_criterium_id, + tartiflette: @tartiflette, + session_id: new_session[:id]) assert_not_nil new_score, "New score should be created" assert_equal 4, new_score.value - assert_equal @session.id, new_score.session_id + assert_equal new_session[:id], new_score.session_id end test "should update existing scores for tartiflette" do + session = { id: SecureRandom.uuid } + existing_score = Score.create!( + tartiflette: @tartiflette, + scoring_criterium: scoring_criteria(:one), + value: 1, + session_id: session[:id] + ) scores = { - "2" => { value: 5, id: @existing_score.id } # Update existing score + existing_score.id => { value: 5 } } - assert_nothing_raised do - TartifletteScoringService.update_scores(@tartiflette, scores, @session) + TartifletteScoringService.update_scores(@tartiflette, scores, session) end - # Assertions for updated score - @existing_score.reload - assert_equal 5, @existing_score.value + existing_score.reload + assert_equal 5, existing_score.value end test "should raise error for invalid data" do scores = { - "1" => { value: nil, id: nil }, # Missing value - "2" => { value: 5, id: nil } # Valid score + "1" => { value: nil, id: nil }, + "2" => { value: 5, id: nil } } assert_raises(StandardError, /Failed to process scores/) do @@ -65,41 +70,38 @@ class TartifletteScoringServiceTest < ActiveSupport::TestCase end test "should calculate average of all scores for a tartiflette" do - session_id = SecureRandom.uuid Score.create!( tartiflette: @tartiflette, scoring_criterium: scoring_criteria(:one), value: 1, - session_id: session_id + session_id: SecureRandom.uuid ) Score.create!( tartiflette: @tartiflette, scoring_criterium: scoring_criteria(:two), value: 4, - session_id: session_id + session_id: SecureRandom.uuid ) average_score = TartifletteScoringService.average_score(@tartiflette) assert_equal 2, average_score, "The average score should be 2" end test "should calculate average score by category for a tartiflette" do - session_id = SecureRandom.uuid + session = { id: SecureRandom.uuid } Score.create!( tartiflette: @tartiflette, - scoring_criterium: scoring_criteria(:one), + scoring_criterium: scoring_criteria(:four), value: 4, - session_id: session_id + session_id: session[:id] ) Score.create!( tartiflette: @tartiflette, - scoring_criterium: scoring_criteria(:two), + scoring_criterium: scoring_criteria(:five), value: 5, - session_id: session_id + session_id: session[:id] ) averages = TartifletteScoringService.average_score_by_category(@tartiflette) - assert_equal 4.0, averages[scoring_criteria(:one)] - assert_equal 5.0, averages[scoring_criteria(:one)] + assert_equal 4.5, averages[scoring_criteria(:four).category] end - end |