spec/mongoid/contextual/atomic_spec.rb in mongoid-8.1.7 vs spec/mongoid/contextual/atomic_spec.rb in mongoid-9.0.0

- old
+ new

@@ -1,6 +1,7 @@ # frozen_string_literal: true +# rubocop:todo all require "spec_helper" describe Mongoid::Contextual::Atomic do @@ -75,11 +76,10 @@ expect(smiths.reload.members).to eq([ "Dave" ]) end end context 'when the criteria has a collation' do - min_server_version '3.4' let(:criteria) do Band.where(members: [ "DAVE" ]).collation(locale: 'en_US', strength: 2) end @@ -172,11 +172,10 @@ expect(smiths.reload.members).to eq([ "Dave", "Joe" ]) end end context 'when the criteria has a collation' do - min_server_version '3.4' let(:criteria) do Band.where(members: [ "DAVE" ]).collation(locale: 'en_US', strength: 2) end @@ -244,11 +243,10 @@ expect(depeche_mode.reload.likes).to eq(14) end end context 'when the criteria has a collation' do - min_server_version '3.4' let!(:depeche_mode) do Band.create!(members: [ "Dave" ], likes: 60) end @@ -304,11 +302,11 @@ end context "when the field does not exist" do it "does not error on the inc" do - expect(smiths.likes).to be_nil + expect(smiths.reload.likes).to be == 10 end end context "when using the alias" do @@ -324,11 +322,10 @@ expect(beatles.reload.y).to eq(3) end end context 'when the criteria has a collation' do - min_server_version '3.4' let!(:depeche_mode) do Band.create!(members: [ "Dave" ]) end @@ -348,10 +345,67 @@ expect(depeche_mode.reload.years).to eq(1) end end end + describe "#mul" do + + let!(:depeche_mode) { Band.create!(likes: 60) } + let!(:smiths) { Band.create! } + let!(:beatles) { Band.create!(years: 2) } + + let(:criteria) { Band.all } + let(:context) { Mongoid::Contextual::Mongo.new(criteria) } + + before { context.mul(likes: 10) } + + context "when the field exists" do + + it "muls the value" do + expect(depeche_mode.reload.likes).to eq(600) + end + end + + context "when the field does not exist" do + + it "sets undefined fields to zero" do + expect(smiths.reload.likes).to be == 0 + end + end + + context "when using the alias" do + + before do + context.mul(years: 2) + end + + it "muls the value and read from alias" do + expect(beatles.reload.years).to eq(4) + end + + it "muls the value and read from field" do + expect(beatles.reload.y).to eq(4) + end + end + + context 'when the criteria has a collation' do + + let!(:depeche_mode) { Band.create!(members: [ "Dave" ], years: 3) } + let(:criteria) do + Band.where(members: [ "DAVE" ]).collation(locale: 'en_US', strength: 2) + end + + let(:context) { Mongoid::Contextual::Mongo.new(criteria) } + + before { context.mul(years: 2) } + + it "muls the value" do + expect(depeche_mode.reload.years).to eq(6) + end + end + end + describe "#pop" do let!(:depeche_mode) do Band.create!(members: [ "Dave", "Martin" ]) end @@ -397,11 +451,10 @@ expect(smiths.reload.members).to be_nil end end context 'when the criteria has a collation' do - min_server_version '3.4' let!(:depeche_mode) do Band.create!(members: [ "Dave" ]) end @@ -452,11 +505,10 @@ it "does not error on non existent fields" do expect(smiths.reload.members).to be_nil end context 'when the criteria has a collation' do - min_server_version '3.4' let!(:depeche_mode) do Band.create!(members: [ "Dave" ]) end @@ -510,11 +562,10 @@ expect(smiths.reload.members).to be_nil end end context 'when the criteria has a collation' do - min_server_version '3.4' let!(:depeche_mode) do Band.create!(members: [ "Dave", "Alan", "Fletch" ]) end @@ -568,11 +619,10 @@ expect(smiths.reload.members).to eq([ "Alan" ]) end end context 'when the criteria has a collation' do - min_server_version '3.4' let!(:depeche_mode) do Band.create!(members: [ "Dave" ]) end @@ -634,11 +684,10 @@ expect(smiths.reload.members).to eq([ "Alan", "Fletch" ]) end end context 'when the criteria has a collation' do - min_server_version '3.4' let!(:depeche_mode) do Band.create!(members: [ "Dave" ]) end @@ -696,11 +745,10 @@ expect(smiths.reload).to_not respond_to(:musicians) end end context 'when the criteria has a collation' do - min_server_version '3.4' let!(:depeche_mode) do Band.create!(members: [ "Dave" ]) end @@ -906,11 +954,10 @@ end end end context 'when the criteria has a collation' do - min_server_version '3.4' let!(:depeche_mode) do Band.create!(name: "Depeche Mode", years: 10) end @@ -933,8 +980,152 @@ it "unsets the unaliased field" do depeche_mode.reload expect(depeche_mode.name).to be_nil expect(depeche_mode.years).to_not be_nil end + end + end + + describe '#set_min' do + let(:member_count) { 5 } + let(:views) { 25_000 } + let(:founded) { 2.years.ago.to_date } + let(:decimal) { 3.1415 } + + let!(:band) do + Band.create!( + member_count: member_count, + founded: founded, + views: views) + end + + let(:criteria) { Band.all } + let(:context) { Mongoid::Contextual::Mongo.new(criteria) } + + shared_examples_for 'min-able comparisons' do + shared_examples_for 'a min-able context' do + it 'chooses the smaller value' do + context.send(min_method, "member_count" => given_members, views: given_views) + expect(band.reload.member_count).to be == [ member_count, given_members ].min + expect(band.views).to be == [ views, given_views ].min + end + end + + context 'when given value < existing value' do + let(:given_views) { views - 1 } + let(:given_members) { member_count - 1 } + + it_behaves_like 'a min-able context' + end + + context 'when given value == existing value' do + let(:given_views) { views } + let(:given_members) { member_count } + + it_behaves_like 'a min-able context' + end + + context 'when given value > existing value' do + let(:given_views) { views + 1 } + let(:given_members) { member_count + 1 } + + it_behaves_like 'a min-able context' + end + + context 'when the named field does not yet exist on the document' do + it 'sets the field to the argument' do + context.send(min_method, new_field: 100) + expect(band.reload.new_field).to be == 100 + end + end + + context 'when arguments are dates/times' do + it 'picks the earliest date' do + context.send(min_method, founded: founded - 1) + expect(band.reload.founded).to be == founded - 1 + end + end + end + + context 'as itself' do + let(:min_method) { :set_min } + include_examples 'min-able comparisons' + end + + context 'as #clamp_upper_bound' do + let(:min_method) { :clamp_upper_bound } + include_examples 'min-able comparisons' + end + end + + describe '#set_max' do + let(:member_count) { 5 } + let(:views) { 25_000 } + let(:founded) { 2.years.ago.to_date } + let(:decimal) { 3.1415 } + + let!(:band) do + Band.create!( + member_count: member_count, + founded: founded, + views: views) + end + + let(:criteria) { Band.all } + let(:context) { Mongoid::Contextual::Mongo.new(criteria) } + + shared_examples_for 'max-able comparisons' do + shared_examples_for 'a max-able context' do + it 'chooses the larger value' do + context.send(max_method, "member_count" => given_members, views: given_views) + expect(band.reload.member_count).to be == [ member_count, given_members ].max + expect(band.views).to be == [ views, given_views ].max + end + end + + context 'when given value < existing value' do + let(:given_views) { views - 1 } + let(:given_members) { member_count - 1 } + + it_behaves_like 'a max-able context' + end + + context 'when given value == existing value' do + let(:given_views) { views } + let(:given_members) { member_count } + + it_behaves_like 'a max-able context' + end + + context 'when given value > existing value' do + let(:given_views) { views + 1 } + let(:given_members) { member_count + 1 } + + it_behaves_like 'a max-able context' + end + + context 'when the named field does not yet exist on the document' do + it 'sets the field to the argument' do + context.send(max_method, new_field: 100) + expect(band.reload.new_field).to be == 100 + end + end + + context 'when arguments are dates/times' do + it 'picks the earliest date' do + context.send(max_method, founded: founded + 1) + expect(band.reload.founded).to be == founded + 1 + end + end + end + + context 'as itself' do + let(:max_method) { :set_max } + include_examples 'max-able comparisons' + end + + context 'as #clamp_lower_bound' do + let(:max_method) { :clamp_lower_bound } + include_examples 'max-able comparisons' end end end