gom-couchdb-adapter 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,7 +38,7 @@ class GOM::Storage::CouchDB::Adapter < GOM::Storage::Adapter
38
38
  def collection(name, options = { })
39
39
  view = @design.views[name.to_s]
40
40
  raise ViewNotFoundError, "there are no view with the name #{name}" unless view
41
- fetcher = GOM::Storage::CouchDB::Collection::Fetcher.new view, options
41
+ fetcher = GOM::Storage::CouchDB::Collection::Fetcher.new view, revisions, options
42
42
  GOM::Object::Collection.new fetcher, configuration.name
43
43
  end
44
44
 
@@ -46,6 +46,10 @@ class GOM::Storage::CouchDB::Adapter < GOM::Storage::Adapter
46
46
  @revisions ||= { }
47
47
  end
48
48
 
49
+ def clear_revisions!
50
+ @revisions = nil
51
+ end
52
+
49
53
  private
50
54
 
51
55
  def initialize_server
@@ -2,8 +2,8 @@
2
2
  # Fetches a result-set of a CouchDB view and provides it to a GOM collection.
3
3
  class GOM::Storage::CouchDB::Collection::Fetcher
4
4
 
5
- def initialize(view, options)
6
- @view, @options = view, options
5
+ def initialize(view, revisions, options)
6
+ @view, @revisions, @options = view, revisions, options
7
7
  end
8
8
 
9
9
  def drafts
@@ -26,8 +26,13 @@ class GOM::Storage::CouchDB::Collection::Fetcher
26
26
 
27
27
  def fetch_drafts
28
28
  @drafts = @collection.documents.map do |document|
29
+ set_revision document
29
30
  GOM::Storage::CouchDB::Draft::Builder.new(document).draft
30
31
  end
31
32
  end
32
33
 
34
+ def set_revision(document)
35
+ @revisions[document.id] = document.rev
36
+ end
37
+
33
38
  end
@@ -0,0 +1,64 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "lib", "gom", "couchdb-adapter"))
3
+
4
+ describe "couchdb adapter map collection" do
5
+
6
+ before :all do
7
+ GOM::Storage.setup
8
+ end
9
+
10
+ before :each do
11
+ @object_one = GOM::Spec::Object.new
12
+ @object_one.number = 11
13
+ GOM::Storage.store @object_one, :test_storage
14
+
15
+ @object_two = GOM::Spec::Object.new
16
+ @object_two.number = 18
17
+ GOM::Storage.store @object_two, :test_storage
18
+ end
19
+
20
+ after :each do
21
+ GOM::Storage.remove @object_one
22
+ GOM::Storage.remove @object_two
23
+ end
24
+
25
+ describe "first" do
26
+
27
+ before :each do
28
+ @collection = GOM::Storage.collection :test_storage, :test_map_view
29
+ end
30
+
31
+ it "should return an object" do
32
+ object = @collection.first
33
+ object.should be_instance_of(GOM::Spec::Object)
34
+ end
35
+
36
+ it "should return a row with the correct result" do
37
+ object = @collection.first
38
+ object.number.should == 11
39
+ end
40
+
41
+ end
42
+
43
+ describe "updating a collection object" do
44
+
45
+ before :each do
46
+ # clear all revisions and re-fetch object two to restore that revision
47
+ GOM::Storage::Configuration[:test_storage].adapter.clear_revisions!
48
+ GOM::Storage.fetch GOM::Object.id(@object_two)
49
+
50
+ @collection = GOM::Storage.collection :test_storage, :test_map_view
51
+
52
+ @object = @collection.first
53
+ @object.number = 21
54
+ GOM::Storage.store @object
55
+ end
56
+
57
+ it "should have the new values if fetched again" do
58
+ object = GOM::Storage.fetch GOM::Object.id(@object)
59
+ object.number.should == 21
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -4,14 +4,14 @@ describe GOM::Storage::CouchDB::Adapter do
4
4
 
5
5
  before :each do
6
6
  @server = mock CouchDB::Server
7
- CouchDB::Server.stub(:new).and_return(@server)
7
+ CouchDB::Server.stub :new => @server
8
8
 
9
9
  @database = mock CouchDB::Database, :delete_if_exists! => nil, :create_if_missing! => nil
10
- CouchDB::Database.stub(:new).and_return(@database)
10
+ CouchDB::Database.stub :new => @database
11
11
 
12
12
  @configuration = mock GOM::Storage::Configuration, :name => "test_storage", :views => :test_views
13
13
  @configuration.stub(:[]).with(:database).and_return("test")
14
- @configuration.stub(:values_at) do |*arguments|
14
+ @configuration.stub :values_at do |*arguments|
15
15
  result = nil
16
16
  result = [ "host", 1234 ] if arguments == [ :host, :port ]
17
17
  result = [ true, true ] if arguments == [ :delete_database_if_exists, :create_database_if_missing ]
@@ -20,7 +20,7 @@ describe GOM::Storage::CouchDB::Adapter do
20
20
 
21
21
  @design = mock CouchDB::Design
22
22
  @pusher = mock GOM::Storage::CouchDB::View::Pusher, :perform => nil, :design => @design
23
- GOM::Storage::CouchDB::View::Pusher.stub(:new).and_return(@pusher)
23
+ GOM::Storage::CouchDB::View::Pusher.stub :new => @pusher
24
24
 
25
25
  @adapter = described_class.new @configuration
26
26
  end
@@ -29,7 +29,7 @@ describe GOM::Storage::CouchDB::Adapter do
29
29
  GOM::Storage::Adapter[:couchdb].should == GOM::Storage::CouchDB::Adapter
30
30
  end
31
31
 
32
- describe "setup" do
32
+ describe "#setup" do
33
33
 
34
34
  it "should initialize a server" do
35
35
  CouchDB::Server.should_receive(:new).with("host", 1234).and_return(@server)
@@ -63,7 +63,7 @@ describe GOM::Storage::CouchDB::Adapter do
63
63
 
64
64
  end
65
65
 
66
- describe "teardown" do
66
+ describe "#teardown" do
67
67
 
68
68
  before :each do
69
69
  @adapter.setup
@@ -83,7 +83,7 @@ describe GOM::Storage::CouchDB::Adapter do
83
83
 
84
84
  end
85
85
 
86
- describe "fetch" do
86
+ describe "#fetch" do
87
87
 
88
88
  before :each do
89
89
  @adapter.setup
@@ -93,7 +93,7 @@ describe GOM::Storage::CouchDB::Adapter do
93
93
  @draft = mock GOM::Object::Draft
94
94
 
95
95
  @fetcher = mock GOM::Storage::CouchDB::Fetcher, :draft => @draft
96
- GOM::Storage::CouchDB::Fetcher.stub(:new).and_return(@fetcher)
96
+ GOM::Storage::CouchDB::Fetcher.stub :new => @fetcher
97
97
  end
98
98
 
99
99
  it "should initialize the fetcher" do
@@ -107,7 +107,7 @@ describe GOM::Storage::CouchDB::Adapter do
107
107
 
108
108
  end
109
109
 
110
- describe "store" do
110
+ describe "#store" do
111
111
 
112
112
  before :each do
113
113
  @adapter.setup
@@ -117,7 +117,7 @@ describe GOM::Storage::CouchDB::Adapter do
117
117
  @object_id = "object_1"
118
118
 
119
119
  @saver = mock GOM::Storage::CouchDB::Saver, :perform => nil, :object_id => @object_id
120
- GOM::Storage::CouchDB::Saver.stub(:new).and_return(@saver)
120
+ GOM::Storage::CouchDB::Saver.stub :new => @saver
121
121
  end
122
122
 
123
123
  it "should initialize the saver" do
@@ -136,7 +136,7 @@ describe GOM::Storage::CouchDB::Adapter do
136
136
 
137
137
  end
138
138
 
139
- describe "remove" do
139
+ describe "#remove" do
140
140
 
141
141
  before :each do
142
142
  @adapter.setup
@@ -145,7 +145,7 @@ describe GOM::Storage::CouchDB::Adapter do
145
145
  @revisions = @adapter.send :revisions
146
146
 
147
147
  @remover = mock GOM::Storage::CouchDB::Remover, :perform => nil
148
- GOM::Storage::CouchDB::Remover.stub(:new).and_return(@remover)
148
+ GOM::Storage::CouchDB::Remover.stub :new => @remover
149
149
  end
150
150
 
151
151
  it "should initialize the remover" do
@@ -160,13 +160,13 @@ describe GOM::Storage::CouchDB::Adapter do
160
160
 
161
161
  end
162
162
 
163
- describe "count" do
163
+ describe "#count" do
164
164
 
165
165
  before :each do
166
166
  @adapter.setup
167
167
 
168
168
  @counter = mock GOM::Storage::CouchDB::Counter, :perform => 1
169
- GOM::Storage::CouchDB::Counter.stub(:new).and_return(@counter)
169
+ GOM::Storage::CouchDB::Counter.stub :new => @counter
170
170
  end
171
171
 
172
172
  it "should initialize the counter" do
@@ -186,22 +186,22 @@ describe GOM::Storage::CouchDB::Adapter do
186
186
 
187
187
  end
188
188
 
189
- describe "collection" do
189
+ describe "#collection" do
190
190
 
191
191
  before :each do
192
192
  @adapter.setup
193
193
 
194
194
  @view = mock CouchDB::Design::View
195
195
  @views = mock Hash, :[] => @view
196
- @design.stub(:views).and_return(@views)
196
+ @design.stub :views => @views
197
197
 
198
198
  @options = mock Hash
199
199
 
200
200
  @fetcher = mock GOM::Storage::CouchDB::Collection::Fetcher
201
- GOM::Storage::CouchDB::Collection::Fetcher.stub(:new).and_return(@fetcher)
201
+ GOM::Storage::CouchDB::Collection::Fetcher.stub :new => @fetcher
202
202
 
203
203
  @collection = mock GOM::Object::Collection
204
- GOM::Object::Collection.stub(:new).and_return(@collection)
204
+ GOM::Object::Collection.stub :new => @collection
205
205
  end
206
206
 
207
207
  it "should select the right view" do
@@ -210,14 +210,14 @@ describe GOM::Storage::CouchDB::Adapter do
210
210
  end
211
211
 
212
212
  it "should raise #{described_class::ViewNotFoundError} if the view name is invalid" do
213
- @views.stub(:[]).and_return(nil)
213
+ @views.stub :[] => nil
214
214
  lambda do
215
215
  @adapter.collection :test_view, @options
216
216
  end.should raise_error(described_class::ViewNotFoundError)
217
217
  end
218
218
 
219
219
  it "should initialize a collection fetcher" do
220
- GOM::Storage::CouchDB::Collection::Fetcher.should_receive(:new).with(@view, @options).and_return(@fetcher)
220
+ GOM::Storage::CouchDB::Collection::Fetcher.should_receive(:new).with(@view, @adapter.revisions, @options).and_return(@fetcher)
221
221
  @adapter.collection :test_view, @options
222
222
  end
223
223
 
@@ -233,4 +233,17 @@ describe GOM::Storage::CouchDB::Adapter do
233
233
 
234
234
  end
235
235
 
236
+ describe "#clear_revisions" do
237
+
238
+ before :each do
239
+ @adapter.revisions[:test] = "value"
240
+ end
241
+
242
+ it "should clear the revisions hash" do
243
+ @adapter.clear_revisions!
244
+ @adapter.revisions.should be_empty
245
+ end
246
+
247
+ end
248
+
236
249
  end
@@ -3,21 +3,23 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..
3
3
  describe GOM::Storage::CouchDB::Collection::Fetcher do
4
4
 
5
5
  before :each do
6
- @document = mock CouchDB::Document
6
+ @document = mock CouchDB::Document, :id => "document_id", :rev => "document_rev"
7
7
  @documents = [ @document ]
8
8
  @collection = mock CouchDB::Collection, :documents => @documents
9
9
  @view = mock CouchDB::Design::View, :reduce => nil, :collection => @collection
10
10
  @options = mock Hash
11
11
 
12
+ @revisions = { }
13
+
12
14
  @draft = mock GOM::Object::Draft
13
15
 
14
16
  @builder = mock GOM::Storage::CouchDB::Draft::Builder, :draft => @draft
15
- GOM::Storage::CouchDB::Draft::Builder.stub(:new).and_return(@builder)
17
+ GOM::Storage::CouchDB::Draft::Builder.stub :new => @builder
16
18
 
17
- @fetcher = described_class.new @view, @options
19
+ @fetcher = described_class.new @view, @revisions, @options
18
20
  end
19
21
 
20
- describe "drafts" do
22
+ describe "#drafts" do
21
23
 
22
24
  context "with a view that don't has a reduce function" do
23
25
 
@@ -31,6 +33,11 @@ describe GOM::Storage::CouchDB::Collection::Fetcher do
31
33
  @fetcher.drafts
32
34
  end
33
35
 
36
+ it "should set the revisions" do
37
+ @fetcher.drafts
38
+ @revisions["document_id"].should == "document_rev"
39
+ end
40
+
34
41
  it "should return an array of drafts" do
35
42
  drafts = @fetcher.drafts
36
43
  drafts.should == [ @draft ]
@@ -41,7 +48,7 @@ describe GOM::Storage::CouchDB::Collection::Fetcher do
41
48
  context "with a view that has a reduce function" do
42
49
 
43
50
  before :each do
44
- @view.stub(:reduce).and_return(:test_reduce_function)
51
+ @view.stub :reduce => :test_reduce_function
45
52
  end
46
53
 
47
54
  it "should return nil" do
@@ -53,7 +60,7 @@ describe GOM::Storage::CouchDB::Collection::Fetcher do
53
60
 
54
61
  end
55
62
 
56
- describe "rows" do
63
+ describe "#rows" do
57
64
 
58
65
  it "should return the original couchdb collection" do
59
66
  rows = @fetcher.rows
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: gom-couchdb-adapter
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.0
5
+ version: 0.4.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Philipp Br\xC3\xBCll"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-10 00:00:00 +02:00
13
+ date: 2011-05-22 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  requirements:
33
33
  - - ">="
34
34
  - !ruby/object:Gem::Version
35
- version: 0.1.2
35
+ version: 0.1.3
36
36
  type: :runtime
37
37
  version_requirements: *id002
38
38
  - !ruby/object:Gem::Dependency
@@ -94,6 +94,7 @@ files:
94
94
  - spec/lib/gom/storage/couchdb/saver_spec.rb
95
95
  - spec/lib/gom/storage/couchdb/adapter_spec.rb
96
96
  - spec/spec_helper.rb
97
+ - spec/acceptance/map_spec.rb
97
98
  - spec/acceptance/map_reduce_spec.rb
98
99
  - spec/acceptance/adapter_spec.rb
99
100
  has_rdoc: true
@@ -134,5 +135,6 @@ test_files:
134
135
  - spec/lib/gom/storage/couchdb/remover_spec.rb
135
136
  - spec/lib/gom/storage/couchdb/saver_spec.rb
136
137
  - spec/lib/gom/storage/couchdb/adapter_spec.rb
138
+ - spec/acceptance/map_spec.rb
137
139
  - spec/acceptance/map_reduce_spec.rb
138
140
  - spec/acceptance/adapter_spec.rb