Skip to content

Commit

Permalink
Added tests on custom foreign key
Browse files Browse the repository at this point in the history
  • Loading branch information
gridanjbf authored and tagliala committed Nov 27, 2022
1 parent d6baa7a commit 0f76b07
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions spec/chrono_model/time_machine/history_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,18 @@
subject(:historical_foo) { Foo::History.find(last_foo.hid) }

let(:last_foo) { Foo.history.last }
let!(:tar1) { Tar.create(foo: last_foo)}

it { is_expected.to eq last_foo }

it 'preserves associations' do
expect(historical_foo.bars).to eq last_foo.bars
end

it 'preserves associations that are using a different fkid' do
expect(historical_foo.tars).not_to be_empty
expect(historical_foo.tars).to eq(last_foo.tars)
end
end

context '.sorted' do
Expand Down
22 changes: 22 additions & 0 deletions spec/support/time_machine/structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ module ChronoTest::TimeMachine
t.string :name
t.integer :fooity
t.references :goo
t.string :refee_foo
end

adapter.create_table 'tars', temporal: true do |t|
t.string :name
t.string :foo_refering
end

adapter.change_table 'tars', temporal: true do
def up
add_reference :foos, column: :foo_refering, primary_key: :refee_foo
end
end

adapter.create_table 'moos', temporal: true do |t|
Expand Down Expand Up @@ -80,6 +92,15 @@ class ::Baz < ActiveRecord::Base
has_timeline with: :bar
end

class ::Tar < ActiveRecord::Base
include ChronoModel::TimeGate

belongs_to :foo, foreign_key: :foo_refering, primary_key: :refee_foo, class_name: 'Foo'

has_timeline with: :foo
end


class ::Boo < ActiveRecord::Base
include ChronoModel::TimeMachine

Expand All @@ -90,6 +111,7 @@ class ::Foo < ActiveRecord::Base
include ChronoModel::TimeMachine

has_many :bars
has_many :tars, foreign_key: :foo_refering, primary_key: :refee_foo
has_many :sub_bars, through: :bars
has_many :sub_sub_bars, through: :sub_bars

Expand Down

0 comments on commit 0f76b07

Please sign in to comment.