dolly 1.1.7 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +35 -0
  3. data/lib/dolly.rb +1 -23
  4. data/lib/dolly/attachment.rb +29 -0
  5. data/lib/dolly/bulk_document.rb +27 -26
  6. data/lib/dolly/class_methods_delegation.rb +15 -0
  7. data/lib/dolly/collection.rb +26 -69
  8. data/lib/dolly/configuration.rb +35 -10
  9. data/lib/dolly/connection.rb +91 -22
  10. data/lib/dolly/depracated_database.rb +24 -0
  11. data/lib/dolly/document.rb +32 -206
  12. data/lib/dolly/document_creation.rb +20 -0
  13. data/lib/dolly/document_state.rb +65 -0
  14. data/lib/dolly/document_type.rb +28 -0
  15. data/lib/dolly/exceptions.rb +21 -0
  16. data/lib/dolly/identity_properties.rb +29 -0
  17. data/lib/dolly/properties.rb +31 -0
  18. data/lib/dolly/property.rb +58 -47
  19. data/lib/dolly/property_manager.rb +47 -0
  20. data/lib/dolly/property_set.rb +18 -0
  21. data/lib/dolly/query.rb +39 -67
  22. data/lib/dolly/query_arguments.rb +35 -0
  23. data/lib/dolly/request.rb +12 -107
  24. data/lib/dolly/request_header.rb +26 -0
  25. data/lib/dolly/timestamp.rb +24 -0
  26. data/lib/dolly/version.rb +1 -1
  27. data/lib/{dolly → railties}/railtie.rb +2 -1
  28. data/lib/refinements/string_refinements.rb +28 -0
  29. data/lib/tasks/db.rake +4 -3
  30. data/test/bulk_document_test.rb +8 -5
  31. data/test/document_test.rb +137 -53
  32. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  33. data/test/dummy/log/test.log +46417 -46858
  34. data/test/test_helper.rb +14 -20
  35. metadata +42 -145
  36. data/Rakefile +0 -11
  37. data/lib/dolly/bulk_error.rb +0 -16
  38. data/lib/dolly/db_config.rb +0 -20
  39. data/lib/dolly/interpreter.rb +0 -5
  40. data/lib/dolly/logger.rb +0 -9
  41. data/lib/dolly/name_space.rb +0 -28
  42. data/lib/dolly/timestamps.rb +0 -21
  43. data/lib/exceptions/dolly.rb +0 -47
  44. data/test/collection_test.rb +0 -59
  45. data/test/configuration_test.rb +0 -9
  46. data/test/dummy/README.rdoc +0 -28
  47. data/test/dummy/Rakefile +0 -6
  48. data/test/dummy/app/assets/javascripts/application.js +0 -13
  49. data/test/dummy/app/assets/stylesheets/application.css +0 -13
  50. data/test/dummy/app/controllers/application_controller.rb +0 -5
  51. data/test/dummy/app/helpers/application_helper.rb +0 -2
  52. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  53. data/test/dummy/bin/bundle +0 -3
  54. data/test/dummy/bin/rails +0 -4
  55. data/test/dummy/bin/rake +0 -4
  56. data/test/dummy/config.ru +0 -4
  57. data/test/dummy/config/application.rb +0 -27
  58. data/test/dummy/config/boot.rb +0 -5
  59. data/test/dummy/config/couchdb.yml +0 -13
  60. data/test/dummy/config/environment.rb +0 -5
  61. data/test/dummy/config/environments/development.rb +0 -29
  62. data/test/dummy/config/environments/production.rb +0 -80
  63. data/test/dummy/config/environments/test.rb +0 -36
  64. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  65. data/test/dummy/config/initializers/inflections.rb +0 -16
  66. data/test/dummy/config/initializers/mime_types.rb +0 -5
  67. data/test/dummy/config/initializers/secret_token.rb +0 -12
  68. data/test/dummy/config/initializers/session_store.rb +0 -3
  69. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  70. data/test/dummy/config/locales/en.yml +0 -23
  71. data/test/dummy/config/routes.rb +0 -56
  72. data/test/dummy/lib/couch_rest_adapter/railtie.rb +0 -10
  73. data/test/dummy/public/404.html +0 -58
  74. data/test/dummy/public/422.html +0 -58
  75. data/test/dummy/public/500.html +0 -57
  76. data/test/dummy/public/favicon.ico +0 -0
  77. data/test/factories/factories.rb +0 -8
  78. data/test/request_test.rb +0 -25
@@ -0,0 +1,26 @@
1
+ require 'forwardable'
2
+
3
+ module Dolly
4
+ class HeaderRequest
5
+ extend Forwardable
6
+
7
+ CONTENT_TYPE_KEY = 'Content-Type'
8
+ JSON_CONTENT = 'application/json'
9
+
10
+ def_delegators :@collection, :[], :[]=, :keys, :each
11
+
12
+ def initialize hash = nil
13
+ @collection = hash || default_value
14
+ end
15
+
16
+ def json?
17
+ @collection[CONTENT_TYPE_KEY] == JSON_CONTENT
18
+ end
19
+
20
+ private
21
+
22
+ def default_value
23
+ { CONTENT_TYPE_KEY => JSON_CONTENT }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ module Dolly
2
+ module Timestamp
3
+ def write_timestamps(is_persisted)
4
+ return unless timestamped?
5
+ write_attribute(:created_at, Time.now) unless is_persisted
6
+ write_attribute(:updated_at, Time.now)
7
+ end
8
+
9
+ def timestamped?
10
+ respond_to?(:created_at) && respond_to?(:updated_at)
11
+ end
12
+
13
+ def self.included(base)
14
+ base.extend(ClassMethods)
15
+ end
16
+
17
+ module ClassMethods
18
+ def timestamps!
19
+ property :created_at, :updated_at, class_name: Time
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -1,3 +1,3 @@
1
1
  module Dolly
2
- VERSION = "1.1.7"
2
+ VERSION = "3.0.0"
3
3
  end
@@ -1,4 +1,4 @@
1
- require 'rails'
1
+ require 'rails/railtie'
2
2
 
3
3
  module Dolly
4
4
  class Railtie < Rails::Railtie
@@ -10,3 +10,4 @@ module Dolly
10
10
  end
11
11
  end
12
12
 
13
+
@@ -0,0 +1,28 @@
1
+ module StringRefinements
2
+ UNESCAPABLE_PATTERNS = [
3
+ %r{_design/.+/_view/.+}
4
+ ]
5
+
6
+ refine String do
7
+ #FROM ActiveModel::Name
8
+ def underscore
9
+ to_s.gsub(/::/, '/').
10
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
11
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
12
+ tr("-", "_").
13
+ downcase
14
+ end
15
+
16
+ def cgi_escape
17
+ return if nil?
18
+ return self unless escapable?
19
+ CGI.escape self
20
+ end
21
+
22
+ def escapable?
23
+ UNESCAPABLE_PATTERNS.none? do |pattern|
24
+ self =~ pattern
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,7 +1,7 @@
1
1
  namespace :db do
2
2
  desc "Will create if missing database and add default views"
3
3
  task setup: :environment do
4
- Dolly::Document.database.put "", nil
4
+ Dolly::Document.connection.put '', {}
5
5
  puts "Database created"
6
6
  end
7
7
 
@@ -38,7 +38,7 @@ namespace :db do
38
38
  view_doc.merge!( '_id' => design_doc_name, 'language' => 'coffeescript')
39
39
 
40
40
  begin
41
- hash_doc = JSON::parse Dolly::Document.database.get(view_doc["_id"]).parsed_response
41
+ hash_doc = Dolly::Document.connection.request(:get, view_doc["_id"])
42
42
 
43
43
  rev = hash_doc.delete('_rev')
44
44
 
@@ -55,8 +55,9 @@ namespace :db do
55
55
  will_save = true
56
56
  end
57
57
 
58
- Dolly::Document.database.put design_doc_name, view_doc.to_json if will_save
58
+ Dolly::Document.connection.request :put, design_doc_name, view_doc if will_save
59
59
  end
60
60
  end
61
61
 
62
62
  end
63
+
@@ -4,8 +4,7 @@ class Doc < Dolly::Document
4
4
  property :name
5
5
  end
6
6
 
7
- class BulkDocumentTest < ActiveSupport::TestCase
8
-
7
+ class BulkDocumentTest < Test::Unit::TestCase
9
8
  setup do
10
9
  @doc = Dolly::Document.bulk_document
11
10
  @req = "http://localhost:5984/test/_bulk_docs"
@@ -28,17 +27,21 @@ class BulkDocumentTest < ActiveSupport::TestCase
28
27
  test 'save document will remove docs from payload' do
29
28
  docs = 3.times.map{ Doc.new name: "a" }
30
29
  docs.each do |d|
31
- d.doc['_id'] = "foo_bar/#{SecureRandom.uuid}"
30
+ d.id = "doc/#{SecureRandom.uuid}"
32
31
  @doc << d
33
32
  end
34
33
 
35
34
  res = docs.map{|d| {ok: true, id: d.id, rev: "2-#{SecureRandom.uuid}"} }.to_json
36
- FakeWeb.register_uri :post, @req, body: res
35
+
36
+ stub_request(:post, @req).
37
+ to_return(body: res)
38
+
39
+ assert_equal docs, @doc.payload[:docs]
37
40
 
38
41
  @doc.save
39
42
 
40
43
  assert_equal [], @doc.errors
41
44
  assert_equal [], @doc.docs
45
+ assert_equal [], @doc.payload[:docs]
42
46
  end
43
-
44
47
  end
@@ -30,7 +30,7 @@ class FooBaz < Dolly::Document
30
30
  end
31
31
 
32
32
  class WithTime < Dolly::Document
33
- property :created_at, default: -> {Time.now}
33
+ property :created_at, default: -> { Time.now }
34
34
  end
35
35
 
36
36
  class TestFoo < Dolly::Document
@@ -41,7 +41,7 @@ class DocumentWithValidMethod < Dolly::Document
41
41
  property :foo
42
42
 
43
43
  def valid?
44
- foo.present?
44
+ !foo.nil?
45
45
  end
46
46
  end
47
47
 
@@ -53,7 +53,7 @@ class Bar < FooBar
53
53
  property :a, :b
54
54
  end
55
55
 
56
- class DocumentTest < ActiveSupport::TestCase
56
+ class DocumentTest < Test::Unit::TestCase
57
57
  DB_BASE_PATH = "http://localhost:5984/test".freeze
58
58
 
59
59
  def setup
@@ -72,23 +72,51 @@ class DocumentTest < ActiveSupport::TestCase
72
72
  build_request [["foo_bar","1"],["foo_bar","2"]], @multi_resp
73
73
 
74
74
  #TODO: Mock Dolly::Request to return helper with expected response. request builder can be tested by itself.
75
- FakeWeb.register_uri :get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%2F%EF%BF%B0%22&include_docs=true", body: @multi_resp.to_json
76
- FakeWeb.register_uri :get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%2F%EF%BF%B0%22&limit=1&include_docs=true", body: view_resp.to_json
77
- FakeWeb.register_uri :get, "#{query_base_path}?endkey=%22foo_bar%2F%22&startkey=%22foo_bar%2F%EF%BF%B0%22&limit=1&descending=true&include_docs=true", body: view_resp.to_json
78
- FakeWeb.register_uri :get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%22%2C%7B%7D&limit=2&include_docs=true", body: @multi_resp.to_json
79
- FakeWeb.register_uri :get, "#{query_base_path}?endkey=%22foo_bar%2F%22&startkey=%22foo_bar%2F%EF%BF%B0%22&limit=2&descending=true&include_docs=true", body: @multi_resp.to_json
80
- FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2F1%22%5D&include_docs=true", body: view_resp.to_json
81
- FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%5D&include_docs=true", body: not_found_resp.to_json
82
- FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2Ferror%22%5D&include_docs=true", body: 'error', status: ["500", "Error"]
83
- FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2F1%22%2C%22foo_bar%2F2%22%5D&include_docs=true", body: @multi_resp.to_json
84
- FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2F2%22%5D&include_docs=true", body: not_found_resp.to_json
85
- FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2Fbig_doc%22%5D&include_docs=true", body: build_view_response([data.merge(other_property: 'other')]).to_json
75
+ stub_request(:get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%2F%EF%BF%B0%22&include_docs=true").
76
+ to_return(body: @multi_resp.to_json)
77
+
78
+ stub_request(:get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%2F%EF%BF%B0%22&limit=1&include_docs=true").
79
+ to_return(body: view_resp.to_json)
80
+
81
+ stub_request(:get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%2F%EF%BF%B0%22&limit=2&include_docs=true").
82
+ to_return(body: view_resp.to_json)
83
+
84
+ stub_request(:get, "#{query_base_path}?endkey=%22foo_bar%2F%22&startkey=%22foo_bar%2F%EF%BF%B0%22&limit=1&descending=true&include_docs=true").
85
+ to_return(body: view_resp.to_json)
86
+
87
+ stub_request(:get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%22%2C%7B%7D&limit=2&include_docs=true").
88
+ to_return(body: @multi_resp.to_json)
89
+
90
+ stub_request(:get, "#{query_base_path}?endkey=%22foo_bar%2F%22&startkey=%22foo_bar%2F%EF%BF%B0%22&limit=2&descending=true&include_docs=true").
91
+ to_return(body: @multi_resp.to_json)
92
+
93
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2F1%22%5D&include_docs=true").
94
+ to_return(body: view_resp.to_json)
95
+
96
+ stub_request(:get, "#{query_base_path}?keys=%5B%5D&include_docs=true").
97
+ to_return(body: not_found_resp.to_json)
98
+
99
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2Ferror%22%5D&include_docs=true").
100
+ to_return(body: 'error', status: ["500", "Error"])
101
+
102
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2F1%22%2C%22foo_bar%2F2%22%5D&include_docs=true").
103
+ to_return(body: @multi_resp.to_json)
104
+
105
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2F2%22%5D&include_docs=true").
106
+ to_return(status: 404, body: not_found_resp.to_json)
107
+
108
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2Fbig_doc%22%5D&include_docs=true").
109
+ to_return(body: build_view_response([data.merge(other_property: 'other')]).to_json)
110
+
86
111
  end
87
112
 
88
113
  test 'new in memory document' do
89
114
  #TODO: clean up all the fake request creation
90
115
  resp = {ok: true, id: "foo_bar/1", rev: "FF0000"}
91
- FakeWeb.register_uri :put, /http:\/\/localhost:5984\/test\/foo_bar%2F.+/, body: resp.to_json
116
+
117
+ stub_request(:put, /http:\/\/localhost:5984\/test\/foo_bar%2F.+/).
118
+ to_return(body: resp.to_json)
119
+
92
120
  properties = {foo: 1, bar: 2, boolean: false}
93
121
  foo = FooBar.new properties
94
122
  assert_equal 1, foo.with_default
@@ -100,15 +128,15 @@ class DocumentTest < ActiveSupport::TestCase
100
128
 
101
129
  test 'empty find should raise error' do
102
130
  assert_raise Dolly::ResourceNotFound do
103
- FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%5D&include_docs=true", :status => ["404", "Not Found"]
104
- foo = FooBar.find
131
+ stub_request(:get, "#{query_base_path}?keys=%5B%5D&include_docs=true").to_return(status: 404)
132
+ FooBar.find
105
133
  end
106
134
  end
107
135
 
108
136
  test 'error on server raises Dolly::ServerError' do
109
137
  assert_raise Dolly::ServerError do
110
- FakeWeb.register_uri :get, "#{query_base_path}?keys=", :status => ["500", "Error"]
111
- foo = FooBar.find 'error'
138
+ stub_request(:get, "#{query_base_path}?keys=").to_return(status: 500)
139
+ FooBar.find 'error'
112
140
  end
113
141
  end
114
142
 
@@ -175,28 +203,36 @@ class DocumentTest < ActiveSupport::TestCase
175
203
 
176
204
  test 'getting not found document' do
177
205
  assert_raise Dolly::ResourceNotFound do
178
- missing = FooBar.find "2"
206
+ FooBar.find "2"
179
207
  end
180
208
  end
181
209
 
182
210
  test 'reload reloads the doc attribute from database' do
183
211
  assert foo = FooBar.find('1')
184
- expected_doc = foo.doc.dup
185
- FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2F0%22%5D&include_docs=true", body: build_view_response([expected_doc]).to_json
212
+ expected_doc = foo.send(:doc).dup
213
+
214
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2F0%22%5D&include_docs=true").
215
+ to_return(body: build_view_response([expected_doc]).to_json)
216
+
186
217
  assert foo.foo = 1
187
- assert_not_equal expected_doc, foo.doc
218
+ assert_not_equal expected_doc, foo.send(:doc)
188
219
  assert foo.reload
189
- assert_equal expected_doc, foo.doc
220
+ assert_equal expected_doc, foo.send(:doc)
190
221
  end
191
222
 
192
223
  test 'accessors work as expected after reload' do
193
224
  resp = {ok: true, id: "foo_bar/1", rev: "FF0000"}
194
- FakeWeb.register_uri :put, "http://localhost:5984/test/foo_bar%2F0", body: resp.to_json
225
+ stub_request(:put, "http://localhost:5984/test/foo_bar%2F0").
226
+ to_return(body: resp.to_json)
227
+
195
228
  assert foo = FooBar.find('1')
196
229
  assert foo.foo = 1
197
230
  assert foo.save
198
- assert expected_doc = foo.doc
199
- FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2F0%22%5D&include_docs=true", body: build_view_response([expected_doc]).to_json
231
+ assert expected_doc = foo.send(:doc)
232
+
233
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2F0%22%5D&include_docs=true").
234
+ to_return(body: build_view_response([expected_doc]).to_json)
235
+
200
236
  assert foo.reload
201
237
  assert_equal 1, foo.foo
202
238
  end
@@ -228,9 +264,11 @@ class DocumentTest < ActiveSupport::TestCase
228
264
 
229
265
  test 'multi response with right data' do
230
266
  all = FooBar.all
231
- ids = @multi_resp[:rows].map{|d| d[:id]}
267
+ @multi_resp[:rows].map{|d| d[:id]}
268
+ @multi_resp[:rows].map{|d| d[:doc][:bar]}
269
+
232
270
  foos = @multi_resp[:rows].map{|d| d[:doc][:foo]}
233
- bars = @multi_resp[:rows].map{|d| d[:doc][:bar]}
271
+
234
272
  all.each do |d|
235
273
  assert foos.include?(d.foo)
236
274
  end
@@ -258,26 +296,34 @@ class DocumentTest < ActiveSupport::TestCase
258
296
 
259
297
  test 'delete method on document' do
260
298
  resp = {ok: true, id: "foo_bar/1", rev: "FF0000"}
261
- FakeWeb.register_uri :delete, /http:\/\/localhost:5984\/test\/foo_bar%2F[^\?]+\?rev=.+/, body: resp.to_json
299
+
300
+ stub_request(:delete, /http:\/\/localhost:5984\/test\/foo_bar%2F[^\?]+\?rev=.+/).
301
+ to_return(body: resp.to_json)
302
+
262
303
  doc = FooBar.find "1"
263
304
  doc.destroy
264
305
  end
265
306
 
266
307
  test 'soft delete on document' do
267
308
  assert doc = FooBar.find("1")
309
+ build_save_request(doc)
268
310
  assert doc.destroy(false)
269
- assert_equal true, doc.doc['_deleted']
311
+ assert_equal true, doc.send(:doc)[:_deleted]
270
312
  end
271
313
 
272
314
  test 'query custom view' do
273
- FakeWeb.register_uri :get, "http://localhost:5984/test/_design/test/_view/custom_view?key=1&include_docs=true", body: @multi_resp.to_json
315
+ stub_request(:get, "http://localhost:5984/test/_design/test/_view/custom_view?key=1&include_docs=true").
316
+ to_return(body: @multi_resp.to_json)
317
+
274
318
  f = FooBar.find_with "test", "custom_view", key: 1
275
319
  assert_equal 2, f.count
276
320
  f.each{ |d| assert d.kind_of?(FooBar) }
277
321
  end
278
322
 
279
323
  test 'query custom view collation' do
280
- FakeWeb.register_uri :get, "http://localhost:5984/test/_design/test/_view/custom_view?startkey=%5B1%5D&endkey=%5B1%2C%7B%7D%5D&include_docs=true", body: @multi_type_resp.to_json
324
+ stub_request(:get, "http://localhost:5984/test/_design/test/_view/custom_view?startkey=%5B1%5D&endkey=%5B1%2C%7B%7D%5D&include_docs=true").
325
+ to_return(body: @multi_type_resp.to_json)
326
+
281
327
  f = FooBar.find_with "test", "custom_view", { startkey: [1], endkey: [1, {}]}
282
328
  assert_equal 2, f.count
283
329
  assert f.first.kind_of?(FooBar)
@@ -328,7 +374,7 @@ class DocumentTest < ActiveSupport::TestCase
328
374
  end
329
375
 
330
376
  test 'update document propertys with bang' do
331
- foo = FooBar.new 'id' => 'a', foo: 'ab', bar: 'ba'
377
+ foo = FooBar.new 'id'=> 'a', foo: 'ab', bar: 'ba'
332
378
  foo.expects(:save).once
333
379
  foo.update_properties! foo: 'c'
334
380
  end
@@ -342,13 +388,17 @@ class DocumentTest < ActiveSupport::TestCase
342
388
 
343
389
  test 'set updated at' do
344
390
  foo = FooBar.new 'id' => 'a', foo: 'ab'
391
+ build_save_request foo
345
392
  foo.update_properties! foo: 'c'
346
393
  assert_equal Time.now.to_s, foo.updated_at.to_s
347
394
  end
348
395
 
349
396
  test 'created at is set' do
350
397
  resp = {ok: true, id: "foo_bar/1", rev: "FF0000"}
351
- FakeWeb.register_uri :put, /http:\/\/localhost:5984\/test\/foo_bar%2F.+/, body: resp.to_json
398
+
399
+ stub_request(:put, /http:\/\/localhost:5984\/test\/foo_bar%2F.+/).
400
+ to_return(body: resp.to_json)
401
+
352
402
  properties = {foo: 1, bar: 2, boolean: false}
353
403
  foo = FooBar.new properties
354
404
  foo.save
@@ -357,6 +407,9 @@ class DocumentTest < ActiveSupport::TestCase
357
407
 
358
408
  test 'reader :bar is not calling the writer :bar=' do
359
409
  foo = FooBar.new
410
+
411
+ build_save_request(foo)
412
+
360
413
  foo.bar = 'bar'
361
414
  foo.save!
362
415
  foo.expects(:bar=).times(0)
@@ -370,6 +423,9 @@ class DocumentTest < ActiveSupport::TestCase
370
423
 
371
424
  test 'persisted? returns false if _rev is not present' do
372
425
  foo = FooBar.new
426
+
427
+ build_save_request(foo)
428
+
373
429
  assert_equal foo.persisted?, false
374
430
  assert foo.save
375
431
  assert_equal foo.persisted?, true
@@ -377,14 +433,20 @@ class DocumentTest < ActiveSupport::TestCase
377
433
 
378
434
  test 'can save without timestamps' do
379
435
  resp = {ok: true, id: "foo_bar/1", rev: "FF0000"}
380
- FakeWeb.register_uri :put, /http:\/\/localhost:5984\/test\/foo_baz%2F.+/, body: resp.to_json
436
+
437
+ stub_request(:put, /http:\/\/localhost:5984\/test\/foo_baz%2F.+/)
438
+ .to_return(body: resp.to_json)
439
+
381
440
  foobaz = FooBaz.new foo: {foo: :bar}
382
441
  assert foobaz.save!
383
442
  end
384
443
 
385
444
  test 'property writes work correctly with pipe equals' do
386
445
  resp = {ok: true, id: "foo_bar/1", rev: "FF0000"}
387
- FakeWeb.register_uri :put, /http:\/\/localhost:5984\/test\/foo_baz%2F.+/, body: resp.to_json
446
+
447
+ stub_request(:put, /http:\/\/localhost:5984\/test\/foo_baz%2F.+/).
448
+ to_return(body: resp.to_json)
449
+
388
450
  foobaz = FooBaz.new foo: {'foo' => 'bar'}
389
451
  foobaz.add_to_foo 'bar', 'bar'
390
452
  assert_equal foobaz.foo, {'foo' => 'bar', 'bar' => 'bar'}
@@ -392,25 +454,28 @@ class DocumentTest < ActiveSupport::TestCase
392
454
 
393
455
  test 'default should populate before save' do
394
456
  test_foo = TestFoo.new
395
- assert_equal 'FOO', test_foo.doc['default_test_property']
457
+ assert_equal 'FOO', test_foo.default_test_property
396
458
  end
397
459
 
398
460
  test 'default should be overridden by params' do
399
461
  test_foo = TestFoo.new(default_test_property: 'bar')
400
- assert_equal 'bar', test_foo.doc['default_test_property']
462
+ assert_equal 'bar', test_foo.default_test_property
401
463
  end
402
464
 
403
465
  test 'doc and method and instance var are the same' do
404
466
  test_foo = FooBar.new
405
467
  test_foo.foo = 'test_value'
406
468
  assert_equal 'test_value', test_foo.foo
407
- assert_equal 'test_value', test_foo.doc['foo']
469
+ assert_equal 'test_value', test_foo.send(:doc)[:foo]
408
470
  assert_equal 'test_value', test_foo.instance_variable_get(:@foo)
409
471
  end
410
472
 
411
473
  test 'created at is current time' do
412
474
  resp = {ok: true, id: "with_time/timed", rev: "FF0000"}
413
- FakeWeb.register_uri :put, /http:\/\/localhost:5984\/test\/with_time%2F.+/, body: resp.to_json
475
+
476
+ stub_request(:put, /http:\/\/localhost:5984\/test\/with_time%2F.+/).
477
+ to_return(body: resp.to_json)
478
+
414
479
  test = WithTime.new id: "timed"
415
480
  assert test.respond_to?(:created_at)
416
481
  assert test.save
@@ -420,7 +485,8 @@ class DocumentTest < ActiveSupport::TestCase
420
485
  test 'nil default' do
421
486
  properties = {foo: nil, is_nil: nil}
422
487
  resp = {ok: true, id: "foo_bar/1", rev: "FF0000"}
423
- FakeWeb.register_uri :put, /http:\/\/localhost:5984\/test\/foo_bar%2F.+/, body: resp.to_json
488
+ stub_request(:put, /http:\/\/localhost:5984\/test\/foo_bar%2F.+/).
489
+ to_return(body: resp.to_json)
424
490
  foo = FooBar.new properties
425
491
  foo.save
426
492
  properties.each do |k, v|
@@ -444,19 +510,21 @@ class DocumentTest < ActiveSupport::TestCase
444
510
 
445
511
  test 'save returns false for invalid document on save' do
446
512
  foo = DocumentWithValidMethod.new
447
- assert_not foo.save
513
+ assert_equal foo.save, false
448
514
  end
449
515
 
450
516
  test 'save succeeds for invalid document if skipping validations' do
451
517
  resp = {ok: true, id: "document_with_valid_method/1", rev: "FF0000"}
452
- FakeWeb.register_uri :put, /http:\/\/localhost:5984\/test\/document_with_valid_method%2F.+/, body: resp.to_json
518
+ stub_request(:put, /http:\/\/localhost:5984\/test\/document_with_valid_method%2F.+/).
519
+ to_return(body: resp.to_json)
520
+
453
521
  foo = DocumentWithValidMethod.new
454
522
  assert foo.save(validate: false)
455
523
  end
456
524
 
457
525
  test 'default objects are not the same in memory' do
458
526
  doc_with_same_default = DocWithSameDefaults.new
459
- assert_not doc_with_same_default.foo.equal? doc_with_same_default.bar
527
+ assert_not_same doc_with_same_default.foo, doc_with_same_default.bar
460
528
  doc_with_same_default.foo.push 'foo'
461
529
  assert doc_with_same_default.bar == []
462
530
  end
@@ -473,26 +541,37 @@ class DocumentTest < ActiveSupport::TestCase
473
541
 
474
542
  test 'attach_file! will add a standalone attachment to the document' do
475
543
  assert save_response = {ok: true, id: "base_dolly/79178957-96ff-40d9-9ecb-217fa35bdea7", rev: "1"}
476
- assert FakeWeb.register_uri :put, /http:\/\/localhost:5984\/test\/base_dolly%2F.+/, body: save_response.to_json
544
+
545
+ assert stub_request(:put, /http:\/\/localhost:5984\/test\/base_dolly%2F.+/).
546
+ to_return(body: save_response.to_json)
547
+
477
548
  assert doc = BaseDolly.new
478
549
  assert doc.save
479
550
  assert resp = {ok: true, id: '79178957-96ff-40d9-9ecb-217fa35bdea7', rev: '2'}
480
- assert FakeWeb.register_uri :put, /http:\/\/localhost:5984\/test\/base_dolly\/79178957-96ff-40d9-9ecb-217fa35bdea7\/test.txt/, body: resp.to_json
551
+ assert stub_request(:put, /http:\/\/localhost:5984\/test\/base_dolly\/79178957-96ff-40d9-9ecb-217fa35bdea7\/test.txt/)
552
+ .to_return(body: resp.to_json)
553
+
481
554
  assert data = File.open("#{FileUtils.pwd}/test/support/test.txt").read
482
555
  assert doc.attach_file! 'test.txt', 'text/plain', data
483
556
  end
484
557
 
485
558
  test 'attach_file! will add an inline attachment if specified' do
486
559
  assert save_response = {ok: true, id: "base_dolly/79178957-96ff-40d9-9ecb-217fa35bdea7", rev: "1"}
487
- assert FakeWeb.register_uri :put, /http:\/\/localhost:5984\/test\/base_dolly%2F.+/, body: save_response.to_json
560
+
561
+ assert stub_request(:put, /http:\/\/localhost:5984\/test\/base_dolly%2F.+/).
562
+ to_return(body: save_response.to_json)
563
+
488
564
  assert doc = BaseDolly.new
489
565
  assert doc.save
490
566
  assert resp = {ok: true, id: '79178957-96ff-40d9-9ecb-217fa35bdea7', rev: '2'}
491
- assert FakeWeb.register_uri :put, /http:\/\/localhost:5984\/test\/base_dolly\/79178957-96ff-40d9-9ecb-217fa35bdea7\/test.txt/, body: resp.to_json
567
+
568
+ assert stub_request(:put, /http:\/\/localhost:5984\/test\/base_dolly\/79178957-96ff-40d9-9ecb-217fa35bdea7\/test.txt/).
569
+ to_return(body: resp.to_json)
570
+
492
571
  assert data = File.open("#{FileUtils.pwd}/test/support/test.txt").read
493
572
  assert doc.attach_file! 'test.txt', 'text/plain', data, inline: true
494
- assert doc.doc['_attachments']['test.txt'].present?
495
- assert_equal Base64.encode64(data), doc.doc['_attachments']['test.txt']['data']
573
+ assert_equal doc.send(:doc)['_attachments']['test.txt']&.empty?, false
574
+ assert_equal Base64.encode64(data), doc.send(:doc)['_attachments']['test.txt']['data']
496
575
  end
497
576
 
498
577
  test "new object from inhereted document" do
@@ -532,12 +611,17 @@ class DocumentTest < ActiveSupport::TestCase
532
611
 
533
612
 
534
613
  def build_request keys, body, view_name = 'foo_bar'
535
- query = "keys=#{CGI::escape keys.to_s.gsub(' ','')}&" unless keys.blank?
536
- FakeWeb.register_uri :get, "#{query_base_path}?#{query.to_s}include_docs=true", body: body.to_json
614
+ query = "keys=#{CGI::escape keys.to_s.gsub(' ','')}&" unless keys&.empty?
615
+ stub_request(:get, "#{query_base_path}?#{query.to_s}include_docs=true").
616
+ to_return(body: body.to_json)
537
617
  end
538
618
 
539
619
  def query_base_path
540
620
  "#{DB_BASE_PATH}/_all_docs"
541
621
  end
542
622
 
623
+ def build_save_request(obj)
624
+ stub_request(:put, "#{DB_BASE_PATH}/#{CGI.escape(obj.id)}").
625
+ to_return(body: {ok: true, id: obj.id, rev: "FF0000" }.to_json)
626
+ end
543
627
  end