summaryrefslogtreecommitdiff
path: root/test/services/tartiflette_scoring_service.rb
blob: 4988b0aca20276b7dc468af3805dec6eb3f1be0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
Copyright 2019--2025 Marius PETER