|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class AnswerTest < ActiveSupport::TestCase |
| 4 | + test "should belong to person" do |
| 5 | + answer = answers(:kathryn_meal) |
| 6 | + assert_equal people(:Kathryn), answer.person |
| 7 | + end |
| 8 | + |
| 9 | + test "should belong to question" do |
| 10 | + answer = answers(:kathryn_meal) |
| 11 | + assert_equal questions(:meal_choice), answer.question |
| 12 | + end |
| 13 | + |
| 14 | + test "should validate uniqueness of person and question combination" do |
| 15 | + # Try to create duplicate answer for same person and question |
| 16 | + duplicate_answer = Answer.new( |
| 17 | + person: people(:Kathryn), |
| 18 | + question: questions(:meal_choice), |
| 19 | + answer_value: "Beef" |
| 20 | + ) |
| 21 | + |
| 22 | + assert_not duplicate_answer.valid? |
| 23 | + assert_includes duplicate_answer.errors[:person_id], "has already been taken" |
| 24 | + end |
| 25 | + |
| 26 | + test "should allow same question for different people" do |
| 27 | + answer = Answer.new( |
| 28 | + person: people(:Arthur), |
| 29 | + question: questions(:meal_choice), |
| 30 | + answer_value: "Beef" |
| 31 | + ) |
| 32 | + |
| 33 | + assert answer.valid? |
| 34 | + end |
| 35 | + |
| 36 | + test "should allow different questions for same person" do |
| 37 | + # Create a new question first |
| 38 | + new_question = Question.create!( |
| 39 | + billable: billables(:two), |
| 40 | + question_text: "Another question", |
| 41 | + question_type: "textarea" |
| 42 | + ) |
| 43 | + |
| 44 | + answer = Answer.new( |
| 45 | + person: people(:Kathryn), |
| 46 | + question: new_question, |
| 47 | + answer_value: "Gluten free" |
| 48 | + ) |
| 49 | + |
| 50 | + assert answer.valid?, "Should allow same person to answer different questions" |
| 51 | + end |
| 52 | + |
| 53 | + test "should allow null answer_value" do |
| 54 | + answer = Answer.new( |
| 55 | + person: people(:Arthur), |
| 56 | + question: questions(:meal_choice), |
| 57 | + answer_value: nil |
| 58 | + ) |
| 59 | + |
| 60 | + assert answer.valid? |
| 61 | + end |
| 62 | + |
| 63 | + test "should allow empty string answer_value" do |
| 64 | + answer = Answer.new( |
| 65 | + person: people(:Arthur), |
| 66 | + question: questions(:meal_choice), |
| 67 | + answer_value: "" |
| 68 | + ) |
| 69 | + |
| 70 | + assert answer.valid? |
| 71 | + end |
| 72 | + |
| 73 | + test "should save radio button choice" do |
| 74 | + answer = Answer.create!( |
| 75 | + person: people(:Arthur), |
| 76 | + question: questions(:meal_choice), |
| 77 | + answer_value: "Vegetarian" |
| 78 | + ) |
| 79 | + |
| 80 | + assert_equal "Vegetarian", answer.answer_value |
| 81 | + end |
| 82 | + |
| 83 | + test "should save textarea answer" do |
| 84 | + answer = Answer.create!( |
| 85 | + person: people(:Arthur), |
| 86 | + question: questions(:dietary_restrictions), |
| 87 | + answer_value: "No shellfish, lactose intolerant" |
| 88 | + ) |
| 89 | + |
| 90 | + assert_equal "No shellfish, lactose intolerant", answer.answer_value |
| 91 | + end |
| 92 | + |
| 93 | + test "should update existing answer" do |
| 94 | + answer = answers(:kathryn_meal) |
| 95 | + assert_equal "Chicken", answer.answer_value |
| 96 | + |
| 97 | + answer.update!(answer_value: "Fish") |
| 98 | + assert_equal "Fish", answer.answer_value |
| 99 | + end |
| 100 | + |
| 101 | + test "should be destroyed when person is destroyed" do |
| 102 | + person = people(:Kathryn) |
| 103 | + answer_count = person.answers.count |
| 104 | + |
| 105 | + assert answer_count > 0, "Person should have answers" |
| 106 | + |
| 107 | + assert_difference 'Answer.count', -answer_count do |
| 108 | + person.destroy |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + test "should be destroyed when question is destroyed" do |
| 113 | + question = questions(:meal_choice) |
| 114 | + answer_count = question.answers.count |
| 115 | + |
| 116 | + assert answer_count > 0, "Question should have answers" |
| 117 | + |
| 118 | + assert_difference 'Answer.count', -answer_count do |
| 119 | + question.destroy |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments