couch_potato 0.5.6 → 0.5.7

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## Changes
2
2
 
3
+ ### 0.5.7
4
+
5
+ * support CouchPotato::Database#first/#first! calls when using `stub_db` from tests (langalex)
6
+ * support RSpec2 block syntax in `stub_db` (langalex)
7
+
3
8
  ### 0.5.6
4
9
 
5
10
  * remove the stale parameter from a view query if it's nil, as couchdb only allows stale to be ok or update\_after (langalex)
data/README.md CHANGED
@@ -400,6 +400,7 @@ For stubbing out the database couch potato offers some helpers:
400
400
  db.stub_view(Comment, :by_commenter_id).with('23').and_return([:comment1, :comment2])
401
401
 
402
402
  CouchPotato.database.view(Comment.by_commenter_id('23)) # => [:comment1, :comment2]
403
+ CouchPotato.database.first(Comment.by_commenter_id('23)) # => :comment1
403
404
 
404
405
  ##### Testing map/reduce functions
405
406
 
@@ -27,6 +27,8 @@ module CouchPotato
27
27
  alias_method :id, :_id
28
28
  alias_method :id=, :_id=
29
29
  end
30
+
31
+ CouchPotato.models << base
30
32
  end
31
33
 
32
34
  # initialize a new instance of the model optionally passing it a hash of attributes.
@@ -8,8 +8,9 @@ module CouchPotato::RSpec
8
8
  @db = db
9
9
  end
10
10
 
11
- def with(*args)
11
+ def with(*args, &block)
12
12
  @args = args
13
+ and_return(block.call) if block
13
14
  self
14
15
  end
15
16
 
@@ -19,11 +20,21 @@ module CouchPotato::RSpec
19
20
  _stub.with(*@args) if @args
20
21
  _stub.and_return(view_stub)
21
22
  @db.stub(:view).with(view_stub).and_return(return_value)
23
+ if return_value.respond_to?(:first)
24
+ @db.stub(:first).with(view_stub).and_return(return_value.first)
25
+ if return_value.first
26
+ @db.stub(:first!).with(view_stub).and_return(return_value.first)
27
+ else
28
+ @db.stub(:first!).with(view_stub).and_raise(CouchPotato::NotFound)
29
+ end
30
+ end
22
31
  end
23
32
  end
24
33
 
25
- def stub_view(clazz, view)
26
- ViewStub.new clazz, view, self
34
+ def stub_view(clazz, view, &block)
35
+ stub = ViewStub.new clazz, view, self
36
+ stub.and_return(block.call) if block
37
+ stub
27
38
  end
28
39
  end
29
40
 
@@ -1,3 +1,3 @@
1
1
  module CouchPotato
2
- VERSION = "0.5.6"
2
+ VERSION = "0.5.7"
3
3
  end
data/lib/couch_potato.rb CHANGED
@@ -13,6 +13,12 @@ module CouchPotato
13
13
  Config.split_design_documents_per_view = false
14
14
 
15
15
  class NotFound < StandardError; end
16
+
17
+ # returns all the classes that implement the CouchPotato::Persistence module
18
+ def self.models
19
+ @models ||= []
20
+ @models
21
+ end
16
22
 
17
23
  # Returns a database instance which you can then use to create objects and query views. You have to set the CouchPotato::Config.database_name before this works.
18
24
  def self.database
@@ -19,6 +19,15 @@ describe CouchPotato, 'full_url_to_database' do
19
19
  end
20
20
  end
21
21
 
22
+ describe CouchPotato, '.models' do
23
+ it "returns all classes that have implemented CouchPotato::Persistence" do
24
+ clazz = Class.new
25
+ clazz.send(:include, CouchPotato::Persistence)
26
+
27
+ CouchPotato.models.should include(clazz)
28
+ end
29
+ end
30
+
22
31
  describe CouchPotato, 'validation_framework' do
23
32
  before(:each) do
24
33
  @original_validation_framework = CouchPotato::Config.validation_framework
@@ -32,4 +32,35 @@ describe "stubbing a view" do
32
32
  it "should stub the database to return fake results when called with the stub" do
33
33
  @db.view(WithStubbedView.stubbed_view('123')).should == [:result]
34
34
  end
35
+
36
+ it "stubs the database to return the first fake result" do
37
+ @db.first(WithStubbedView.stubbed_view('123')).should == :result
38
+ @db.first!(WithStubbedView.stubbed_view('123')).should == :result
39
+ end
40
+
41
+ it "raises an error if there is no first result" do
42
+ @db.stub_view(WithStubbedView, :stubbed_view).and_return([])
43
+ lambda {
44
+ @db.first!(WithStubbedView.stubbed_view('123'))
45
+ }.should raise_error(CouchPotato::NotFound)
46
+ end
47
+
48
+ it "skips stubbing the first view (i.e. doesn't crash) if the fake result does not respond to first" do
49
+ @db.stub_view(WithStubbedView, :stubbed_view).with('123').and_return(:results)
50
+
51
+ @db.view(WithStubbedView.stubbed_view('123')).should == :results
52
+ end
53
+
54
+ it "supports the block style return syntax with `with`" do
55
+ @db.stub_view(WithStubbedView, :stubbed_view).with('123') {:results}
56
+
57
+ @db.view(WithStubbedView.stubbed_view('123')).should == :results
58
+ end
59
+
60
+ it "supports the block style return syntax without `with`" do
61
+ @db.stub_view(WithStubbedView, :stubbed_view) {:results}
62
+
63
+ @db.view(WithStubbedView.stubbed_view('123')).should == :results
64
+ end
65
+
35
66
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: couch_potato
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.5.6
5
+ version: 0.5.7
6
6
  platform: ruby
7
7
  authors:
8
8
  - Alexander Lang
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-12 00:00:00 +02:00
13
+ date: 2011-08-10 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency