diff options
Diffstat (limited to 'test/services/tartiflette_scoring_service.rb')
-rw-r--r-- | test/services/tartiflette_scoring_service.rb | 77 |
1 files changed, 48 insertions, 29 deletions
diff --git a/test/services/tartiflette_scoring_service.rb b/test/services/tartiflette_scoring_service.rb index 0a66d5b..4988b0a 100644 --- a/test/services/tartiflette_scoring_service.rb +++ b/test/services/tartiflette_scoring_service.rb @@ -17,20 +17,30 @@ class TartifletteScoringServiceTest < ActiveSupport::TestCase assert_includes @session[:scored_tartiflettes], @tartiflette.id end - test "should process scores for tartiflette" do + test "should submit new scores for tartiflette" do scores = { - "1" => { value: 4, id: nil }, # New score for criterium_id 1 - "2" => { value: 5, id: @existing_score.id } # Update existing score + "1" => { value: 4, id: nil } # New score for criterium_id 1 } assert_nothing_raised do - TartifletteScoringService.process_scores(@tartiflette, scores, @session) + TartifletteScoringService.submit_scores(@tartiflette, scores, @session) end # Assertions for new score new_score = Score.find_by(scoring_criterium_id: 1, tartiflette: @tartiflette) + 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 @session.id, new_score.session_id + end + + test "should update existing scores for tartiflette" do + scores = { + "2" => { value: 5, id: @existing_score.id } # Update existing score + } + + assert_nothing_raised do + TartifletteScoringService.update_scores(@tartiflette, scores, @session) + end # Assertions for updated score @existing_score.reload @@ -55,32 +65,41 @@ class TartifletteScoringServiceTest < ActiveSupport::TestCase end test "should calculate average of all scores for a tartiflette" do - Score.create!(tartiflette: @tartiflette, - scoring_criterium: scoring_criteria(:one), - value: 4) - Score.create!(tartiflette: @tartiflette, - scoring_criterium: scoring_criteria(:two), - value: 5) - assert_equal 4.5, TartifletteScoringService.average_score(@tartiflette) + session_id = SecureRandom.uuid + Score.create!( + tartiflette: @tartiflette, + scoring_criterium: scoring_criteria(:one), + value: 1, + session_id: session_id + ) + + Score.create!( + tartiflette: @tartiflette, + scoring_criterium: scoring_criteria(:two), + value: 4, + session_id: session_id + ) + 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 - Score.create!(tartiflette: @tartiflette, - scoring_criterium: scoring_criteria(:one), - value: 1) - Score.create!(tartiflette: @tartiflette, - scoring_criterium: scoring_criteria(:one), - value: 2) - Score.create!(tartiflette: @tartiflette, - scoring_criterium: scoring_criteria(:four), - value: 4) - Score.create!(tartiflette: @tartiflette, - scoring_criterium: scoring_criteria(:four), - value: 5) - average_score_by_category = { scoring_criteria(:one)[:category] => 1.5, - scoring_criteria(:four)[:category] => 4.5 - } - assert_equal average_score_by_category, - TartifletteScoringService.average_score_by_category(@tartiflette) + session_id = SecureRandom.uuid + Score.create!( + tartiflette: @tartiflette, + scoring_criterium: scoring_criteria(:one), + value: 4, + session_id: session_id + ) + Score.create!( + tartiflette: @tartiflette, + scoring_criterium: scoring_criteria(:two), + value: 5, + 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)] end + end |