spec/unit/designs/design_spec.rb in couchrest_model-1.2.0.beta vs spec/unit/designs/design_spec.rb in couchrest_model-2.0.0.beta

- old
+ new

@@ -6,11 +6,11 @@ before :all do reset_test_db! end - class DesignSampleModel < CouchRest::Model::Base + class DesignSampleModelBase < CouchRest::Model::Base use_database DB property :name property :surname design do view :by_name @@ -18,10 +18,13 @@ design :stats do view :by_surname end end + class DesignSampleModel < DesignSampleModelBase + end + describe "class methods" do before :all do @klass = CouchRest::Model::Designs::Design end @@ -158,12 +161,91 @@ end end - describe "checksum" do + describe "#migrate" do + # WARNING! ORDER IS IMPORTANT! + describe "with limited changes" do + + class DesignSampleModelMigrate < DesignSampleModelBase + end + + before :all do + reset_test_db! + @mod = DesignSampleModelMigrate + @doc = @mod.design_doc + @db = @mod.database + end + + it "should create new design if non exists" do + @db.should_receive(:view).with("#{@doc['_id']}/_view/#{@doc['views'].keys.first}", {:limit => 1, :reduce => false}) + callback = @doc.migrate do |res| + res.should eql(:created) + end + doc = @db.get(@doc['_id']) + doc['views']['all'].should eql(@doc['views']['all']) + callback.should be_nil + end + + it "should not change anything if design is up to date" do + @doc.sync + @db.should_not_receive(:view) + callback = @doc.migrate do |res| + res.should eql(:no_change) + end + callback.should be_nil + end + + end + + describe "migrating a document if there are changes" do + + class DesignSampleModelMigrate2 < DesignSampleModelBase + end + + before :all do + reset_test_db! + @mod = DesignSampleModelMigrate2 + @doc = @mod.design_doc + @db = @mod.database + @doc.sync! + @doc.create_view(:by_name_and_surname) + @doc_id = @doc['_id'] + '_migration' + end + + it "should save new migration design doc" do + @db.should_receive(:view).with("#{@doc_id}/_view/by_name", {:limit => 1, :reduce => false}) + @callback = @doc.migrate do |res| + res.should eql(:migrated) + end + @callback.should_not be_nil + + # should not have updated original view until cleanup + doc = @db.get(@doc['_id']) + doc['views'].should_not have_key('by_name_and_surname') + + # Should have created the migration + new_doc = @db.get(@doc_id) + new_doc.should_not be_nil + + # should be possible to perform cleanup + @callback.call + lambda { new_doc = @db.get(@doc_id) }.should raise_error RestClient::ResourceNotFound + + doc = @db.get(@doc['_id']) + doc['views'].should have_key('by_name_and_surname') + end + + end + + end + + + describe "#checksum" do + before :all do @mod = DesignSampleModel @doc = @mod.design_doc end @@ -171,11 +253,11 @@ @doc.checksum.should_not be_blank end it "should provide same checksum without refresh on re-request" do chk = @doc.checksum - @doc.should_not_receive(:chaecksum!) + @doc.should_not_receive(:checksum!) @doc.checksum.should eql(chk) end it "should provide new checksum if the design has changed" do chk = @doc.checksum @@ -269,15 +351,15 @@ describe "Checksum calculations" do it "should calculate a consistent checksum for model" do #WithTemplateAndUniqueID.design_doc.checksum.should eql('caa2b4c27abb82b4e37421de76d96ffc') - WithTemplateAndUniqueID.design_doc.checksum.should eql('f0973aaa72e4db0efeb2a281ea297cec') + WithTemplateAndUniqueID.design_doc.checksum.should eql('7f44e88afbce06204010c49b76f31bcf') end it "should calculate checksum for complex model" do #Article.design_doc.checksum.should eql('70dff8caea143bf40fad09adf0701104') - Article.design_doc.checksum.should eql('7ef39bffdf5837e8b078411ac417d860') + Article.design_doc.checksum.should eql('0f25a2d9f86e31ebd61b29863a41d5ed') end it "should cache the generated checksum value" do Article.design_doc.checksum Article.design_doc['couchrest-hash'].should_not be_blank