Skip to content

Commit

Permalink
Add support to verify touch option (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
sairamsrinivasan authored May 30, 2024
1 parent f55e0f9 commit 9b74fc1
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
15 changes: 15 additions & 0 deletions lib/matchers/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]}"
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/rspec/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Mongoid
module RSpec
VERSION = '4.1.1'.freeze
VERSION = '4.2.0'.freeze
end
end
8 changes: 7 additions & 1 deletion spec/models/article.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'user'

class Article
include Mongoid::Document
include Mongoid::Timestamps
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions spec/models/permalink.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'article'

class Permalink
include Mongoid::Document

Expand Down
4 changes: 3 additions & 1 deletion spec/models/record.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion spec/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class User
include Mongoid::Document
include Mongoid::Timestamps::Created
include Mongoid::Timestamps

field :login
field :email
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 9b74fc1

Please sign in to comment.