diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/models/score_test.rb | 20 | ||||
| -rw-r--r-- | test/services/tartiflette_scoring_service.rb | 42 | 
2 files changed, 54 insertions, 8 deletions
| diff --git a/test/models/score_test.rb b/test/models/score_test.rb index a895158..ecf5558 100644 --- a/test/models/score_test.rb +++ b/test/models/score_test.rb @@ -11,31 +11,39 @@ class ScoreTest < ActiveSupport::TestCase    test "should have associated tartiflette" do      @score.tartiflette = nil -    assert_not @score.valid?, "Score has no associated tartiflette." +    assert_not @score.valid?, "Score should not be valid without an associated tartiflette."    end    test "should have associated scoring_criterium" do      @score.scoring_criterium = nil -    assert_not @score.valid?, "Score has no associated scoring_criterium." +    assert_not @score.valid?, "Score should not be valid without an associated scoring criterium."    end    test "should have value" do      @score.value = nil -    assert_not @score.valid?, "Score has no value." +    assert_not @score.valid?, "Score should not be valid without a value."    end    test "value should be between 1 and 5" do      @score.value = 3 -    assert @score.valid?, "Score value is invalid." +    assert @score.valid?, "Score with a value of 3 should be valid."    end    test "value should be greater than or equal to 1" do      @score.value = 0 -    assert_not @score.valid?, "Score is less than 1." +    assert_not @score.valid?, "Score should not be valid with a value less than 1."    end    test "value should be less than or equal to 5" do      @score.value = 6 -    assert_not @score.valid?, "Score is greater than 5." +    assert_not @score.valid?, "Score should not be valid with a value greater than 5."    end + +  test "should have a session_id" do +    @score.session_id = nil +    assert_not @score.valid?, "Score should not be valid without a session_id." +    assert_includes @score.errors[:session_id], "can't be blank" +  end + +    end diff --git a/test/services/tartiflette_scoring_service.rb b/test/services/tartiflette_scoring_service.rb index 6796342..0a66d5b 100644 --- a/test/services/tartiflette_scoring_service.rb +++ b/test/services/tartiflette_scoring_service.rb @@ -2,8 +2,9 @@ require "test_helper"  class TartifletteScoringServiceTest < ActiveSupport::TestCase    setup do -    @tartiflette = Tartiflette.new(scoring_id: 1) -    @session = {} +    @tartiflette = tartiflettes(:one) +    @existing_score = scores(:one) +    @session = { id: @existing_score.session_id }    end    test "should check if tartiflette is already scored" do @@ -16,6 +17,43 @@ class TartifletteScoringServiceTest < ActiveSupport::TestCase      assert_includes @session[:scored_tartiflettes], @tartiflette.id    end +  test "should process 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 +    } + +    assert_nothing_raised do +      TartifletteScoringService.process_scores(@tartiflette, scores, @session) +    end + +    # Assertions for new score +    new_score = Score.find_by(scoring_criterium_id: 1, tartiflette: @tartiflette) +    assert_equal 4, new_score.value +    assert_equal @session[:id], new_score.session_id + +    # 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      Score.create!(tartiflette: @tartiflette,                    scoring_criterium: scoring_criteria(:one), | 
