2011-06-09 01:05:22 +02:00
|
|
|
require "spec_helper"
|
2011-02-06 02:52:09 +01:00
|
|
|
|
|
|
|
class DesignModel < CouchRest::Model::Base
|
|
|
|
end
|
|
|
|
|
2011-06-09 01:05:22 +02:00
|
|
|
describe CouchRest::Model::Designs do
|
2011-02-06 02:52:09 +01:00
|
|
|
|
|
|
|
it "should accessable from model" do
|
|
|
|
DesignModel.respond_to?(:design).should be_true
|
|
|
|
end
|
|
|
|
|
2011-02-27 19:06:37 +01:00
|
|
|
describe "class methods" do
|
2011-02-06 17:17:14 +01:00
|
|
|
|
2011-02-27 19:06:37 +01:00
|
|
|
describe ".design" do
|
|
|
|
before :each do
|
|
|
|
@mapper = mock('DesignMapper')
|
|
|
|
@mapper.stub!(:create_view_method)
|
|
|
|
end
|
2011-02-06 02:52:09 +01:00
|
|
|
|
2011-02-27 19:06:37 +01:00
|
|
|
it "should instantiate a new DesignMapper" do
|
|
|
|
CouchRest::Model::Designs::DesignMapper.should_receive(:new).with(DesignModel).and_return(@mapper)
|
|
|
|
@mapper.should_receive(:create_view_method).with(:all)
|
|
|
|
@mapper.should_receive(:instance_eval)
|
|
|
|
DesignModel.design() { }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow methods to be called in mapper" do
|
|
|
|
@mapper.should_receive(:foo)
|
|
|
|
CouchRest::Model::Designs::DesignMapper.stub!(:new).and_return(@mapper)
|
|
|
|
DesignModel.design { foo }
|
|
|
|
end
|
|
|
|
|
2011-03-13 13:39:26 +01:00
|
|
|
it "should work even if a block is not provided" do
|
|
|
|
lambda { DesignModel.design }.should_not raise_error
|
|
|
|
end
|
|
|
|
|
2011-02-06 02:52:09 +01:00
|
|
|
end
|
|
|
|
|
2011-02-27 19:06:37 +01:00
|
|
|
describe "default_per_page" do
|
|
|
|
it "should return 25 default" do
|
|
|
|
DesignModel.default_per_page.should eql(25)
|
|
|
|
end
|
2011-02-06 02:52:09 +01:00
|
|
|
end
|
|
|
|
|
2011-02-27 19:06:37 +01:00
|
|
|
describe ".paginates_per" do
|
|
|
|
it "should set the default per page value" do
|
|
|
|
DesignModel.paginates_per(21)
|
|
|
|
DesignModel.default_per_page.should eql(21)
|
|
|
|
end
|
|
|
|
end
|
2011-02-06 02:52:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "DesignMapper" do
|
|
|
|
|
|
|
|
before :all do
|
2011-02-06 04:32:23 +01:00
|
|
|
@klass = CouchRest::Model::Designs::DesignMapper
|
2011-02-06 02:52:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should initialize and set model" do
|
|
|
|
object = @klass.new(DesignModel)
|
|
|
|
object.send(:model).should eql(DesignModel)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#view" do
|
|
|
|
|
|
|
|
before :each do
|
|
|
|
@object = @klass.new(DesignModel)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should call create method on view" do
|
2011-02-06 04:32:23 +01:00
|
|
|
CouchRest::Model::Designs::View.should_receive(:create).with(DesignModel, 'test', {})
|
2011-02-06 02:52:09 +01:00
|
|
|
@object.view('test')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should create a method on parent model" do
|
2011-02-06 04:32:23 +01:00
|
|
|
CouchRest::Model::Designs::View.stub!(:create)
|
2011-02-06 02:52:09 +01:00
|
|
|
@object.view('test_view')
|
|
|
|
DesignModel.should respond_to(:test_view)
|
|
|
|
end
|
|
|
|
|
2011-02-06 17:17:14 +01:00
|
|
|
it "should create a method for view instance" do
|
2011-02-06 04:32:23 +01:00
|
|
|
CouchRest::Model::Designs::View.stub!(:create)
|
2011-02-06 17:17:14 +01:00
|
|
|
@object.should_receive(:create_view_method).with('test')
|
|
|
|
@object.view('test')
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-04-27 13:30:08 +02:00
|
|
|
describe "#filter" do
|
|
|
|
|
|
|
|
before :each do
|
|
|
|
@object = @klass.new(DesignModel)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should add the provided function to the design doc" do
|
|
|
|
@object.filter(:important, "function(doc, req) { return doc.priority == 'high'; }")
|
|
|
|
DesignModel.design_doc['filters'].should_not be_empty
|
|
|
|
DesignModel.design_doc['filters']['important'].should_not be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-02-06 17:17:14 +01:00
|
|
|
describe "#create_view_method" do
|
|
|
|
before :each do
|
|
|
|
@object = @klass.new(DesignModel)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should create a method that returns view instance" do
|
2011-02-06 04:32:23 +01:00
|
|
|
CouchRest::Model::Designs::View.should_receive(:new).with(DesignModel, {}, 'test_view').and_return(nil)
|
2011-02-06 17:17:14 +01:00
|
|
|
@object.create_view_method('test_view')
|
2011-02-06 02:52:09 +01:00
|
|
|
DesignModel.test_view
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|