From e48a6c886692e76f72b2506674725d473fde696f Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Tue, 26 May 2009 18:27:49 -0700 Subject: [PATCH] fixed all the specs so we are back to green --- lib/couchrest.rb | 10 ++++------ lib/couchrest/core/database.rb | 10 ++++++++-- lib/couchrest/mixins/design_doc.rb | 10 +++++++--- lib/couchrest/monkeypatches.rb | 1 - lib/couchrest/more/casted_model.rb | 2 +- lib/couchrest/validation/auto_validate.rb | 2 -- spec/couchrest/more/extended_doc_spec.rb | 3 +++ spec/couchrest/more/extended_doc_view_spec.rb | 16 +++++++++++++--- 8 files changed, 36 insertions(+), 18 deletions(-) diff --git a/lib/couchrest.rb b/lib/couchrest.rb index 9db73dd..af0b83c 100644 --- a/lib/couchrest.rb +++ b/lib/couchrest.rb @@ -23,9 +23,7 @@ require 'rest_client' $:.unshift File.dirname(__FILE__) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) - -$COUCHREST_DEBUG ||= false - + require 'couchrest/monkeypatches' # = CouchDB, close to the metal @@ -145,7 +143,7 @@ module CouchRest begin JSON.parse(RestClient.put(uri, payload)) rescue Exception => e - if $COUCHREST_DEBUG == true + if $DEBUG raise "Error while sending a PUT request #{uri}\npayload: #{payload.inspect}\n#{e}" else raise e @@ -157,7 +155,7 @@ module CouchRest begin JSON.parse(RestClient.get(uri), :max_nesting => false) rescue => e - if $COUCHREST_DEBUG == true + if $DEBUG raise "Error while sending a GET request #{uri}\n: #{e}" else raise e @@ -170,7 +168,7 @@ module CouchRest begin JSON.parse(RestClient.post(uri, payload)) rescue Exception => e - if $COUCHREST_DEBUG == true + if $DEBUG raise "Error while sending a POST request #{uri}\npayload: #{payload.inspect}\n#{e}" else raise e diff --git a/lib/couchrest/core/database.rb b/lib/couchrest/core/database.rb index 8b81db7..ed97127 100644 --- a/lib/couchrest/core/database.rb +++ b/lib/couchrest/core/database.rb @@ -142,8 +142,14 @@ module CouchRest bulk_save end result = if doc['_id'] - slug = escape_docid(doc['_id']) - CouchRest.put "#{@uri}/#{slug}", doc + slug = escape_docid(doc['_id']) + begin + CouchRest.put "#{@uri}/#{slug}", doc + rescue RestClient::ResourceNotFound + p "resource not found when saving even tho an id was passed" + slug = doc['_id'] = @server.next_uuid + CouchRest.put "#{@uri}/#{slug}", doc + end else begin slug = doc['_id'] = @server.next_uuid diff --git a/lib/couchrest/mixins/design_doc.rb b/lib/couchrest/mixins/design_doc.rb index 6f1fb2a..4ed6fdf 100644 --- a/lib/couchrest/mixins/design_doc.rb +++ b/lib/couchrest/mixins/design_doc.rb @@ -67,9 +67,13 @@ module CouchRest private def reset_design_doc - design_doc['_id'] = design_doc_id - design_doc.delete('_rev') - #design_doc.database = nil + current = self.database.get(design_doc_id) rescue nil + design_doc['_id'] = design_doc_id + if current.nil? + design_doc.delete('_rev') + else + design_doc['_rev'] = current['_rev'] + end self.design_doc_fresh = true end diff --git a/lib/couchrest/monkeypatches.rb b/lib/couchrest/monkeypatches.rb index c7678f6..2fad1f3 100644 --- a/lib/couchrest/monkeypatches.rb +++ b/lib/couchrest/monkeypatches.rb @@ -92,7 +92,6 @@ module RestClient # establish_connection(uri) # http = Thread.current[:connection] # require 'ruby-debug' -# debugger # req.body_stream = nil # # res = http.request(req, payload) diff --git a/lib/couchrest/more/casted_model.rb b/lib/couchrest/more/casted_model.rb index 16a9b11..6f442c8 100644 --- a/lib/couchrest/more/casted_model.rb +++ b/lib/couchrest/more/casted_model.rb @@ -10,11 +10,11 @@ module CouchRest def initialize(keys={}) raise StandardError unless self.is_a? Hash + apply_defaults # defined in CouchRest::Mixins::Properties super() keys.each do |k,v| self[k.to_s] = v end if keys - apply_defaults # defined in CouchRest::Mixins::Properties cast_keys # defined in CouchRest::Mixins::Properties end diff --git a/lib/couchrest/validation/auto_validate.rb b/lib/couchrest/validation/auto_validate.rb index 10fce9d..4b1b925 100644 --- a/lib/couchrest/validation/auto_validate.rb +++ b/lib/couchrest/validation/auto_validate.rb @@ -16,8 +16,6 @@ module CouchRest # # This feature is still not fully ported over, # # test are lacking, so please use with caution # def auto_validate! - # require 'ruby-debug' - # debugger # auto_validation = true # end diff --git a/spec/couchrest/more/extended_doc_spec.rb b/spec/couchrest/more/extended_doc_spec.rb index ad4c11f..6fa886f 100644 --- a/spec/couchrest/more/extended_doc_spec.rb +++ b/spec/couchrest/more/extended_doc_spec.rb @@ -224,6 +224,7 @@ describe "ExtendedDocument" do describe "finding all instances of a model" do before(:all) do + WithTemplateAndUniqueID.design_doc_fresh = false WithTemplateAndUniqueID.all.map{|o| o.destroy(true)} WithTemplateAndUniqueID.database.bulk_delete WithTemplateAndUniqueID.new('important-field' => '1').save @@ -245,6 +246,7 @@ describe "ExtendedDocument" do describe "counting all instances of a model" do before(:each) do @db = reset_test_db! + WithTemplateAndUniqueID.design_doc_fresh = false end it ".count should return 0 if there are no docuemtns" do @@ -263,6 +265,7 @@ describe "ExtendedDocument" do describe "finding the first instance of a model" do before(:each) do @db = reset_test_db! + WithTemplateAndUniqueID.design_doc_fresh = false WithTemplateAndUniqueID.new('important-field' => '1').save WithTemplateAndUniqueID.new('important-field' => '2').save WithTemplateAndUniqueID.new('important-field' => '3').save diff --git a/spec/couchrest/more/extended_doc_view_spec.rb b/spec/couchrest/more/extended_doc_view_spec.rb index 39f2158..5b60aeb 100644 --- a/spec/couchrest/more/extended_doc_view_spec.rb +++ b/spec/couchrest/more/extended_doc_view_spec.rb @@ -66,6 +66,7 @@ describe "ExtendedDocument views" do end end it "should make the design doc upon first query" do + Course.design_doc_fresh = false Course.by_title doc = Course.design_doc doc['views']['all']['map'].should include('Course') @@ -73,11 +74,13 @@ describe "ExtendedDocument views" do it "should can query via view" do # register methods with method-missing, for local dispatch. method # missing lookup table, no heuristics. + Course.design_doc_fresh = false view = Course.view :by_title designed = Course.by_title view.should == designed end it "should get them" do + Course.design_doc_fresh = false rs = Course.by_title rs.length.should == 4 end @@ -100,6 +103,7 @@ describe "ExtendedDocument views" do describe "a ducktype view" do before(:all) do + reset_test_db! @id = TEST_SERVER.default_database.save_doc({:dept => true})['id'] end it "should setup" do @@ -107,11 +111,13 @@ describe "ExtendedDocument views" do duck["dept"].should == true end it "should make the design doc" do + Course.design_doc_fresh = false @as = Course.by_dept @doc = Course.design_doc @doc["views"]["by_dept"]["map"].should_not include("couchrest") end - it "should not look for class" do |variable| + it "should not look for class" do + Course.design_doc_fresh = false @as = Course.by_dept @as[0]['_id'].should == @id end @@ -207,10 +213,12 @@ describe "ExtendedDocument views" do end end it "should query all" do + Unattached.design_doc_fresh = false rs = @us.all rs.length.should == 4 end it "should make the design doc upon first query" do + Unattached.design_doc_fresh = false @us.by_title doc = @us.design_doc doc['views']['all']['map'].should include('Unattached') @@ -249,7 +257,6 @@ describe "ExtendedDocument views" do it "should clean up design docs left around on specific database" do @us.by_title original_id = @us.model_design_doc['_rev'] - debugger Unattached.view_by :professor @us.by_professor @us.model_design_doc['_rev'].should_not == original_id @@ -302,16 +309,19 @@ describe "ExtendedDocument views" do end end it "should be available raw" do + Article.design_doc_fresh = false view = Article.by_tags :raw => true view['rows'].length.should == 5 end it "should be default to :reduce => false" do + Article.design_doc_fresh = false ars = Article.by_tags ars.first.tags.first.should == 'cool' end it "should be raw when reduce is true" do + Article.design_doc_fresh = false view = Article.by_tags :reduce => true, :group => true view['rows'].find{|r|r['key'] == 'cool'}['value'].should == 3 end @@ -321,7 +331,7 @@ describe "ExtendedDocument views" do describe "adding a view" do before(:each) do reset_test_db! - Article.database.recreate! + Article.design_doc_fresh = false Article.by_date @original_doc_rev = Article.model_design_doc['_rev'] @design_docs = Article.database.documents :startkey => "_design/", :endkey => "_design/\u9999"