Skip to content

Commit

Permalink
More refactoring of tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregschmit committed Dec 14, 2024
1 parent 99b7ab6 commit 1fafe03
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 221 deletions.
16 changes: 0 additions & 16 deletions test/app/controllers/api/demo/marbles_controller.rb

This file was deleted.

7 changes: 0 additions & 7 deletions test/app/controllers/api/demo/users/marbles_controller.rb

This file was deleted.

3 changes: 0 additions & 3 deletions test/app/controllers/api/plain/marbles_controller.rb

This file was deleted.

9 changes: 9 additions & 0 deletions test/app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Movie < ApplicationRecord
has_one_attached :cover
has_many_attached :pictures

validates_presence_of :name
validates_uniqueness_of :name
validates_numericality_of :price, greater_than: 0, allow_nil: true

def self.ransackable_attributes(*args)
Expand All @@ -17,4 +19,11 @@ def self.ransackable_attributes(*args)
def self.ransackable_associations(*args)
return []
end

before_destroy do
if self.name == "Undestroyable"
errors.add(:base, "This record is undestroyable.")
throw(:abort)
end
end
end
3 changes: 0 additions & 3 deletions test/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
rest_root
rest_resources :emails
rest_resources :genres
rest_resources :marbles
rest_resources :movies
rest_resources :phone_numbers
rest_resources :users
Expand All @@ -34,11 +33,9 @@
rest_root
rest_resources :emails
rest_resources :genres
rest_resources :marbles
rest_resources :movies
rest_resources :phone_numbers
rest_resources :users do
rest_resources :marbles
rest_resources :movies
end
end
Expand Down
96 changes: 0 additions & 96 deletions test/test/controllers/api/demo/marbles_controller_test.rb

This file was deleted.

89 changes: 89 additions & 0 deletions test/test/controllers/api/demo/movies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,93 @@ def test_ransack_simple
assert_equal(1, @response.parsed_body["results"].length)
end
end

def test_bulk_create
post(
:create,
as: :json,
params: {_json: [{name: "test_bulk_1"}, {name: "test_bulk_2"}]},
)
assert_response(:success)
assert(@response.parsed_body.all? { |r| Movie.find(r["id"]) })
end

def test_bulk_create_with_error
post(
:create,
as: :json,
params: {_json: [{name: "test_bulk_1"}, {name: "test_bulk_1"}]},
)
assert_response(:success)
parsed_body = @response.parsed_body
assert(Movie.find(parsed_body[0]["id"]))
assert_nil(parsed_body[0]["errors"])
assert_nil(parsed_body[1]["id"])
assert(parsed_body[1]["errors"])
end

def test_bulk_update
movie1 = Movie.create!(name: "test_bulk_1", price: 4)
movie2 = Movie.create!(name: "test_bulk_2", price: 23)
old_count = Movie.count

patch(
:update_all,
as: :json,
params: {_json: [{id: movie1.id, price: 5}, {id: movie2.id, price: 24}]},
)
assert_response(:success)

movie1.reload
movie2.reload
assert_equal(5, movie1.price)
assert_equal(24, movie2.price)
assert_equal(old_count, Movie.count)
end

def test_bulk_update_with_error
movie1 = Movie.create!(name: "test_bulk_1", price: 4)
movie2 = Movie.create!(name: "test_bulk_2", price: 23)
old_count = Movie.count

patch(
:update_all,
as: :json,
params: {_json: [{id: movie1.id, price: 5}, {id: movie2.id, price: 24, name: nil}]},
)
assert_response(:success)
parsed_body = @response.parsed_body
assert_nil(parsed_body[0]["errors"])
assert(parsed_body[1]["errors"])

movie1.reload
movie2.reload
assert_equal(5, movie1.price)
assert_equal(23, movie2.price)
assert_equal(old_count, Movie.count)
end

def test_bulk_destroy
movie_names = ["Test Movie 1", "Test Movie 2"]
movie_names.each { |n| Movie.create!(name: n) }
movies = Movie.where(name: movie_names)
assert_equal(2, movies.count)
delete(:destroy_all, as: :json, params: {_json: movies.pluck(:id)})
assert_response(:success)
assert_equal(0, movies.count)
end

def test_bulk_destroy_validation_error
movie_names = ["Test Movie 1", "Undestroyable"]
movie_names.each { |n| Movie.create!(name: n) }
movies = Movie.where(name: movie_names)
assert_equal(2, movies.count)
delete(:destroy_all, as: :json, params: {_json: movies.pluck(:id)})
assert_response(:success)
parsed_body = @response.parsed_body
assert_equal(2, parsed_body.count)
assert_nil(parsed_body[0]["errors"])
assert(parsed_body[1]["errors"])
assert_equal(1, movies.count)
end
end
92 changes: 0 additions & 92 deletions test/test/controllers/api/plain/marbles_controller_test.rb

This file was deleted.

8 changes: 4 additions & 4 deletions test/test/integration/api/demo_routing_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "test_helper"

# The goal of this test is to ensure that the proper routes are defined for API1.
# The goal of this test is to ensure that the proper routes are defined for the demo API.
class Api::DemoRoutingTest < ActionDispatch::IntegrationTest
def test_can_get_root
get("/api/demo")
Expand All @@ -9,10 +9,10 @@ def test_can_get_root
assert_response(:success)
end

def test_can_get_marbles
get("/api/demo/marbles")
def test_can_get_users
get("/api/demo/users")
assert_response(:success)
get("/api/demo/marbles.json")
get("/api/demo/users.json")
assert_response(:success)
end

Expand Down

0 comments on commit 1fafe03

Please sign in to comment.