From b34d2b51174b511d59fa324d9f42abc75a1fa09a Mon Sep 17 00:00:00 2001 From: Marius Peter Date: Thu, 2 Jan 2025 19:11:34 +0100 Subject: Ensure session[:id] before scoring: all tests now pass. Never too late for all tests to pass... --- test/services/tartiflette_scoring_service.rb | 105 --------------------- test/services/tartiflette_scoring_service_test.rb | 107 ++++++++++++++++++++++ 2 files changed, 107 insertions(+), 105 deletions(-) delete mode 100644 test/services/tartiflette_scoring_service.rb create mode 100644 test/services/tartiflette_scoring_service_test.rb (limited to 'test') diff --git a/test/services/tartiflette_scoring_service.rb b/test/services/tartiflette_scoring_service.rb deleted file mode 100644 index 4988b0a..0000000 --- a/test/services/tartiflette_scoring_service.rb +++ /dev/null @@ -1,105 +0,0 @@ -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) - end - - test "should mark tartiflette as scored" do - 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 - } - - assert_nothing_raised do - 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 - 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 - 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 - } - - assert_raises(StandardError, /Failed to process scores/) do - TartifletteScoringService.process_scores(@tartiflette, scores, @session) - end - end - - test "should mark tartiflette as scored in session" do - session = { scored_tartiflettes: [] } - TartifletteScoringService.mark_as_scored(@tartiflette, session) - assert_includes session[:scored_tartiflettes], @tartiflette.id - 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 - ) - - 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 - 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 diff --git a/test/services/tartiflette_scoring_service_test.rb b/test/services/tartiflette_scoring_service_test.rb new file mode 100644 index 0000000..2004da0 --- /dev/null +++ b/test/services/tartiflette_scoring_service_test.rb @@ -0,0 +1,107 @@ +require "test_helper" + +class TartifletteScoringServiceTest < ActiveSupport::TestCase + setup do + @tartiflette = tartiflettes(:one) + end + + test "should check if tartiflette is already scored" do + session = { scored_tartiflettes: [ @tartiflette.id ] } + assert TartifletteScoringService.scored?(@tartiflette, session) + end + + test "should mark tartiflette as scored" do + 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 + 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, new_session) + end + + 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 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 = { + existing_score.id => { value: 5 } + } + assert_nothing_raised do + TartifletteScoringService.update_scores(@tartiflette, scores, session) + end + + existing_score.reload + assert_equal 5, existing_score.value + end + + test "should raise error for invalid data" do + scores = { + "1" => { value: nil, id: nil }, + "2" => { value: 5, id: nil } + } + + assert_raises(StandardError, /Failed to process scores/) do + TartifletteScoringService.process_scores(@tartiflette, scores, @session) + end + end + + test "should mark tartiflette as scored in session" do + session = { scored_tartiflettes: [] } + TartifletteScoringService.mark_as_scored(@tartiflette, session) + assert_includes session[:scored_tartiflettes], @tartiflette.id + end + + test "should calculate average of all scores for a tartiflette" do + Score.create!( + tartiflette: @tartiflette, + scoring_criterium: scoring_criteria(:one), + value: 1, + session_id: SecureRandom.uuid + ) + + Score.create!( + tartiflette: @tartiflette, + scoring_criterium: scoring_criteria(:two), + value: 4, + 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 } + Score.create!( + tartiflette: @tartiflette, + scoring_criterium: scoring_criteria(:four), + value: 4, + session_id: session[:id] + ) + Score.create!( + tartiflette: @tartiflette, + scoring_criterium: scoring_criteria(:five), + value: 5, + session_id: session[:id] + ) + averages = TartifletteScoringService.average_score_by_category(@tartiflette) + assert_equal 4.5, averages[scoring_criteria(:four).category] + end +end -- cgit v1.2.3