got view queries happening correctly

This commit is contained in:
Chris Anderson 2008-11-21 16:21:20 -08:00
parent 32ffbfe019
commit 19a70ffd7d
7 changed files with 106 additions and 60 deletions

View file

@ -144,6 +144,8 @@ module CouchRest
# DELETE the document from CouchDB that has the given <tt>_id</tt> and # DELETE the document from CouchDB that has the given <tt>_id</tt> and
# <tt>_rev</tt>. # <tt>_rev</tt>.
def delete doc def delete doc
raise ArgumentError, "_id and _rev required for deleting" unless doc['_id'] && doc['_rev']
slug = CGI.escape(doc['_id']) slug = CGI.escape(doc['_id'])
CouchRest.delete "#{@root}/#{slug}?rev=#{doc['_rev']}" CouchRest.delete "#{@root}/#{slug}?rev=#{doc['_rev']}"
end end

View file

@ -1,7 +1,6 @@
module CouchRest module CouchRest
class Design < Document class Design < Document
def view_by *keys def view_by *keys
# @stale = true
opts = keys.pop if keys.last.is_a?(Hash) opts = keys.pop if keys.last.is_a?(Hash)
opts ||= {} opts ||= {}
self['views'] ||= {} self['views'] ||= {}
@ -29,6 +28,7 @@ module CouchRest
self['views'][method_name] = { self['views'][method_name] = {
'map' => map_function 'map' => map_function
} }
self['views'][method_name]['couchrest-defaults'] = opts
method_name method_name
end end
end end
@ -43,24 +43,23 @@ module CouchRest
# end # end
# Dispatches to any named view. # Dispatches to any named view.
def view name, query={}, &block def view view_name, query={}, &block
# if @stale view_name = view_name.to_s
# self.save view_slug = "#{name}/#{view_name}"
# end defaults = (self['views'][view_name] && self['views'][view_name]["couchrest-defaults"]) || {}
view_name = "#{slug}/#{name}" fetch_view(view_slug, defaults.merge(query), &block)
fetch_view(view_name, query, &block)
end end
def slug def name
id.sub('_design/','') id.sub('_design/','') if id
end end
def slug= newslug def name= newname
self['_id'] = "_design/#{newslug}" self['_id'] = "_design/#{newname}"
end end
def save def save
raise ArgumentError, "_design" unless slug && slug.length > 0 raise ArgumentError, "_design docs require a name" unless name && name.length > 0
super super
end end

View file

@ -37,17 +37,15 @@ module CouchRest
# CouchDB's response. # CouchDB's response.
def save def save
raise ArgumentError, "doc.database required for saving" unless database raise ArgumentError, "doc.database required for saving" unless database
if new_record? result = database.save self
create result['ok']
else
update
end
end end
# Deletes the document from the database. Runs the :delete callbacks. # Deletes the document from the database. Runs the :delete callbacks.
# Removes the <tt>_id</tt> and <tt>_rev</tt> fields, preparing the # Removes the <tt>_id</tt> and <tt>_rev</tt> fields, preparing the
# document to be saved to a new <tt>_id</tt>. # document to be saved to a new <tt>_id</tt>.
def destroy def destroy
raise ArgumentError, "doc.database required to destroy" unless database
result = database.delete self result = database.delete self
if result['ok'] if result['ok']
self['_rev'] = nil self['_rev'] = nil
@ -56,27 +54,6 @@ module CouchRest
result['ok'] result['ok']
end end
protected
# Saves a document for the first time, after running the before(:create)
# callbacks, and applying the unique_id.
def create
set_unique_id if respond_to?(:set_unique_id) # hack
save_doc
end
# Saves the document and runs the :update callbacks.
def update
save_doc
end
private
def save_doc
result = database.save self
result['ok']
end
end end

View file

@ -418,6 +418,9 @@ describe CouchRest::Database do
@db.delete doc @db.delete doc
lambda{@db.get @docid}.should raise_error lambda{@db.get @docid}.should raise_error
end end
it "should fail without an _id" do
lambda{@db.delete({"not"=>"a real doc"})}.should raise_error(ArgumentError)
end
end end
it "should list documents" do it "should list documents" do

View file

@ -2,22 +2,13 @@ require File.dirname(__FILE__) + '/../../spec_helper'
describe CouchRest::Design do describe CouchRest::Design do
# before(:each) do
# @db = reset_test_db!
# end
describe "defining a view" do describe "defining a view" do
# before(:each) do
# @design_docs = @db.documents :startkey => "_design/",
# :endkey => "_design/\u9999"
# end
it "should add a view to the design doc" do it "should add a view to the design doc" do
@des = CouchRest::Design.new @des = CouchRest::Design.new
method = @des.view_by :name method = @des.view_by :name
method.should == "by_name" method.should == "by_name"
@des["views"]["by_name"].should_not be_nil @des["views"]["by_name"].should_not be_nil
end end
end end
describe "with an unsaved view" do describe "with an unsaved view" do
@ -25,9 +16,9 @@ describe CouchRest::Design do
@des = CouchRest::Design.new @des = CouchRest::Design.new
method = @des.view_by :name method = @des.view_by :name
end end
it "should accept a slug" do it "should accept a name" do
@des.slug = "mytest" @des.name = "mytest"
@des.slug.should == "mytest" @des.name.should == "mytest"
end end
it "should not save on view definition" do it "should not save on view definition" do
@des.rev.should be_nil @des.rev.should be_nil
@ -37,6 +28,21 @@ describe CouchRest::Design do
end end
end end
describe "saving" do
before(:each) do
@des = CouchRest::Design.new
method = @des.view_by :name
@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
describe "when it's saved" do describe "when it's saved" do
before(:each) do before(:each) do
@db = reset_test_db! @db = reset_test_db!
@ -45,11 +51,8 @@ describe CouchRest::Design do
@des.database = @db @des.database = @db
method = @des.view_by :name method = @des.view_by :name
end end
it "should become angry when saved without a slug" do
lambda{@des.save}.should raise_error
end
it "should by queryable when it's saved" do it "should by queryable when it's saved" do
@des.slug = "mydesign" @des.name = "mydesign"
@des.save @des.save
res = @des.view :by_name res = @des.view :by_name
res["rows"][0]["key"].should == "x" res["rows"][0]["key"].should == "x"
@ -73,9 +76,9 @@ describe CouchRest::Design do
it "should be a Design" do it "should be a Design" do
@des.should be_an_instance_of CouchRest::Design @des.should be_an_instance_of CouchRest::Design
end end
it "should have a slug" do it "should have a modifiable name" do
@des.slug.should == "test" @des.name.should == "test"
@des.slug = "supertest" @des.name = "supertest"
@des.id.should == "_design/supertest" @des.id.should == "_design/supertest"
end end
it "should by queryable" do it "should by queryable" do
@ -83,4 +86,45 @@ describe CouchRest::Design do
res["rows"][0]["key"].should == "a" res["rows"][0]["key"].should == "a"
end end
end end
describe "a view with default options" do
before(:all) do
@db = reset_test_db!
@des = CouchRest::Design.new
@des.name = "test"
method = @des.view_by :name, :descending => true
@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"
method = @des.view_by :name, :age
@des.database = @db
@des.save
@db.bulk_save([{"name" => "a", "age" => 2},{"name" => "a", "age" => 4},{"name" => "z", "age" => 9}])
end
it "should work" do
res = @des.view :by_name_and_age
res["rows"].first["key"].should == ["a",2]
end
end
end end

View file

@ -44,7 +44,7 @@ describe CouchRest::Document, "saving using a database" do
@db = reset_test_db! @db = reset_test_db!
@resp = @db.save(@doc) @resp = @db.save(@doc)
end end
it "should get the database" do it "should apply the database" do
@doc.database.should == @db @doc.database.should == @db
end end
it "should get id and rev" do it "should get id and rev" do
@ -67,9 +67,30 @@ describe "getting from a database" do
it "should have a database" do it "should have a database" do
@doc.database.should == @db @doc.database.should == @db
end end
it "should be saveable" do it "should be saveable and resavable" do
@doc["more"] = "keys" @doc["more"] = "keys"
@doc.save @doc.save
@db.get(@resp['id'])["more"].should == "keys" @db.get(@resp['id'])["more"].should == "keys"
@doc["more"] = "these keys"
@doc.save
@db.get(@resp['id'])["more"].should == "these keys"
end
end
describe "destroying a document from a db" do
before(:all) do
@db = reset_test_db!
@resp = @db.save({
"key" => "value"
})
@doc = @db.get @resp['id']
end
it "should make it disappear" do
@doc.destroy
lambda{@db.get @resp['id']}.should raise_error
end
it "should error when there's no db" do
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
lambda{@doc.destroy}.should raise_error(ArgumentError)
end end
end end

View file

@ -1,5 +1,5 @@
require File.dirname(__FILE__) + '/../../spec_helper' require File.dirname(__FILE__) + '/../../spec_helper'
__END__
class Basic < CouchRest::Model class Basic < CouchRest::Model
end end