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

Add part-way compatibility with Fixtures #24

Merged
merged 2 commits into from
Aug 22, 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
10 changes: 6 additions & 4 deletions lib/oaken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,26 @@ def update(id, **attributes)

class Stored::ActiveRecord < Stored::Abstract
def find(id)
@type.find(id.hash)
@type.find identify id
end

def update(id, **attributes)
attributes = super

if record = @type.find_by(id: id.hash)
if record = @type.find_by(id: identify(id))
record.update!(**attributes)
else
@type.create!(id: id.hash, **attributes)
@type.create!(id: identify(id), **attributes)
end
end

def upsert(id, **attributes)
attributes = super
@type.new(attributes).validate!
@type.upsert({ id: id.hash, **attributes })
@type.upsert({ id: identify(id), **attributes })
end

private def identify(id) = ::ActiveRecord::FixtureSet.identify(id, @type.type_for_attribute(@type.primary_key).type)
end

module Data
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/yaml_records.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
first:
name: "YAML"
account: business
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swapping to FixtureSet.identify above means that we can safely refer to the account created with accounts.update :business in here and get it connected.

5 changes: 5 additions & 0 deletions test/oaken_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def test_that_it_has_a_version_number
refute_nil ::Oaken::VERSION
end

def test_fixture_yml_compatibility
assert_equal "YAML", YamlRecord.first.name
assert_equal accounts.business, YamlRecord.first.account
end

def test_accessing_fixture
assert_equal "Kasper", users.kasper.name
assert_equal "Coworker", users.coworker.name
Expand Down
13 changes: 13 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
t.string :title, null: false
t.timestamps
end

create_table :yaml_records, force: true do |t|
t.integer :account_id, null: false
t.string :name, null: false
end
end

class Account < ActiveRecord::Base
Expand All @@ -51,12 +56,20 @@ class Comment < ActiveRecord::Base
after_create { raise "after_create" }
end

class YamlRecord < ActiveRecord::Base
belongs_to :account
end

require "active_record/fixtures"

Oaken::Data.records.preregister ActiveRecord::Base.connection.tables.grep_v(/^ar_/)
Oaken::Data.load_from "test/seeds"

class Oaken::Test < ActiveSupport::TestCase
include ActiveRecord::TestFixtures
self.fixture_path = "test/fixtures"
self.use_transactional_tests = true
fixtures :all

include Oaken::Data
end