lotus-rethinkdb 0.1.1 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 737a2817a5f539d7027d688b4bc510037206d0c5
4
- data.tar.gz: a118133ad334d38dd49b3cb505aa596e92af4ded
3
+ metadata.gz: 7dd4f3f3377f4a03062e673ee934296589c1ff1e
4
+ data.tar.gz: dcedbcd89c9aa0685e8d2a2c181006099c7057a2
5
5
  SHA512:
6
- metadata.gz: eaebeddd187f711f8ac12cdfd88637543978e0297590bc2e90b343f8bddb3acdd2f5ced130911ba16b6e7ae45af5caf79bf9f986e6a9cdd43ceb3b4224d08b95
7
- data.tar.gz: 5c235fe25c5c2a977f200e2877e3607caa2ad8585be679f669a5f187bd97404aa6fa35d0445dce415e42796b74e8a73a3b3d2e159e00c7e2e8691143c16209c6
6
+ metadata.gz: 643f2c1c4600ace72f144c2ff8788e27c21a069618e2cabede36988047278fb642e2bf3c1993a79deb473aaaddb36ab96b209c824c77a269d7db59ecd5bb1cce
7
+ data.tar.gz: 20455fed86bdb86691d7568b06db69fb82f8228f925374ee6c90dc8e06a485695c44f5f790ef34c0d7ada708d7d6e8481d281bd4599e58643cc5859ecc05486e
data/.travis.yml CHANGED
@@ -10,7 +10,10 @@ rvm:
10
10
  - 2.1.5
11
11
  - 2.2.0
12
12
 
13
- services: rethinkdb
14
-
15
- before_script:
16
- - sudo add-apt-repository ppa:rethinkdb/ppa && sudo apt-get update && sudo apt-get install rethinkdb
13
+ before_install:
14
+ - source /etc/lsb-release && echo "deb http://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list
15
+ - wget -qO- http://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add -
16
+ - sudo apt-get update -q
17
+ - sudo apt-get install rethinkdb
18
+ - sudo cp /etc/rethinkdb/default.conf.sample /etc/rethinkdb/instances.d/instance1.conf
19
+ - sudo service rethinkdb restart
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in lotus-rethinkdb.gemspec
4
2
  gemspec
3
+
4
+ gem 'coveralls', require: false
data/README.md CHANGED
@@ -1,6 +1,19 @@
1
1
  # Lotus::Model RethinkDB Adapter
2
2
 
3
- This adapter implements persistence layer for RethinkDB. Under development.
3
+ This adapter implements a [Lotus::Model](https://github.com/lotus/model) persistence layer for [RethinkDB](http://rethinkdb.com).
4
+
5
+ ## Status
6
+
7
+ [![Gem Version](https://badge.fury.io/rb/lotus-rethinkdb.svg)](http://badge.fury.io/rb/lotus-rethinkdb)
8
+ [![Build Status](https://secure.travis-ci.org/angeloashmore/lotus-rethinkdb.svg?branch=master)](http://travis-ci.org/angeloashmore/lotus-rethinkdb?branch=master)
9
+ [![Coverage Status](https://img.shields.io/coveralls/angeloashmore/lotus-rethinkdb.svg)](https://coveralls.io/r/angeloashmore/lotus-rethinkdb)
10
+ [![Code Climate](https://codeclimate.com/github/angeloashmore/lotus-rethinkdb/badges/gpa.svg)](https://codeclimate.com/github/angeloashmore/lotus-rethinkdb)
11
+ [![Inline docs](http://inch-ci.org/github/angeloashmore/lotus-rethinkdb.svg?branch=master&style=flat)](http://inch-ci.org/github/angeloashmore/lotus-rethinkdb)
12
+
13
+ ## Links
14
+
15
+ * API Doc: [http://rdoc.info/gems/lotus-rethinkdb](http://rdoc.info/gems/lotus-rethinkdb)
16
+ * Bugs/Issues: [https://github.com/angeloashmore/lotus-rethinkdb/issues](https://github.com/angeloashmore/lotus-rethinkdb/issues)
4
17
 
5
18
  ## Installation
6
19
 
@@ -20,7 +33,36 @@ Or install it yourself as:
20
33
 
21
34
  ## Usage
22
35
 
23
- TODO: Write usage instructions here
36
+ Please refer to the [Lotus::Model](https://github.com/lotus/model#usage) docs for any details related to Entity, Repository, Data Mapper and Adapter.
37
+
38
+ ### Repository methods
39
+
40
+ See the [complete list](https://github.com/lotus/model#repositories) of Repository methods provided by ```Lotus::Model```.
41
+
42
+ Following methods are not supported since it's incompatible with RethinkDB:
43
+
44
+ * first
45
+ * last
46
+
47
+ ### Query methods
48
+
49
+ Generic query methods supported by the RethinkDB adapter:
50
+
51
+ * [all](http://rubydoc.info/gems/lotus-rethinkdb/Lotus/Model/Adapters/Rethinkdb/Query#all-instance_method)
52
+ * [where](http://rubydoc.info/gems/lotus-rethinkdb/Lotus/Model/Adapters/Rethinkdb/Query#where-instance_method)
53
+ * [limit](http://rubydoc.info/gems/lotus-rethinkdb/Lotus/Model/Adapters/Rethinkdb/Query#limit-instance_method)
54
+ * [order](http://rubydoc.info/gems/lotus-rethinkdb/Lotus/Model/Adapters/Rethinkdb/Query#order-instance_method) (alias: ```asc```)
55
+ * [desc](http://rubydoc.info/gems/lotus-rethinkdb/Lotus/Model/Adapters/Rethinkdb/Query#desc-instance_method)
56
+ * [sum](http://rubydoc.info/gems/lotus-rethinkdb/Lotus/Model/Adapters/Rethinkdb/Query#dum-instance_method)
57
+ * [average](http://rubydoc.info/gems/lotus-rethinkdb/Lotus/Model/Adapters/Rethinkdb/Query#average-instance_method) (alias: ```avg```)
58
+ * [max](http://rubydoc.info/gems/lotus-rethinkdb/Lotus/Model/Adapters/Rethinkdb/Query#max-instance_method)
59
+ * [min](http://rubydoc.info/gems/lotus-rethinkdb/Lotus/Model/Adapters/Rethinkdb/Query#min-instance_method)
60
+ * [count](http://rubydoc.info/gems/lotus-rethinkdb/Lotus/Model/Adapters/Rethinkdb/Query#count-instance_method)
61
+
62
+ RethinkDB-specific methods:
63
+
64
+ * [pluck](http://rubydoc.info/gems/lotus-rethinkdb/Lotus/Model/Adapters/Rethinkdb/Query#pluck-instance_method)
65
+ * [has_fields](http://rubydoc.info/gems/lotus-rethinkdb/Lotus/Model/Adapters/Rethinkdb/Query#has_fields-instance_method)
24
66
 
25
67
  ## Contributing
26
68
 
@@ -105,6 +105,21 @@ module Lotus
105
105
  _collection(super, @mapped_collection)
106
106
  end
107
107
 
108
+ # Filters the current scope with a `has_fields` directive.
109
+ #
110
+ # @param args [Array] the array of arguments
111
+ #
112
+ # @see Lotus::Model::Adapters::Rethinkdb::Query#has_fields
113
+ #
114
+ # @return [Lotus::Model::Adapters::Rethinkdb::Collection] the filtered
115
+ # collection
116
+ #
117
+ # @api private
118
+ # @since 0.1.0
119
+ def has_fields(*args) # rubocop:disable Style/PredicateName
120
+ _collection(super, @mapped_collection)
121
+ end
122
+
108
123
  # Filters the current scope with a `limit` directive.
109
124
  #
110
125
  # @param args [Array] the array of arguments
@@ -136,6 +151,86 @@ module Lotus
136
151
  _collection(super, @mapped_collection)
137
152
  end
138
153
 
154
+ # Returns the sum of the values for the given field.
155
+ #
156
+ # @param args [Array] the array of arguments
157
+ #
158
+ # @see Lotus::Model::Adapters::Rethinkdb::Query#sum
159
+ #
160
+ # @return [Numeric]
161
+ #
162
+ # @api private
163
+ # @since 0.1.0
164
+ def sum(*args)
165
+ _run do
166
+ super
167
+ end
168
+ end
169
+
170
+ # Returns the average of the values for the given column.
171
+ #
172
+ # @param args [Array] the array of arguments
173
+ #
174
+ # @see Lotus::Model::Adapters::Rethinkdb::Query#avg
175
+ #
176
+ # @return [Numeric]
177
+ #
178
+ # @api private
179
+ # @since 0.1.0
180
+ def avg(*args)
181
+ _run do
182
+ super.default(nil)
183
+ end
184
+ end
185
+
186
+ # Returns the maximum value for the given field.
187
+ #
188
+ # @param args [Array] the array of arguments
189
+ #
190
+ # @see Lotus::Model::Adapters::Rethinkdb::Query#max
191
+ #
192
+ # @return [Numeric]
193
+ #
194
+ # @api private
195
+ # @since 0.1.0
196
+ def max(field, *args)
197
+ _run do
198
+ super[field].default(nil)
199
+ end
200
+ end
201
+
202
+ # Returns the minimum value for the given field.
203
+ #
204
+ # @param args [Array] the array of arguments
205
+ #
206
+ # @see Lotus::Model::Adapters::Rethinkdb::Query#min
207
+ #
208
+ # @return [Numeric]
209
+ #
210
+ # @api private
211
+ # @since 0.1.0
212
+ def min(field, *args)
213
+ _run do
214
+ super[field].default(nil)
215
+ end
216
+ end
217
+
218
+ # Returns a count of the documents for the current conditions.
219
+ #
220
+ # @param args [Array] the array of arguments
221
+ #
222
+ # @see Lotus::Model::Adapters::Rethinkdb::Query#count
223
+ #
224
+ # @return [Numeric]
225
+ #
226
+ # @api private
227
+ # @since 0.1.0
228
+ def count
229
+ _run do
230
+ super
231
+ end
232
+ end
233
+
139
234
  # Resolves self by fetching the documents from the database and
140
235
  # translating them into entities.
141
236
  #
@@ -132,6 +132,30 @@ module Lotus
132
132
  self
133
133
  end
134
134
 
135
+ # Only include documents with the given fields.
136
+ #
137
+ # @param fields [Array<Symbol>]
138
+ #
139
+ # @return self
140
+ #
141
+ # @since 0.1.0
142
+ #
143
+ # @example Single column
144
+ #
145
+ # query.has_fields(:name)
146
+ #
147
+ # # => r.has_fields(:name)
148
+ #
149
+ # @example Multiple columns
150
+ #
151
+ # query.has_fields(:name, :year)
152
+ #
153
+ # # => r.has_fields(:name, :year)
154
+ def has_fields(*fields) # rubocop:disable Style/PredicateName
155
+ conditions.push([:has_fields, *fields])
156
+ self
157
+ end
158
+
135
159
  # Limit the number of documents to return.
136
160
  #
137
161
  # This operation is performed at the database level with r.limit().
@@ -229,14 +253,112 @@ module Lotus
229
253
  #
230
254
  # @example Mixed fields and index
231
255
  #
232
- # query.desc(r.desc(:name), r.desc(:year), index: r.desc(:date))
256
+ # query.desc(:name, :year, { index: r.desc(:date) })
233
257
  #
234
- # # => r.order_by(:name, :year, index: :date)
258
+ # # => r.order_by(r.desc(:name), r.desc(:year), { index:
259
+ # r.desc(:date) })
235
260
  def desc(*fields)
236
261
  conditions.push([:order_by, *_desc_wrapper(*fields)])
237
262
  self
238
263
  end
239
264
 
265
+ # Returns the sum of the values for the given field.
266
+ #
267
+ # @param field [Symbol] the field name
268
+ #
269
+ # @return [Numeric]
270
+ #
271
+ # @since 0.1.0
272
+ #
273
+ # @example
274
+ #
275
+ # query.sum(:comments_count)
276
+ #
277
+ # # => r.sum(:comments_count)
278
+ def sum(field)
279
+ scoped.sum(field)
280
+ end
281
+
282
+ # Returns the average of the values for the given field.
283
+ #
284
+ # @param field [Symbol] the column name
285
+ #
286
+ # @return [Numeric]
287
+ #
288
+ # @since 0.1.0
289
+ #
290
+ # @example
291
+ #
292
+ # query.average(:comments_count)
293
+ #
294
+ # # => r.avg(:comments_count)
295
+ def average(field)
296
+ scoped.avg(field)
297
+ end
298
+
299
+ alias_method :avg, :average
300
+
301
+ # Returns the maximum value for the given field.
302
+ #
303
+ # @param field [Symbol] the field name
304
+ #
305
+ # @return result
306
+ #
307
+ # @since 0.1.0
308
+ #
309
+ # @example With numeric type
310
+ #
311
+ # query.max(:comments_count)
312
+ #
313
+ # # r.max(:comments_count)
314
+ #
315
+ # @example With string type
316
+ #
317
+ # query.max(:title)
318
+ #
319
+ # # => r.max(:title)
320
+ def max(field)
321
+ has_fields(field)
322
+ scoped.max(field)
323
+ end
324
+
325
+ # Returns the minimum value for the given field.
326
+ #
327
+ # @param field [Symbol] the field name
328
+ #
329
+ # @return result
330
+ #
331
+ # @since 0.1.0
332
+ #
333
+ # @example With numeric type
334
+ #
335
+ # query.min(:comments_count)
336
+ #
337
+ # # => r.min(:comments_count)
338
+ #
339
+ # @example With string type
340
+ #
341
+ # query.min(:title)
342
+ #
343
+ # # => r.min(:title)
344
+ def min(field)
345
+ has_fields(field)
346
+ scoped.min(field)
347
+ end
348
+
349
+ # Returns a count of the records for the current conditions.
350
+ #
351
+ # @return [Fixnum]
352
+ #
353
+ # @since 0.1.0
354
+ #
355
+ # @example
356
+ #
357
+ # query.where(author_id: 23).count # => 5
358
+ def count
359
+ scoped.count
360
+ end
361
+
240
362
  # Apply all the conditions and returns a filtered collection.
241
363
  #
242
364
  # This operation is idempotent, and the returned result didn't
@@ -16,6 +16,13 @@ module Lotus
16
16
  class RethinkdbAdapter < Abstract
17
17
  include ::RethinkDB::Shortcuts
18
18
 
19
+ # @attr_reader parsed_uri [Hash] the database connection details parsed
20
+ # from a URI
21
+ #
22
+ # @since 0.1.0
23
+ # @api private
24
+ attr_reader :parsed_uri
25
+
19
26
  # Initialize the adapter.
20
27
  #
21
28
  # Lotus::Model uses RethinkDB.
@@ -30,9 +37,9 @@ module Lotus
30
37
  #
31
38
  # @api private
32
39
  # @since 0.1.0
33
- def initialize(mapper, connection)
34
- super(mapper)
35
- @connection = connection
40
+ def initialize(mapper, uri)
41
+ super
42
+ @connection = r.connect(_parse_uri)
36
43
  end
37
44
 
38
45
  # Creates or updates a document in the database for the given entity.
@@ -192,6 +199,26 @@ module Lotus
192
199
 
193
200
  private
194
201
 
202
+ # Sets a parsed URI hash to be used when creating the database
203
+ # connection.
204
+ #
205
+ # @api private
206
+ # @since 0.1.0
207
+ def _parse_uri
208
+ parsed = URI.parse(@uri)
209
+ db = parsed.path.gsub(/^\//, '')
210
+
211
+ fail DatabaseAdapterNotFound if parsed.scheme != 'rethinkdb'
212
+ fail "No database specified in #{@uri}" if db.empty? || db.nil?
213
+
214
+ {
215
+ auth_key: parsed.password,
216
+ host: parsed.host,
217
+ port: parsed.port || 28_015,
218
+ db: db
219
+ }
220
+ end
221
+
195
222
  # Returns a collection from the given name.
196
223
  #
197
224
  # @param name [Symbol] a name of the collection (it must be mapped).
@@ -1,16 +1,12 @@
1
1
  module Lotus
2
- module Model
3
- module Adapters
4
- # Adapter for RethinkDB databases
5
- #
6
- # @api private
7
- # @since 0.1.0
8
- module Rethinkdb
9
- # Defines the version
10
- #
11
- # @since 0.1.0
12
- VERSION = '0.1.1'
13
- end
14
- end
2
+ # Adapter for RethinkDB databases
3
+ #
4
+ # @api private
5
+ # @since 0.1.0
6
+ module Rethinkdb
7
+ # Defines the version
8
+ #
9
+ # @since 0.1.0
10
+ VERSION = '0.2.0'
15
11
  end
16
12
  end
@@ -5,7 +5,7 @@ require 'lotus/rethinkdb/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'lotus-rethinkdb'
8
- spec.version = Lotus::Model::Adapters::Rethinkdb::VERSION
8
+ spec.version = Lotus::Rethinkdb::VERSION
9
9
  spec.authors = ['Angelo Ashmore']
10
10
  spec.email = ['angeloashmore@gmail.com']
11
11
  spec.summary = 'RethinkDB adapter for Lotus::Model'
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_runtime_dependency 'rethinkdb', '~> 1.15'
23
23
  spec.add_runtime_dependency 'activesupport', '~> 4.2'
24
24
 
25
- spec.add_development_dependency 'bundler', '~> 1.7'
25
+ spec.add_development_dependency 'bundler', '~> 1.6'
26
26
  spec.add_development_dependency 'minitest', '~> 5.5'
27
27
  spec.add_development_dependency 'minitest-line', '~> 0.6.2'
28
28
  spec.add_development_dependency 'rake', '~> 10.0'
@@ -39,7 +39,7 @@ describe Lotus::Model::Adapters::RethinkdbAdapter do
39
39
  end.load!
40
40
 
41
41
  @adapter = Lotus::Model::Adapters::RethinkdbAdapter.new(
42
- @mapper, RETHINKDB_TEST_CONNECTION
42
+ @mapper, RETHINKDB_TEST_URI
43
43
  )
44
44
  end
45
45
 
@@ -263,7 +263,7 @@ describe Lotus::Model::Adapters::RethinkdbAdapter do
263
263
  @adapter.clear(collection)
264
264
  end
265
265
 
266
- let(:user1) { TestUser.new(name: 'L', age: '32') }
266
+ let(:user1) { TestUser.new(name: 'L', age: 32) }
267
267
  let(:user2) { TestUser.new(name: 'MG', age: 31) }
268
268
 
269
269
  describe 'where' do
@@ -408,6 +408,67 @@ describe Lotus::Model::Adapters::RethinkdbAdapter do
408
408
  end
409
409
  end
410
410
 
411
+ describe 'has_fields' do
412
+ describe 'with an empty collection' do
413
+ it 'returns an empty result' do
414
+ result = @adapter.query(collection) do
415
+ has_fields(:age)
416
+ end.all
417
+
418
+ result.must_be_empty
419
+ end
420
+ end
421
+
422
+ describe 'with a filled collection' do
423
+ before do
424
+ @adapter.create(collection, user1)
425
+ @adapter.create(collection, user2)
426
+ @adapter.create(collection, user3)
427
+ end
428
+
429
+ let(:user1) { TestUser.new(name: 'L', age: 32) }
430
+ let(:user3) { TestUser.new(name: 'S') }
431
+ let(:users) { [user1, user2, user3] }
432
+
433
+ it 'returns documents that have the selected fields' do
434
+ query = proc do
435
+ has_fields(:age)
436
+ end
437
+
438
+ result = @adapter.query(collection, &query).all
439
+ result.each do |user|
440
+ user.age.wont_be_nil
441
+ end
442
+ end
443
+
444
+ it 'returns only the select of requested records' do
445
+ name = user2.name
446
+
447
+ query = proc do
448
+ where(name: name).pluck(:age)
449
+ end
450
+
451
+ result = @adapter.query(collection, &query).all
452
+ result.each do |user|
453
+ user.age.wont_be_nil
454
+ end
455
+ end
456
+
457
+ it 'returns only the multiple select of requested records' do
458
+ name = user2.name
459
+
460
+ query = proc do
461
+ where(name: name).pluck(:name, :age)
462
+ end
463
+
464
+ result = @adapter.query(collection, &query).all
465
+ result.each do |user|
466
+ user.age.wont_be_nil
467
+ end
468
+ end
469
+ end
470
+ end
471
+
411
472
  describe 'order' do
412
473
  describe 'with an empty collection' do
413
474
  it 'returns an empty result set' do
@@ -558,5 +619,291 @@ describe Lotus::Model::Adapters::RethinkdbAdapter do
558
619
  end
559
620
  end
560
621
  end
622
+
623
+ describe 'count' do
624
+ describe 'with an empty collection' do
625
+ it 'returns 0' do
626
+ result = @adapter.query(collection) do
627
+ all
628
+ end.count
629
+
630
+ result.must_equal 0
631
+ end
632
+ end
633
+
634
+ describe 'with a filled collection' do
635
+ before do
636
+ @adapter.create(collection, user1)
637
+ @adapter.create(collection, user2)
638
+ end
639
+
640
+ it 'returns the count of all the records' do
641
+ query = proc do
642
+ all
643
+ end
644
+
645
+ result = @adapter.query(collection, &query).count
646
+ result.must_equal 2
647
+ end
648
+
649
+ it 'returns the count from an empty query block' do
650
+ query = proc do
651
+ end
652
+
653
+ result = @adapter.query(collection, &query).count
654
+ result.must_equal 2
655
+ end
656
+
657
+ it 'returns only the count of requested records' do
658
+ name = user2.name
659
+
660
+ query = proc do
661
+ where(name: name)
662
+ end
663
+
664
+ result = @adapter.query(collection, &query).count
665
+ result.must_equal 1
666
+ end
667
+ end
668
+ end
669
+
670
+ describe 'sum' do
671
+ describe 'with an empty collection' do
672
+ it 'returns 0' do
673
+ result = @adapter.query(collection) do
674
+ all
675
+ end.sum(:age)
676
+
677
+ result.must_equal 0
678
+ end
679
+ end
680
+
681
+ describe 'with a filled collection' do
682
+ before do
683
+ @adapter.create(collection, user1)
684
+ @adapter.create(collection, user2)
685
+ @adapter.create(collection, TestUser.new(name: 'S'))
686
+ end
687
+
688
+ it 'returns the sum of all the records' do
689
+ query = proc do
690
+ all
691
+ end
692
+
693
+ result = @adapter.query(collection, &query).sum(:age)
694
+ result.must_equal 63
695
+ end
696
+
697
+ it 'returns the sum from an empty query block' do
698
+ query = proc {}
699
+
700
+ result = @adapter.query(collection, &query).sum(:age)
701
+ result.must_equal 63
702
+ end
703
+
704
+ it 'returns only the sum of requested records' do
705
+ name = user2.name
706
+
707
+ query = proc do
708
+ where(name: name)
709
+ end
710
+
711
+ result = @adapter.query(collection, &query).sum(:age)
712
+ result.must_equal 31
713
+ end
714
+ end
715
+ end
716
+
717
+ describe 'average' do
718
+ describe 'with an empty collection' do
719
+ it 'returns nil' do
720
+ result = @adapter.query(collection) do
721
+ all
722
+ end.average(:age)
723
+
724
+ result.must_be_nil
725
+ end
726
+ end
727
+
728
+ describe 'with a filled collection' do
729
+ before do
730
+ @adapter.create(collection, user1)
731
+ @adapter.create(collection, user2)
732
+ @adapter.create(collection, TestUser.new(name: 'S'))
733
+ end
734
+
735
+ it 'returns the average of all the records' do
736
+ query = proc do
737
+ all
738
+ end
739
+
740
+ result = @adapter.query(collection, &query).average(:age)
741
+ result.must_equal 31.5
742
+ end
743
+
744
+ it 'returns the average from an empty query block' do
745
+ query = proc {}
746
+
747
+ result = @adapter.query(collection, &query).average(:age)
748
+ result.must_equal 31.5
749
+ end
750
+
751
+ it 'returns only the average of requested records' do
752
+ name = user2.name
753
+
754
+ query = proc do
755
+ where(name: name)
756
+ end
757
+
758
+ result = @adapter.query(collection, &query).average(:age)
759
+ result.must_equal 31
760
+ end
761
+ end
762
+ end
763
+
764
+ describe 'avg' do
765
+ let(:user1) { TestUser.new(name: 'L', age: 32) }
766
+ let(:user2) { TestUser.new(name: 'MG', age: 31) }
767
+
768
+ describe 'with an empty collection' do
769
+ it 'returns nil' do
770
+ result = @adapter.query(collection) do
771
+ all
772
+ end.avg(:age)
773
+
774
+ result.must_be_nil
775
+ end
776
+ end
777
+
778
+ describe 'with a filled collection' do
779
+ before do
780
+ @adapter.create(collection, user1)
781
+ @adapter.create(collection, user2)
782
+ @adapter.create(collection, TestUser.new(name: 'S'))
783
+ end
784
+
785
+ it 'returns the average of all the records' do
786
+ query = proc do
787
+ all
788
+ end
789
+
790
+ result = @adapter.query(collection, &query).avg(:age)
791
+ result.must_equal 31.5
792
+ end
793
+
794
+ it 'returns the average from an empty query block' do
795
+ query = proc {}
796
+
797
+ result = @adapter.query(collection, &query).avg(:age)
798
+ result.must_equal 31.5
799
+ end
800
+
801
+ it 'returns only the average of requested records' do
802
+ name = user2.name
803
+
804
+ query = proc do
805
+ where(name: name)
806
+ end
807
+
808
+ result = @adapter.query(collection, &query).avg(:age)
809
+ result.must_equal 31
810
+ end
811
+ end
812
+ end
813
+
814
+ describe 'max' do
815
+ describe 'with an empty collection' do
816
+ it 'returns nil' do
817
+ result = @adapter.query(collection) do
818
+ all
819
+ end.max(:age)
820
+
821
+ result.must_be_nil
822
+ end
823
+ end
824
+
825
+ describe 'with a filled collection' do
826
+ before do
827
+ @adapter.create(collection, user1)
828
+ @adapter.create(collection, user2)
829
+ @adapter.create(collection, TestUser.new(name: 'S'))
830
+ end
831
+
832
+ it 'returns the maximum of all the records' do
833
+ query = proc do
834
+ all
835
+ end
836
+
837
+ result = @adapter.query(collection, &query).max(:age)
838
+ result.must_equal 32
839
+ end
840
+
841
+ it 'returns the maximum from an empty query block' do
842
+ query = proc do
843
+ end
844
+
845
+ result = @adapter.query(collection, &query).max(:age)
846
+ result.must_equal 32
847
+ end
848
+
849
+ it 'returns only the maximum of requested records' do
850
+ name = user2.name
851
+
852
+ query = proc do
853
+ where(name: name)
854
+ end
855
+
856
+ result = @adapter.query(collection, &query).max(:age)
857
+ result.must_equal 31
858
+ end
859
+ end
860
+ end
861
+
862
+ describe 'min' do
863
+ describe 'with an empty collection' do
864
+ it 'returns nil' do
865
+ result = @adapter.query(collection) do
866
+ all
867
+ end.min(:age)
868
+
869
+ result.must_be_nil
870
+ end
871
+ end
872
+
873
+ describe 'with a filled collection' do
874
+ before do
875
+ @adapter.create(collection, user1)
876
+ @adapter.create(collection, user2)
877
+ @adapter.create(collection, TestUser.new(name: 'S'))
878
+ end
879
+
880
+ it 'returns the minimum of all the records' do
881
+ query = proc do
882
+ all
883
+ end
884
+
885
+ result = @adapter.query(collection, &query).min(:age)
886
+ result.must_equal 31
887
+ end
888
+
889
+ it 'returns the minimum from an empty query block' do
890
+ query = proc {}
891
+
892
+ result = @adapter.query(collection, &query).min(:age)
893
+ result.must_equal 31
894
+ end
895
+
896
+ it 'returns only the minimum of requested records' do
897
+ name = user1.name
898
+
899
+ query = proc do
900
+ where(name: name)
901
+ end
902
+
903
+ result = @adapter.query(collection, &query).min(:age)
904
+ result.must_equal 32
905
+ end
906
+ end
907
+ end
561
908
  end
562
909
  end
data/test/test_helper.rb CHANGED
@@ -5,25 +5,21 @@ require 'minitest/autorun'
5
5
  $LOAD_PATH.unshift 'lib'
6
6
  require 'lotus-rethinkdb'
7
7
 
8
+ require 'coveralls'
9
+ Coveralls.wear!
10
+
8
11
  include RethinkDB::Shortcuts
9
12
 
10
- RETHINKDB_TEST_CONNECTION = r.connect
13
+ RETHINKDB_TEST_URI = 'rethinkdb://localhost:28015/test'
11
14
 
12
- def run
13
- yield.run(RETHINKDB_TEST_CONNECTION)
14
- end
15
+ conn = r.connect
15
16
 
16
- begin
17
- run { r.table_create('test_users') }
18
- rescue RethinkDB::RqlRuntimeError => _e
19
- puts 'Table `test_users` already exists'
20
- end
17
+ [:test_users, :test_devices].each do |t_name|
18
+ begin
19
+ r.table_create(t_name).run(conn)
20
+ rescue RethinkDB::RqlRuntimeError => _e
21
+ puts "Table `#{t_name}` already exists"
22
+ end
21
23
 
22
- begin
23
- run { r.table_create('test_devices') }
24
- rescue RethinkDB::RqlRuntimeError => _e
25
- puts 'Table `test_devices` already exists'
24
+ r.table(t_name).delete
26
25
  end
27
-
28
- run { r.table('test_users').delete }
29
- run { r.table('test_devices').delete }
data/test/version_test.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
- describe Lotus::Model::Adapters::Rethinkdb::VERSION do
3
+ describe Lotus::Rethinkdb::VERSION do
4
4
  it 'returns current version' do
5
- Lotus::Model::Adapters::Rethinkdb::VERSION.must_equal '0.1.1'
5
+ Lotus::Rethinkdb::VERSION.must_equal '0.2.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lotus-rethinkdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Angelo Ashmore
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '1.7'
61
+ version: '1.6'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '1.7'
68
+ version: '1.6'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: minitest
71
71
  requirement: !ruby/object:Gem::Requirement