require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require 'rid/design_document' describe "DesignDocument" do before do @doc = Rid::DesignDocument.new end describe "read" do it "should assign key-value pair" do @doc.read("key") do |filename| "value" end @doc.hash.should == { "key" => "value" } end it "should assign two key-value pairs" do @doc.read("key1", "key2") do |filename| "value" end @doc.hash.should == { "key1" => "value", "key2" => "value" } end it "should assign an array of two key-value pairs" do @doc.read(["key1", "key2"]) do |filename| "value" end @doc.hash.should == { "key1" => "value", "key2" => "value" } end it "should assign nested hash" do @doc.read("hash/key") do |filename| "value" end @doc.hash.should == { "hash" => { "key" => "value" } } end it "should assign deep nested hash" do @doc.read("hash/hash/key") do |filename| "value" end @doc.hash.should == { "hash" => { "hash" => { "key" => "value" } } } end describe "exclude files should not be mapped" do it "should not map README" do @doc.read("README") do |filename| "value" end @doc.hash.should == {} end end describe "javascript files should be stripped off extension" do it "should strip validate_doc_update extension" do @doc.read("validate_doc_update.js") do |filename| "value" end @doc.hash.should == { "validate_doc_update" => "value" } end it "should strip lists/my_list.js" do @doc.read("lists/my_list.js") do |filename| "value" end @doc.hash.should == { "lists" => { "my_list" => "value" } } end it "should strip views/my_view/map.js" do @doc.read("views/my_view/map.js") do |filename| "value" end @doc.hash.should == { "views" => { "my_view" => { "map" => "value" } } } end end end describe "write" do it "should return key-value pair" do @doc.hash = { "key" => "value" } @doc.write do |key, value| key.should == "key" value.should == "value" end end it "should return subdirectory for nested hash" do @doc.hash = { "hash" => { "key" => "value" } } @doc.write do |key, value| key.should == "hash/key" value.should == "value" end end it "should return subdirectory for nested hash" do @doc.hash = { "hash" => { "hash" => { "key" => "value" } } } @doc.write do |key, value| key.should == "hash/hash/key" value.should == "value" end end describe "javascript extensions" do it "should append validate_doc_update" do @doc.hash = { "validate_doc_update" => "value" } @doc.write do |key, value| key.should == "validate_doc_update.js" end end it "should append lists/my_list" do @doc.hash = { "lists" => { "my_list" => "value" } } @doc.write do |key, value| key.should == "lists/my_list.js" end end it "should append views/my_view/map" do @doc.hash = { "views" => { "my_view" => { "map" => "value" } } } @doc.write do |key, value| key.should == "views/my_view/map.js" end end end end describe "json" do it "should convert key-value pair" do @doc.hash = { "key" => "value" } @doc.json.should == '{"key":"value"}' end it "should convert nested hash" do @doc.hash = { "hash" => { "key" => "value" } } @doc.json.should == '{"hash":{"key":"value"}}' end end describe "json=" do it "should read key-value pair" do @doc.json = '{"key":"value"}' @doc.hash.should == { "key" => "value" } end it "should read nested hash" do @doc.json = '{"hash":{"key":"value"}}' @doc.hash.should == { "hash" => { "key" => "value" } } end end describe "id accessor" do it "should return id from hash" do @doc.hash = { "_id" => "my_id" } @doc.id.should == "my_id" end it "should return id from rid" do Rid.stub!(:id).and_return("my_id") @doc.hash = {} @doc.id.should == "my_id" end end describe "rev accessor" do it "should return rev from hash" do @doc.hash = { "_rev" => "my_rev" } @doc.rev.should == "my_rev" end it "should return rev from rid" do Rid.stub!(:rev).and_return("my_rev") @doc.hash = {} @doc.rev.should == "my_rev" end it "should update rev in hash" do @doc.hash = {} @doc.rev = "my_rev" @doc.hash.should == { "_rev" => "my_rev" } end end describe "database accessor" do it "should return database rid" do Rid.stub!(:database).and_return("my_db") @doc.database.should == "my_db" end end describe "base url" do it "should combine database and id" do @doc.should_receive(:id).and_return("my_id") @doc.should_receive(:database).and_return("my_db") @doc.base_url.should == "my_db/my_id" end end describe "url" do it "should return base_url without options" do @doc.should_receive(:base_url).and_return("my_base_url") @doc.url.should == "my_base_url" end it "should return base_url with options string" do @doc.should_receive(:base_url).and_return("my_base_url") @doc.url(:key => :value).should == "my_base_url?key=value" end it "should return base_url with properly escaped options string" do @doc.should_receive(:base_url).and_return("my_base_url") @doc.url(:key => "value value").should == "my_base_url?key=value%20value" end end end