spec/mongoid/clients_spec.rb in mongoid-5.4.1 vs spec/mongoid/clients_spec.rb in mongoid-6.0.0.beta

- old
+ new

@@ -4,57 +4,69 @@ describe "#collection" do shared_examples_for "an overridden collection at the class level" do - let(:band) do - klass.new - end - it "returns the collection for the model" do - expect(band.collection).to be_a(Mongo::Collection) + expect(instance_collection).to be_a(Mongo::Collection) end it "sets the correct collection name" do - expect(band.collection.name).to eq("artists") + expect(instance_collection.name).to eq("artists") end context "when accessing from the class level" do it "returns the collection for the model" do - expect(klass.collection).to be_a(Mongo::Collection) + expect(class_collection).to be_a(Mongo::Collection) end it "sets the correct collection name" do - expect(klass.collection.name).to eq("artists") + expect(class_collection.name).to eq("artists") end end end context "when overriding the persistence options" do - let(:klass) do - Band.with(collection: "artists") + let(:instance_collection) do + Band.with(collection: "artists") do |klass| + klass.new.collection + end end + let(:class_collection) do + Band.with(collection: "artists") do |klass| + klass.collection + end + end + it_behaves_like "an overridden collection at the class level" end context "when overriding store_in and persistence options" do - let(:klass) do - Band.with(collection: "artists") - end - before do Band.store_in collection: "foo" end after do Band.reset_storage_options! end + let(:instance_collection) do + Band.with(collection: "artists") do |klass| + klass.new.collection + end + end + + let(:class_collection) do + Band.with(collection: "artists") do |klass| + klass.collection + end + end + it_behaves_like "an overridden collection at the class level" end context "when overriding the default with store_in" do @@ -75,46 +87,64 @@ end end context "when overriding with a proc" do - let(:klass) { Band } - before do Band.store_in(collection: ->{ "artists" }) end + let(:instance_collection) do + Band.new.collection + end + + let(:class_collection) do + Band.collection + end + it_behaves_like "an overridden collection at the class level" end context "when overriding with a string" do - let(:klass) { Band } - before do Band.store_in(collection: "artists") end after do Band.reset_storage_options! end + let(:instance_collection) do + Band.new.collection + end + + let(:class_collection) do + Band.collection + end + it_behaves_like "an overridden collection at the class level" end context "when overriding with a symbol" do - let(:klass) { Band } - before do - klass.store_in(collection: :artists) + Band.store_in(collection: :artists) end after do - klass.reset_storage_options! + Band.reset_storage_options! end + let(:instance_collection) do + Band.new.collection + end + + let(:class_collection) do + Band.collection + end + it_behaves_like "an overridden collection at the class level" end end context "when not overriding the default" do @@ -146,44 +176,56 @@ describe "#collection_name" do shared_examples_for "an overridden collection name at the class level" do - let(:band) do - klass.new - end - context "when accessing from the instance" do it "returns the overridden value" do - expect(band.collection_name).to eq(:artists) + expect(instance_collection_name).to eq(:artists) end end context "when accessing from the class level" do it "returns the overridden value" do - expect(klass.collection_name).to eq(:artists) + expect(class_collection_name).to eq(:artists) end end end context "when overriding the persistence options" do - let(:klass) do - Band.with(collection: "artists") + let(:instance_collection_name) do + Band.with(collection: "artists") do |klass| + klass.new.collection_name + end end + let(:class_collection_name) do + Band.with(collection: "artists") do |klass| + klass.collection_name + end + end + it_behaves_like "an overridden collection name at the class level" end context "when overriding store_in and persistence options" do - let(:klass) do - Band.with(collection: "artists") + let(:instance_collection_name) do + Band.with(collection: "artists") do |klass| + klass.new.collection_name + end end + let(:class_collection_name) do + Band.with(collection: "artists") do |klass| + klass.collection_name + end + end + before do Band.store_in collection: "foo" end after do @@ -193,12 +235,18 @@ it_behaves_like "an overridden collection name at the class level" end context "when overriding the default with store_in" do - let(:klass) { Band } + let(:instance_collection_name) do + Band.new.collection_name + end + let(:class_collection_name) do + Band.collection_name + end + after do Band.reset_storage_options! end context "when overriding with a proc" do @@ -264,51 +312,70 @@ end end end end - describe "#database_name", if: non_legacy_server? do + describe "#database_name" do shared_examples_for "an overridden database name" do - let(:band) do - klass.new - end - context "when accessing from the instance" do it "returns the overridden value" do - # @todo - # expect(band.mongo_client.options[:database].to_s).to eq(database_id_alt) + expect(instance_database.name.to_s).to eq(database_id_alt) end end context "when accessing from the class level" do it "returns the overridden value" do - expect(klass.database_name.to_s).to eq(database_id_alt) + expect(class_database.name.to_s).to eq(database_id_alt) end it "client returns the overridden value" do - expect(klass.mongo_client.options[:database].to_s).to eq(database_id_alt) + expect(class_mongo_client.database.name).to eq(database_id_alt) end end end context "when overriding the persistence options" do - let(:klass) do - Band.with(database: database_id_alt) + let(:instance_database) do + Band.with(database: database_id_alt) do |klass| + klass.new.mongo_client.database + end end + let(:class_database) do + Band.with(database: database_id_alt) do |klass| + klass.mongo_client.database + end + end + + let(:class_mongo_client) do + Band.with(database: database_id_alt) do |klass| + klass.new.mongo_client + end + end + it_behaves_like "an overridden database name" end context "when overriding with store_in" do - let(:klass) { Band } + let(:instance_database) do + Band.new.mongo_client.database + end + let(:class_database) do + Band.mongo_client.database + end + + let(:class_mongo_client) do + Band.mongo_client + end + before do Band.store_in database: database_id_alt end after do @@ -318,14 +385,28 @@ it_behaves_like "an overridden database name" end context "when overriding store_in and persistence options" do - let(:klass) do - Band.with(database: database_id_alt) + let(:instance_database) do + Band.with(database: database_id_alt) do |klass| + klass.new.mongo_client.database + end end + let(:class_database) do + Band.with(database: database_id_alt) do |klass| + klass.mongo_client.database + end + end + + let(:class_mongo_client) do + Band.with(database: database_id_alt) do |klass| + klass.new.mongo_client + end + end + before do Band.store_in database: "foo" end after do @@ -347,21 +428,45 @@ Mongoid.clients.delete(client_name) end context "when overriding the persistence options" do - let(:klass) do - Band.with(client: client_name) + let(:instance_database) do + Band.with(client: :alternative) do |klass| + klass.new.mongo_client.database + end end + let(:class_database) do + Band.with(client: :alternative) do |klass| + klass.mongo_client.database + end + end + + let(:class_mongo_client) do + Band.with(client: :alternative) do |klass| + klass.new.mongo_client + end + end + it_behaves_like "an overridden database name" end context "when overriding with store_in" do - let(:klass) { Band } + let(:instance_database) do + Band.new.mongo_client.database + end + let(:class_database) do + Band.mongo_client.database + end + + let(:class_mongo_client) do + Band.mongo_client + end + before do Band.store_in(client: client_name) end after do @@ -371,11 +476,11 @@ it_behaves_like "an overridden database name" end end end - describe "#mongo_client", if: non_legacy_server? do + describe "#mongo_client" do let(:file) do File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml") end @@ -406,100 +511,31 @@ end it "returns the default client" do expect(mongo_client.options[:database].to_s).to eq(database_id) end - - it "sets the platform to Mongoid's platform constant" do - expect(mongo_client.options[:platform]).to eq(Mongoid::PLATFORM_DETAILS) - end - - it "sets the app_name to the config value" do - expect(mongo_client.options[:app_name]).to eq('testing') - end end context "when no client exists with the key" do before(:all) do Band.store_in(client: :nonexistent) end - after do - Band.reset_storage_options! - end - let(:band) do Band.new end it "raises an error" do expect { band.mongo_client }.to raise_error(Mongoid::Errors::NoClientConfig) end end - - context "when getting a client by name", if: testing_locally? do - - let(:file) do - File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml") - end - - before do - described_class.clear - Mongoid.load!(file, :test) - Band.store_in(client: :reports) - end - - after do - mongo_client.close - Mongoid::Config.reset - Band.reset_storage_options! - end - - let!(:band) do - Band.store_in(client: :reports) - end - - let!(:mongo_client) do - Band.new.mongo_client - end - - it "uses the reports client" do - expect(mongo_client.options[:database].to_s).to eq('reports') - end - - it "sets the platform to Mongoid's platform constant" do - expect(mongo_client.options[:platform]).to eq(Mongoid::PLATFORM_DETAILS) - end - - it "sets the app_name to the config value" do - expect(mongo_client.options[:app_name]).to eq('testing') - end - end - - context 'when the app_name is not set in the config' do - - before do - Mongoid::Config.reset - Mongoid.configure do |config| - config.load_configuration(CONFIG) - end - end - - let(:mongo_client) do - Band.new.mongo_client - end - - it 'does not set the Mongoid.app_name option' do - expect(mongo_client.options.has_key?(:app_name)).to be(false) - end - end end - describe ".mongo_client", if: non_legacy_server? do + describe ".mongo_client" do let(:file) do File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml") end @@ -524,25 +560,17 @@ described_class.clear Mongoid.load!(file, :test) Mongoid.clients[:default][:database] = database_id end - let(:mongo_client) do + let!(:mongo_client) do Band.mongo_client end it "returns the default client" do expect(mongo_client.options[:database].to_s).to eq(database_id) end - - it "sets the platform to Mongoid's platform constant" do - expect(mongo_client.options[:platform]).to eq(Mongoid::PLATFORM_DETAILS) - end - - it "sets the app_name to the config value" do - expect(mongo_client.options[:app_name]).to eq('testing') - end end context "when no client exists with the key" do before(:all) do @@ -566,11 +594,11 @@ Band.store_in :artists }.to raise_error(Mongoid::Errors::InvalidStorageOptions) end end - context "when provided a class that extend another document" do + context "when provided a class that extends another document" do let(:klass) do Class.new(Band) end @@ -597,11 +625,13 @@ describe ".with", if: non_legacy_server? do context "when changing write concern options" do let(:client_one) do - Band.with(write: { w: 2 }).mongo_client + Band.with(write: { w: 2 }) do |klass| + klass.mongo_client + end end let(:client_two) do Band.mongo_client end @@ -612,63 +642,85 @@ end context "when sending operations to a different database" do after do - Band.with(database: database_id_alt).delete_all + Band.with(database: database_id_alt) do |klass| + klass.delete_all + end end describe ".create" do let!(:band) do - Band.with(database: database_id_alt).create + Band.with(database: database_id_alt) do |klass| + klass.create + end end it "does not persist to the default database" do expect { Band.find(band.id) }.to raise_error(Mongoid::Errors::DocumentNotFound) end let(:from_db) do - Band.with(database: database_id_alt).find(band.id) + Band.with(database: database_id_alt) do |klass| + klass.find(band.id) + end end it "persists to the specified database" do expect(from_db).to eq(band) end + let(:count) do + Band.with(database: database_id_alt) do |klass| + klass.count + end + end + it "persists the correct number of documents" do - expect(Band.with(database: database_id_alt).count).to eq(1) + expect(count).to eq(1) end end end context "when sending operations to a different collection" do describe ".create" do let!(:band) do - Band.with(collection: "artists").create + Band.with(collection: "artists") do |klass| + klass.create + end end it "does not persist to the default database" do expect { Band.find(band.id) }.to raise_error(Mongoid::Errors::DocumentNotFound) end let(:from_db) do - Band.with(collection: "artists").find(band.id) + Band.with(collection: "artists") do |klass| + klass.find(band.id) + end end it "persists to the specified database" do expect(from_db).to eq(band) end + let(:count) do + Band.with(collection: "artists") do |klass| + klass.count + end + end + it "persists the correct number of documents" do - expect(Band.with(collection: "artists").count).to eq(1) + expect(count).to eq(1) end end end context "when sending operations with safe mode" do @@ -794,11 +846,11 @@ end end end end - context "when overriding the default database", if: non_legacy_server? do + context "when overriding the default database" do let(:file) do File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml") end @@ -806,45 +858,30 @@ Mongoid::Config.load!(file, :test) end context "when the override is global" do - shared_examples_for "a global database override" do + before do + Mongoid.override_database(:mongoid_optional) + end - before do - Mongoid.override_database(:mongoid_optional) - end + after do + Band.delete_all + Mongoid.override_database(nil) + end - after do - Band.delete_all - Mongoid.override_database(nil) - end + let!(:band) do + Band.create(name: "Tool") + end - let!(:band) do - klass.create(name: "Tool") + it "persists to the overridden database" do + Band.mongo_client.with(database: :mongoid_optional) do |sess| + expect(sess[:bands].find(name: "Tool")).to_not be_nil end - - it "persists to the overridden database" do - mongo_client = Band.mongo_client.with(database: :mongoid_optional) - expect(mongo_client[:bands].count(name: "Tool")).to eq(1) - end - - it 'uses that database for the model mongo_client' do - expect(Band.mongo_client.database.name).to eq('mongoid_optional') - end end - context "when normal usage" do - let(:klass) { Band } - - it_behaves_like "a global database override" - end - - context "when overriding the persistence options" do - - let(:klass) { Band.with(connect_timeout: 10) } - - it_behaves_like "a global database override" + it 'uses that database for the model mongo_client' do + expect(Band.mongo_client.database.name).to eq('mongoid_optional') end end end end