From 0f76b07fb2dc2cf8b35c6315c6f993667e924f08 Mon Sep 17 00:00:00 2001 From: Danilo Grieco Date: Thu, 10 Nov 2022 10:13:52 +0100 Subject: [PATCH] Added tests on custom foreign key --- .../chrono_model/time_machine/history_spec.rb | 6 +++++ spec/support/time_machine/structure.rb | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/spec/chrono_model/time_machine/history_spec.rb b/spec/chrono_model/time_machine/history_spec.rb index 6700d3c3..8157e015 100644 --- a/spec/chrono_model/time_machine/history_spec.rb +++ b/spec/chrono_model/time_machine/history_spec.rb @@ -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 diff --git a/spec/support/time_machine/structure.rb b/spec/support/time_machine/structure.rb index 376ec74f..7913f549 100644 --- a/spec/support/time_machine/structure.rb +++ b/spec/support/time_machine/structure.rb @@ -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| @@ -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 @@ -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