slight change of API, CR::Document now uses <action>_doc instead of <action>, also added #create! and #recreate! to Document instances
This commit is contained in:
parent
e9f7456eab
commit
fda5be213a
|
@ -3,7 +3,7 @@ require "base64"
|
|||
|
||||
module CouchRest
|
||||
class Database
|
||||
attr_reader :server, :host, :name, :root
|
||||
attr_reader :server, :host, :name, :root, :uri
|
||||
attr_accessor :bulk_save_cache_limit
|
||||
|
||||
# Create a CouchRest::Database adapter for the supplied CouchRest::Server
|
||||
|
@ -13,11 +13,11 @@ module CouchRest
|
|||
# server<CouchRest::Server>:: database host
|
||||
# name<String>:: database name
|
||||
#
|
||||
def initialize server, name
|
||||
def initialize(server, name)
|
||||
@name = name
|
||||
@server = server
|
||||
@host = server.uri
|
||||
@root = "#{host}/#{name}"
|
||||
@uri = @root = "#{host}/#{name}"
|
||||
@streamer = Streamer.new(self)
|
||||
@bulk_save_cache = []
|
||||
@bulk_save_cache_limit = 50
|
||||
|
@ -25,18 +25,18 @@ module CouchRest
|
|||
|
||||
# returns the database's uri
|
||||
def to_s
|
||||
@root
|
||||
@uri
|
||||
end
|
||||
|
||||
# GET the database info from CouchDB
|
||||
def info
|
||||
CouchRest.get @root
|
||||
CouchRest.get @uri
|
||||
end
|
||||
|
||||
# Query the <tt>_all_docs</tt> view. Accepts all the same arguments as view.
|
||||
def documents params = {}
|
||||
def documents(params = {})
|
||||
keys = params.delete(:keys)
|
||||
url = CouchRest.paramify_url "#{@root}/_all_docs", params
|
||||
url = CouchRest.paramify_url "#{@uri}/_all_docs", params
|
||||
if keys
|
||||
CouchRest.post(url, {:keys => keys})
|
||||
else
|
||||
|
@ -47,10 +47,10 @@ module CouchRest
|
|||
# POST a temporary view function to CouchDB for querying. This is not
|
||||
# recommended, as you don't get any performance benefit from CouchDB's
|
||||
# materialized views. Can be quite slow on large databases.
|
||||
def slow_view funcs, params = {}
|
||||
def slow_view(funcs, params = {})
|
||||
keys = params.delete(:keys)
|
||||
funcs = funcs.merge({:keys => keys}) if keys
|
||||
url = CouchRest.paramify_url "#{@root}/_temp_view", params
|
||||
url = CouchRest.paramify_url "#{@uri}/_temp_view", params
|
||||
JSON.parse(RestClient.post(url, funcs.to_json, {"Content-Type" => 'application/json'}))
|
||||
end
|
||||
|
||||
|
@ -59,9 +59,9 @@ module CouchRest
|
|||
|
||||
# Query a CouchDB view as defined by a <tt>_design</tt> document. Accepts
|
||||
# paramaters as described in http://wiki.apache.org/couchdb/HttpViewApi
|
||||
def view name, params = {}, &block
|
||||
def view(name, params = {}, &block)
|
||||
keys = params.delete(:keys)
|
||||
url = CouchRest.paramify_url "#{@root}/_view/#{name}", params
|
||||
url = CouchRest.paramify_url "#{@uri}/_view/#{name}", params
|
||||
if keys
|
||||
CouchRest.post(url, {:keys => keys})
|
||||
else
|
||||
|
@ -74,9 +74,9 @@ module CouchRest
|
|||
end
|
||||
|
||||
# GET a document from CouchDB, by id. Returns a Ruby Hash.
|
||||
def get id
|
||||
def get(id)
|
||||
slug = escape_docid(id)
|
||||
hash = CouchRest.get("#{@root}/#{slug}")
|
||||
hash = CouchRest.get("#{@uri}/#{slug}")
|
||||
doc = if /^_design/ =~ hash["_id"]
|
||||
Design.new(hash)
|
||||
else
|
||||
|
@ -87,20 +87,20 @@ module CouchRest
|
|||
end
|
||||
|
||||
# GET an attachment directly from CouchDB
|
||||
def fetch_attachment docid, name
|
||||
def fetch_attachment(docid, name)
|
||||
slug = escape_docid(docid)
|
||||
name = CGI.escape(name)
|
||||
RestClient.get "#{@root}/#{slug}/#{name}"
|
||||
RestClient.get "#{@uri}/#{slug}/#{name}"
|
||||
end
|
||||
|
||||
# PUT an attachment directly to CouchDB
|
||||
def put_attachment doc, name, file, options = {}
|
||||
def put_attachment(doc, name, file, options = {})
|
||||
docid = escape_docid(doc['_id'])
|
||||
name = CGI.escape(name)
|
||||
uri = if doc['_rev']
|
||||
"#{@root}/#{docid}/#{name}?rev=#{doc['_rev']}"
|
||||
"#{@uri}/#{docid}/#{name}?rev=#{doc['_rev']}"
|
||||
else
|
||||
"#{@root}/#{docid}/#{name}"
|
||||
"#{@uri}/#{docid}/#{name}"
|
||||
end
|
||||
|
||||
JSON.parse(RestClient.put(uri, file, options))
|
||||
|
@ -115,7 +115,7 @@ module CouchRest
|
|||
#
|
||||
# If <tt>bulk</tt> is true (false by default) the document is cached for bulk-saving later.
|
||||
# Bulk saving happens automatically when #bulk_save_cache limit is exceded, or on the next non bulk save.
|
||||
def save (doc, bulk = false)
|
||||
def save_doc(doc, bulk = false)
|
||||
if doc['_attachments']
|
||||
doc['_attachments'] = encode_attachments(doc['_attachments'])
|
||||
end
|
||||
|
@ -128,13 +128,13 @@ module CouchRest
|
|||
end
|
||||
result = if doc['_id']
|
||||
slug = escape_docid(doc['_id'])
|
||||
CouchRest.put "#{@root}/#{slug}", doc
|
||||
CouchRest.put "#{@uri}/#{slug}", doc
|
||||
else
|
||||
begin
|
||||
slug = doc['_id'] = @server.next_uuid
|
||||
CouchRest.put "#{@root}/#{slug}", doc
|
||||
CouchRest.put "#{@uri}/#{slug}", doc
|
||||
rescue #old version of couchdb
|
||||
CouchRest.post @root, doc
|
||||
CouchRest.post @uri, doc
|
||||
end
|
||||
end
|
||||
if result['ok']
|
||||
|
@ -145,6 +145,13 @@ module CouchRest
|
|||
result
|
||||
end
|
||||
|
||||
### DEPRECATION NOTICE
|
||||
def save(doc, bulk=false)
|
||||
puts "CouchRest::Database's save method is being deprecated, please use save_doc instead"
|
||||
save_doc(doc, bulk)
|
||||
end
|
||||
|
||||
|
||||
# POST an array of documents to CouchDB. If any of the documents are
|
||||
# missing ids, supply one from the uuid cache.
|
||||
#
|
||||
|
@ -162,7 +169,7 @@ module CouchRest
|
|||
doc['_id'] = nextid if nextid
|
||||
end
|
||||
end
|
||||
CouchRest.post "#{@root}/_bulk_docs", {:docs => docs}
|
||||
CouchRest.post "#{@uri}/_bulk_docs", {:docs => docs}
|
||||
end
|
||||
|
||||
# DELETE the document from CouchDB that has the given <tt>_id</tt> and
|
||||
|
@ -170,7 +177,7 @@ module CouchRest
|
|||
#
|
||||
# If <tt>bulk</tt> is true (false by default) the deletion is recorded for bulk-saving (bulk-deletion :) later.
|
||||
# Bulk saving happens automatically when #bulk_save_cache limit is exceded, or on the next non bulk save.
|
||||
def delete (doc, bulk = false)
|
||||
def delete_doc(doc, bulk = false)
|
||||
raise ArgumentError, "_id and _rev required for deleting" unless doc['_id'] && doc['_rev']
|
||||
if bulk
|
||||
@bulk_save_cache << { '_id' => doc['_id'], '_rev' => doc['_rev'], '_deleted' => true }
|
||||
|
@ -178,13 +185,19 @@ module CouchRest
|
|||
return { "ok" => true } # Mimic the non-deferred version
|
||||
end
|
||||
slug = escape_docid(doc['_id'])
|
||||
CouchRest.delete "#{@root}/#{slug}?rev=#{doc['_rev']}"
|
||||
CouchRest.delete "#{@uri}/#{slug}?rev=#{doc['_rev']}"
|
||||
end
|
||||
|
||||
### DEPRECATION NOTICE
|
||||
def delete(doc, bulk=false)
|
||||
puts "CouchRest::Database's delete method is being deprecated, please use delete_doc instead"
|
||||
delete_doc(doc, bulk)
|
||||
end
|
||||
|
||||
# COPY an existing document to a new id. If the destination id currently exists, a rev must be provided.
|
||||
# <tt>dest</tt> can take one of two forms if overwriting: "id_to_overwrite?rev=revision" or the actual doc
|
||||
# hash with a '_rev' key
|
||||
def copy doc, dest
|
||||
def copy_doc(doc, dest)
|
||||
raise ArgumentError, "_id is required for copying" unless doc['_id']
|
||||
slug = escape_docid(doc['_id'])
|
||||
destination = if dest.respond_to?(:has_key?) && dest['_id'] && dest['_rev']
|
||||
|
@ -192,13 +205,19 @@ module CouchRest
|
|||
else
|
||||
dest
|
||||
end
|
||||
CouchRest.copy "#{@root}/#{slug}", destination
|
||||
CouchRest.copy "#{@uri}/#{slug}", destination
|
||||
end
|
||||
|
||||
### DEPRECATION NOTICE
|
||||
def copy(doc, dest)
|
||||
puts "CouchRest::Database's copy method is being deprecated, please use copy_doc instead"
|
||||
copy_doc(doc, dest)
|
||||
end
|
||||
|
||||
# MOVE an existing document to a new id. If the destination id currently exists, a rev must be provided.
|
||||
# <tt>dest</tt> can take one of two forms if overwriting: "id_to_overwrite?rev=revision" or the actual doc
|
||||
# hash with a '_rev' key
|
||||
def move doc, dest
|
||||
def move_doc(doc, dest)
|
||||
raise ArgumentError, "_id and _rev are required for moving" unless doc['_id'] && doc['_rev']
|
||||
slug = escape_docid(doc['_id'])
|
||||
destination = if dest.respond_to?(:has_key?) && dest['_id'] && dest['_rev']
|
||||
|
@ -206,27 +225,48 @@ module CouchRest
|
|||
else
|
||||
dest
|
||||
end
|
||||
CouchRest.move "#{@root}/#{slug}?rev=#{doc['_rev']}", destination
|
||||
CouchRest.move "#{@uri}/#{slug}?rev=#{doc['_rev']}", destination
|
||||
end
|
||||
|
||||
### DEPRECATION NOTICE
|
||||
def move(doc, dest)
|
||||
puts "CouchRest::Database's move method is being deprecated, please use move_doc instead"
|
||||
move_doc(doc, dest)
|
||||
end
|
||||
|
||||
# Compact the database, removing old document revisions and optimizing space use.
|
||||
def compact!
|
||||
CouchRest.post "#{@root}/_compact"
|
||||
CouchRest.post "#{@uri}/_compact"
|
||||
end
|
||||
|
||||
# Create the database
|
||||
def create!
|
||||
bool = server.create_db(@name) rescue false
|
||||
bool && true
|
||||
end
|
||||
|
||||
# Delete and re create the database
|
||||
def recreate!
|
||||
delete!
|
||||
create!
|
||||
rescue RestClient::ResourceNotFound
|
||||
ensure
|
||||
create!
|
||||
end
|
||||
|
||||
# DELETE the database itself. This is not undoable and could be rather
|
||||
# catastrophic. Use with care!
|
||||
def delete!
|
||||
CouchRest.delete @root
|
||||
CouchRest.delete @uri
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def escape_docid id
|
||||
def escape_docid(id)
|
||||
/^_design\/(.*)/ =~ id ? "_design/#{CGI.escape($1)}" : CGI.escape(id)
|
||||
end
|
||||
|
||||
def encode_attachments attachments
|
||||
def encode_attachments(attachments)
|
||||
attachments.each do |k,v|
|
||||
next if v['stub']
|
||||
v['data'] = base64(v['data'])
|
||||
|
@ -234,7 +274,7 @@ module CouchRest
|
|||
attachments
|
||||
end
|
||||
|
||||
def base64 data
|
||||
def base64(data)
|
||||
Base64.encode64(data).gsub(/\s/,'')
|
||||
end
|
||||
end
|
||||
|
|
|
@ -38,7 +38,7 @@ module CouchRest
|
|||
# If <tt>bulk</tt> is <tt>true</tt> (defaults to false) the document is cached for bulk save.
|
||||
def save(bulk = false)
|
||||
raise ArgumentError, "doc.database required for saving" unless database
|
||||
result = database.save self, bulk
|
||||
result = database.save_doc self, bulk
|
||||
result['ok']
|
||||
end
|
||||
|
||||
|
@ -49,7 +49,7 @@ module CouchRest
|
|||
# actually be deleted from the db until bulk save.
|
||||
def destroy(bulk = false)
|
||||
raise ArgumentError, "doc.database required to destroy" unless database
|
||||
result = database.delete(self, bulk)
|
||||
result = database.delete_doc(self, bulk)
|
||||
if result['ok']
|
||||
self['_rev'] = nil
|
||||
self['_id'] = nil
|
||||
|
@ -59,13 +59,13 @@ module CouchRest
|
|||
|
||||
def copy(dest)
|
||||
raise ArgumentError, "doc.database required to copy" unless database
|
||||
result = database.copy(self, dest)
|
||||
result = database.copy_doc(self, dest)
|
||||
result['ok']
|
||||
end
|
||||
|
||||
def move(dest)
|
||||
raise ArgumentError, "doc.database required to copy" unless database
|
||||
result = database.move(self, dest)
|
||||
result = database.move_doc(self, dest)
|
||||
result['ok']
|
||||
end
|
||||
|
||||
|
|
|
@ -365,7 +365,7 @@ module CouchRest
|
|||
ddocs = all_design_doc_versions
|
||||
ddocs["rows"].each do |row|
|
||||
if (row['id'] != design_doc_id)
|
||||
database.delete({
|
||||
database.delete_doc({
|
||||
"_id" => row['id'],
|
||||
"_rev" => row['value']['rev']
|
||||
})
|
||||
|
@ -485,9 +485,11 @@ module CouchRest
|
|||
alias :new_record? :new_document?
|
||||
|
||||
# Overridden to set the unique ID.
|
||||
# Returns a boolean value
|
||||
def save bulk = false
|
||||
set_unique_id if new_document? && self.respond_to?(:set_unique_id)
|
||||
super(bulk)
|
||||
result = database.save_doc(self, bulk)
|
||||
result["ok"] == true
|
||||
end
|
||||
|
||||
# Saves the document to the db using create or update. Raises an exception
|
||||
|
@ -500,7 +502,7 @@ module CouchRest
|
|||
# Removes the <tt>_id</tt> and <tt>_rev</tt> fields, preparing the
|
||||
# document to be saved to a new <tt>_id</tt>.
|
||||
def destroy
|
||||
result = database.delete self
|
||||
result = database.delete_doc self
|
||||
if result['ok']
|
||||
self['_rev'] = nil
|
||||
self['_id'] = nil
|
||||
|
|
|
@ -61,7 +61,7 @@ describe CouchRest::Database do
|
|||
emit(doc.word,null);
|
||||
}
|
||||
}'}}
|
||||
@db.save({
|
||||
@db.save_doc({
|
||||
"_id" => "_design/test",
|
||||
:views => @view
|
||||
})
|
||||
|
@ -80,7 +80,7 @@ describe CouchRest::Database do
|
|||
|
||||
describe "select from an existing view" do
|
||||
before(:each) do
|
||||
r = @db.save({
|
||||
r = @db.save_doc({
|
||||
"_id" => "_design/first",
|
||||
:views => {
|
||||
:test => {
|
||||
|
@ -129,9 +129,9 @@ describe CouchRest::Database do
|
|||
|
||||
describe "GET (document by id) when the doc exists" do
|
||||
before(:each) do
|
||||
@r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
|
||||
@r = @db.save_doc({'lemons' => 'from texas', 'and' => 'spain'})
|
||||
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
||||
@db.save({'_id' => @docid, 'will-exist' => 'here'})
|
||||
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
||||
end
|
||||
it "should get the document" do
|
||||
doc = @db.get(@r['id'])
|
||||
|
@ -176,7 +176,7 @@ describe CouchRest::Database do
|
|||
end
|
||||
|
||||
it "in the case of an id conflict should not insert anything" do
|
||||
@r = @db.save({'lemons' => 'from texas', 'and' => 'how', "_id" => "oneB"})
|
||||
@r = @db.save_doc({'lemons' => 'from texas', 'and' => 'how', "_id" => "oneB"})
|
||||
|
||||
lambda do
|
||||
rs = @db.bulk_save([
|
||||
|
@ -192,7 +192,7 @@ describe CouchRest::Database do
|
|||
end
|
||||
|
||||
it "should empty the bulk save cache if no documents are given" do
|
||||
@db.save({"_id" => "bulk_cache_1", "val" => "test"}, true)
|
||||
@db.save_doc({"_id" => "bulk_cache_1", "val" => "test"}, true)
|
||||
lambda do
|
||||
@db.get('bulk_cache_1')
|
||||
end.should raise_error(RestClient::ResourceNotFound)
|
||||
|
@ -201,7 +201,7 @@ describe CouchRest::Database do
|
|||
end
|
||||
|
||||
it "should raise an error that is useful for recovery" do
|
||||
@r = @db.save({"_id" => "taken", "field" => "stuff"})
|
||||
@r = @db.save_doc({"_id" => "taken", "field" => "stuff"})
|
||||
begin
|
||||
rs = @db.bulk_save([
|
||||
{"_id" => "taken", "wild" => "and random"},
|
||||
|
@ -220,13 +220,13 @@ describe CouchRest::Database do
|
|||
@db.documents["total_rows"].should == 0
|
||||
end
|
||||
it "should create the document and return the id" do
|
||||
r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
|
||||
r = @db.save_doc({'lemons' => 'from texas', 'and' => 'spain'})
|
||||
r2 = @db.get(r['id'])
|
||||
r2["lemons"].should == "from texas"
|
||||
end
|
||||
it "should use PUT with UUIDs" do
|
||||
CouchRest.should_receive(:put).and_return({"ok" => true, "id" => "100", "rev" => "55"})
|
||||
r = @db.save({'just' => ['another document']})
|
||||
r = @db.save_doc({'just' => ['another document']})
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -260,7 +260,7 @@ describe CouchRest::Database do
|
|||
}
|
||||
}
|
||||
}
|
||||
@db.save(@doc)
|
||||
@db.save_doc(@doc)
|
||||
end
|
||||
it "should save and be indicated" do
|
||||
doc = @db.get("mydocwithattachment")
|
||||
|
@ -284,10 +284,10 @@ describe CouchRest::Database do
|
|||
}
|
||||
}
|
||||
}
|
||||
@db.save(doc)
|
||||
@db.save_doc(doc)
|
||||
doc = @db.get('mydocwithattachment')
|
||||
doc['field'] << 'another value'
|
||||
@db.save(doc)
|
||||
@db.save_doc(doc)
|
||||
end
|
||||
|
||||
it 'should be there' do
|
||||
|
@ -314,7 +314,7 @@ describe CouchRest::Database do
|
|||
}
|
||||
}
|
||||
}
|
||||
@db.save(@doc)
|
||||
@db.save_doc(@doc)
|
||||
end
|
||||
it "should save and be indicated" do
|
||||
doc = @db.get("mydocwithattachment")
|
||||
|
@ -343,7 +343,7 @@ describe CouchRest::Database do
|
|||
}
|
||||
}
|
||||
}
|
||||
@docid = @db.save(@doc)['id']
|
||||
@docid = @db.save_doc(@doc)['id']
|
||||
end
|
||||
it "should save and be indicated" do
|
||||
doc = @db.get(@docid)
|
||||
|
@ -358,15 +358,15 @@ describe CouchRest::Database do
|
|||
describe "PUT (new document with url id)" do
|
||||
it "should create the document" do
|
||||
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
||||
@db.save({'_id' => @docid, 'will-exist' => 'here'})
|
||||
lambda{@db.save({'_id' => @docid})}.should raise_error(RestClient::Request::RequestFailed)
|
||||
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
||||
lambda{@db.save_doc({'_id' => @docid})}.should raise_error(RestClient::Request::RequestFailed)
|
||||
@db.get(@docid)['will-exist'].should == 'here'
|
||||
end
|
||||
end
|
||||
|
||||
describe "PUT (new document with id)" do
|
||||
it "should start without the document" do
|
||||
# r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
|
||||
# r = @db.save_doc({'lemons' => 'from texas', 'and' => 'spain'})
|
||||
@db.documents['rows'].each do |doc|
|
||||
doc['id'].should_not == 'my-doc'
|
||||
end
|
||||
|
@ -375,17 +375,17 @@ describe CouchRest::Database do
|
|||
# or instead make it return something with a fancy <=> method
|
||||
end
|
||||
it "should create the document" do
|
||||
@db.save({'_id' => 'my-doc', 'will-exist' => 'here'})
|
||||
lambda{@db.save({'_id' => 'my-doc'})}.should raise_error(RestClient::Request::RequestFailed)
|
||||
@db.save_doc({'_id' => 'my-doc', 'will-exist' => 'here'})
|
||||
lambda{@db.save_doc({'_id' => 'my-doc'})}.should raise_error(RestClient::Request::RequestFailed)
|
||||
end
|
||||
end
|
||||
|
||||
describe "PUT (existing document with rev)" do
|
||||
before(:each) do
|
||||
@db.save({'_id' => 'my-doc', 'will-exist' => 'here'})
|
||||
@db.save_doc({'_id' => 'my-doc', 'will-exist' => 'here'})
|
||||
@doc = @db.get('my-doc')
|
||||
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
||||
@db.save({'_id' => @docid, 'now' => 'save'})
|
||||
@db.save_doc({'_id' => @docid, 'now' => 'save'})
|
||||
end
|
||||
it "should start with the document" do
|
||||
@doc['will-exist'].should == 'here'
|
||||
|
@ -394,18 +394,18 @@ describe CouchRest::Database do
|
|||
it "should save with url id" do
|
||||
doc = @db.get(@docid)
|
||||
doc['yaml'] = ['json', 'word.']
|
||||
@db.save doc
|
||||
@db.save_doc doc
|
||||
@db.get(@docid)['yaml'].should == ['json', 'word.']
|
||||
end
|
||||
it "should fail to resave without the rev" do
|
||||
@doc['them-keys'] = 'huge'
|
||||
@doc['_rev'] = 'wrong'
|
||||
# @db.save(@doc)
|
||||
lambda {@db.save(@doc)}.should raise_error
|
||||
# @db.save_doc(@doc)
|
||||
lambda {@db.save_doc(@doc)}.should raise_error
|
||||
end
|
||||
it "should update the document" do
|
||||
@doc['them-keys'] = 'huge'
|
||||
@db.save(@doc)
|
||||
@db.save_doc(@doc)
|
||||
now = @db.get('my-doc')
|
||||
now['them-keys'].should == 'huge'
|
||||
end
|
||||
|
@ -414,7 +414,7 @@ describe CouchRest::Database do
|
|||
describe "cached bulk save" do
|
||||
it "stores documents in a database-specific cache" do
|
||||
td = {"_id" => "btd1", "val" => "test"}
|
||||
@db.save(td, true)
|
||||
@db.save_doc(td, true)
|
||||
@db.instance_variable_get("@bulk_save_cache").should == [td]
|
||||
|
||||
end
|
||||
|
@ -423,8 +423,8 @@ describe CouchRest::Database do
|
|||
@db.bulk_save_cache_limit = 3
|
||||
td1 = {"_id" => "td1", "val" => true}
|
||||
td2 = {"_id" => "td2", "val" => 4}
|
||||
@db.save(td1, true)
|
||||
@db.save(td2, true)
|
||||
@db.save_doc(td1, true)
|
||||
@db.save_doc(td2, true)
|
||||
lambda do
|
||||
@db.get(td1["_id"])
|
||||
end.should raise_error(RestClient::ResourceNotFound)
|
||||
|
@ -432,7 +432,7 @@ describe CouchRest::Database do
|
|||
@db.get(td2["_id"])
|
||||
end.should raise_error(RestClient::ResourceNotFound)
|
||||
td3 = {"_id" => "td3", "val" => "foo"}
|
||||
@db.save(td3, true)
|
||||
@db.save_doc(td3, true)
|
||||
@db.get(td1["_id"])["val"].should == td1["val"]
|
||||
@db.get(td2["_id"])["val"].should == td2["val"]
|
||||
@db.get(td3["_id"])["val"].should == td3["val"]
|
||||
|
@ -442,11 +442,11 @@ describe CouchRest::Database do
|
|||
td1 = {"_id" => "blah", "val" => true}
|
||||
td2 = {"_id" => "steve", "val" => 3}
|
||||
@db.bulk_save_cache_limit = 50
|
||||
@db.save(td1, true)
|
||||
@db.save_doc(td1, true)
|
||||
lambda do
|
||||
@db.get(td1["_id"])
|
||||
end.should raise_error(RestClient::ResourceNotFound)
|
||||
@db.save(td2)
|
||||
@db.save_doc(td2)
|
||||
@db.get(td1["_id"])["val"].should == td1["val"]
|
||||
@db.get(td2["_id"])["val"].should == td2["val"]
|
||||
end
|
||||
|
@ -454,27 +454,27 @@ describe CouchRest::Database do
|
|||
|
||||
describe "DELETE existing document" do
|
||||
before(:each) do
|
||||
@r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
|
||||
@r = @db.save_doc({'lemons' => 'from texas', 'and' => 'spain'})
|
||||
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
|
||||
@db.save({'_id' => @docid, 'will-exist' => 'here'})
|
||||
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
||||
end
|
||||
it "should work" do
|
||||
doc = @db.get(@r['id'])
|
||||
doc['and'].should == 'spain'
|
||||
@db.delete doc
|
||||
@db.delete_doc doc
|
||||
lambda{@db.get @r['id']}.should raise_error
|
||||
end
|
||||
it "should work with uri id" do
|
||||
doc = @db.get(@docid)
|
||||
@db.delete doc
|
||||
@db.delete_doc doc
|
||||
lambda{@db.get @docid}.should raise_error
|
||||
end
|
||||
it "should fail without an _id" do
|
||||
lambda{@db.delete({"not"=>"a real doc"})}.should raise_error(ArgumentError)
|
||||
lambda{@db.delete_doc({"not"=>"a real doc"})}.should raise_error(ArgumentError)
|
||||
end
|
||||
it "should defer actual deletion when using bulk save" do
|
||||
doc = @db.get(@docid)
|
||||
@db.delete doc, true
|
||||
@db.delete_doc doc, true
|
||||
lambda{@db.get @docid}.should_not raise_error
|
||||
@db.bulk_save
|
||||
lambda{@db.get @docid}.should raise_error
|
||||
|
@ -484,13 +484,13 @@ describe CouchRest::Database do
|
|||
|
||||
describe "COPY existing document" do
|
||||
before :each do
|
||||
@r = @db.save({'artist' => 'Zappa', 'title' => 'Muffin Man'})
|
||||
@r = @db.save_doc({'artist' => 'Zappa', 'title' => 'Muffin Man'})
|
||||
@docid = 'tracks/zappa/muffin-man'
|
||||
@doc = @db.get(@r['id'])
|
||||
end
|
||||
describe "to a new location" do
|
||||
it "should work" do
|
||||
@db.copy @doc, @docid
|
||||
@db.copy_doc @doc, @docid
|
||||
newdoc = @db.get(@docid)
|
||||
newdoc['artist'].should == 'Zappa'
|
||||
end
|
||||
|
@ -500,20 +500,20 @@ describe CouchRest::Database do
|
|||
end
|
||||
describe "to an existing location" do
|
||||
before :each do
|
||||
@db.save({'_id' => @docid, 'will-exist' => 'here'})
|
||||
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
||||
end
|
||||
it "should fail without a rev" do
|
||||
lambda{@db.copy @doc, @docid}.should raise_error(RestClient::RequestFailed)
|
||||
lambda{@db.copy_doc @doc, @docid}.should raise_error(RestClient::RequestFailed)
|
||||
end
|
||||
it "should succeed with a rev" do
|
||||
@to_be_overwritten = @db.get(@docid)
|
||||
@db.copy @doc, "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
|
||||
@db.copy_doc @doc, "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
|
||||
newdoc = @db.get(@docid)
|
||||
newdoc['artist'].should == 'Zappa'
|
||||
end
|
||||
it "should succeed given the doc to overwrite" do
|
||||
@to_be_overwritten = @db.get(@docid)
|
||||
@db.copy @doc, @to_be_overwritten
|
||||
@db.copy_doc @doc, @to_be_overwritten
|
||||
newdoc = @db.get(@docid)
|
||||
newdoc['artist'].should == 'Zappa'
|
||||
end
|
||||
|
@ -522,13 +522,13 @@ describe CouchRest::Database do
|
|||
|
||||
describe "MOVE existing document" do
|
||||
before :each do
|
||||
@r = @db.save({'artist' => 'Zappa', 'title' => 'Muffin Man'})
|
||||
@r = @db.save_doc({'artist' => 'Zappa', 'title' => 'Muffin Man'})
|
||||
@docid = 'tracks/zappa/muffin-man'
|
||||
@doc = @db.get(@r['id'])
|
||||
end
|
||||
describe "to a new location" do
|
||||
it "should work" do
|
||||
@db.move @doc, @docid
|
||||
@db.move_doc @doc, @docid
|
||||
newdoc = @db.get(@docid)
|
||||
newdoc['artist'].should == 'Zappa'
|
||||
lambda {@db.get(@r['id'])}.should raise_error(RestClient::ResourceNotFound)
|
||||
|
@ -540,22 +540,22 @@ describe CouchRest::Database do
|
|||
end
|
||||
describe "to an existing location" do
|
||||
before :each do
|
||||
@db.save({'_id' => @docid, 'will-exist' => 'here'})
|
||||
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
||||
end
|
||||
it "should fail without a rev" do
|
||||
lambda{@db.move @doc, @docid}.should raise_error(RestClient::RequestFailed)
|
||||
lambda{@db.move_doc @doc, @docid}.should raise_error(RestClient::RequestFailed)
|
||||
lambda{@db.get(@r['id'])}.should_not raise_error
|
||||
end
|
||||
it "should succeed with a rev" do
|
||||
@to_be_overwritten = @db.get(@docid)
|
||||
@db.move @doc, "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
|
||||
@db.move_doc @doc, "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
|
||||
newdoc = @db.get(@docid)
|
||||
newdoc['artist'].should == 'Zappa'
|
||||
lambda {@db.get(@r['id'])}.should raise_error(RestClient::ResourceNotFound)
|
||||
end
|
||||
it "should succeed given the doc to overwrite" do
|
||||
@to_be_overwritten = @db.get(@docid)
|
||||
@db.move @doc, @to_be_overwritten
|
||||
@db.move_doc @doc, @to_be_overwritten
|
||||
newdoc = @db.get(@docid)
|
||||
newdoc['artist'].should == 'Zappa'
|
||||
lambda {@db.get(@r['id'])}.should raise_error(RestClient::ResourceNotFound)
|
||||
|
@ -566,7 +566,7 @@ describe CouchRest::Database do
|
|||
|
||||
it "should list documents" do
|
||||
5.times do
|
||||
@db.save({'another' => 'doc', 'will-exist' => 'anywhere'})
|
||||
@db.save_doc({'another' => 'doc', 'will-exist' => 'anywhere'})
|
||||
end
|
||||
ds = @db.documents
|
||||
ds['rows'].should be_an_instance_of(Array)
|
||||
|
@ -577,7 +577,7 @@ describe CouchRest::Database do
|
|||
describe "documents / _all_docs" do
|
||||
before(:each) do
|
||||
9.times do |i|
|
||||
@db.save({'_id' => "doc#{i}",'another' => 'doc', 'will-exist' => 'here'})
|
||||
@db.save_doc({'_id' => "doc#{i}",'another' => 'doc', 'will-exist' => 'here'})
|
||||
end
|
||||
end
|
||||
it "should list documents with keys and such" do
|
||||
|
@ -625,5 +625,40 @@ describe CouchRest::Database do
|
|||
end
|
||||
end
|
||||
|
||||
describe "creating a database" do
|
||||
before(:each) do
|
||||
@db = @cr.database('couchrest-test-db_to_create')
|
||||
@db.delete!
|
||||
end
|
||||
|
||||
it "should just work fine" do
|
||||
@cr.databases.should_not include('couchrest-test-db_to_create')
|
||||
@db.create!
|
||||
@cr.databases.should include('couchrest-test-db_to_create')
|
||||
end
|
||||
end
|
||||
|
||||
describe "recreating a database" do
|
||||
before(:each) do
|
||||
@db = @cr.database('couchrest-test-db_to_create')
|
||||
@db2 = @cr.database('couchrest-test-db_to_recreate')
|
||||
@cr.databases.include?(@db.name) ? nil : @db.create!
|
||||
@cr.databases.include?(@db2.name) ? @db2.delete! : nil
|
||||
end
|
||||
|
||||
it "should drop and recreate a database" do
|
||||
@cr.databases.should include(@db.name)
|
||||
@db.recreate!
|
||||
@cr.databases.should include(@db.name)
|
||||
end
|
||||
|
||||
it "should recreate a db even tho it doesn't exist" do
|
||||
@cr.databases.should_not include(@db2.name)
|
||||
@db2.recreate!
|
||||
@cr.databases.should include(@db2.name)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
|
@ -62,7 +62,7 @@ describe CouchRest::Design do
|
|||
describe "from a saved document" do
|
||||
before(:each) do
|
||||
@db = reset_test_db!
|
||||
@db.save({
|
||||
@db.save_doc({
|
||||
"_id" => "_design/test",
|
||||
"views" => {
|
||||
"by_name" => {
|
||||
|
|
|
@ -42,7 +42,7 @@ describe CouchRest::Document, "saving using a database" do
|
|||
before(:all) do
|
||||
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
|
||||
@db = reset_test_db!
|
||||
@resp = @db.save(@doc)
|
||||
@resp = @db.save_doc(@doc)
|
||||
end
|
||||
it "should apply the database" do
|
||||
@doc.database.should == @db
|
||||
|
@ -71,7 +71,7 @@ end
|
|||
describe "getting from a database" do
|
||||
before(:all) do
|
||||
@db = reset_test_db!
|
||||
@resp = @db.save({
|
||||
@resp = @db.save_doc({
|
||||
"key" => "value"
|
||||
})
|
||||
@doc = @db.get @resp['id']
|
||||
|
@ -95,7 +95,7 @@ end
|
|||
describe "destroying a document from a db" do
|
||||
before(:all) do
|
||||
@db = reset_test_db!
|
||||
@resp = @db.save({
|
||||
@resp = @db.save_doc({
|
||||
"key" => "value"
|
||||
})
|
||||
@doc = @db.get @resp['id']
|
||||
|
@ -114,7 +114,7 @@ end
|
|||
describe "destroying a document from a db using bulk save" do
|
||||
before(:all) do
|
||||
@db = reset_test_db!
|
||||
@resp = @db.save({
|
||||
@resp = @db.save_doc({
|
||||
"key" => "value"
|
||||
})
|
||||
@doc = @db.get @resp['id']
|
||||
|
@ -132,7 +132,7 @@ end
|
|||
describe "copying a document" do
|
||||
before :each do
|
||||
@db = reset_test_db!
|
||||
@resp = @db.save({'key' => 'value'})
|
||||
@resp = @db.save_doc({'key' => 'value'})
|
||||
@docid = 'new-location'
|
||||
@doc = @db.get(@resp['id'])
|
||||
end
|
||||
|
@ -148,7 +148,7 @@ describe "copying a document" do
|
|||
end
|
||||
describe "to an existing location" do
|
||||
before :each do
|
||||
@db.save({'_id' => @docid, 'will-exist' => 'here'})
|
||||
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
||||
end
|
||||
it "should fail without a rev" do
|
||||
lambda{@doc.copy @docid}.should raise_error(RestClient::RequestFailed)
|
||||
|
@ -171,7 +171,7 @@ end
|
|||
describe "MOVE existing document" do
|
||||
before :each do
|
||||
@db = reset_test_db!
|
||||
@resp = @db.save({'key' => 'value'})
|
||||
@resp = @db.save_doc({'key' => 'value'})
|
||||
@docid = 'new-location'
|
||||
@doc = @db.get(@resp['id'])
|
||||
end
|
||||
|
@ -189,7 +189,7 @@ describe "MOVE existing document" do
|
|||
end
|
||||
describe "to an existing location" do
|
||||
before :each do
|
||||
@db.save({'_id' => @docid, 'will-exist' => 'here'})
|
||||
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
||||
end
|
||||
it "should fail without a rev" do
|
||||
lambda{@doc.move @docid}.should raise_error(RestClient::RequestFailed)
|
||||
|
|
|
@ -275,7 +275,7 @@ describe CouchRest::Model do
|
|||
}
|
||||
]
|
||||
}
|
||||
r = Course.database.save course_doc
|
||||
r = Course.database.save_doc course_doc
|
||||
@course = Course.get r['id']
|
||||
end
|
||||
it "should load the course" do
|
||||
|
@ -336,7 +336,7 @@ describe CouchRest::Model do
|
|||
},
|
||||
"final_test_at" => "2008/12/19 13:00:00 +0800"
|
||||
}
|
||||
r = Course.database.save course_doc
|
||||
r = Course.database.save_doc course_doc
|
||||
@course = Course.get r['id']
|
||||
end
|
||||
it "should load the course" do
|
||||
|
@ -353,7 +353,7 @@ describe CouchRest::Model do
|
|||
describe "cast keys to any type" do
|
||||
before(:all) do
|
||||
event_doc = { :subject => "Some event", :occurs_at => Time.now }
|
||||
e = Event.database.save event_doc
|
||||
e = Event.database.save_doc event_doc
|
||||
|
||||
@event = Event.get e['id']
|
||||
end
|
||||
|
@ -408,7 +408,7 @@ describe CouchRest::Model do
|
|||
before(:each) do
|
||||
@art = Article.new
|
||||
@old = Article.database.get('this-is-the-title') rescue nil
|
||||
Article.database.delete(@old) if @old
|
||||
Article.database.delete_doc(@old) if @old
|
||||
end
|
||||
|
||||
it "should be a new document" do
|
||||
|
@ -538,7 +538,7 @@ describe CouchRest::Model do
|
|||
end
|
||||
|
||||
it "should not include non-Articles" do
|
||||
Article.database.save({"date" => 1})
|
||||
Article.database.save_doc({"date" => 1})
|
||||
view = Article.by_date :raw => true
|
||||
view['rows'].length.should == 4
|
||||
end
|
||||
|
@ -591,7 +591,7 @@ describe CouchRest::Model do
|
|||
|
||||
describe "a ducktype view" do
|
||||
before(:all) do
|
||||
@id = @db.save({:dept => true})['id']
|
||||
@id = @db.save_doc({:dept => true})['id']
|
||||
end
|
||||
it "should setup" do
|
||||
duck = Course.get(@id) # from a different db
|
||||
|
|
|
@ -60,7 +60,7 @@ describe CouchRest::Pager do
|
|||
@docs << ({:number => (i % 10)})
|
||||
end
|
||||
@db.bulk_save(@docs)
|
||||
@db.save({
|
||||
@db.save_doc({
|
||||
'_id' => '_design/magic',
|
||||
'views' => {
|
||||
'number' => {
|
||||
|
|
Loading…
Reference in a new issue