dolly 1.1.7 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +78 -0
  3. data/lib/dolly/attachment.rb +29 -0
  4. data/lib/dolly/bulk_document.rb +27 -26
  5. data/lib/dolly/class_methods_delegation.rb +15 -0
  6. data/lib/dolly/collection.rb +32 -65
  7. data/lib/dolly/configuration.rb +35 -10
  8. data/lib/dolly/connection.rb +93 -22
  9. data/lib/dolly/depracated_database.rb +24 -0
  10. data/lib/dolly/document.rb +61 -208
  11. data/lib/dolly/document_creation.rb +27 -0
  12. data/lib/dolly/document_state.rb +66 -0
  13. data/lib/dolly/document_type.rb +47 -0
  14. data/lib/dolly/exceptions.rb +32 -0
  15. data/lib/dolly/framework_helper.rb +7 -0
  16. data/lib/dolly/identity_properties.rb +29 -0
  17. data/lib/dolly/mango.rb +156 -0
  18. data/lib/dolly/mango_index.rb +73 -0
  19. data/lib/dolly/properties.rb +36 -0
  20. data/lib/dolly/property.rb +76 -46
  21. data/lib/dolly/property_manager.rb +53 -0
  22. data/lib/dolly/property_set.rb +23 -0
  23. data/lib/dolly/query.rb +63 -75
  24. data/lib/dolly/query_arguments.rb +35 -0
  25. data/lib/dolly/request.rb +12 -107
  26. data/lib/dolly/request_header.rb +26 -0
  27. data/lib/dolly/timestamp.rb +24 -0
  28. data/lib/dolly/version.rb +1 -1
  29. data/lib/dolly/view_query.rb +21 -0
  30. data/lib/dolly.rb +2 -23
  31. data/lib/{dolly → railties}/railtie.rb +2 -1
  32. data/lib/refinements/hash_refinements.rb +27 -0
  33. data/lib/refinements/string_refinements.rb +28 -0
  34. data/lib/tasks/db.rake +27 -4
  35. data/test/bulk_document_test.rb +8 -5
  36. data/test/document_test.rb +130 -95
  37. data/test/document_type_test.rb +28 -0
  38. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  39. data/test/dummy/log/test.log +46417 -46858
  40. data/test/inheritance_test.rb +23 -0
  41. data/test/mango_index_test.rb +64 -0
  42. data/test/mango_test.rb +273 -0
  43. data/test/property_manager_test.rb +18 -0
  44. data/test/test_helper.rb +63 -18
  45. data/test/view_query_test.rb +27 -0
  46. metadata +67 -140
  47. data/Rakefile +0 -11
  48. data/lib/dolly/bulk_error.rb +0 -16
  49. data/lib/dolly/db_config.rb +0 -20
  50. data/lib/dolly/interpreter.rb +0 -5
  51. data/lib/dolly/logger.rb +0 -9
  52. data/lib/dolly/name_space.rb +0 -28
  53. data/lib/dolly/timestamps.rb +0 -21
  54. data/lib/exceptions/dolly.rb +0 -47
  55. data/test/collection_test.rb +0 -59
  56. data/test/configuration_test.rb +0 -9
  57. data/test/dummy/README.rdoc +0 -28
  58. data/test/dummy/Rakefile +0 -6
  59. data/test/dummy/app/assets/javascripts/application.js +0 -13
  60. data/test/dummy/app/assets/stylesheets/application.css +0 -13
  61. data/test/dummy/app/controllers/application_controller.rb +0 -5
  62. data/test/dummy/app/helpers/application_helper.rb +0 -2
  63. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  64. data/test/dummy/bin/bundle +0 -3
  65. data/test/dummy/bin/rails +0 -4
  66. data/test/dummy/bin/rake +0 -4
  67. data/test/dummy/config/application.rb +0 -27
  68. data/test/dummy/config/boot.rb +0 -5
  69. data/test/dummy/config/couchdb.yml +0 -13
  70. data/test/dummy/config/environment.rb +0 -5
  71. data/test/dummy/config/environments/development.rb +0 -29
  72. data/test/dummy/config/environments/production.rb +0 -80
  73. data/test/dummy/config/environments/test.rb +0 -36
  74. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  75. data/test/dummy/config/initializers/inflections.rb +0 -16
  76. data/test/dummy/config/initializers/mime_types.rb +0 -5
  77. data/test/dummy/config/initializers/secret_token.rb +0 -12
  78. data/test/dummy/config/initializers/session_store.rb +0 -3
  79. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  80. data/test/dummy/config/locales/en.yml +0 -23
  81. data/test/dummy/config/routes.rb +0 -56
  82. data/test/dummy/config.ru +0 -4
  83. data/test/dummy/lib/couch_rest_adapter/railtie.rb +0 -10
  84. data/test/dummy/public/404.html +0 -58
  85. data/test/dummy/public/422.html +0 -58
  86. data/test/dummy/public/500.html +0 -57
  87. data/test/dummy/public/favicon.ico +0 -0
  88. data/test/factories/factories.rb +0 -8
  89. data/test/request_test.rb +0 -25
@@ -1,7 +1,5 @@
1
1
  require 'test_helper'
2
2
 
3
- class BaseDolly < Dolly::Document; end
4
-
5
3
  class BarFoo < BaseDolly
6
4
  property :a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :persist
7
5
  end
@@ -30,7 +28,7 @@ class FooBaz < Dolly::Document
30
28
  end
31
29
 
32
30
  class WithTime < Dolly::Document
33
- property :created_at, default: -> {Time.now}
31
+ property :created_at, default: -> { Time.now }
34
32
  end
35
33
 
36
34
  class TestFoo < Dolly::Document
@@ -41,7 +39,7 @@ class DocumentWithValidMethod < Dolly::Document
41
39
  property :foo
42
40
 
43
41
  def valid?
44
- foo.present?
42
+ !foo.nil?
45
43
  end
46
44
  end
47
45
 
@@ -53,8 +51,7 @@ class Bar < FooBar
53
51
  property :a, :b
54
52
  end
55
53
 
56
- class DocumentTest < ActiveSupport::TestCase
57
- DB_BASE_PATH = "http://localhost:5984/test".freeze
54
+ class DocumentTest < Test::Unit::TestCase
58
55
 
59
56
  def setup
60
57
  data = {foo: 'Foo', bar: 'Bar', type: 'foo_bar'}
@@ -72,23 +69,51 @@ class DocumentTest < ActiveSupport::TestCase
72
69
  build_request [["foo_bar","1"],["foo_bar","2"]], @multi_resp
73
70
 
74
71
  #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
72
+ stub_request(:get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%2F%EF%BF%B0%22&include_docs=true").
73
+ to_return(body: @multi_resp.to_json)
74
+
75
+ stub_request(:get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%2F%EF%BF%B0%22&limit=1&include_docs=true").
76
+ to_return(body: view_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=2&include_docs=true").
79
+ to_return(body: view_resp.to_json)
80
+
81
+ 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").
82
+ to_return(body: view_resp.to_json)
83
+
84
+ stub_request(:get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%22%2C%7B%7D&limit=2&include_docs=true").
85
+ to_return(body: @multi_resp.to_json)
86
+
87
+ 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").
88
+ to_return(body: @multi_resp.to_json)
89
+
90
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2F1%22%5D&include_docs=true").
91
+ to_return(body: view_resp.to_json)
92
+
93
+ stub_request(:get, "#{query_base_path}?keys=%5B%5D&include_docs=true").
94
+ to_return(body: not_found_resp.to_json)
95
+
96
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2Ferror%22%5D&include_docs=true").
97
+ to_return(body: 'error', status: ["500", "Error"])
98
+
99
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2F1%22%2C%22foo_bar%2F2%22%5D&include_docs=true").
100
+ to_return(body: @multi_resp.to_json)
101
+
102
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2F2%22%5D&include_docs=true").
103
+ to_return(status: 404, body: not_found_resp.to_json)
104
+
105
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2Fbig_doc%22%5D&include_docs=true").
106
+ to_return(body: build_view_response([data.merge(other_property: 'other')]).to_json)
107
+
86
108
  end
87
109
 
88
110
  test 'new in memory document' do
89
111
  #TODO: clean up all the fake request creation
90
112
  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
113
+
114
+ stub_request(:put, /http:\/\/localhost:5984\/test\/foo_bar%2F.+/).
115
+ to_return(body: resp.to_json)
116
+
92
117
  properties = {foo: 1, bar: 2, boolean: false}
93
118
  foo = FooBar.new properties
94
119
  assert_equal 1, foo.with_default
@@ -100,15 +125,15 @@ class DocumentTest < ActiveSupport::TestCase
100
125
 
101
126
  test 'empty find should raise error' do
102
127
  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
128
+ stub_request(:get, "#{query_base_path}?keys=%5B%5D&include_docs=true").to_return(status: 404)
129
+ FooBar.find
105
130
  end
106
131
  end
107
132
 
108
133
  test 'error on server raises Dolly::ServerError' do
109
134
  assert_raise Dolly::ServerError do
110
- FakeWeb.register_uri :get, "#{query_base_path}?keys=", :status => ["500", "Error"]
111
- foo = FooBar.find 'error'
135
+ stub_request(:get, "#{query_base_path}?keys=").to_return(status: 500)
136
+ FooBar.find 'error'
112
137
  end
113
138
  end
114
139
 
@@ -175,28 +200,36 @@ class DocumentTest < ActiveSupport::TestCase
175
200
 
176
201
  test 'getting not found document' do
177
202
  assert_raise Dolly::ResourceNotFound do
178
- missing = FooBar.find "2"
203
+ FooBar.find "2"
179
204
  end
180
205
  end
181
206
 
182
207
  test 'reload reloads the doc attribute from database' do
183
208
  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
209
+ expected_doc = foo.send(:doc).dup
210
+
211
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2F0%22%5D&include_docs=true").
212
+ to_return(body: build_view_response([expected_doc]).to_json)
213
+
186
214
  assert foo.foo = 1
187
- assert_not_equal expected_doc, foo.doc
215
+ assert_not_equal expected_doc, foo.send(:doc)
188
216
  assert foo.reload
189
- assert_equal expected_doc, foo.doc
217
+ assert_equal expected_doc, foo.send(:doc)
190
218
  end
191
219
 
192
220
  test 'accessors work as expected after reload' do
193
221
  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
222
+ stub_request(:put, "http://localhost:5984/test/foo_bar%2F0").
223
+ to_return(body: resp.to_json)
224
+
195
225
  assert foo = FooBar.find('1')
196
226
  assert foo.foo = 1
197
227
  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
228
+ assert expected_doc = foo.send(:doc)
229
+
230
+ stub_request(:get, "#{query_base_path}?keys=%5B%22foo_bar%2F0%22%5D&include_docs=true").
231
+ to_return(body: build_view_response([expected_doc]).to_json)
232
+
200
233
  assert foo.reload
201
234
  assert_equal 1, foo.foo
202
235
  end
@@ -228,9 +261,11 @@ class DocumentTest < ActiveSupport::TestCase
228
261
 
229
262
  test 'multi response with right data' do
230
263
  all = FooBar.all
231
- ids = @multi_resp[:rows].map{|d| d[:id]}
264
+ @multi_resp[:rows].map{|d| d[:id]}
265
+ @multi_resp[:rows].map{|d| d[:doc][:bar]}
266
+
232
267
  foos = @multi_resp[:rows].map{|d| d[:doc][:foo]}
233
- bars = @multi_resp[:rows].map{|d| d[:doc][:bar]}
268
+
234
269
  all.each do |d|
235
270
  assert foos.include?(d.foo)
236
271
  end
@@ -258,26 +293,34 @@ class DocumentTest < ActiveSupport::TestCase
258
293
 
259
294
  test 'delete method on document' do
260
295
  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
296
+
297
+ stub_request(:delete, /http:\/\/localhost:5984\/test\/foo_bar%2F[^\?]+\?rev=.+/).
298
+ to_return(body: resp.to_json)
299
+
262
300
  doc = FooBar.find "1"
263
301
  doc.destroy
264
302
  end
265
303
 
266
304
  test 'soft delete on document' do
267
305
  assert doc = FooBar.find("1")
306
+ build_save_request(doc)
268
307
  assert doc.destroy(false)
269
- assert_equal true, doc.doc['_deleted']
308
+ assert_equal true, doc.send(:doc)[:_deleted]
270
309
  end
271
310
 
272
311
  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
312
+ stub_request(:get, "http://localhost:5984/test/_design/test/_view/custom_view?key=1&include_docs=true").
313
+ to_return(body: @multi_resp.to_json)
314
+
274
315
  f = FooBar.find_with "test", "custom_view", key: 1
275
316
  assert_equal 2, f.count
276
317
  f.each{ |d| assert d.kind_of?(FooBar) }
277
318
  end
278
319
 
279
320
  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
321
+ stub_request(:get, "http://localhost:5984/test/_design/test/_view/custom_view?startkey=%5B1%5D&endkey=%5B1%2C%7B%7D%5D&include_docs=true").
322
+ to_return(body: @multi_type_resp.to_json)
323
+
281
324
  f = FooBar.find_with "test", "custom_view", { startkey: [1], endkey: [1, {}]}
282
325
  assert_equal 2, f.count
283
326
  assert f.first.kind_of?(FooBar)
@@ -328,7 +371,7 @@ class DocumentTest < ActiveSupport::TestCase
328
371
  end
329
372
 
330
373
  test 'update document propertys with bang' do
331
- foo = FooBar.new 'id' => 'a', foo: 'ab', bar: 'ba'
374
+ foo = FooBar.new 'id'=> 'a', foo: 'ab', bar: 'ba'
332
375
  foo.expects(:save).once
333
376
  foo.update_properties! foo: 'c'
334
377
  end
@@ -342,13 +385,17 @@ class DocumentTest < ActiveSupport::TestCase
342
385
 
343
386
  test 'set updated at' do
344
387
  foo = FooBar.new 'id' => 'a', foo: 'ab'
388
+ build_save_request foo
345
389
  foo.update_properties! foo: 'c'
346
390
  assert_equal Time.now.to_s, foo.updated_at.to_s
347
391
  end
348
392
 
349
393
  test 'created at is set' do
350
394
  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
395
+
396
+ stub_request(:put, /http:\/\/localhost:5984\/test\/foo_bar%2F.+/).
397
+ to_return(body: resp.to_json)
398
+
352
399
  properties = {foo: 1, bar: 2, boolean: false}
353
400
  foo = FooBar.new properties
354
401
  foo.save
@@ -357,6 +404,9 @@ class DocumentTest < ActiveSupport::TestCase
357
404
 
358
405
  test 'reader :bar is not calling the writer :bar=' do
359
406
  foo = FooBar.new
407
+
408
+ build_save_request(foo)
409
+
360
410
  foo.bar = 'bar'
361
411
  foo.save!
362
412
  foo.expects(:bar=).times(0)
@@ -370,6 +420,9 @@ class DocumentTest < ActiveSupport::TestCase
370
420
 
371
421
  test 'persisted? returns false if _rev is not present' do
372
422
  foo = FooBar.new
423
+
424
+ build_save_request(foo)
425
+
373
426
  assert_equal foo.persisted?, false
374
427
  assert foo.save
375
428
  assert_equal foo.persisted?, true
@@ -377,14 +430,20 @@ class DocumentTest < ActiveSupport::TestCase
377
430
 
378
431
  test 'can save without timestamps' do
379
432
  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
433
+
434
+ stub_request(:put, /http:\/\/localhost:5984\/test\/foo_baz%2F.+/)
435
+ .to_return(body: resp.to_json)
436
+
381
437
  foobaz = FooBaz.new foo: {foo: :bar}
382
438
  assert foobaz.save!
383
439
  end
384
440
 
385
441
  test 'property writes work correctly with pipe equals' do
386
442
  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
443
+
444
+ stub_request(:put, /http:\/\/localhost:5984\/test\/foo_baz%2F.+/).
445
+ to_return(body: resp.to_json)
446
+
388
447
  foobaz = FooBaz.new foo: {'foo' => 'bar'}
389
448
  foobaz.add_to_foo 'bar', 'bar'
390
449
  assert_equal foobaz.foo, {'foo' => 'bar', 'bar' => 'bar'}
@@ -392,25 +451,28 @@ class DocumentTest < ActiveSupport::TestCase
392
451
 
393
452
  test 'default should populate before save' do
394
453
  test_foo = TestFoo.new
395
- assert_equal 'FOO', test_foo.doc['default_test_property']
454
+ assert_equal 'FOO', test_foo.default_test_property
396
455
  end
397
456
 
398
457
  test 'default should be overridden by params' do
399
458
  test_foo = TestFoo.new(default_test_property: 'bar')
400
- assert_equal 'bar', test_foo.doc['default_test_property']
459
+ assert_equal 'bar', test_foo.default_test_property
401
460
  end
402
461
 
403
462
  test 'doc and method and instance var are the same' do
404
463
  test_foo = FooBar.new
405
464
  test_foo.foo = 'test_value'
406
465
  assert_equal 'test_value', test_foo.foo
407
- assert_equal 'test_value', test_foo.doc['foo']
466
+ assert_equal 'test_value', test_foo.send(:doc)[:foo]
408
467
  assert_equal 'test_value', test_foo.instance_variable_get(:@foo)
409
468
  end
410
469
 
411
470
  test 'created at is current time' do
412
471
  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
472
+
473
+ stub_request(:put, /http:\/\/localhost:5984\/test\/with_time%2F.+/).
474
+ to_return(body: resp.to_json)
475
+
414
476
  test = WithTime.new id: "timed"
415
477
  assert test.respond_to?(:created_at)
416
478
  assert test.save
@@ -420,7 +482,8 @@ class DocumentTest < ActiveSupport::TestCase
420
482
  test 'nil default' do
421
483
  properties = {foo: nil, is_nil: nil}
422
484
  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
485
+ stub_request(:put, /http:\/\/localhost:5984\/test\/foo_bar%2F.+/).
486
+ to_return(body: resp.to_json)
424
487
  foo = FooBar.new properties
425
488
  foo.save
426
489
  properties.each do |k, v|
@@ -444,19 +507,21 @@ class DocumentTest < ActiveSupport::TestCase
444
507
 
445
508
  test 'save returns false for invalid document on save' do
446
509
  foo = DocumentWithValidMethod.new
447
- assert_not foo.save
510
+ assert_equal foo.save, false
448
511
  end
449
512
 
450
513
  test 'save succeeds for invalid document if skipping validations' do
451
514
  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
515
+ stub_request(:put, /http:\/\/localhost:5984\/test\/document_with_valid_method%2F.+/).
516
+ to_return(body: resp.to_json)
517
+
453
518
  foo = DocumentWithValidMethod.new
454
519
  assert foo.save(validate: false)
455
520
  end
456
521
 
457
522
  test 'default objects are not the same in memory' do
458
523
  doc_with_same_default = DocWithSameDefaults.new
459
- assert_not doc_with_same_default.foo.equal? doc_with_same_default.bar
524
+ assert_not_same doc_with_same_default.foo, doc_with_same_default.bar
460
525
  doc_with_same_default.foo.push 'foo'
461
526
  assert doc_with_same_default.bar == []
462
527
  end
@@ -473,71 +538,41 @@ class DocumentTest < ActiveSupport::TestCase
473
538
 
474
539
  test 'attach_file! will add a standalone attachment to the document' do
475
540
  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
541
+
542
+ assert stub_request(:put, /http:\/\/localhost:5984\/test\/base_dolly%2F.+/).
543
+ to_return(body: save_response.to_json)
544
+
477
545
  assert doc = BaseDolly.new
478
546
  assert doc.save
479
547
  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
548
+ assert stub_request(:put, /http:\/\/localhost:5984\/test\/base_dolly\/79178957-96ff-40d9-9ecb-217fa35bdea7\/test.txt/)
549
+ .to_return(body: resp.to_json)
550
+
481
551
  assert data = File.open("#{FileUtils.pwd}/test/support/test.txt").read
482
552
  assert doc.attach_file! 'test.txt', 'text/plain', data
483
553
  end
484
554
 
485
555
  test 'attach_file! will add an inline attachment if specified' do
486
556
  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
557
+
558
+ assert stub_request(:put, /http:\/\/localhost:5984\/test\/base_dolly%2F.+/).
559
+ to_return(body: save_response.to_json)
560
+
488
561
  assert doc = BaseDolly.new
489
562
  assert doc.save
490
563
  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
564
+
565
+ assert stub_request(:put, /http:\/\/localhost:5984\/test\/base_dolly\/79178957-96ff-40d9-9ecb-217fa35bdea7\/test.txt/).
566
+ to_return(body: resp.to_json)
567
+
492
568
  assert data = File.open("#{FileUtils.pwd}/test/support/test.txt").read
493
569
  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']
570
+ assert_equal doc.send(:doc)['_attachments']['test.txt']&.empty?, false
571
+ assert_equal Base64.encode64(data), doc.send(:doc)['_attachments']['test.txt']['data']
496
572
  end
497
573
 
498
574
  test "new object from inhereted document" do
499
575
  assert bar = Bar.new(a: 1)
500
576
  assert_equal 1, bar.a
501
577
  end
502
-
503
- private
504
- def generic_response rows, count = 1
505
- {total_rows: count, offset:0, rows: rows}
506
- end
507
-
508
- def build_view_response properties
509
- rows = properties.map.with_index do |v, i|
510
- {
511
- id: "foo_bar/#{i}",
512
- key: "foo_bar",
513
- value: 1,
514
- doc: {_id: "foo_bar/#{i}", _rev: SecureRandom.hex}.merge!(v)
515
- }
516
- end
517
- generic_response rows, properties.count
518
- end
519
-
520
- def build_view_collation_response properties
521
- rows = properties.map.with_index do |v, i|
522
- id = i.zero? ? "foo_bar/#{i}" : "baz/#{i}"
523
- {
524
- id: id,
525
- key: "foo_bar",
526
- value: 1,
527
- doc: {_id: id, _rev: SecureRandom.hex}.merge!(v)
528
- }
529
- end
530
- generic_response rows, properties.count
531
- end
532
-
533
-
534
- 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
537
- end
538
-
539
- def query_base_path
540
- "#{DB_BASE_PATH}/_all_docs"
541
- end
542
-
543
578
  end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ class TypedDoc < Dolly::Document
4
+ typed_model
5
+ end
6
+
7
+ class UntypedDoc < Dolly::Document
8
+ end
9
+
10
+ class DocumentTypeTest < Test::Unit::TestCase
11
+ test 'typed?' do
12
+ assert_equal(TypedDoc.new.typed?, true)
13
+ assert_equal(UntypedDoc.new.typed?, false)
14
+ end
15
+
16
+ test 'typed_model' do
17
+ assert_equal(TypedDoc.new.type, nil)
18
+ assert_equal(UntypedDoc.new.respond_to?(:type), false)
19
+ assert_raise NoMethodError do
20
+ UntypedDoc.new.type
21
+ end
22
+ end
23
+
24
+ test 'set_type' do
25
+ assert_equal(TypedDoc.new.set_type, TypedDoc.name_paramitized)
26
+ assert_equal(UntypedDoc.new.set_type, nil)
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Configure sensitive parameters which will be filtered from the log file.
4
+ Rails.application.config.filter_parameters += [:password]