Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adds removing deleted documents from view results when include_docs=true

  • Loading branch information...
commit 74445d04aa95723d113afbb96577b10f7238b424 1 parent df02e1a
@langalex authored
View
2  Gemfile.lock
@@ -48,7 +48,7 @@ PLATFORMS
DEPENDENCIES
couch_potato!
rake
- rspec (>= 2.0)
+ rspec (~> 2.11.0)
therubyracer
timecop
tzinfo
View
22 lib/couch_potato/view/custom_view_spec.rb
@@ -8,35 +8,35 @@ class CustomViewSpec < BaseViewSpec
def map_function
options[:map]
end
-
+
def reduce_function
options[:reduce]
end
-
+
def view_parameters
{:include_docs => options[:include_docs] || false}.merge(super)
end
-
+
def process_results(results)
if count?
results['rows'].first.try(:[], 'value') || 0
- else
+ else
results['rows'].map do |row|
if row['doc'].instance_of?(klass)
row['doc']
else
- klass.json_create row['doc'] || row['value'].merge(:_id => row['id'] || row['key'])
+ result = row['doc'] || (row['value'].merge(:_id => row['id'] || row['key']) unless view_parameters[:include_docs])
+ klass.json_create result if result
end
- end
+ end.compact
end
end
-
- private
-
+
+ private
+
def count?
view_parameters[:reduce]
end
-
end
end
-end
+end
View
4 lib/couch_potato/view/model_view_spec.rb
@@ -140,7 +140,9 @@ def process_results(results)
if count?
results['rows'].first.try(:[], 'value') || 0
else
- results['rows'].map { |row| row['doc'] || row['id'] }
+ results['rows'].map {|row|
+ row['doc'] || (row['id'] unless view_parameters[:include_docs])
+ }.compact
end
end
View
24 spec/unit/custom_view_spec_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe CouchPotato::View::CustomViewSpec, '#process_results' do
+ it 'returns the documents' do
+ spec = CouchPotato::View::CustomViewSpec.new(Child, 'all', {}, {})
+ processed_results = spec.process_results('rows' => [{'doc' => {JSON.create_id => 'Child'}}])
+
+ expect(processed_results.map{|row| row.class}).to eql([Child])
+ end
+
+ it 'returns values where there are no documents' do
+ spec = CouchPotato::View::CustomViewSpec.new(Child, 'all', {}, {:include_docs => false})
+ processed_results = spec.process_results('rows' => [{'value' => {JSON.create_id => 'Child'}}])
+
+ expect(processed_results.map{|row| row.class}).to eql([Child])
+ end
+
+ it 'filters out rows without documents when include_docs=true (i.e. doc has been deleted)' do
+ spec = CouchPotato::View::CustomViewSpec.new(Child, 'all', {}, {:include_docs => true})
+ processed_results = spec.process_results('rows' => [{'value' => {JSON.create_id => 'Child'}}])
+
+ expect(processed_results).to be_empty
+ end
+end
View
25 spec/unit/model_view_spec_spec.rb
@@ -1,6 +1,29 @@
require 'spec_helper'
-describe CouchPotato::View::ModelViewSpec, 'map_function' do
+describe CouchPotato::View::ModelViewSpec, '#process_results' do
+ it 'returns the documents' do
+ spec = CouchPotato::View::ModelViewSpec.new(Object, 'all', {}, {})
+ processed_results = spec.process_results('rows' => [{'doc' => 'doc-1'}])
+
+ expect(processed_results).to eql(['doc-1'])
+ end
+
+ it 'returns ids where there are no documents' do
+ spec = CouchPotato::View::ModelViewSpec.new(Object, 'all', {}, {:include_docs => false})
+ processed_results = spec.process_results('rows' => [{'id' => 'doc-1'}])
+
+ expect(processed_results).to eql(['doc-1'])
+ end
+
+ it 'filters out rows without documents when include_docs=true (i.e. doc has been deleted)' do
+ spec = CouchPotato::View::ModelViewSpec.new(Object, 'all', {}, {:include_docs => true})
+ processed_results = spec.process_results('rows' => [{'id' => 'doc-1'}])
+
+ expect(processed_results).to be_empty
+ end
+end
+
+describe CouchPotato::View::ModelViewSpec, '#map_function' do
it "should include conditions" do
spec = CouchPotato::View::ModelViewSpec.new Object, 'all', {:conditions => 'doc.closed = true'}, {}
spec.map_function.should include('if(doc.ruby_class && doc.ruby_class == \'Object\' && (doc.closed = true))')
View
11 spec/views_spec.rb
@@ -278,4 +278,15 @@ class Coworker
@db.view(Coworker.all(:list => :append_doe)).first.name.should == 'joe doe'
end
end
+
+ describe 'with stale views' do
+ it 'does not return deleted documents' do
+ build = Build.new
+ @db.save_document! build
+ @db.view(Build.timeline)
+ @db.destroy build
+
+ expect(@db.view(Build.timeline(:stale => 'ok'))).to be_empty
+ end
+ end
end

0 comments on commit 74445d0

Please sign in to comment.
Something went wrong with that request. Please try again.