couchrest_model/spec/couchrest/design_doc_spec.rb

212 lines
6.3 KiB
Ruby
Raw Normal View History

2011-04-13 22:46:11 +02:00
# encoding: utf-8
require File.expand_path("../../spec_helper", __FILE__)
require File.join(FIXTURE_PATH, 'base')
require File.join(FIXTURE_PATH, 'more', 'article')
describe "Design Documents" do
before :all do
reset_test_db!
end
2011-04-13 22:46:11 +02:00
describe "CouchRest Extension" do
it "should have created a checksum! method" do
::CouchRest::Design.new.should respond_to(:checksum!)
2011-04-13 22:46:11 +02:00
end
it "should calculate a consistent checksum for model" do
WithTemplateAndUniqueID.design_doc.checksum!.should eql('caa2b4c27abb82b4e37421de76d96ffc')
2011-04-13 22:46:11 +02:00
end
it "should calculate checksum for complex model" do
Article.design_doc.checksum!.should eql('70dff8caea143bf40fad09adf0701104')
end
it "should cache the generated checksum value" do
Article.design_doc.checksum!
Article.design_doc['couchrest-hash'].should_not be_blank
2011-04-13 22:46:11 +02:00
end
end
describe "class methods" do
describe ".design_doc" do
it "should provide Design document" do
Article.design_doc.should be_a(::CouchRest::Design)
end
end
describe ".design_doc_id" do
it "should provide a reasonable id" do
Article.design_doc_id.should eql("_design/Article")
end
end
describe ".design_doc_slug" do
it "should provide slug part of design doc" do
Article.design_doc_slug.should eql('Article')
end
end
2011-04-17 20:55:28 +02:00
describe ".design_doc_uri" do
it "should provide complete url" do
2011-04-17 20:55:28 +02:00
Article.design_doc_uri.should eql("#{COUCHHOST}/#{TESTDB}/_design/Article")
end
it "should provide complete url for new DB" do
db = mock("Database")
2011-04-17 20:55:28 +02:00
db.should_receive(:root).and_return('db')
Article.design_doc_uri(db).should eql("db/_design/Article")
end
end
describe ".stored_design_doc" do
it "should load a stored design from the database" do
Article.by_date
Article.stored_design_doc['_rev'].should_not be_blank
end
it "should return nil if not already stored" do
WithDefaultValues.stored_design_doc.should be_nil
end
end
describe ".save_design_doc" do
it "should call up the design updater" do
Article.should_receive(:update_design_doc).with('db', false)
Article.save_design_doc('db')
end
end
describe ".save_design_doc!" do
it "should call save_design_doc with force" do
Article.should_receive(:save_design_doc).with('db', true)
Article.save_design_doc!('db')
end
end
end
2011-04-13 22:46:11 +02:00
describe "basics" do
before :all do
reset_test_db!
end
2011-04-13 22:46:11 +02:00
it "should have been instantiated with views" do
d = Article.design_doc
d['views']['all']['map'].should include('Article')
end
it "should not have been saved yet" do
lambda { Article.database.get(Article.design_doc.id) }.should raise_error(RestClient::ResourceNotFound)
end
describe "after requesting a view" do
before :each do
Article.all
end
it "should have saved the design doc after view request" do
Article.database.get(Article.design_doc.id).should_not be_nil
end
end
describe "model with simple views" do
before(:all) do
Article.all.map{|a| a.destroy(true)}
Article.database.bulk_delete
written_at = Time.now - 24 * 3600 * 7
@titles = ["this and that", "also interesting", "more fun", "some junk"]
@titles.each do |title|
a = Article.new(:title => title)
a.date = written_at
a.save
written_at += 24 * 3600
end
end
it "will send request for the saved design doc on view request" do
reset_test_db!
Article.should_receive(:stored_design_doc).and_return(nil)
Article.by_date
end
2011-04-13 22:46:11 +02:00
it "should have generated a design doc" do
Article.design_doc["views"]["by_date"].should_not be_nil
end
it "should save the design doc when view requested" do
Article.by_date
doc = Article.database.get Article.design_doc.id
doc['views']['by_date'].should_not be_nil
end
it "should save design doc if a view changed" do
Article.by_date
orig = Article.stored_design_doc
design = Article.design_doc
view = design['views']['by_date']['map']
design['views']['by_date']['map'] = view + ' ' # little bit of white space
Article.by_date
Article.stored_design_doc['_rev'].should_not eql(orig['_rev'])
orig['views']['by_date']['map'].should_not eql(Article.design_doc['views']['by_date']['map'])
2011-04-13 22:46:11 +02:00
end
it "should not save design doc if not changed" do
Article.by_date
orig = Article.stored_design_doc['_rev']
Article.by_date
Article.stored_design_doc['_rev'].should eql(orig)
end
end
describe "when auto_update_design_doc false" do
before :all do
Article.auto_update_design_doc = false
Article.save_design_doc!
end
after :all do
Article.auto_update_design_doc = true
end
it "will not send a request for the saved design doc" do
Article.should_not_receive(:stored_design_doc)
Article.by_date
end
it "will not update stored design doc if view changed" do
Article.by_date
orig = Article.stored_design_doc
design = Article.design_doc
view = design['views']['by_date']['map']
design['views']['by_date']['map'] = view + ' '
Article.by_date
Article.stored_design_doc['_rev'].should eql(orig['_rev'])
end
it "will update stored design if forced" do
Article.by_date
orig = Article.stored_design_doc
design = Article.design_doc
view = design['views']['by_date']['map']
design['views']['by_date']['map'] = view + ' '
Article.save_design_doc!
Article.stored_design_doc['_rev'].should_not eql(orig['_rev'])
end
end
2011-04-13 22:46:11 +02:00
end
describe "lazily refreshing the design document" do
before(:all) do
@db = reset_test_db!
WithTemplateAndUniqueID.new('important-field' => '1').save
end
it "should not save the design doc twice" do
WithTemplateAndUniqueID.all
rev = WithTemplateAndUniqueID.design_doc['_rev']
WithTemplateAndUniqueID.design_doc['_rev'].should eql(rev)
end
end
end