|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +class Graph::Mutations::FilmRateTest < ActiveSupport::TestCase |
| 4 | + def setup |
| 5 | + @context = { |
| 6 | + user: @user = User.first |
| 7 | + } |
| 8 | + |
| 9 | + @query_string = " |
| 10 | + mutation ($input: FilmRateInput!) { |
| 11 | + filmRate(input: $input) { |
| 12 | + film { |
| 13 | + title |
| 14 | + } |
| 15 | + rating { |
| 16 | + rating |
| 17 | + } |
| 18 | + errors { |
| 19 | + field |
| 20 | + message |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + " |
| 25 | + |
| 26 | + @film = Film.first |
| 27 | + |
| 28 | + @variables = { |
| 29 | + "input" => { |
| 30 | + "filmId" => @film.to_global_id.to_s, |
| 31 | + "rating" => 5, |
| 32 | + } |
| 33 | + } |
| 34 | + end |
| 35 | + |
| 36 | + test "raises an execution error when user is not logged in" do |
| 37 | + expected = { |
| 38 | + "data" => { "filmRate" => nil}, |
| 39 | + "errors" => [{ |
| 40 | + "message" => "Authentication required to rate a film.", |
| 41 | + "locations" => [{ "line" => 3, "column" => 9}], |
| 42 | + "path" => ["filmRate"]} |
| 43 | + ] |
| 44 | + } |
| 45 | + |
| 46 | + result = Graph::Schema.execute(@query_string, variables: @variables, context: {}) |
| 47 | + assert_equal expected, result |
| 48 | + end |
| 49 | + |
| 50 | + test "raises an execution error when filmId is invalid" do |
| 51 | + @variables['input']['filmId'] = 'invalid' |
| 52 | + expected = { |
| 53 | + "data" => { "filmRate" => nil}, |
| 54 | + "errors" => [{ |
| 55 | + "message" => "Invalid filmId.", |
| 56 | + "locations" => [{ "line" => 3, "column" => 9}], |
| 57 | + "path" => ["filmRate"]} |
| 58 | + ] |
| 59 | + } |
| 60 | + |
| 61 | + result = Graph::Schema.execute(@query_string, variables: @variables, context: @context) |
| 62 | + assert_equal expected, result |
| 63 | + end |
| 64 | + |
| 65 | + test "returns error when an invalid rating is inputted" do |
| 66 | + @variables['input']['rating'] = 100 |
| 67 | + expected = { |
| 68 | + "data" => { |
| 69 | + "filmRate" => { |
| 70 | + "film" => { |
| 71 | + "title" => @film.title |
| 72 | + }, |
| 73 | + "rating" => nil, |
| 74 | + "errors" => [ |
| 75 | + { "field" => "rating", "message" => "must be less than or equal to 5" } |
| 76 | + ] |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + result = Graph::Schema.execute(@query_string, variables: @variables, context: @context) |
| 82 | + assert_equal expected, result |
| 83 | + end |
| 84 | + |
| 85 | + test "creates a new rating on success" do |
| 86 | + expected = { |
| 87 | + "data" => { |
| 88 | + "filmRate" => { |
| 89 | + "film" => { |
| 90 | + "title" => @film.title |
| 91 | + }, |
| 92 | + "rating" => { |
| 93 | + "rating" => 5 |
| 94 | + }, |
| 95 | + "errors" => [], |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + assert_difference "Rating.count", 1 do |
| 101 | + result = Graph::Schema.execute(@query_string, variables: @variables, context: @context) |
| 102 | + assert_equal expected, result |
| 103 | + end |
| 104 | + |
| 105 | + rating = Rating.last |
| 106 | + assert_equal @film, rating.film |
| 107 | + assert_equal @variables['input']['rating'], rating.rating |
| 108 | + assert_equal @user, rating.user |
| 109 | + end |
| 110 | + |
| 111 | + test "updates existing rating if user already rated film" do |
| 112 | + expected = { |
| 113 | + "data" => { |
| 114 | + "filmRate" => { |
| 115 | + "film" => { |
| 116 | + "title" => @film.title |
| 117 | + }, |
| 118 | + "rating" => { |
| 119 | + "rating" => 5 |
| 120 | + }, |
| 121 | + "errors" => [], |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + rating = @user.ratings.create(film: @film, rating: 1) |
| 127 | + |
| 128 | + assert_difference "Rating.count", 0 do |
| 129 | + result = Graph::Schema.execute(@query_string, variables: @variables, context: @context) |
| 130 | + assert_equal expected, result |
| 131 | + end |
| 132 | + |
| 133 | + rating.reload |
| 134 | + assert_equal @variables['input']['rating'], rating.rating |
| 135 | + end |
| 136 | +end |
0 commit comments