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 "_attachments encoding and content_type" do it "should proper encode and add plain text content type" do @doc.read("_attachments/key") do |filename| "value" end @doc.hash.should == { "_attachments" => { "key" => { "data" => "dmFsdWU=", "content_type" => "text/plain" } } } end it "should proper encode and add html content type" do @doc.read("_attachments/key.html") do |filename| "value" end @doc.hash.should == { "_attachments" => { "key.html" => { "data" => "dmFsdWU=", "content_type" => "text/html" } } } end it "should proper encode nested keys" do @doc.read("_attachments/hash/key") do |filename| "value" end @doc.hash.should == { "_attachments" => { "hash/key" => { "data" => "dmFsdWU=", "content_type" => "text/plain" } } } end 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 describe "code makro" do it "should expand code" do idx = 0 @doc.read("key", "lib/code.js") do |filename| case idx += 1 when 1 "// !code code.js" when 2 "value" end end @doc.hash.should == { "key" => "value", "lib" => { "code.js" => "value" } } end end describe "json makro" do it "should expand json" do idx = 0 @doc.read("key", "lib/json.json") do |filename| case idx += 1 when 1 "// !json json.json" when 2 "value" end end @doc.hash.should == { "key" => "var json = \"value\";", "lib" => { "json.json" => "value" } } end end end describe "write" do it "should return key-value pair" do @doc.hash = { "key" => "value" } filename, content = nil, nil @doc.write do |key, value| filename, content = key, value end filename.should == "key" content.should == "value" end it "should return subdirectory for nested hash" do @doc.hash = { "hash" => { "key" => "value" } } filename, content = nil, nil @doc.write do |key, value| filename, content = key, value end filename.should == "hash/key" content.should == "value" end it "should return subdirectory for nested hash" do @doc.hash = { "hash" => { "hash" => { "key" => "value" } } } filename, content = nil, nil @doc.write do |key, value| filename, content = key, value end filename.should == "hash/hash/key" content.should == "value" end it "should return decoded _attachments data" do @doc.hash = { "_attachments" => { "key" => { "data" => "dmFsdWU=" } } } filename, content = nil, nil @doc.write do |key, value| filename, content = key, value end filename.should == "_attachments/key" content.should == "value" end describe "javascript extensions" do it "should append validate_doc_update" do @doc.hash = { "validate_doc_update" => "value" } filename = nil @doc.write do |key, value| filename = key end filename.should == "validate_doc_update.js" end it "should append lists/my_list" do @doc.hash = { "lists" => { "my_list" => "value" } } filename = nil @doc.write do |key, value| filename = key end filename.should == "lists/my_list.js" end it "should append views/my_view/map" do @doc.hash = { "views" => { "my_view" => { "map" => "value" } } } filename = nil @doc.write do |key, value| filename = key end filename.should == "views/my_view/map.js" end end describe "code makro" do it "should reject code" do @doc.hash = { "key" => "value", "lib" => { "code.js" => "value" } } content = nil @doc.write do |key, value| content = value end content.should == "// !code code.js" end it "should reject nested code" do @doc.hash = { "key" => "value", "lib" => { "hash" => { "code.js" => "value" } } } content = nil @doc.write do |key, value| content = value end content.should == "// !code hash/code.js" end end describe "json makro" do it "should reject json" do @doc.hash = { "key" => "var json = \"value\";", "lib" => { "json.json" => "value" } } content = nil @doc.write do |key, value| content = value end content.should == "// !json json.json" end it "should reject nested json" do @doc.hash = { "key" => "var json = \"value\";", "lib" => { "hash" => { "json.json" => "value" } } } content = nil @doc.write do |key, value| content = value end content.should == "// !json hash/json.json" 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