Skip to content

Commit

Permalink
fix CI: prevent models mutations in auto_save_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
antonsavitskiy committed May 6, 2024
1 parent bbbd82b commit 89c3ed7
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 19 deletions.
38 changes: 19 additions & 19 deletions spec/mongoid/association/auto_save_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
describe '.auto_save' do

before(:all) do
Person.has_many :drugs, validate: false, autosave: true
Person.has_one :account, validate: false, autosave: true
PersonAuto.has_many :drugs, class_name: 'DrugAuto', validate: false, autosave: true
PersonAuto.has_one :account, class_name: 'AccountAuto', validate: false, autosave: true
end

after(:all) do
Person.reset_callbacks(:save)
PersonAuto.reset_callbacks(:save)
end

let(:person) do
Person.new
PersonAuto.new
end

context 'when the option is not provided' do
Expand Down Expand Up @@ -85,11 +85,11 @@
context 'when the relation has already had the autosave callback added' do

before do
Person.has_many :drugs, validate: false, autosave: true
PersonAuto.has_many :drugs, class_name: 'DrugAuto', validate: false, autosave: true
end

let(:drug) do
Drug.new(name: 'Percocet')
DrugAuto.new(name: 'Percocet')
end

it 'does not add the autosave callback twice' do
Expand All @@ -102,15 +102,15 @@
context 'when the relation is a references many' do

let(:drug) do
Drug.new(name: 'Percocet')
DrugAuto.new(name: 'Percocet')
end

context 'when saving a new parent document' do

context 'when persistence options are not set on the parent' do

before do
Person.has_many :drugs, validate: false, autosave: true
PersonAuto.has_many :drugs, class_name: 'DrugAuto', validate: false, autosave: true
end

before do
Expand All @@ -130,8 +130,8 @@
end

after do
Person.with(database: other_database, &:delete_all)
Drug.with(database: other_database, &:delete_all)
PersonAuto.with(database: other_database, &:delete_all)
DrugAuto.with(database: other_database, &:delete_all)
end

before do
Expand All @@ -142,7 +142,7 @@
end

it 'saves the relation with the persistence options' do
Drug.with(database: other_database) do |drug_class|
DrugAuto.with(database: other_database) do |drug_class|
expect(drug_class.count).to eq(1)
end
end
Expand All @@ -165,7 +165,7 @@
context 'when not updating the document' do

let(:from_db) do
Person.find person.id
PersonAuto.find person.id
end

before do
Expand All @@ -183,7 +183,7 @@
context 'when the relation is a references one' do

let(:account) do
Account.new(name: 'Testing')
AccountAuto.new(name: 'Testing')
end

context 'when saving a new parent document' do
Expand Down Expand Up @@ -237,7 +237,7 @@
context 'when not updating the document' do

let(:from_db) do
Person.find person.id
PersonAuto.find person.id
end

before do
Expand Down Expand Up @@ -291,25 +291,25 @@
context 'when it has two relations with autosaves' do

let!(:person) do
Person.create!(drugs: [percocet], account: account)
PersonAuto.create!(drugs: [percocet], account: account)
end

let(:from_db) do
Person.find person.id
PersonAuto.find person.id
end

let(:percocet) do
Drug.new(name: 'Percocet')
DrugAuto.new(name: 'Percocet')
end

let(:account) do
Account.new(name: 'Testing')
AccountAuto.new(name: 'Testing')
end

context 'when updating one document' do

let(:placebo) do
Drug.new(name: 'Placebo')
DrugAuto.new(name: 'Placebo')
end

before do
Expand Down
38 changes: 38 additions & 0 deletions spec/support/models/account_auto.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

class AccountAuto
include Mongoid::Document

field :_id, type: String, overwrite: true, default: -> { name.try(:parameterize) }

field :number, type: String
field :balance, type: Integer
field :nickname, type: String
field :name, type: String
field :balanced, type: Mongoid::Boolean, default: -> { balance? }

field :overridden, type: String

embeds_many :memberships
belongs_to :creator, class_name: 'User'
belongs_to :person, class_name: 'PersonAuto'
has_many :alerts, autosave: false
has_and_belongs_to_many :agents
has_one :comment, validate: false

validates_presence_of :name
validates_presence_of :nickname, on: :upsert
validates_length_of :name, maximum: 10, on: :create

def overridden
self[:overridden] = 'not recommended'
end

# MONGOID-3365
field :period_started_at, type: Time
has_many :consumption_periods, dependent: :destroy, validate: false

def current_consumption
consumption_periods.find_or_create_by(started_at: period_started_at)
end
end
8 changes: 8 additions & 0 deletions spec/support/models/drug_auto.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class DrugAuto
include Mongoid::Document
field :name, type: String
field :generic, type: Mongoid::Boolean
belongs_to :person, class_name: 'PersonAuto', inverse_of: nil, counter_cache: true
end
Loading

0 comments on commit 89c3ed7

Please sign in to comment.