Skip to content

Commit aca446d

Browse files
committed
Refactor tests to use seed data.
1 parent c99cfbf commit aca446d

File tree

12 files changed

+62
-85
lines changed

12 files changed

+62
-85
lines changed

bin/reloader

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ LISTEN_DIRS = %w[
1818
test/test
1919
].map { |dir| File.join(PROJECT_ROOT, dir) }
2020

21-
def reload
21+
def reload(skip_seed: false)
2222
# Reload Puma.
2323
system("kill -USR1 $(cat #{PUMA_PID_PATH})")
2424

2525
# Run the test suite (generates coverage report).
26-
system("bin/rails test")
26+
system("#{"SKIP_SEED=1" if skip_seed} bin/rails test".strip)
2727

2828
# Generate brakeman report.
2929
system("bin/brakeman")
@@ -34,7 +34,7 @@ reload
3434

3535
listener = Listen.to(*LISTEN_DIRS) do |modified, added, removed|
3636
puts(modified: modified, added: added, removed: removed)
37-
reload
37+
reload(skip_seed: true)
3838
end
3939

4040
listener.start

test/app/models/email.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Email < ApplicationRecord
2-
belongs_to :user
2+
belongs_to :user, optional: true
33
has_one :billing_user, class_name: "User", inverse_of: :billing_email
44
end

test/app/models/user.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ class User < ApplicationRecord
99

1010
# This association is purposefully named differently than the column to test how this affects the
1111
# framework.
12-
belongs_to(
13-
:billing_email,
14-
optional: true,
15-
class_name: "Email",
16-
foreign_key: "email_id",
17-
)
12+
belongs_to(:billing_email, optional: true, class_name: "Email", foreign_key: "finance_email_id")
1813

1914
belongs_to :manager, class_name: "User", optional: true
2015
has_and_belongs_to_many :movies

test/db/migrate/20240906180000_test_app_tables.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ def change
2828
t.references(:user, null: true, foreign_key: {on_delete: :nullify})
2929
end
3030

31-
add_reference(:users, :email, foreign_key: {on_delete: :nullify}, index: {unique: true})
31+
add_reference(
32+
:users,
33+
:finance_email,
34+
foreign_key: {to_table: :emails, on_delete: :nullify},
35+
index: {unique: true},
36+
)
3237

3338
create_table(:phone_numbers) do |t|
3439
t.string(:number, null: false, default: "", index: {unique: true})

test/db/schema.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@
115115
t.integer "manager_id"
116116
t.datetime "created_at"
117117
t.datetime "updated_at"
118-
t.integer "email_id"
119-
t.index ["email_id"], name: "index_users_on_email_id", unique: true
118+
t.integer "finance_email_id"
119+
t.index ["finance_email_id"], name: "index_users_on_finance_email_id", unique: true
120120
t.index ["login"], name: "index_users_on_login", unique: true
121121
t.index ["manager_id"], name: "index_users_on_manager_id"
122122
end
@@ -130,6 +130,6 @@
130130
add_foreign_key "movies_users", "movies", on_delete: :cascade
131131
add_foreign_key "movies_users", "users", on_delete: :cascade
132132
add_foreign_key "phone_numbers", "users", on_delete: :cascade
133-
add_foreign_key "users", "emails", on_delete: :nullify
133+
add_foreign_key "users", "emails", column: "finance_email_id", on_delete: :nullify
134134
add_foreign_key "users", "users", column: "manager_id", on_delete: :nullify
135135
end

test/db/seeds.rb

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
# Seed data is used for the Demo API.
1+
# Seed data is used for the Demo API as well as initial test data in lieu of fixtures.
2+
3+
# Always clear the database before seeding.
4+
puts "Clearing database..."
5+
Email.delete_all
6+
Genre.delete_all
7+
Movie.delete_all
8+
PhoneNumber.delete_all
9+
User.delete_all
210

311
Faker::Config.random = Random.new(42)
12+
srand(42)
13+
14+
puts "Seed data loading..."
415

516
example = User.create!(
617
login: "example",
@@ -18,25 +29,37 @@
1829
example.update!(manager: admin)
1930

2031
1000.times do |_|
21-
# Create with faker data:
2232
legal_name = Faker::Name.name_with_middle
23-
short_name = legal_name.split(" ").first
24-
begin
25-
User.create!(
26-
login: Faker::Internet.username,
27-
legal_name: legal_name,
28-
short_name: short_name,
29-
age: rand(18..65),
30-
is_admin: rand < 0.2,
31-
balance: rand(0.0..10000.0),
32-
state: [0, 0, 0, 0, User.states.keys.sample].sample,
33-
status: User::STATUS_OPTS.keys.sample,
34-
day_start: ["7:30", "8:00", "8:30", "9:00", "9:30"].sample,
35-
last_reviewed_on: Time.zone.today - rand(0..365).days,
36-
manager: [nil, example, admin, User.first(10).sample].sample,
37-
)
38-
rescue ActiveRecord::RecordInvalid
33+
short_name = legal_name.gsub(/^[a-zA-Z]*\./, "").split(" ").first
34+
login = Faker::Internet.username(specifier: legal_name) + rand(1..100).to_s
35+
36+
# Generate either 0, 1, or 2 emails:
37+
erand = rand
38+
emails = (erand < 0.1 ? 0 : erand > 0.9 ? 2 : 1).times.map do |i|
39+
Email.create!(email: Faker::Internet.email(name: legal_name), is_primary: i == 0)
3940
end
41+
42+
u = User.create!(
43+
login: login,
44+
legal_name: legal_name,
45+
short_name: short_name,
46+
age: rand(18..65),
47+
is_admin: rand < 0.2,
48+
balance: rand(0.0..10000.0),
49+
state: [0, 0, 0, 0, User.states.keys.sample].sample,
50+
status: User::STATUS_OPTS.keys.sample,
51+
day_start: ["7:30", "8:00", "8:30", "9:00", "9:30"].sample,
52+
last_reviewed_on: Time.zone.today - rand(0..365).days,
53+
manager: [nil, nil, nil, example, admin, User.first(10).sample].sample,
54+
billing_email: rand < 0.5 ? emails.first : nil,
55+
emails: emails,
56+
)
57+
58+
# Triggers stack error on users controller somehow.
59+
if rand < 0.7
60+
PhoneNumber.create!(number: Faker::PhoneNumber.cell_phone, user: u)
61+
end
62+
rescue ActiveRecord::RecordInvalid
4063
end
4164

4265
# Created by copilot:
@@ -76,3 +99,5 @@
7699

77100
example.movies += Movie.all.sample(5)
78101
admin.movies += Movie.all.sample(20)
102+
103+
puts "Seed data loaded."

test/test/controllers/api/demo/movies_controller_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class Api::Demo::MoviesControllerTest < ActionController::TestCase
88

99
if defined?(Ransack)
1010
def test_ransack_simple
11-
get(:index, as: :json, params: {q: {price_gt: 9}})
11+
get(:index, as: :json, params: {q: {price_gt: 9}, page_size: 0})
1212
assert_response(:success)
13-
assert_equal(2, @response.parsed_body["results"].length)
13+
assert_equal(Movie.where("price > 9").count, @response.parsed_body.length)
1414
end
1515
end
1616

test/test/fixtures/genres.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

test/test/fixtures/movies.yml

Lines changed: 0 additions & 16 deletions
This file was deleted.

test/test/fixtures/users.yml

Lines changed: 0 additions & 19 deletions
This file was deleted.

test/test/integration/rescuing_unknown_formats_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ def test_rescue_unknown_format
2121
assert_response(:success)
2222
resp2 = @response.body
2323

24-
assert_equal(resp1, resp2)
24+
assert_equal(resp1, resp2.gsub(".jsom", ".json"))
2525
end
2626
end

test/test/test_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@
2626

2727
class ActiveSupport::TestCase
2828
end
29+
30+
Rails.application.load_seed unless ENV["SKIP_SEED"]

0 commit comments

Comments
 (0)