Skip to content

Commit 78bb69c

Browse files
committed
[MODEL] Port basic response tests to rspec (#833)
1 parent 8a77b8b commit 78bb69c

25 files changed

+1492
-1431
lines changed

elasticsearch-model/Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
22

33
# Specify your gem's dependencies in elasticsearch-model.gemspec
44
gemspec
5+
6+
group :development, :testing do
7+
gem 'rspec'
8+
gem 'pry-nav'
9+
end

elasticsearch-model/Rakefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ end
1717

1818
require 'rake/testtask'
1919
namespace :test do
20+
2021
Rake::TestTask.new(:run_unit) do |test|
2122
test.libs << 'lib' << 'test'
2223
test.test_files = FileList["test/unit/**/*_test.rb"]
@@ -33,9 +34,11 @@ namespace :test do
3334

3435
desc "Run unit tests against ActiveModel 3, 4 and 5"
3536
task :unit do
36-
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/3.0.gemfile', __FILE__)}' bundle exec rake test:run_unit"
37-
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/4.0.gemfile', __FILE__)}' bundle exec rake test:run_unit"
38-
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/5.0.gemfile', __FILE__)}' bundle exec rake test:run_unit"
37+
['3.0.gemfile', '4.0.gemfile', '5.0.gemfile'].each do |gemfile|
38+
['bundle exec rake test:run_unit', 'bundle exec rspec'].each do |cmd|
39+
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/'+gemfile, __FILE__)}' #{cmd}"
40+
end
41+
end
3942
end
4043

4144
desc "Run integration tests against latest stable ActiveModel (5)"

elasticsearch-model/gemfiles/3.0.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ gem 'activemodel', '>= 3.0'
1111
gem 'activerecord', '~> 3.2'
1212
gem 'mongoid', '>= 3.0'
1313
gem 'sqlite3' unless defined?(JRUBY_VERSION)
14+
15+
group :development, :testing do
16+
gem 'rspec'
17+
gem 'pry-nav'
18+
end

elasticsearch-model/gemfiles/4.0.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ gemspec path: '../'
1010
gem 'activemodel', '~> 4'
1111
gem 'activerecord', '~> 4'
1212
gem 'sqlite3' unless defined?(JRUBY_VERSION)
13+
14+
group :development, :testing do
15+
gem 'rspec'
16+
gem 'pry-nav'
17+
end

elasticsearch-model/gemfiles/5.0.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ gemspec path: '../'
1010
gem 'activemodel', '~> 5'
1111
gem 'activerecord', '~> 5'
1212
gem 'sqlite3' unless defined?(JRUBY_VERSION)
13+
14+
group :development, :testing do
15+
gem 'rspec'
16+
gem 'pry-nav'
17+
end
Lines changed: 2 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -1,192 +1,2 @@
1-
module Elasticsearch
2-
module Model
3-
module Response
4-
5-
# Pagination for search results/records
6-
#
7-
module Pagination
8-
# Allow models to be paginated with the "kaminari" gem [https://github.com/amatsuda/kaminari]
9-
#
10-
module Kaminari
11-
def self.included(base)
12-
# Include the Kaminari configuration and paging method in response
13-
#
14-
base.__send__ :include, ::Kaminari::ConfigurationMethods::ClassMethods
15-
base.__send__ :include, ::Kaminari::PageScopeMethods
16-
17-
# Include the Kaminari paging methods in results and records
18-
#
19-
Elasticsearch::Model::Response::Results.__send__ :include, ::Kaminari::ConfigurationMethods::ClassMethods
20-
Elasticsearch::Model::Response::Results.__send__ :include, ::Kaminari::PageScopeMethods
21-
Elasticsearch::Model::Response::Records.__send__ :include, ::Kaminari::PageScopeMethods
22-
23-
Elasticsearch::Model::Response::Results.__send__ :delegate, :limit_value, :offset_value, :total_count, :max_pages, to: :response
24-
Elasticsearch::Model::Response::Records.__send__ :delegate, :limit_value, :offset_value, :total_count, :max_pages, to: :response
25-
26-
base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
27-
# Define the `page` Kaminari method
28-
#
29-
def #{::Kaminari.config.page_method_name}(num=nil)
30-
@results = nil
31-
@records = nil
32-
@response = nil
33-
@page = [num.to_i, 1].max
34-
@per_page ||= __default_per_page
35-
36-
self.search.definition.update size: @per_page,
37-
from: @per_page * (@page - 1)
38-
39-
self
40-
end
41-
RUBY
42-
end
43-
44-
# Returns the current "limit" (`size`) value
45-
#
46-
def limit_value
47-
case
48-
when search.definition[:size]
49-
search.definition[:size]
50-
else
51-
__default_per_page
52-
end
53-
end
54-
55-
# Returns the current "offset" (`from`) value
56-
#
57-
def offset_value
58-
case
59-
when search.definition[:from]
60-
search.definition[:from]
61-
else
62-
0
63-
end
64-
end
65-
66-
# Set the "limit" (`size`) value
67-
#
68-
def limit(value)
69-
return self if value.to_i <= 0
70-
@results = nil
71-
@records = nil
72-
@response = nil
73-
@per_page = value.to_i
74-
75-
search.definition.update :size => @per_page
76-
search.definition.update :from => @per_page * (@page - 1) if @page
77-
self
78-
end
79-
80-
# Set the "offset" (`from`) value
81-
#
82-
def offset(value)
83-
return self if value.to_i < 0
84-
@results = nil
85-
@records = nil
86-
@response = nil
87-
@page = nil
88-
search.definition.update :from => value.to_i
89-
self
90-
end
91-
92-
# Returns the total number of results
93-
#
94-
def total_count
95-
results.total
96-
end
97-
98-
# Returns the models's `per_page` value or the default
99-
#
100-
# @api private
101-
#
102-
def __default_per_page
103-
klass.respond_to?(:default_per_page) && klass.default_per_page || ::Kaminari.config.default_per_page
104-
end
105-
end
106-
107-
# Allow models to be paginated with the "will_paginate" gem [https://github.com/mislav/will_paginate]
108-
#
109-
module WillPaginate
110-
def self.included(base)
111-
base.__send__ :include, ::WillPaginate::CollectionMethods
112-
113-
# Include the paging methods in results and records
114-
#
115-
methods = [:current_page, :offset, :length, :per_page, :total_entries, :total_pages, :previous_page, :next_page, :out_of_bounds?]
116-
Elasticsearch::Model::Response::Results.__send__ :delegate, *methods, to: :response
117-
Elasticsearch::Model::Response::Records.__send__ :delegate, *methods, to: :response
118-
end
119-
120-
def offset
121-
(current_page - 1) * per_page
122-
end
123-
124-
def length
125-
search.definition[:size]
126-
end
127-
128-
# Main pagination method
129-
#
130-
# @example
131-
#
132-
# Article.search('foo').paginate(page: 1, per_page: 30)
133-
#
134-
def paginate(options)
135-
param_name = options[:param_name] || :page
136-
page = [options[param_name].to_i, 1].max
137-
per_page = (options[:per_page] || __default_per_page).to_i
138-
139-
search.definition.update size: per_page,
140-
from: (page - 1) * per_page
141-
self
142-
end
143-
144-
# Return the current page
145-
#
146-
def current_page
147-
search.definition[:from] / per_page + 1 if search.definition[:from] && per_page
148-
end
149-
150-
# Pagination method
151-
#
152-
# @example
153-
#
154-
# Article.search('foo').page(2)
155-
#
156-
def page(num)
157-
paginate(page: num, per_page: per_page) # shorthand
158-
end
159-
160-
# Return or set the "size" value
161-
#
162-
# @example
163-
#
164-
# Article.search('foo').per_page(15).page(2)
165-
#
166-
def per_page(num = nil)
167-
if num.nil?
168-
search.definition[:size]
169-
else
170-
paginate(page: current_page, per_page: num) # shorthand
171-
end
172-
end
173-
174-
# Returns the total number of results
175-
#
176-
def total_entries
177-
results.total
178-
end
179-
180-
# Returns the models's `per_page` value or the default
181-
#
182-
# @api private
183-
#
184-
def __default_per_page
185-
klass.respond_to?(:per_page) && klass.per_page || ::WillPaginate.per_page
186-
end
187-
end
188-
end
189-
190-
end
191-
end
192-
end
1+
require 'elasticsearch/model/response/pagination/kaminari'
2+
require 'elasticsearch/model/response/pagination/will_paginate'
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
module Elasticsearch
2+
module Model
3+
module Response
4+
5+
# Pagination for search results/records
6+
#
7+
module Pagination
8+
# Allow models to be paginated with the "kaminari" gem [https://github.com/amatsuda/kaminari]
9+
#
10+
module Kaminari
11+
def self.included(base)
12+
# Include the Kaminari configuration and paging method in response
13+
#
14+
base.__send__ :include, ::Kaminari::ConfigurationMethods::ClassMethods
15+
base.__send__ :include, ::Kaminari::PageScopeMethods
16+
17+
# Include the Kaminari paging methods in results and records
18+
#
19+
Elasticsearch::Model::Response::Results.__send__ :include, ::Kaminari::ConfigurationMethods::ClassMethods
20+
Elasticsearch::Model::Response::Results.__send__ :include, ::Kaminari::PageScopeMethods
21+
Elasticsearch::Model::Response::Records.__send__ :include, ::Kaminari::PageScopeMethods
22+
23+
Elasticsearch::Model::Response::Results.__send__ :delegate, :limit_value, :offset_value, :total_count, :max_pages, to: :response
24+
Elasticsearch::Model::Response::Records.__send__ :delegate, :limit_value, :offset_value, :total_count, :max_pages, to: :response
25+
26+
base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
27+
# Define the `page` Kaminari method
28+
#
29+
def #{::Kaminari.config.page_method_name}(num=nil)
30+
@results = nil
31+
@records = nil
32+
@response = nil
33+
@page = [num.to_i, 1].max
34+
@per_page ||= __default_per_page
35+
36+
self.search.definition.update size: @per_page,
37+
from: @per_page * (@page - 1)
38+
39+
self
40+
end
41+
RUBY
42+
end
43+
44+
# Returns the current "limit" (`size`) value
45+
#
46+
def limit_value
47+
case
48+
when search.definition[:size]
49+
search.definition[:size]
50+
else
51+
__default_per_page
52+
end
53+
end
54+
55+
# Returns the current "offset" (`from`) value
56+
#
57+
def offset_value
58+
case
59+
when search.definition[:from]
60+
search.definition[:from]
61+
else
62+
0
63+
end
64+
end
65+
66+
# Set the "limit" (`size`) value
67+
#
68+
def limit(value)
69+
return self if value.to_i <= 0
70+
@results = nil
71+
@records = nil
72+
@response = nil
73+
@per_page = value.to_i
74+
75+
search.definition.update :size => @per_page
76+
search.definition.update :from => @per_page * (@page - 1) if @page
77+
self
78+
end
79+
80+
# Set the "offset" (`from`) value
81+
#
82+
def offset(value)
83+
return self if value.to_i < 0
84+
@results = nil
85+
@records = nil
86+
@response = nil
87+
@page = nil
88+
search.definition.update :from => value.to_i
89+
self
90+
end
91+
92+
# Returns the total number of results
93+
#
94+
def total_count
95+
results.total
96+
end
97+
98+
# Returns the models's `per_page` value or the default
99+
#
100+
# @api private
101+
#
102+
def __default_per_page
103+
klass.respond_to?(:default_per_page) && klass.default_per_page || ::Kaminari.config.default_per_page
104+
end
105+
end
106+
end
107+
end
108+
end
109+
end

0 commit comments

Comments
 (0)