spec/mongo/fixture_spec.rb in mongo-fixture-0.0.4 vs spec/mongo/fixture_spec.rb in mongo-fixture-0.0.5

- old
+ new

@@ -1,6 +1,6 @@ -require "mongo-fixture" +require "spec_helper" describe Mongo::Fixture do describe ".path" do it "should return 'test/fixtures'" do Mongo::Fixture.path.should == "test/fixtures" @@ -82,11 +82,11 @@ after do Fast.dir.remove! :test end end end - + describe "#force_checked!" do it "check should return true and should not call [] in the passed database" do database = stub 'database' database.should_not_receive :[] @@ -94,11 +94,11 @@ fix = Mongo::Fixture.new :anything, database, false fix.force_checked!.should === true fix.check.should === true end end - + describe "#[]" do context "a valid fixture has been loaded" do before do Fast.file.write "test/fixtures/test/users.yaml", "john: { name: John, last_name: Wayne }" Fast.file.write "test/fixtures/test/actions.yaml", "walk: { user_id: 1, action: Walks }" @@ -120,11 +120,11 @@ after do Fast.dir.remove! :test end end end - + describe "#method_missing" do context "a valid fixture has been loaded" do context "a collection key is passed" do before do Fast.file.write "test/fixtures/test/users.yaml", "john: { name: John, last_name: Wayne }" @@ -151,11 +151,11 @@ it "should raise no method error if matches nothing" do expect { Mongo::Fixture.new.nothing = "hola" }.to raise_error NoMethodError end end - + describe "#fixtures_path" do it "should call Mongo::Fixture.path" do Mongo::Fixture.should_receive :path Mongo::Fixture.new.fixtures_path end @@ -286,11 +286,11 @@ connection = stub fix = Mongo::Fixture.new nil, connection fix.connection.should === connection end end - + describe "#connection=" do it "sets the connection" do fix = Mongo::Fixture.new connection = stub fix.connection = connection @@ -316,11 +316,11 @@ after do Fast.dir.remove! :test end end end - + describe "#data" do context "a fixture has been loaded" do before do Fast.file.write "test/fixtures/test/users.yaml", "jane { name: Jessica Dore }" end @@ -341,11 +341,11 @@ fix = Mongo::Fixture.new fix.data.should be_nil end end end - + describe "#push" do it "should call #check" do fix = Mongo::Fixture.new def fix.stub_data @data = {} @@ -405,51 +405,56 @@ it "should fail" do database = double 'database', :[] => stub( 'collection', :count => 0, :drop => nil ) fix = Mongo::Fixture.new :test, database, false expect { fix.push - }.to raise_error Mongo::Fixture::MissingProcessedValueError, - "In record 'hey' to be inserted into 'users', the processed value of field 'pass' is missing, aborting" + }.to raise_error Mongo::Fixture::ReferencedRecordNotFoundError, + "This fixture does not include data for the collections [raw]" end it "should call the rollback" do database = double 'database', :[] => stub( 'collection', :count => 0, :drop => nil ) fix = Mongo::Fixture.new :test, database, false fix.should_receive :rollback expect { fix.push - }.to raise_error Mongo::Fixture::MissingProcessedValueError + }.to raise_error Mongo::Fixture::ReferencedRecordNotFoundError end after do Fast.dir.remove! :test end end - + context "a fixture with a field with one alternative name matches a collection name" do context "the alternative value matches a record and in the collection" do before do Fast.file.write "test/fixtures/test/users.yaml", "pepe: { name: Jonah }" Fast.file.write "test/fixtures/test/comments.yaml", "flamewar: { user: { users: pepe }, text: 'FLAME' }" end it "should insert the comment so that the comment user value matches the '_id' of the user" do - database = double 'database' - comm = double 'comments', :count => 0, :drop => nil - comm.should_receive( :insert ).with( :user => "un id", :text => "FLAME" ) + comments = double 'comments', :count => 0, :drop => nil + comments.should_receive( :insert ).with( :user => "un id", :text => "FLAME" ) + record = stub 'record' record.should_receive( :[] ).with( "_id" ).and_return "un id" + usrs = double 'users', :count => 0, :find => stub( :first => record ), :drop => nil, :insert => nil + + database = double 'database' database.stub :[] do |coll| case coll when :users usrs when :comments - comm + comments end end - fix = Mongo::Fixture.new :test, database + + fix = Mongo::Fixture.new :test, database, false + fix.push end context "the collection is ordered so that the comment collection comes before the users one" do it "should stop and process the users first" do database = double 'database' @@ -471,125 +476,29 @@ fix.stub_data fix.push end end - + after do Fast.dir.remove! :test end - end - end - end - - describe "#data_was_inserted_in?" do - it "should be private" do - fix = Mongo::Fixture.new - fix.private_methods(false).should include :data_was_inserted_in? - end - - context "there is a simple fixture and a collection has been inserted by this fixture" do - before do - Fast.file.write "test/fixtures/test/users.yaml", "pepe: { user: pepe }" end - - it "should return true" do - database = double 'database' - coll = double 'collection', :count => 0, :insert => nil - database.stub :[] => coll - fix = Mongo::Fixture.new :test, database - def fix.loot - data_was_inserted_in?(:users).should === true - end - fix.loot - end - - after do - Fast.dir.remove! :test - end - end - - context "there is a simple fixture and a collection was inserted but not this" do - before do - Fast.file.write "test/fixtures/test/users.yaml", "pepe: { user: pepe }" - end - - it "should return false" do - database = double 'database' - coll = double 'collection', :count => 0, :insert => nil - database.stub :[] => coll - fix = Mongo::Fixture.new :test, database - def fix.loot - data_was_inserted_in?(:comment).should === false - end - fix.loot - end - - after do - Fast.dir.remove! :test - end - end + end end - - # This should go in a dependency, pending refactoring TODO - describe "#simplify" do - context "when receiving a multidimensional hash containing a field with raw and processed" do - it "should convert it in a simple hash using the processed value as replacement" do - base_hash = { - :name => "Jane", - :band => "Witherspoons", - :pass => { - :raw => "secret", - :processed => "53oih7fhjdgj3f8=" - }, - :email => { - :raw => "Jane@gmail.com ", - :processed => "jane@gmail.com" - } - } - - fix = Mongo::Fixture.new - def fix.stub_data - @data = {} - end - fix.stub_data - simplified = fix.simplify base_hash - simplified.should == { - :name => "Jane", - :band => "Witherspoons", - :pass => "53oih7fhjdgj3f8=", - :email => "jane@gmail.com" - } - end + + describe "#inserter" do + it "should return an inserter with a reference to this" do + database = double 'database' + Mongo::Fixture.any_instance.stub :load + fixture = Mongo::Fixture.new :test, database, false + inserter = fixture.inserter + inserter.should be_a Mongo::Fixture::Inserter + inserter.fixture.should === fixture end - - context "the multidimensional array is missing the processed part of the field" do - it "should raise an exception" do - base_hash = { - :name => "Jane", - :pass => { - :raw => "secret", - :not_processed => "53oih7fhjdgj3f8=" - }, - :email => { - :raw => "Jane@gmail.com ", - :processed => "jane@gmail.com" - } - } - - fix = Mongo::Fixture.new - def fix.stub_data - @data = {} - end - fix.stub_data - expect { fix.simplify base_hash - }.to raise_error Mongo::Fixture::MissingProcessedValueError, - "The processed value to insert into the db is missing from the field 'pass', aborting" - end - end end - + describe "#rollback" do it "should check" do fix = Mongo::Fixture.new def fix.stub_data @data = {} @@ -628,44 +537,8 @@ it "should call drop on each of the used collections" do @truncable.should_receive(:drop).exactly(3).times @fix.rollback end - end - end - - context "two collections are to be inserted with references" do - before do - Fast.file.write "test/fixtures/references/users.yaml", "pepe: - username: pepe - password: - raw: secreto - processed: 252db48960f032db4bb604bc26f97106fa85ff88dedef3a28671b6bcd9f9644bf90d7e444d587c9351dfa237a6fc8fe38641a8469d084a166c7807d9c6564860 - name: Pepe" - Fast.file.write "test/fixtures/references/sessions.yaml", "14_horas: - user: - users: pepe - time: 2012-07-30T14:02:40-03:00 - y_tres_minutos: - user: - users: pepe - time: 2012-07-30T14:03:40-03:00 - y_cuatro_minutos: - user: - users: pepe - time: 2012-07-30T14:04:40-03:00" - end - - it "should not fail!" do - collection = double 'coll', :count => 0, :insert => nil - database = double 'database' - database.stub :[] do |argument| - collection - end - Mongo::Fixture.new :references, database - end - - after do - Fast.dir.remove! :test end end end