dolly 0.7.3 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4aebf024e5e48154c3725af48715816d015b6444
4
- data.tar.gz: e1a4d17548ecdd9e9d4cceb9c482ba77de1ba983
3
+ metadata.gz: 56886a385508910eeaa3205270cca2b4d1576930
4
+ data.tar.gz: 2f0e1bb676c10ac81943456ddf81a4355d487698
5
5
  SHA512:
6
- metadata.gz: 2262642fce24061d8141d160be3aec870a8cf101931bb0bf6371b3e08d5e590ddb80121d0cac6246f1530ac5a175557d9c343e36066ad1bb7eca6cc086b425ea
7
- data.tar.gz: 98160dca126b53ca2c3840719463747d29067985777445149744fb003720842428a1026f66e1ccab3dd80cc5c03b4687e1ba1c53524d70009f1caaf7a00af6aa
6
+ metadata.gz: 87161563ce0d86a810f216b73834071f1836d0ae3394066318ee80f01517353fb3390c8a0f31beeb047ed64c15dde9551ecd327d3179628b91d2e72629328f62
7
+ data.tar.gz: 7bde8a8ec5b1bf9eb59ddb25ac12c90151dbfdfba287f7f70bab1e43697b68dd3b6426c9ddeeac02ec8db0ae44fb3737085fbc596c04d6e45c25b127ec03c9dd
@@ -48,7 +48,8 @@ module Dolly
48
48
  properties = r['doc']
49
49
  id = properties.delete '_id'
50
50
  rev = properties.delete '_rev' if properties['_rev']
51
- document = docs_class.new properties
51
+ doc_class = doc_class id
52
+ document = doc_class.new properties
52
53
  document.doc = properties.merge({'_id' => id, '_rev' => rev})
53
54
  @set << document
54
55
  end
@@ -70,6 +71,15 @@ module Dolly
70
71
  @docs_class
71
72
  end
72
73
 
74
+ def doc_class id
75
+ # TODO: We need to improve and document the way we return
76
+ # multiple types when querying from a class, as it might
77
+ # be confusing. We *could* also get dolly to parse the result
78
+ # before sending it back to the client.
79
+ doc_class = id[/^[a-z_]+/].camelize.constantize
80
+ docs_class == doc_class ? docs_class : doc_class
81
+ end
82
+
73
83
  def json
74
84
  @json
75
85
  end
@@ -7,7 +7,7 @@ module Dolly
7
7
  @class_name = opts.delete(:class_name) if opts.present?
8
8
  @name = opts.delete(:name).to_s
9
9
  @default = opts.delete(:default)
10
- warn 'There are some unprocesed options' if opts.present?
10
+ warn 'There are some unprocessed options!' if opts.present?
11
11
  end
12
12
 
13
13
  def value
@@ -41,6 +41,18 @@ module Dolly
41
41
  @value.to_f
42
42
  end
43
43
 
44
+ def date_value
45
+ @value.to_date
46
+ end
47
+
48
+ def time_value
49
+ @value.to_time
50
+ end
51
+
52
+ def date_time_value
53
+ @value.to_datetime
54
+ end
55
+
44
56
  def true_class_value
45
57
  truthy_value?
46
58
  end
data/lib/dolly/query.rb CHANGED
@@ -29,7 +29,7 @@ module Dolly
29
29
  end
30
30
 
31
31
  def default_query_args
32
- {startkey: "#{name_paramitized}/", endkey: "#{name_paramitized}/{}"}
32
+ {startkey: "#{name_paramitized}/", endkey: "#{name_paramitized}/\ufff0"}
33
33
  end
34
34
 
35
35
  def all
data/lib/dolly/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dolly
2
- VERSION = "0.7.3"
2
+ VERSION = "0.7.5"
3
3
  end
@@ -4,10 +4,15 @@ class FooBar < Dolly::Document
4
4
  property :foo, :bar
5
5
  property :with_default, default: 1
6
6
  property :boolean, class_name: TrueClass, default: true
7
+ property :date, class_name: Date
8
+ property :time, class_name: Time
9
+ property :datetime, class_name: DateTime
7
10
 
8
11
  timestamps!
9
12
  end
10
13
 
14
+ class Baz < Dolly::Document; end
15
+
11
16
  class DocumentTest < ActiveSupport::TestCase
12
17
  DB_BASE_PATH = "http://localhost:5984/test".freeze
13
18
 
@@ -20,17 +25,18 @@ class DocumentTest < ActiveSupport::TestCase
20
25
  empty_resp = build_view_response []
21
26
  not_found_resp = generic_response [{ key: "foo_bar/2", error: "not_found" }]
22
27
  @multi_resp = build_view_response all_docs
28
+ @multi_type_resp = build_view_collation_response all_docs
23
29
 
24
30
  build_request [["foo_bar","1"]], view_resp
25
31
  build_request [["foo_bar","2"]], empty_resp
26
32
  build_request [["foo_bar","1"],["foo_bar","2"]], @multi_resp
27
33
 
28
34
  #TODO: Mock Dolly::Request to return helper with expected response. request builder can be tested by itself.
29
- FakeWeb.register_uri :get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%2F%7B%7D%22&include_docs=true", body: @multi_resp.to_json
30
- FakeWeb.register_uri :get, "#{query_base_path}?startkey=%22foo_bar%2F%22&endkey=%22foo_bar%2F%7B%7D%22&limit=1&include_docs=true", body: view_resp.to_json
31
- FakeWeb.register_uri :get, "#{query_base_path}?endkey=%22foo_bar%2F%22&startkey=%22foo_bar%2F%7B%7D%22&limit=1&descending=true&include_docs=true", body: view_resp.to_json
35
+ 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
36
+ 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
37
+ 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
32
38
  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
33
- FakeWeb.register_uri :get, "#{query_base_path}?endkey=%22foo_bar%2F%22&startkey=%22foo_bar%2F%7B%7D%22&limit=2&descending=true&include_docs=true", body: @multi_resp.to_json
39
+ 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
34
40
  FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2F1%22%5D&include_docs=true", body: view_resp.to_json
35
41
  FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%5D&include_docs=true", body: not_found_resp.to_json
36
42
  FakeWeb.register_uri :get, "#{query_base_path}?keys=%5B%22foo_bar%2Ferror%22%5D&include_docs=true", body: 'error', status: ["500", "Error"]
@@ -88,6 +94,24 @@ class DocumentTest < ActiveSupport::TestCase
88
94
  assert_equal false, foo.boolean?
89
95
  end
90
96
 
97
+ test 'will have object with Date method' do
98
+ foo = FooBar.find 1
99
+ foo.date = Date.today
100
+ assert_equal true, foo.date.is_a?(Date)
101
+ end
102
+
103
+ test 'will have object with Time method' do
104
+ foo = FooBar.find 1
105
+ foo.time = Time.now
106
+ assert_equal true, foo.time.is_a?(Time)
107
+ end
108
+
109
+ test 'will have object with DateTime method' do
110
+ foo = FooBar.find 1
111
+ foo.datetime = DateTime.now
112
+ assert_equal true, foo.datetime.is_a?(DateTime)
113
+ end
114
+
91
115
  test 'find will get a FooBar document' do
92
116
  foo = FooBar.find "1"
93
117
  assert_equal true, foo.kind_of?(FooBar)
@@ -202,6 +226,14 @@ class DocumentTest < ActiveSupport::TestCase
202
226
  f.each{ |d| assert d.kind_of?(FooBar) }
203
227
  end
204
228
 
229
+ test 'query custom view collation' do
230
+ 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
231
+ f = FooBar.find_with "test", "custom_view", { startkey: [1], endkey: [1, {}]}
232
+ assert_equal 2, f.count
233
+ assert f.first.kind_of?(FooBar)
234
+ assert f.last.kind_of?(Baz)
235
+ end
236
+
205
237
  test 'new document have id' do
206
238
  foo = FooBar.new
207
239
  assert_equal 0, (foo.id =~ /^foo_bar\/[abcdef0-9]+/i)
@@ -238,6 +270,14 @@ class DocumentTest < ActiveSupport::TestCase
238
270
  assert foo.id.match(uuid)
239
271
  end
240
272
 
273
+ test 'reader :bar is not calling the writer :bar=' do
274
+ foo = FooBar.new
275
+ foo.bar = 'bar'
276
+ foo.save!
277
+ foo.expects(:bar=).times(0)
278
+ assert_equal 'bar', foo.bar
279
+ end
280
+
241
281
  private
242
282
  def generic_response rows, count = 1
243
283
  {total_rows: count, offset:0, rows: rows}
@@ -255,6 +295,20 @@ class DocumentTest < ActiveSupport::TestCase
255
295
  generic_response rows, properties.count
256
296
  end
257
297
 
298
+ def build_view_collation_response properties
299
+ rows = properties.map.with_index do |v, i|
300
+ id = i.zero? ? "foo_bar/#{i}" : "baz/#{i}"
301
+ {
302
+ id: id,
303
+ key: "foo_bar",
304
+ value: 1,
305
+ doc: {_id: id, _rev: SecureRandom.hex}.merge!(v)
306
+ }
307
+ end
308
+ generic_response rows, properties.count
309
+ end
310
+
311
+
258
312
  def build_request keys, body, view_name = 'foo_bar'
259
313
  query = "keys=#{CGI::escape keys.to_s.gsub(' ','')}&" unless keys.blank?
260
314
  FakeWeb.register_uri :get, "#{query_base_path}?#{query.to_s}include_docs=true", body: body.to_json
@@ -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]