dolly 1.1.5 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +78 -0
  3. data/lib/dolly.rb +2 -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 +32 -65
  8. data/lib/dolly/configuration.rb +35 -10
  9. data/lib/dolly/connection.rb +86 -22
  10. data/lib/dolly/depracated_database.rb +24 -0
  11. data/lib/dolly/document.rb +48 -198
  12. data/lib/dolly/document_creation.rb +20 -0
  13. data/lib/dolly/document_state.rb +66 -0
  14. data/lib/dolly/document_type.rb +47 -0
  15. data/lib/dolly/exceptions.rb +32 -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 +58 -47
  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 -105
  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 → railties}/railtie.rb +2 -1
  31. data/lib/refinements/hash_refinements.rb +27 -0
  32. data/lib/refinements/string_refinements.rb +28 -0
  33. data/lib/tasks/db.rake +27 -4
  34. data/test/bulk_document_test.rb +8 -5
  35. data/test/document_test.rb +132 -95
  36. data/test/document_type_test.rb +28 -0
  37. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  38. data/test/dummy/log/test.log +55435 -33484
  39. data/test/inheritance_test.rb +23 -0
  40. data/test/mango_index_test.rb +64 -0
  41. data/test/mango_test.rb +273 -0
  42. data/test/property_manager_test.rb +18 -0
  43. data/test/test_helper.rb +63 -18
  44. data/test/view_query_test.rb +27 -0
  45. metadata +66 -138
  46. data/Rakefile +0 -11
  47. data/lib/dolly/bulk_error.rb +0 -16
  48. data/lib/dolly/db_config.rb +0 -20
  49. data/lib/dolly/interpreter.rb +0 -5
  50. data/lib/dolly/logger.rb +0 -9
  51. data/lib/dolly/name_space.rb +0 -28
  52. data/lib/dolly/timestamps.rb +0 -21
  53. data/lib/exceptions/dolly.rb +0 -38
  54. data/test/collection_test.rb +0 -59
  55. data/test/configuration_test.rb +0 -9
  56. data/test/dummy/README.rdoc +0 -28
  57. data/test/dummy/Rakefile +0 -6
  58. data/test/dummy/app/assets/javascripts/application.js +0 -13
  59. data/test/dummy/app/assets/stylesheets/application.css +0 -13
  60. data/test/dummy/app/controllers/application_controller.rb +0 -5
  61. data/test/dummy/app/helpers/application_helper.rb +0 -2
  62. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  63. data/test/dummy/bin/bundle +0 -3
  64. data/test/dummy/bin/rails +0 -4
  65. data/test/dummy/bin/rake +0 -4
  66. data/test/dummy/config.ru +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/lib/couch_rest_adapter/railtie.rb +0 -10
  83. data/test/dummy/public/404.html +0 -58
  84. data/test/dummy/public/422.html +0 -58
  85. data/test/dummy/public/500.html +0 -57
  86. data/test/dummy/public/favicon.ico +0 -0
  87. data/test/factories/factories.rb +0 -8
data/lib/dolly/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dolly
2
- VERSION = "1.1.5"
2
+ VERSION = "3.1.0"
3
3
  end
@@ -0,0 +1,21 @@
1
+ require 'dolly/class_methods_delegation'
2
+
3
+ module Dolly
4
+ module ViewQuery
5
+ def raw_view(design, view_name, opts = {})
6
+ design = "_design/#{design}/_view/#{view_name}"
7
+ connection.view(design, opts)
8
+ end
9
+
10
+ def view_value(doc, view_name, opts = {})
11
+ raw_view(doc, view_name, opts)[:rows].flat_map { |result| result[:value] }
12
+ end
13
+
14
+ def collection_view(design, view_name, opts = {})
15
+ opts.delete(:include_docs)
16
+ design = "_design/#{design}/_view/#{view_name}"
17
+ response = connection.view(design, opts)
18
+ Dolly::Collection.new(rows: response, options: opts)
19
+ end
20
+ end
21
+ 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,27 @@
1
+ module HashRefinements
2
+ refine Hash do
3
+ # File activesupport/lib/active_support/core_ext/hash/keys.rb, line 82
4
+ def deep_transform_keys(&block)
5
+ _deep_transform_keys_in_object(self, &block)
6
+ end
7
+
8
+ def slice(*keys)
9
+ keys.each_with_object(Hash.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
10
+ end
11
+
12
+ private
13
+
14
+ def _deep_transform_keys_in_object(object, &block)
15
+ case object
16
+ when Hash
17
+ object.each_with_object({}) do |(key, value), result|
18
+ result[yield(key)] = _deep_transform_keys_in_object(value, &block)
19
+ end
20
+ when Array
21
+ object.map { |e| _deep_transform_keys_in_object(e, &block) }
22
+ else
23
+ object
24
+ end
25
+ end
26
+ end
27
+ end
@@ -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
data/lib/tasks/db.rake CHANGED
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  namespace :db do
2
4
  desc "Will create if missing database and add default views"
3
5
  task setup: :environment do
4
- Dolly::Document.database.put "", nil
6
+ Dolly::Document.connection.put '', {}
5
7
  puts "Database created"
6
8
  end
7
9
 
@@ -38,9 +40,9 @@ namespace :db do
38
40
  view_doc.merge!( '_id' => design_doc_name, 'language' => 'coffeescript')
39
41
 
40
42
  begin
41
- hash_doc = JSON::parse Dolly::Document.database.get(view_doc["_id"]).parsed_response
43
+ hash_doc = Dolly::Document.connection.request(:get, view_doc["_id"])
42
44
 
43
- rev = hash_doc.delete('_rev')
45
+ rev = hash_doc.delete(:_rev)
44
46
 
45
47
  if hash_doc == view_doc
46
48
  puts 'everything up to date'
@@ -55,8 +57,29 @@ namespace :db do
55
57
  will_save = true
56
58
  end
57
59
 
58
- Dolly::Document.database.put design_doc_name, view_doc.to_json if will_save
60
+ Dolly::Document.connection.request :put, design_doc_name, view_doc if will_save
59
61
  end
60
62
  end
61
63
 
64
+ namespace :index do
65
+ desc 'Creates indexes for mango querys located in db/indexes/*.json'
66
+ task create: :environment do
67
+ indexes_dir = Rails.root.join('db', 'indexes')
68
+ files = Dir.glob File.join(indexes_dir, '**', '*.json')
69
+
70
+ files.each do |file|
71
+ index_data = JSON.parse(File.read(file))
72
+ database = index_data.fetch('db', 'default').to_sym
73
+ puts "*" * 100
74
+ puts "Creating index: #{index_data["name"]} for database: #{database}"
75
+
76
+ if database == Dolly::Connection::DEFAULT_DATABASE
77
+ puts Dolly::MangoIndex.create(index_data['name'], index_data['fields'])
78
+ else
79
+ puts Dolly::MangoIndex.create_in_database(database, index_data['name'], index_data['fields'])
80
+ end
81
+ end
82
+ end
83
+ end
62
84
  end
85
+
@@ -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
@@ -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,24 +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
- skip "delete should be able to do soft deletion."
305
+ assert doc = FooBar.find("1")
306
+ build_save_request(doc)
307
+ assert doc.destroy(false)
308
+ assert_equal true, doc.send(:doc)[:_deleted]
268
309
  end
269
310
 
270
311
  test 'query custom view' do
271
- 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
+
272
315
  f = FooBar.find_with "test", "custom_view", key: 1
273
316
  assert_equal 2, f.count
274
317
  f.each{ |d| assert d.kind_of?(FooBar) }
275
318
  end
276
319
 
277
320
  test 'query custom view collation' do
278
- 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
+
279
324
  f = FooBar.find_with "test", "custom_view", { startkey: [1], endkey: [1, {}]}
280
325
  assert_equal 2, f.count
281
326
  assert f.first.kind_of?(FooBar)
@@ -326,7 +371,7 @@ class DocumentTest < ActiveSupport::TestCase
326
371
  end
327
372
 
328
373
  test 'update document propertys with bang' do
329
- foo = FooBar.new 'id' => 'a', foo: 'ab', bar: 'ba'
374
+ foo = FooBar.new 'id'=> 'a', foo: 'ab', bar: 'ba'
330
375
  foo.expects(:save).once
331
376
  foo.update_properties! foo: 'c'
332
377
  end
@@ -340,13 +385,17 @@ class DocumentTest < ActiveSupport::TestCase
340
385
 
341
386
  test 'set updated at' do
342
387
  foo = FooBar.new 'id' => 'a', foo: 'ab'
388
+ build_save_request foo
343
389
  foo.update_properties! foo: 'c'
344
390
  assert_equal Time.now.to_s, foo.updated_at.to_s
345
391
  end
346
392
 
347
393
  test 'created at is set' do
348
394
  resp = {ok: true, id: "foo_bar/1", rev: "FF0000"}
349
- 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
+
350
399
  properties = {foo: 1, bar: 2, boolean: false}
351
400
  foo = FooBar.new properties
352
401
  foo.save
@@ -355,6 +404,9 @@ class DocumentTest < ActiveSupport::TestCase
355
404
 
356
405
  test 'reader :bar is not calling the writer :bar=' do
357
406
  foo = FooBar.new
407
+
408
+ build_save_request(foo)
409
+
358
410
  foo.bar = 'bar'
359
411
  foo.save!
360
412
  foo.expects(:bar=).times(0)
@@ -368,6 +420,9 @@ class DocumentTest < ActiveSupport::TestCase
368
420
 
369
421
  test 'persisted? returns false if _rev is not present' do
370
422
  foo = FooBar.new
423
+
424
+ build_save_request(foo)
425
+
371
426
  assert_equal foo.persisted?, false
372
427
  assert foo.save
373
428
  assert_equal foo.persisted?, true
@@ -375,14 +430,20 @@ class DocumentTest < ActiveSupport::TestCase
375
430
 
376
431
  test 'can save without timestamps' do
377
432
  resp = {ok: true, id: "foo_bar/1", rev: "FF0000"}
378
- 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
+
379
437
  foobaz = FooBaz.new foo: {foo: :bar}
380
438
  assert foobaz.save!
381
439
  end
382
440
 
383
441
  test 'property writes work correctly with pipe equals' do
384
442
  resp = {ok: true, id: "foo_bar/1", rev: "FF0000"}
385
- 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
+
386
447
  foobaz = FooBaz.new foo: {'foo' => 'bar'}
387
448
  foobaz.add_to_foo 'bar', 'bar'
388
449
  assert_equal foobaz.foo, {'foo' => 'bar', 'bar' => 'bar'}
@@ -390,25 +451,28 @@ class DocumentTest < ActiveSupport::TestCase
390
451
 
391
452
  test 'default should populate before save' do
392
453
  test_foo = TestFoo.new
393
- assert_equal 'FOO', test_foo.doc['default_test_property']
454
+ assert_equal 'FOO', test_foo.default_test_property
394
455
  end
395
456
 
396
457
  test 'default should be overridden by params' do
397
458
  test_foo = TestFoo.new(default_test_property: 'bar')
398
- assert_equal 'bar', test_foo.doc['default_test_property']
459
+ assert_equal 'bar', test_foo.default_test_property
399
460
  end
400
461
 
401
462
  test 'doc and method and instance var are the same' do
402
463
  test_foo = FooBar.new
403
464
  test_foo.foo = 'test_value'
404
465
  assert_equal 'test_value', test_foo.foo
405
- assert_equal 'test_value', test_foo.doc['foo']
466
+ assert_equal 'test_value', test_foo.send(:doc)[:foo]
406
467
  assert_equal 'test_value', test_foo.instance_variable_get(:@foo)
407
468
  end
408
469
 
409
470
  test 'created at is current time' do
410
471
  resp = {ok: true, id: "with_time/timed", rev: "FF0000"}
411
- 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
+
412
476
  test = WithTime.new id: "timed"
413
477
  assert test.respond_to?(:created_at)
414
478
  assert test.save
@@ -418,7 +482,8 @@ class DocumentTest < ActiveSupport::TestCase
418
482
  test 'nil default' do
419
483
  properties = {foo: nil, is_nil: nil}
420
484
  resp = {ok: true, id: "foo_bar/1", rev: "FF0000"}
421
- 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)
422
487
  foo = FooBar.new properties
423
488
  foo.save
424
489
  properties.each do |k, v|
@@ -442,19 +507,21 @@ class DocumentTest < ActiveSupport::TestCase
442
507
 
443
508
  test 'save returns false for invalid document on save' do
444
509
  foo = DocumentWithValidMethod.new
445
- assert_not foo.save
510
+ assert_equal foo.save, false
446
511
  end
447
512
 
448
513
  test 'save succeeds for invalid document if skipping validations' do
449
514
  resp = {ok: true, id: "document_with_valid_method/1", rev: "FF0000"}
450
- 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
+
451
518
  foo = DocumentWithValidMethod.new
452
519
  assert foo.save(validate: false)
453
520
  end
454
521
 
455
522
  test 'default objects are not the same in memory' do
456
523
  doc_with_same_default = DocWithSameDefaults.new
457
- 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
458
525
  doc_with_same_default.foo.push 'foo'
459
526
  assert doc_with_same_default.bar == []
460
527
  end
@@ -471,71 +538,41 @@ class DocumentTest < ActiveSupport::TestCase
471
538
 
472
539
  test 'attach_file! will add a standalone attachment to the document' do
473
540
  assert save_response = {ok: true, id: "base_dolly/79178957-96ff-40d9-9ecb-217fa35bdea7", rev: "1"}
474
- 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
+
475
545
  assert doc = BaseDolly.new
476
546
  assert doc.save
477
547
  assert resp = {ok: true, id: '79178957-96ff-40d9-9ecb-217fa35bdea7', rev: '2'}
478
- 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
+
479
551
  assert data = File.open("#{FileUtils.pwd}/test/support/test.txt").read
480
552
  assert doc.attach_file! 'test.txt', 'text/plain', data
481
553
  end
482
554
 
483
555
  test 'attach_file! will add an inline attachment if specified' do
484
556
  assert save_response = {ok: true, id: "base_dolly/79178957-96ff-40d9-9ecb-217fa35bdea7", rev: "1"}
485
- 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
+
486
561
  assert doc = BaseDolly.new
487
562
  assert doc.save
488
563
  assert resp = {ok: true, id: '79178957-96ff-40d9-9ecb-217fa35bdea7', rev: '2'}
489
- 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
+
490
568
  assert data = File.open("#{FileUtils.pwd}/test/support/test.txt").read
491
569
  assert doc.attach_file! 'test.txt', 'text/plain', data, inline: true
492
- assert doc.doc['_attachments']['test.txt'].present?
493
- 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']
494
572
  end
495
573
 
496
574
  test "new object from inhereted document" do
497
575
  assert bar = Bar.new(a: 1)
498
576
  assert_equal 1, bar.a
499
577
  end
500
-
501
- private
502
- def generic_response rows, count = 1
503
- {total_rows: count, offset:0, rows: rows}
504
- end
505
-
506
- def build_view_response properties
507
- rows = properties.map.with_index do |v, i|
508
- {
509
- id: "foo_bar/#{i}",
510
- key: "foo_bar",
511
- value: 1,
512
- doc: {_id: "foo_bar/#{i}", _rev: SecureRandom.hex}.merge!(v)
513
- }
514
- end
515
- generic_response rows, properties.count
516
- end
517
-
518
- def build_view_collation_response properties
519
- rows = properties.map.with_index do |v, i|
520
- id = i.zero? ? "foo_bar/#{i}" : "baz/#{i}"
521
- {
522
- id: id,
523
- key: "foo_bar",
524
- value: 1,
525
- doc: {_id: id, _rev: SecureRandom.hex}.merge!(v)
526
- }
527
- end
528
- generic_response rows, properties.count
529
- end
530
-
531
-
532
- def build_request keys, body, view_name = 'foo_bar'
533
- query = "keys=#{CGI::escape keys.to_s.gsub(' ','')}&" unless keys.blank?
534
- FakeWeb.register_uri :get, "#{query_base_path}?#{query.to_s}include_docs=true", body: body.to_json
535
- end
536
-
537
- def query_base_path
538
- "#{DB_BASE_PATH}/_all_docs"
539
- end
540
-
541
578
  end