couchrest_model/spec/couchrest/core/design_spec.rb

138 lines
3.6 KiB
Ruby
Raw Normal View History

2008-11-09 01:28:58 +01:00
require File.dirname(__FILE__) + '/../../spec_helper'
describe CouchRest::Design do
describe "defining a view" do
it "should add a view to the design doc" do
@des = CouchRest::Design.new
method = @des.view_by :name
method.should == "by_name"
@des["views"]["by_name"].should_not be_nil
end
end
describe "with an unsaved view" do
before(:each) do
@des = CouchRest::Design.new
@des.view_by :name
2008-11-09 01:28:58 +01:00
end
2008-11-22 01:21:20 +01:00
it "should accept a name" do
@des.name = "mytest"
@des.name.should == "mytest"
2008-11-09 01:28:58 +01:00
end
it "should not save on view definition" do
@des.rev.should be_nil
end
it "should freak out on view access" do
lambda{@des.view :by_name}.should raise_error
end
end
2008-11-22 01:21:20 +01:00
describe "saving" do
before(:each) do
@des = CouchRest::Design.new
@des.view_by :name
2008-11-22 01:21:20 +01:00
@des.database = reset_test_db!
end
it "should fail without a name" do
lambda{@des.save}.should raise_error(ArgumentError)
end
it "should work with a name" do
@des.name = "myview"
@des.save
end
end
2008-11-09 01:28:58 +01:00
describe "when it's saved" do
before(:each) do
@db = reset_test_db!
@db.bulk_save([{"name" => "x"},{"name" => "y"}])
@des = CouchRest::Design.new
@des.database = @db
@des.view_by :name
2008-11-09 01:28:58 +01:00
end
it "should by queryable when it's saved" do
2008-11-22 01:21:20 +01:00
@des.name = "mydesign"
2008-11-09 01:28:58 +01:00
@des.save
res = @des.view :by_name
res["rows"][0]["key"].should == "x"
end
it "should be queryable on specified database" do
@des.name = "mydesign"
@des.save
@des.database = nil
res = @des.view_on @db, :by_name
res["rows"][0]["key"].should == "x"
end
2008-11-09 01:28:58 +01:00
end
describe "from a saved document" do
2008-11-23 01:09:34 +01:00
before(:each) do
2008-11-09 01:28:58 +01:00
@db = reset_test_db!
@db.save_doc({
2008-11-09 01:28:58 +01:00
"_id" => "_design/test",
"views" => {
"by_name" => {
"map" => "function(doc){if (doc.name) emit(doc.name, null)}"
}
}
})
@db.bulk_save([{"name" => "a"},{"name" => "b"}])
@des = @db.get "_design/test"
end
it "should be a Design" do
2009-01-13 04:50:00 +01:00
@des.should be_an_instance_of(CouchRest::Design)
2008-11-09 01:28:58 +01:00
end
2008-11-22 01:21:20 +01:00
it "should have a modifiable name" do
@des.name.should == "test"
@des.name = "supertest"
2008-11-09 01:28:58 +01:00
@des.id.should == "_design/supertest"
end
it "should by queryable" do
res = @des.view :by_name
res["rows"][0]["key"].should == "a"
end
end
2008-11-22 01:21:20 +01:00
describe "a view with default options" do
before(:all) do
@db = reset_test_db!
@des = CouchRest::Design.new
@des.name = "test"
@des.view_by :name, :descending => true
2008-11-22 01:21:20 +01:00
@des.database = @db
@des.save
@db.bulk_save([{"name" => "a"},{"name" => "z"}])
end
it "should save them" do
@d2 = @db.get(@des.id)
@d2["views"]["by_name"]["couchrest-defaults"].should == {"descending"=>true}
end
it "should use them" do
res = @des.view :by_name
res["rows"].first["key"].should == "z"
end
it "should override them" do
res = @des.view :by_name, :descending => false
res["rows"].first["key"].should == "a"
end
end
describe "a view with multiple keys" do
before(:all) do
@db = reset_test_db!
@des = CouchRest::Design.new
@des.name = "test"
@des.view_by :name, :age
2008-11-22 01:21:20 +01:00
@des.database = @db
@des.save
@db.bulk_save([{"name" => "a", "age" => 2},
{"name" => "a", "age" => 4},{"name" => "z", "age" => 9}])
2008-11-22 01:21:20 +01:00
end
it "should work" do
res = @des.view :by_name_and_age
res["rows"].first["key"].should == ["a",2]
end
end
2008-11-09 01:28:58 +01:00
end