From a19df15e3039ce62ec5b585d7687370fa3770686 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Fri, 3 May 2024 18:10:37 +0200 Subject: [PATCH] Add failing spec for issue #283 --- spec/chrono_model/history_models_spec.rb | 4 ++ .../historical_association_spec.rb | 45 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 spec/chrono_model/time_machine/historical_association_spec.rb diff --git a/spec/chrono_model/history_models_spec.rb b/spec/chrono_model/history_models_spec.rb index d35b9ee9..b86a7720 100644 --- a/spec/chrono_model/history_models_spec.rb +++ b/spec/chrono_model/history_models_spec.rb @@ -27,6 +27,10 @@ expected['sections'] = Section::History if defined?(Section::History) expected['articles'] = Article::History if defined?(Article::History) + # historical_associations_spec + expected['countries'] = Country::History if defined?(Country::History) + expected['cities'] = City::History if defined?(City::History) + # sti_spec expected['animals'] = Animal::History if defined?(Animal::History) expected['elements'] = Element::History if defined?(Element::History) diff --git a/spec/chrono_model/time_machine/historical_association_spec.rb b/spec/chrono_model/time_machine/historical_association_spec.rb new file mode 100644 index 00000000..ce074170 --- /dev/null +++ b/spec/chrono_model/time_machine/historical_association_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'support/time_machine/structure' + +class Country < ActiveRecord::Base + include ChronoModel::TimeMachine + + has_many :cities +end + +class City < ActiveRecord::Base + include ChronoModel::TimeMachine + + belongs_to :country +end + +RSpec.describe ChronoModel::TimeMachine do + include ChronoTest::TimeMachine::Helpers + + describe 'historical association' do + adapter.create_table :countries, temporal: true do |t| + t.string :name + t.timestamps + end + + adapter.create_table :cities, temporal: true do |t| + t.references :country + t.string :name + t.timestamps + end + + it 'returns historical association at the last time when record is valid' do + country = Country.create name: 'Country' + city = country.cities.create name: 'City' + + city.transaction do + country.update_column :name, 'Country 2' + city.update_column :name, 'City 2' + end + + expect(country.history.first.cities.first.name).to eq 'City' + end + end +end