Skip to content

Commit

Permalink
Refactor tests to use fixtures.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregschmit committed Dec 15, 2024
1 parent 69fbc69 commit c81462e
Show file tree
Hide file tree
Showing 26 changed files with 155 additions and 73 deletions.
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ ruby ENV["CUSTOM_RUBY_VERSION"] || File.read(
RAILS_VERSION = Gem::Version.new(
ENV["RAILS_VERSION"] || File.read(File.expand_path(".rails-version", __dir__)).strip,
)

gem "puma"
gem "rails", "~> #{RAILS_VERSION}"
gem "rake"
gem "sqlite3", RAILS_VERSION >= Gem::Version.new("8") ? ">= 2" : "~> 1.4"
gem "puma"

gem "faker"
gem "rack-cors"

# Only Rails >=7 gems.
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ GEM
erubi (1.13.0)
et-orbi (1.2.11)
tzinfo
faker (3.5.1)
i18n (>= 1.8.11, < 2)
foreman (0.88.1)
fugit (1.11.1)
et-orbi (~> 1, >= 1.2.11)
Expand Down Expand Up @@ -335,6 +337,7 @@ DEPENDENCIES
binding_of_caller
byebug
cloudflare-rails
faker
foreman
httparty
kamal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Api::Test::UsersWithoutRescueUnknownFormatController < Api::TestController
class Api::Test::NoRescueUnknownFormatController < Api::TestController
include RESTFramework::ModelControllerMixin

self.fields = %w(id login)
Expand Down
5 changes: 3 additions & 2 deletions test/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@
rest_root :test
namespace :test do
rest_resources :users

rest_resources :added_select
rest_resources :bare_create, force_plural: true, only: [:create]
rest_resources :fields_hash_exclude
rest_resources :fields_hash_only_except
rest_resources :no_rescue_unknown_format
rest_resources :read_only
rest_resources :users_with_string_serializer
rest_resources :users_with_sub_fields
rest_resources :users_without_rescue_unknown_format
rest_resources :read_only

rest_route :network

Expand Down
10 changes: 9 additions & 1 deletion test/db/migrate/20240906180000_test_app_tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ class TestAppTables < ActiveRecord::Migration[6.0]
def change
create_table(:users) do |t|
t.string(:login, null: false, default: "", index: {unique: true})
t.boolean(:is_admin, default: false)
t.string(:legal_name, null: false, default: "")
t.string(:short_name, null: false, default: "")
t.integer(:age)

t.boolean(:is_admin, null: false, default: false)

t.decimal(:balance, precision: 8, scale: 2)

t.integer(:state, null: false, default: 0)
t.string(:status, null: false, default: "")

t.time(:day_start)
t.date(:last_reviewed_on)

t.references(:manager, foreign_key: {on_delete: :nullify, to_table: :users})

t.timestamps(null: true)
Expand Down
6 changes: 5 additions & 1 deletion test/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,15 @@

create_table "users", force: :cascade do |t|
t.string "login", default: "", null: false
t.boolean "is_admin", default: false
t.string "legal_name", default: "", null: false
t.string "short_name", default: "", null: false
t.integer "age"
t.boolean "is_admin", default: false, null: false
t.decimal "balance", precision: 8, scale: 2
t.integer "state", default: 0, null: false
t.string "status", default: "", null: false
t.time "day_start"
t.date "last_reviewed_on"
t.integer "manager_id"
t.datetime "created_at"
t.datetime "updated_at"
Expand Down
76 changes: 60 additions & 16 deletions test/db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Seed data is used for controller tests which need data to be useful, and also for the Demo API.
# Seed data is used for the Demo API.

Faker::Config.random = Random.new(42)

example = User.create!(
login: "example",
Expand All @@ -15,20 +17,62 @@
)
example.update!(manager: admin)

action = Genre.create!(name: "Action", description: "Action movies are fast-paced and exciting.")
_comedy = Genre.create!(name: "Comedy", description: "Comedy movies are funny.")
_drama = Genre.create!(name: "Drama", description: "Drama movies are serious.")
fantasy = Genre.create!(name: "Fantasy", description: "Fantasy movies are magical.")
adventure = Genre.create!(name: "Adventure", description: "Adventure movies are exciting.")
1000.times do |_|
# Create with faker data:
legal_name = Faker::Name.name_with_middle
short_name = legal_name.split(" ").first
begin
User.create!(
login: Faker::Internet.username,
legal_name: legal_name,
short_name: short_name,
age: rand(18..65),
is_admin: rand < 0.2,
balance: rand(0.0..10000.0),
state: [0, 0, 0, 0, User.states.keys.sample].sample,
status: User::STATUS_OPTS.keys.sample,
day_start: ["7:30", "8:00", "8:30", "9:00", "9:30"].sample,
last_reviewed_on: Time.zone.today - rand(0..365).days,
manager: [nil, example, admin, User.first(10).sample].sample,
)
rescue ActiveRecord::RecordInvalid
end
end

star_wars = Movie.create!(name: "Star Wars IV: A New Hope", price: 14.99, main_genre: action)
dark_knight = Movie.create!(name: "The Dark Knight", price: 13.99, main_genre: action)
fellowship = Movie.create!(
name: "The Lord of the Rings: The Fellowship of the Ring", price: 12.99, main_genre: adventure,
)
matrix = Movie.create!(name: "The Matrix", price: 14.50, main_genre: action)
_avengers = Movie.create!(name: "The Avengers", price: 7.00, main_genre: action)
_inception = Movie.create!(name: "Inception", price: 15.99, main_genre: fantasy)
# Created by copilot:
Genre.create!(name: "Action", description: "Action movies are fast-paced and exciting.")
Genre.create!(name: "Comedy", description: "Comedy movies are funny.")
Genre.create!(name: "Drama", description: "Drama movies are serious.")
Genre.create!(name: "Fantasy", description: "Fantasy movies are magical.")
Genre.create!(name: "Adventure", description: "Adventure movies are exciting.")
Genre.create!(name: "Sci-Fi", description: "Sci-Fi movies are futuristic.")
Genre.create!(name: "Horror", description: "Horror movies are scary.")
Genre.create!(name: "Thriller", description: "Thriller movies are suspenseful.")
Genre.create!(name: "Mystery", description: "Mystery movies are puzzling.")
Genre.create!(name: "Romance", description: "Romance movies are about love.")
Genre.create!(name: "Musical", description: "Musical movies have singing and dancing.")
Genre.create!(name: "Documentary", description: "Documentary movies are non-fiction.")
Genre.create!(name: "Animation", description: "Animation movies are animated.")
Genre.create!(name: "Family", description: "Family movies are for all ages.")
Genre.create!(name: "Western", description: "Western movies are about the American West.")
Genre.create!(name: "War", description: "War movies are about military conflict.")
Genre.create!(name: "History", description: "History movies are about the past.")
Genre.create!(name: "Biography", description: "Biography movies are about real people.")
Genre.create!(name: "Sport", description: "Sport movies are about athletics.")
Genre.create!(name: "Music", description: "Music movies are about musicians.")
Genre.create!(name: "Crime", description: "Crime movies are about criminals.")
Genre.create!(name: "Noir", description: "Noir movies are dark and cynical.")
Genre.create!(name: "Superhero", description: "Superhero movies are about heroes.")
Genre.create!(name: "Spy", description: "Spy movies are about espionage.")

50.times do |_|
Movie.create!(
name: Faker::Movie.title,
price: rand(5.0..20.0),
main_genre: Genre.all.sample,
)
rescue ActiveRecord::RecordInvalid
end

example.movies += [star_wars, dark_knight]
admin.movies += [star_wars, fellowship, matrix]
example.movies += Movie.all.sample(5)
admin.movies += Movie.all.sample(20)
1 change: 0 additions & 1 deletion test/test/controllers/api/demo/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Api::Demo::Base
def self.included(base)
base.class_attribute(:create_params)
base.class_attribute(:update_params)
base.setup { Rails.application.load_seed }
end

def test_index_with_filtering
Expand Down
4 changes: 2 additions & 2 deletions test/test/controllers/api/demo/movies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class Api::Demo::MoviesControllerTest < ActionController::TestCase

if defined?(Ransack)
def test_ransack_simple
get(:index, as: :json, params: {q: {price_gt: 15}})
get(:index, as: :json, params: {q: {price_gt: 9}})
assert_response(:success)
assert_equal(1, @response.parsed_body["results"].length)
assert_equal(2, @response.parsed_body["results"].length)
end
end

Expand Down
1 change: 0 additions & 1 deletion test/test/controllers/api/plain/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Api::Plain::Base
def self.included(base)
base.class_attribute(:create_params)
base.class_attribute(:update_params)
base.setup { Rails.application.load_seed }
end

def test_index_with_filtering
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require_relative "base"
require "test_helper"

class Api::Test::AddedSelectControllerTest < ActionController::TestCase
include BaseApi::TestControllerTests

def test_only_works
get(:index, as: :json, params: {only: "id,selected_value"})
assert_response(:success)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require_relative "base"
require "test_helper"

class Api::Test::BareCreateControllerTest < ActionController::TestCase
include BaseApi::TestControllerTests

def test_bare_create
post(:create, as: :json, params: {name: "Test Bare Create"})
assert_response(:success)
Expand Down
9 changes: 0 additions & 9 deletions test/test/controllers/api/test/base.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require_relative "base"
require "test_helper"

class Api::Test::FieldsHashOnlyExceptControllerTest < ActionController::TestCase
include BaseApi::TestControllerTests

def test_list
get(:index, as: :json)
assert_response(:success)
Expand Down
4 changes: 1 addition & 3 deletions test/test/controllers/api/test/network_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require_relative "base"
require "test_helper"

class Api::Test::NetworkControllerTest < ActionController::TestCase
include BaseApi::TestControllerTests

def test_cannot_list
assert_raises(ActionController::UrlGenerationError) do
get(:index)
Expand Down
4 changes: 1 addition & 3 deletions test/test/controllers/api/test/read_only_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require_relative "base"
require "test_helper"

class Api::Test::ReadOnlyControllerTest < ActionController::TestCase
include BaseApi::TestControllerTests

def test_list
get(:index, as: :json)
assert_response(:success)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require_relative "base"
require "test_helper"

class Api::Test::UsersWithStringSerializerControllerTest < ActionController::TestCase
include BaseApi::TestControllerTests
end
2 changes: 0 additions & 2 deletions test/test/controllers/render_json_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

if defined?(ActiveModel::Serializer)
class RenderJsonControllerTest < ActionController::TestCase
setup { Rails.application.load_seed }

def test_list_rest_serializer
get(:list_rest_serializer)
assert_response(:success)
Expand Down
15 changes: 15 additions & 0 deletions test/test/fixtures/genres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
action:
name: Action
description: Action movies are fast-paced and exciting.
comedy:
name: Comedy
description: Comedy movies are funny.
drama:
name: Drama
description: Drama movies are serious.
fantasy:
name: Fantasy
description: Fantasy movies are magical.
adventure:
name: Adventure
description: Adventure movies are exciting.
16 changes: 16 additions & 0 deletions test/test/fixtures/movies.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
dark_knight:
name: The Dark Knight
price: 10.0
main_genre: action
inception:
name: Inception
price: 12.0
main_genre: action
the_hangover:
name: The Hangover
price: 8.0
main_genre: comedy
the_shawshank_redemption:
name: The Shawshank Redemption
price: 9.0
main_genre: drama
19 changes: 19 additions & 0 deletions test/test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
example:
login: example
age: 23
balance: 20.34
state: 0
status: online
manager: admin
movies:
- dark_knight
- inception
admin:
login: admin
is_admin: true
age: 34
balance: 230.34
state: 0
status: offline
movies:
- the_shawshank_redemption
13 changes: 5 additions & 8 deletions test/test/integration/api/demo_routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ def test_can_get_users
assert_response(:success)
end

# TODO: Disabled because test is broken in Rails 8, but behavior tested manually and working.
def _test_can_not_get_network_resourceful_routes
assert_raises(ActionController::RoutingError) do
get("/api/demo/network.json")
end
assert_raises(ActionController::RoutingError) do
get("/api/demo/network")
end
def test_can_not_get_network_resourceful_routes
get("/api/demo/network.json")
assert_response(:not_found)
get("/api/demo/network")
assert_response(:not_found)
end
end
2 changes: 0 additions & 2 deletions test/test/integration/api/test_routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

# The goal of this test is to ensure that the proper routes are defined for API2.
class Api::TestRoutingTest < ActionDispatch::IntegrationTest
setup { Rails.application.load_seed }

def test_can_get_root
get("/api/test")
assert_response(:success)
Expand Down
10 changes: 3 additions & 7 deletions test/test/integration/rescuing_unknown_formats_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

# The goal of this test is to ensure unknown formats are rescued, when configured.
class RescuingUnknownFormatsTest < ActionDispatch::IntegrationTest
setup { Rails.application.load_seed }

# Test that an invalid format raises an uncaught exception.
# TODO: Disabled because test is broken in Rails 8, but behavior tested manually and working.
def _test_raise_unknown_format
assert_raises(ActionController::UnknownFormat) do
get("/api/test/users_without_rescue_unknown_format.jsom")
end
def test_raise_unknown_format
get("/api/test/no_rescue_unknown_format.jsom")
assert_response(:not_acceptable)
end

# Test that we rescue an unknown format and also that it defaults to :json.
Expand Down
2 changes: 0 additions & 2 deletions test/test/models/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ def self.included(base)
base.setup do
@model ||= self.class.name.match(/(.*)Test$/)[1].constantize
@title_field ||= TITLE_FIELDS.select { |f| f.to_s.in?(@model.column_names) }[0]

Rails.application.load_seed
end
end

Expand Down
Loading

0 comments on commit c81462e

Please sign in to comment.