From 9b74fc18643a566ec8f1308cbc066c630d9be1bc Mon Sep 17 00:00:00 2001 From: Sai Srinivasan <37251147+sairamsrinivasan@users.noreply.github.com> Date: Thu, 30 May 2024 08:01:04 -0700 Subject: [PATCH] Add support to verify touch option (#246) --- CHANGELOG.md | 1 + README.md | 21 +++++++++++++++++++++ lib/matchers/associations.rb | 15 +++++++++++++++ lib/mongoid/rspec/version.rb | 2 +- spec/models/article.rb | 8 +++++++- spec/models/message.rb | 2 +- spec/models/permalink.rb | 2 ++ spec/models/record.rb | 4 +++- spec/models/user.rb | 3 ++- spec/unit/associations_spec.rb | 8 ++++---- 10 files changed, 57 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf3fb90..6fd7d8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * [#239](https://github.com/mongoid/mongoid-rspec/pull/239): Add support for if/unless validator options - [@knovoselic](https://github.com/knovoselic). * [#244](https://github.com/mongoid/mongoid-rspec/pull/244): Migration from TravisCI to GitHub Actions - [@skalibog](https://github.com/skalibog). * [#245](https://github.com/mongoid/mongoid-rspec/pull/245): Add support for Mongoid 9 - [@saisrinivasan](https://github.com/SairamSrinivasan). +* [#246](https://github.com/mongoid/mongoid-rspec/pull/246): Add support for touch options - [@saisrinivasan](https://github.com/SairamSrinivasan). * Your contribution here. ### 4.1.0 (6/12/2020) diff --git a/README.md b/README.md index 2b3343a..329c9ec 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,27 @@ RSpec.describe Article do it { is_expected.to embed_many(:comments) } end +# when the touch option is provided, then we can also verify that it is set + +# by default, with_touch matches true when no parameters are provided +describe Article do + it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index.with_touch } +end + +# parameters are supported for explicit matching +describe Comment do + it { is_expected.to be_embedded_in(:article).as_inverse_of(:comments).with_polymorphism.with_touch(true) } +end + +describe Permalink do + it { is_expected.to be_embedded_in(:linkable).as_inverse_of(:link).with_touch(false) } +end + +# touch can also take a symbol representing a field on the parent to touch +describe Record do + it { is_expected.to belong_to(:user).as_inverse_of(:record).with_touch(:record_updated_at) } +end + RSpec.describe Comment do it { is_expected.to be_embedded_in(:article).as_inverse_of(:comments) } it { is_expected.to belong_to(:user).as_inverse_of(:comments) } diff --git a/lib/matchers/associations.rb b/lib/matchers/associations.rb index 0c81dfb..d15ba29 100644 --- a/lib/matchers/associations.rb +++ b/lib/matchers/associations.rb @@ -119,6 +119,12 @@ def with_counter_cache self end + def with_touch(touch = true) + @association[:touch] = touch + @expectation_message << " which specifies touch as #{@association[:touch]}" + self + end + def with_optional @association[:optional] = true @expectation_message << " which specifies optional as #{@association[:optional]}" @@ -272,6 +278,15 @@ def matches?(actual) end end + unless @association[:touch].nil? + if metadata.options[:touch] != @association[:touch] + @negative_result_message = "#{@positive_result_message} which sets touch as #{metadata.options[:touch].inspect}" + return false + else + @positive_result_message = "#{@positive_result_message} which sets touch as #{metadata.options[:touch].inspect}" + end + end + if @association[:optional] if metadata.options[:optional] != true @negative_result_message = "#{@positive_result_message} which did not set optional" diff --git a/lib/mongoid/rspec/version.rb b/lib/mongoid/rspec/version.rb index 2ec9f06..a64099c 100644 --- a/lib/mongoid/rspec/version.rb +++ b/lib/mongoid/rspec/version.rb @@ -1,5 +1,5 @@ module Mongoid module RSpec - VERSION = '4.1.1'.freeze + VERSION = '4.2.0'.freeze end end diff --git a/spec/models/article.rb b/spec/models/article.rb index ce18470..e0b46ae 100644 --- a/spec/models/article.rb +++ b/spec/models/article.rb @@ -1,3 +1,5 @@ +require 'user' + class Article include Mongoid::Document include Mongoid::Timestamps @@ -13,7 +15,11 @@ class Article embeds_many :comments, cascade_callbacks: true, inverse_of: :article embeds_one :permalink, inverse_of: :linkable, class_name: 'Permalink' - belongs_to :author, class_name: 'User', inverse_of: :articles, index: true + belongs_to :author, + class_name: 'User', + inverse_of: :articles, + index: true, + touch: true validates :title, presence: true diff --git a/spec/models/message.rb b/spec/models/message.rb index 11582da..e12ee35 100644 --- a/spec/models/message.rb +++ b/spec/models/message.rb @@ -6,7 +6,7 @@ class Message field :to if Mongoid::Compatibility::Version.mongoid6_or_newer? - belongs_to :user, optional: true + belongs_to :user, optional: true, touch: true else belongs_to :user end diff --git a/spec/models/permalink.rb b/spec/models/permalink.rb index 64d8d7f..4ecb8dc 100644 --- a/spec/models/permalink.rb +++ b/spec/models/permalink.rb @@ -1,3 +1,5 @@ +require 'article' + class Permalink include Mongoid::Document diff --git a/spec/models/record.rb b/spec/models/record.rb index 86e000e..43b9c78 100644 --- a/spec/models/record.rb +++ b/spec/models/record.rb @@ -1,5 +1,7 @@ +require 'user' + class Record include Mongoid::Document - belongs_to :user, inverse_of: :record + belongs_to :user, inverse_of: :record, touch: :record_updated_at end diff --git a/spec/models/user.rb b/spec/models/user.rb index 670c261..35b05ef 100644 --- a/spec/models/user.rb +++ b/spec/models/user.rb @@ -1,6 +1,6 @@ class User include Mongoid::Document - include Mongoid::Timestamps::Created + include Mongoid::Timestamps field :login field :email @@ -9,6 +9,7 @@ class User field :password, type: String field :provider_uid field :locale + field :record_updated_at, type: Time belongs_to :site, inverse_of: :users has_many :articles, foreign_key: :author_id, order: :title diff --git a/spec/unit/associations_spec.rb b/spec/unit/associations_spec.rb index b4e4c14..bf17a9e 100644 --- a/spec/unit/associations_spec.rb +++ b/spec/unit/associations_spec.rb @@ -20,7 +20,7 @@ end describe Article do - it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index } + it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index.with_touch } it { is_expected.to embed_many(:comments).as_inverse_of(:article).with_cascading_callbacks } it { is_expected.to embed_one(:permalink).as_inverse_of(:linkable) } end @@ -31,11 +31,11 @@ end describe Record do - it { is_expected.to belong_to(:user).as_inverse_of(:record) } + it { is_expected.to belong_to(:user).as_inverse_of(:record).with_touch(:record_updated_at) } end describe Permalink do - it { is_expected.to be_embedded_in(:linkable).as_inverse_of(:link) } + it { is_expected.to be_embedded_in(:linkable).as_inverse_of(:link).with_touch(false) } end describe Site do @@ -48,7 +48,7 @@ describe Message do if Mongoid::Compatibility::Version.mongoid6_or_newer? - it { is_expected.to belong_to(:user).with_optional } + it { is_expected.to belong_to(:user).with_optional.with_touch(true) } end end end