diff options
author | Marius Peter <marius.peter@tutanota.com> | 2024-12-29 15:14:43 +0100 |
---|---|---|
committer | Marius Peter <marius.peter@tutanota.com> | 2024-12-29 15:14:43 +0100 |
commit | be2a93525069de2dfa3c23b0c23e7a9f7ad4c03d (patch) | |
tree | b5493e9d35d024ce7be072ec2168b4a98ba0e63f /test |
First commit.
Diffstat (limited to 'test')
-rw-r--r-- | test/application_system_test_case.rb | 5 | ||||
-rw-r--r-- | test/controllers/.keep | 0 | ||||
-rw-r--r-- | test/controllers/home_controller_test.rb | 16 | ||||
-rw-r--r-- | test/controllers/scores_controller_test.rb | 70 | ||||
-rw-r--r-- | test/fixtures/files/.keep | 0 | ||||
-rw-r--r-- | test/fixtures/scores.yml | 11 | ||||
-rw-r--r-- | test/fixtures/scoring_criteria.yml | 50 | ||||
-rw-r--r-- | test/fixtures/tartiflettes.yml | 7 | ||||
-rw-r--r-- | test/fixtures/users.yml | 9 | ||||
-rw-r--r-- | test/helpers/.keep | 0 | ||||
-rw-r--r-- | test/integration/.keep | 0 | ||||
-rw-r--r-- | test/mailers/.keep | 0 | ||||
-rw-r--r-- | test/mailers/previews/passwords_mailer_preview.rb | 7 | ||||
-rw-r--r-- | test/models/.keep | 0 | ||||
-rw-r--r-- | test/models/score_test.rb | 41 | ||||
-rw-r--r-- | test/models/scoring_criterium_test.rb | 21 | ||||
-rw-r--r-- | test/models/tartiflette_test.rb | 11 | ||||
-rw-r--r-- | test/models/user_test.rb | 7 | ||||
-rw-r--r-- | test/services/tartiflette_scoring_service.rb | 48 | ||||
-rw-r--r-- | test/system/.keep | 0 | ||||
-rw-r--r-- | test/test_helper.rb | 15 |
21 files changed, 318 insertions, 0 deletions
diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb new file mode 100644 index 0000000..cee29fd --- /dev/null +++ b/test/application_system_test_case.rb @@ -0,0 +1,5 @@ +require "test_helper" + +class ApplicationSystemTestCase < ActionDispatch::SystemTestCase + driven_by :selenium, using: :headless_chrome, screen_size: [ 1400, 1400 ] +end diff --git a/test/controllers/.keep b/test/controllers/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/controllers/.keep diff --git a/test/controllers/home_controller_test.rb b/test/controllers/home_controller_test.rb new file mode 100644 index 0000000..aab02bb --- /dev/null +++ b/test/controllers/home_controller_test.rb @@ -0,0 +1,16 @@ +require "test_helper" + +class HomeControllerTest < ActionDispatch::IntegrationTest + test "should get index" do + get root_url + assert_response :success + assert_select "h1", "World Tartiflette Tour 2024" + end + + test "should list tartiflettes in the index" do + tartiflette = Tartiflette.create!(scoring_id: 1) + get root_url + assert_response :success + assert_select "li", tartiflette.scoring_id.to_s + end +end diff --git a/test/controllers/scores_controller_test.rb b/test/controllers/scores_controller_test.rb new file mode 100644 index 0000000..8fb4cb8 --- /dev/null +++ b/test/controllers/scores_controller_test.rb @@ -0,0 +1,70 @@ +require "test_helper" + +class ScoresControllerTest < ActionDispatch::IntegrationTest + setup do + @tartiflette = Tartiflette.create!(scoring_id: 1) + @criterium = ScoringCriterium.create!(name: "Taste", category: "Flavor") + @score = Score.create!(tartiflette: @tartiflette, scoring_criterium: @criterium, value: 4) + end + + test "should get new score form" do + get new_tartiflette_score_path(@tartiflette) + assert_response :success + assert_select "form" + assert_select "select[name=?]", "scores[#{@criterium.id}][value]" + end + + test "should create scores for tartiflette" do + assert_difference("Score.count", 1) do + post tartiflette_scores_path(@tartiflette), params: { + scores: { @criterium.id => { value: 5 } } + } + end + assert_redirected_to root_path + end + + # test "should not create scores if already scored" do + # session = { :scored_tartiflettes => [ @tartiflette.id ] } + # assert_no_difference("Score.count") do + # post tartiflette_scores_path(@tartiflette), params: { + # scores: { @criterium.id => { value: 5 } } + # } + # end + # assert_redirected_to root_path + # assert_match /Vous avez déja noté cette tartiflette/, flash[:alert] + # end + + test "should not create scores with invalid data" do + assert_no_difference("Score.count") do + post tartiflette_scores_path(@tartiflette), params: { + scores: { @criterium.id => { value: nil } } + } + end + assert_response :unprocessable_entity + end + + test "should get edit scores form" do + get tartiflette_edit_scores_path(@tartiflette) + assert_response :success + assert_select "form" + assert_select "select[name=?]", "scores[#{@criterium.id}][value]" + end + + test "should update scores for tartiflette" do + patch tartiflette_update_scores_path(@tartiflette), params: { + scores: { @score.id => { value: 3 } } + } + assert_redirected_to root_path + @score.reload + assert_equal 3, @score.value + end + + # test "should not update scores with invalid data" do + # patch tartiflette_update_scores_path(@tartiflette), params: { + # scores: { @score.id => { value: nil } } + # } + # @score.reload + # assert_not_equal nil, @score.value + # assert_redirected_to tartiflette_edit_scores_path(@tartiflette) + # end +end diff --git a/test/fixtures/files/.keep b/test/fixtures/files/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/fixtures/files/.keep diff --git a/test/fixtures/scores.yml b/test/fixtures/scores.yml new file mode 100644 index 0000000..b71217b --- /dev/null +++ b/test/fixtures/scores.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + tartiflette: one + scoring_criterium: one + value: 1 + +two: + tartiflette: two + scoring_criterium: two + value: 2 diff --git a/test/fixtures/scoring_criteria.yml b/test/fixtures/scoring_criteria.yml new file mode 100644 index 0000000..e469210 --- /dev/null +++ b/test/fixtures/scoring_criteria.yml @@ -0,0 +1,50 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + category: visuel + name: présence de la pastille + +two: + category: visuel + name: croûte dorée + +three: + category: visuel + name: lardons apparents + + +four: + category: texture + name: Pommes de terre fondantes + +five: + category: texture + name: lardons grillés + +six: + category: texture + name: oignons biens cuits + + +seven: + category: goût + name: Reblochon savoureux + +eight: + category: goût + name: lardons appétants + +nine: + category: goût + name: vin blanc équilibré + + +ten: + category: special + name: on en reveut ! +eleven: + category: special + name: Quelqu'un cherche à dépasser les maestros... +twelve: + category: special + name: on reconnaît la patte du tartifleur diff --git a/test/fixtures/tartiflettes.yml b/test/fixtures/tartiflettes.yml new file mode 100644 index 0000000..ad75608 --- /dev/null +++ b/test/fixtures/tartiflettes.yml @@ -0,0 +1,7 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + scoring_id: 1 + +two: + scoring_id: 2 diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..0951563 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,9 @@ +<% password_digest = BCrypt::Password.create("password") %> + +one: + email_address: one@example.com + password_digest: <%= password_digest %> + +two: + email_address: two@example.com + password_digest: <%= password_digest %> diff --git a/test/helpers/.keep b/test/helpers/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/helpers/.keep diff --git a/test/integration/.keep b/test/integration/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/integration/.keep diff --git a/test/mailers/.keep b/test/mailers/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/mailers/.keep diff --git a/test/mailers/previews/passwords_mailer_preview.rb b/test/mailers/previews/passwords_mailer_preview.rb new file mode 100644 index 0000000..01d07ec --- /dev/null +++ b/test/mailers/previews/passwords_mailer_preview.rb @@ -0,0 +1,7 @@ +# Preview all emails at http://localhost:3000/rails/mailers/passwords_mailer +class PasswordsMailerPreview < ActionMailer::Preview + # Preview this email at http://localhost:3000/rails/mailers/passwords_mailer/reset + def reset + PasswordsMailer.reset(User.take) + end +end diff --git a/test/models/.keep b/test/models/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/models/.keep diff --git a/test/models/score_test.rb b/test/models/score_test.rb new file mode 100644 index 0000000..a895158 --- /dev/null +++ b/test/models/score_test.rb @@ -0,0 +1,41 @@ +require "test_helper" + +class ScoreTest < ActiveSupport::TestCase + def setup + @score = scores(:one) + end + + test "should be valid with valid attributes" do + assert @score.valid? + end + + test "should have associated tartiflette" do + @score.tartiflette = nil + assert_not @score.valid?, "Score has no associated tartiflette." + end + + test "should have associated scoring_criterium" do + @score.scoring_criterium = nil + assert_not @score.valid?, "Score has no associated scoring_criterium." + end + + test "should have value" do + @score.value = nil + assert_not @score.valid?, "Score has no value." + end + + test "value should be between 1 and 5" do + @score.value = 3 + assert @score.valid?, "Score value is invalid." + end + + test "value should be greater than or equal to 1" do + @score.value = 0 + assert_not @score.valid?, "Score is 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." + end +end diff --git a/test/models/scoring_criterium_test.rb b/test/models/scoring_criterium_test.rb new file mode 100644 index 0000000..754e876 --- /dev/null +++ b/test/models/scoring_criterium_test.rb @@ -0,0 +1,21 @@ +require "test_helper" + +class ScoringCriteriumTest < ActiveSupport::TestCase + def setup + @scoring_criterium = scoring_criteria(:one) + end + + test "should be valid with valid attributes" do + assert @scoring_criterium.valid? + end + + test "should have category" do + @scoring_criterium.category = nil + assert_not @scoring_criterium.valid?, "Scoring Criterium has no category." + end + + test "should have name" do + @scoring_criterium.name = nil + assert_not @scoring_criterium.valid?, "Scoring Criterium has no name." + end +end diff --git a/test/models/tartiflette_test.rb b/test/models/tartiflette_test.rb new file mode 100644 index 0000000..63a4150 --- /dev/null +++ b/test/models/tartiflette_test.rb @@ -0,0 +1,11 @@ +require "test_helper" + +class TartifletteTest < ActiveSupport::TestCase + def setup + @tartiflette = tartiflettes(:one) + end + + test "should be valid with valid attributes" do + assert @tartiflette.valid? + end +end diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..5c07f49 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/services/tartiflette_scoring_service.rb b/test/services/tartiflette_scoring_service.rb new file mode 100644 index 0000000..6796342 --- /dev/null +++ b/test/services/tartiflette_scoring_service.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class TartifletteScoringServiceTest < ActiveSupport::TestCase + setup do + @tartiflette = Tartiflette.new(scoring_id: 1) + @session = {} + 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 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) + 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) + end +end diff --git a/test/system/.keep b/test/system/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/system/.keep diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..0c22470 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,15 @@ +ENV["RAILS_ENV"] ||= "test" +require_relative "../config/environment" +require "rails/test_help" + +module ActiveSupport + class TestCase + # Run tests in parallel with specified workers + parallelize(workers: :number_of_processors) + + # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. + fixtures :all + + # Add more helper methods to be used by all tests here... + end +end |