Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group seeds by top level model + plus data tables #28

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/oaken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def update(id, **attributes)
attributes = super

if record = @type.find_by(id: identify(id))
record.update!(**attributes)
record.tap { _1.update!(**attributes) }
else
@type.create!(id: identify(id), **attributes)
end
Expand Down
27 changes: 8 additions & 19 deletions test/oaken_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,25 @@ def test_accessing_fixture
assert_equal [users.kasper, users.coworker], accounts.business.users
end

def test_default_attributes_override
assert_equal "Big Business Co.", accounts.business.name
end

def test_default_attributes_last_into_test
users.update :homer
assert_equal [accounts.business], users.homer.accounts
end

def test_default_attributes_block
users.with accounts: [accounts.update(:home_co)] do
def test_default_attributes
users.with name: -> { id.to_s.humanize }, accounts: [accounts.update(:home_co, name: "Home Co.")] do
users.update :homer
end
assert_equal "Homer", users.homer.name
assert_equal [accounts.home_co], users.homer.accounts

users.update :homer
assert_equal [accounts.business], users.homer.accounts
end

def test_updating_fixture
users.update :kasper, name: "Kasper2"
assert_equal "Kasper2", users.kasper.name
kasper = users.update :kasper, name: "Kasper2"
assert_equal "Kasper2", kasper.name
end

def test_upserting_vs_updating
assert_equal "Nice!", comments.praise.title
assert_equal "Basic", plans.basic.title

error = assert_raises RuntimeError do
comments.update :salty, title: "foo"
plans.update :salty, title: "foo", price_cents: 0
end
assert_equal "after_create", error.message
assert_equal "after_save", error.message
end
end
7 changes: 0 additions & 7 deletions test/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
accounts.with name: -> { id.to_s.humanize }
accounts.update :business, name: "Big Business Co."

users.with name: -> { id.to_s.capitalize }, accounts: [accounts.business]
users.update :kasper
users.update :coworker

comments.upsert :praise, title: "Nice!"
4 changes: 4 additions & 0 deletions test/seeds/accounts/business.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
business = accounts.update :business, name: "Big Business Co."

users.update :kasper, name: "Kasper", accounts: [business]
users.update :coworker, name: "Coworker", accounts: [business]
1 change: 1 addition & 0 deletions test/seeds/data/plans.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plans.upsert :basic, title: "Basic", price_cents: 1000
7 changes: 4 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
t.timestamps
end

create_table :comments, force: true do |t|
create_table :plans, force: true do |t|
t.string :title, null: false
t.integer :price_cents, null: false
t.timestamps
end

Expand All @@ -52,8 +53,8 @@ class User < ActiveRecord::Base
has_many :accounts, through: :memberships
end

class Comment < ActiveRecord::Base
after_create { raise "after_create" }
class Plan < ActiveRecord::Base
after_save { raise "after_save" }
end

class YamlRecord < ActiveRecord::Base
Expand Down