trailblazer-finder 0.70.0 → 0.80.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/trailblazer/finder/version.rb +1 -1
- data/trailblazer-finder.gemspec +4 -3
- metadata +9 -26
- data/.github/workflows/ci.yml +0 -16
- data/.gitignore +0 -24
- data/.rubocop.yml +0 -11
- data/CHANGES.md +0 -46
- data/Gemfile +0 -16
- data/LICENSE.txt +0 -9
- data/README.md +0 -451
- data/Rakefile +0 -25
- data/spec/spec_helper.rb +0 -13
- data/spec/spec_helper_active_record.rb +0 -48
- data/spec/spec_helper_sequel.rb +0 -33
- data/spec/spec_helper_will_paginate.rb +0 -13
- data/spec/trailblazer/finder/adapters/active_record_spec.rb +0 -267
- data/spec/trailblazer/finder/adapters/basic_spec.rb +0 -260
- data/spec/trailblazer/finder/adapters/kaminari_spec.rb +0 -115
- data/spec/trailblazer/finder/adapters/sequel_spec.rb +0 -267
- data/spec/trailblazer/finder/adapters/will_paginate_spec.rb +0 -117
- data/spec/trailblazer/finder/base_spec.rb +0 -417
- data/spec/trailblazer/finder/dsl_spec.rb +0 -204
- data/spec/trailblazer/finder/utils/hash_spec.rb +0 -26
- data/spec/trailblazer/finder/utils/splitter_spec.rb +0 -39
- data/spec/trailblazer/finder/utils/string_spec.rb +0 -69
- data/spec/trailblazer/operation/finder_spec.rb +0 -121
@@ -1,267 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper_active_record"
|
4
|
-
|
5
|
-
module Trailblazer
|
6
|
-
class Finder
|
7
|
-
module Adapters
|
8
|
-
describe ActiveRecord do
|
9
|
-
after do
|
10
|
-
Product.delete_all
|
11
|
-
Product.reset_pk_sequence
|
12
|
-
end
|
13
|
-
|
14
|
-
def define_finder_class(&block)
|
15
|
-
Class.new(Trailblazer::Finder) do
|
16
|
-
adapter "ActiveRecord"
|
17
|
-
entity { Product }
|
18
|
-
|
19
|
-
class_eval(&block)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def finder_class(&block)
|
24
|
-
define_finder_class do
|
25
|
-
class_eval(&block) unless block.nil?
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def new_finder(filter = {}, &block)
|
30
|
-
finder_class(&block).new params: filter
|
31
|
-
end
|
32
|
-
|
33
|
-
describe "#property" do
|
34
|
-
it "sets the property and works with eq predicate" do
|
35
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
36
|
-
finder = new_finder name_eq: "product_2" do
|
37
|
-
property :name, type: Types::String
|
38
|
-
end
|
39
|
-
|
40
|
-
expect(finder.result.first.id).to eq 3
|
41
|
-
expect(finder.result.count).to eq 1
|
42
|
-
end
|
43
|
-
|
44
|
-
it "sets the property and works with not_eq predicate" do
|
45
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
46
|
-
finder = new_finder name_not_eq: "product_2" do
|
47
|
-
property :name, type: Types::String
|
48
|
-
end
|
49
|
-
|
50
|
-
expect(finder.result.count).to eq 9
|
51
|
-
end
|
52
|
-
|
53
|
-
it "sets the property and works with blank predicate" do
|
54
|
-
9.times { |i| Product.create name: "product_#{i}" }
|
55
|
-
Product.create name: "", slug: "Blank Test"
|
56
|
-
finder = new_finder name_blank: true do
|
57
|
-
property :name, type: Types::String
|
58
|
-
end
|
59
|
-
|
60
|
-
expect(finder.result.count).to eq 1
|
61
|
-
expect(finder.result.first.slug).to eq "Blank Test"
|
62
|
-
expect(finder.result.first.id).to eq 10
|
63
|
-
end
|
64
|
-
|
65
|
-
it "sets the property and works with not blank predicate" do
|
66
|
-
9.times { |i| Product.create name: "product_#{i}" }
|
67
|
-
Product.create name: "", slug: "Blank Test"
|
68
|
-
finder = new_finder name_not_blank: true do
|
69
|
-
property :name, type: Types::String
|
70
|
-
end
|
71
|
-
|
72
|
-
expect(finder.result.count).to eq 9
|
73
|
-
end
|
74
|
-
|
75
|
-
it "sets the property and works with gt (greater than) predicate" do
|
76
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
77
|
-
finder = new_finder id_gt: 3 do
|
78
|
-
property :id, type: Types::Integer
|
79
|
-
end
|
80
|
-
|
81
|
-
expect(finder.result.count).to eq 7
|
82
|
-
end
|
83
|
-
|
84
|
-
it "sets the property and works with gte (greater than or equal to) predicate" do
|
85
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
86
|
-
finder = new_finder id_gte: 3 do
|
87
|
-
property :id, type: Types::Integer
|
88
|
-
end
|
89
|
-
|
90
|
-
expect(finder.result.count).to eq 8
|
91
|
-
end
|
92
|
-
|
93
|
-
it "sets the property and works with lt (less than) predicate" do
|
94
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
95
|
-
finder = new_finder id_lt: 3 do
|
96
|
-
property :id, type: Types::Integer
|
97
|
-
end
|
98
|
-
|
99
|
-
expect(finder.result.count).to eq 2
|
100
|
-
end
|
101
|
-
|
102
|
-
it "sets the property and works with lte (less than or equal to) predicate" do
|
103
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
104
|
-
finder = new_finder id_lte: 3 do
|
105
|
-
property :id, type: Types::Integer
|
106
|
-
end
|
107
|
-
|
108
|
-
expect(finder.result.count).to eq 3
|
109
|
-
end
|
110
|
-
|
111
|
-
it "sets the property and works with cont predicate" do
|
112
|
-
5.times { |i| Product.create name: "product_#{i}" }
|
113
|
-
5.times { |i| Product.create name: "none_#{i}" }
|
114
|
-
finder = new_finder name_cont: "product" do
|
115
|
-
property :name, type: Types::String
|
116
|
-
end
|
117
|
-
|
118
|
-
expect(finder.result.first.id).to eq 1
|
119
|
-
expect(finder.result.count).to eq 5
|
120
|
-
end
|
121
|
-
|
122
|
-
it "sets the property and works with not_cont predicate" do
|
123
|
-
5.times { |i| Product.create name: "product_#{i}" }
|
124
|
-
5.times { |i| Product.create name: "none_#{i}" }
|
125
|
-
finder = new_finder name_not_cont: "product" do
|
126
|
-
property :name, type: Types::String
|
127
|
-
end
|
128
|
-
|
129
|
-
expect(finder.result.first.id).to eq 6
|
130
|
-
expect(finder.result.count).to eq 5
|
131
|
-
end
|
132
|
-
|
133
|
-
it "sets the property and works with sw predicate" do
|
134
|
-
5.times { |i| Product.create name: "product_#{i}" }
|
135
|
-
Product.create name: "predicate_0"
|
136
|
-
4.times { |i| Product.create name: "none_#{i}" }
|
137
|
-
finder = new_finder name_sw: "pr" do
|
138
|
-
property :name, type: Types::String
|
139
|
-
end
|
140
|
-
|
141
|
-
expect(finder.result.first.id).to eq 1
|
142
|
-
expect(finder.result.count).to eq 6
|
143
|
-
end
|
144
|
-
|
145
|
-
it "sets the property and works with not_sw predicate" do
|
146
|
-
5.times { |i| Product.create name: "product_#{i}" }
|
147
|
-
Product.create name: "predicate_0"
|
148
|
-
4.times { |i| Product.create name: "none_#{i}" }
|
149
|
-
finder = new_finder name_not_sw: "pr" do
|
150
|
-
property :name, type: Types::String
|
151
|
-
end
|
152
|
-
|
153
|
-
expect(finder.result.first.id).to eq 7
|
154
|
-
expect(finder.result.count).to eq 4
|
155
|
-
end
|
156
|
-
|
157
|
-
it "sets the property and works with ew predicate" do
|
158
|
-
5.times { |i| Product.create name: "product_#{i}_end" }
|
159
|
-
Product.create name: "predicate_0_endwow"
|
160
|
-
4.times { |i| Product.create name: "none_#{i}" }
|
161
|
-
finder = new_finder name_ew: "end" do
|
162
|
-
property :name, type: Types::String
|
163
|
-
end
|
164
|
-
|
165
|
-
expect(finder.result.first.id).to eq 1
|
166
|
-
expect(finder.result.count).to eq 5
|
167
|
-
end
|
168
|
-
|
169
|
-
it "sets the property and works with not_ew predicate" do
|
170
|
-
5.times { |i| Product.create name: "product_#{i}_end" }
|
171
|
-
Product.create name: "predicate_0_endwow"
|
172
|
-
4.times { |i| Product.create name: "none_#{i}" }
|
173
|
-
finder = new_finder name_not_ew: "end" do
|
174
|
-
property :name, type: Types::String
|
175
|
-
end
|
176
|
-
|
177
|
-
expect(finder.result.first.id).to eq 6
|
178
|
-
expect(finder.result.count).to eq 5
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
describe "#paging" do
|
183
|
-
it "sets the paging values and shows only the first page results" do
|
184
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
185
|
-
finder = new_finder do
|
186
|
-
paging per_page: 2, min_per_page: 1, max_per_page: 5
|
187
|
-
end
|
188
|
-
|
189
|
-
expect(finder.result.map { |n| n[:name] }).to eq %w[product_0 product_1]
|
190
|
-
end
|
191
|
-
|
192
|
-
it "accepts per_page as a parameter" do
|
193
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
194
|
-
finder = new_finder page: 2, per_page: 4 do
|
195
|
-
paging per_page: 5, min_per_page: 2, max_per_page: 8
|
196
|
-
end
|
197
|
-
|
198
|
-
expect(finder.result.first.id).to eq 5
|
199
|
-
expect(finder.result.map(&:id)).to eq [5, 6, 7, 8]
|
200
|
-
end
|
201
|
-
|
202
|
-
it "uses max_per_page in finder as maximum per_page" do
|
203
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
204
|
-
finder = new_finder page: 2, per_page: 9 do
|
205
|
-
paging per_page: 5, min_per_page: 2, max_per_page: 8
|
206
|
-
end
|
207
|
-
|
208
|
-
expect(finder.result.first.id).to eq 9
|
209
|
-
expect(finder.result.map(&:id)).to eq [9, 10]
|
210
|
-
end
|
211
|
-
|
212
|
-
it "uses min_per_page in finder as minimum per_page" do
|
213
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
214
|
-
finder = new_finder page: 2, per_page: 1 do
|
215
|
-
paging per_page: 5, min_per_page: 2, max_per_page: 8
|
216
|
-
end
|
217
|
-
|
218
|
-
expect(finder.result.first.id).to eq 3
|
219
|
-
expect(finder.result.map(&:id)).to eq [3, 4]
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
describe "#sorting" do
|
224
|
-
it "sets the property and works with eq predicate and sorting" do
|
225
|
-
5.times { |i| Product.create name: "product_#{i}" }
|
226
|
-
5.times { |_i| Product.create name: "product" }
|
227
|
-
finder = new_finder name_eq: "product", sort: "id desc" do
|
228
|
-
property :name, type: Types::String
|
229
|
-
property :id, type: Types::Integer, sortable: true
|
230
|
-
end
|
231
|
-
|
232
|
-
expect(finder.result.first.id).to eq 10
|
233
|
-
expect(finder.result.count).to eq 5
|
234
|
-
end
|
235
|
-
|
236
|
-
it "sets the property and sorts by multiple columns" do
|
237
|
-
5.times { |i| Product.create name: "product_#{i}", slug: "slug #{i}" }
|
238
|
-
5.times { |i| Product.create name: "product", slug: "slug #{i}" }
|
239
|
-
finder = new_finder sort: "name asc, slug desc" do
|
240
|
-
property :name, type: Types::String, sortable: true
|
241
|
-
property :slug, type: Types::String, sortable: true
|
242
|
-
property :id, type: Types::Integer, sortable: true
|
243
|
-
end
|
244
|
-
|
245
|
-
expect(finder.result.map { |n| n[:id] }).to eq [10, 9, 8, 7, 6, 1, 2, 3, 4, 5]
|
246
|
-
expect(finder.result.count).to eq 10
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
|
-
describe "Predicates, Paging and Sorting together" do
|
251
|
-
it "sets the property and works with eq predicate and paging" do
|
252
|
-
5.times { |i| Product.create name: "product_#{i}" }
|
253
|
-
5.times { |_i| Product.create name: "product" }
|
254
|
-
finder = new_finder name_eq: "product", sort: "id desc", page: 2 do
|
255
|
-
paging per_page: 2, min_per_page: 1, max_per_page: 5
|
256
|
-
property :name, type: Types::String
|
257
|
-
property :id, type: Types::Integer, sortable: true
|
258
|
-
end
|
259
|
-
|
260
|
-
expect(finder.result.first.id).to eq 8
|
261
|
-
expect(finder.result.map(&:id)).to eq [8, 7]
|
262
|
-
end
|
263
|
-
end
|
264
|
-
end
|
265
|
-
end
|
266
|
-
end
|
267
|
-
end
|
@@ -1,260 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
module Trailblazer
|
6
|
-
class Finder
|
7
|
-
module Adapters
|
8
|
-
describe Basic do
|
9
|
-
def define_finder_class(&block)
|
10
|
-
Class.new(Trailblazer::Finder) do
|
11
|
-
class_eval(&block)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def finder_class(default_entity = [], &block)
|
16
|
-
define_finder_class do
|
17
|
-
entity { default_entity }
|
18
|
-
|
19
|
-
class_eval(&block) unless block.nil?
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def new_finder(default_entity = [], filter = {}, &block)
|
24
|
-
finder_class(default_entity, &block).new params: filter
|
25
|
-
end
|
26
|
-
|
27
|
-
describe "#property" do
|
28
|
-
it "sets the property and works with eq predicate" do
|
29
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "Test 2"}, {id: 3, value: "Test 3"}]
|
30
|
-
finder = new_finder entity, value_eq: "Test 1" do
|
31
|
-
property :value, type: Types::String
|
32
|
-
end
|
33
|
-
|
34
|
-
expect(finder.result.map { |n| n[:value] }).to eq ["Test 1"]
|
35
|
-
end
|
36
|
-
|
37
|
-
it "sets the property and works with not_eq predicate" do
|
38
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "Test 2"}, {id: 3, value: "Test 3"}]
|
39
|
-
finder = new_finder entity, value_not_eq: "Test 1" do
|
40
|
-
property :value, type: Types::String
|
41
|
-
end
|
42
|
-
|
43
|
-
expect(finder.result.map { |n| n[:value] }).to eq ["Test 2", "Test 3"]
|
44
|
-
end
|
45
|
-
|
46
|
-
it "sets the property and works with blank predicate" do
|
47
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "Test 2"}, {id: 3, value: ""}, {id: 4, value: "Test 4"}]
|
48
|
-
finder = new_finder entity, value_blank: true do
|
49
|
-
property :value, type: Types::String
|
50
|
-
end
|
51
|
-
|
52
|
-
expect(finder.result.map { |n| n[:id] }).to eq [3]
|
53
|
-
end
|
54
|
-
|
55
|
-
it "sets the property and works with not_blank predicate" do
|
56
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "Test 2"}, {id: 3, value: ""}, {id: 4, value: "Test 4"}]
|
57
|
-
finder = new_finder entity, value_not_blank: true do
|
58
|
-
property :value, type: Types::String
|
59
|
-
end
|
60
|
-
|
61
|
-
expect(finder.result.map { |n| n[:id] }).to eq [1, 2, 4]
|
62
|
-
end
|
63
|
-
|
64
|
-
it "sets the property and works gt (greater than) predicate" do
|
65
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "Test 2"}, {id: 3, value: ""}, {id: 4, value: "Test 4"}]
|
66
|
-
finder = new_finder entity, id_gt: 1 do
|
67
|
-
property :id, type: Types::String
|
68
|
-
end
|
69
|
-
|
70
|
-
expect(finder.result.map { |n| n[:id] }).to eq [2, 3, 4]
|
71
|
-
end
|
72
|
-
|
73
|
-
it "sets the property and works gte (greater than or equal to) predicate" do
|
74
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "Test 2"}, {id: 3, value: ""}, {id: 4, value: "Test 4"}]
|
75
|
-
finder = new_finder entity, id_gte: 2 do
|
76
|
-
property :id, type: Types::String
|
77
|
-
end
|
78
|
-
|
79
|
-
expect(finder.result.map { |n| n[:id] }).to eq [2, 3, 4]
|
80
|
-
end
|
81
|
-
|
82
|
-
it "sets the property and works lt (less than) predicate" do
|
83
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "Test 2"}, {id: 3, value: ""}, {id: 4, value: "Test 4"}]
|
84
|
-
finder = new_finder entity, id_lt: 4 do
|
85
|
-
property :id, type: Types::String
|
86
|
-
end
|
87
|
-
|
88
|
-
expect(finder.result.map { |n| n[:id] }).to eq [1, 2, 3]
|
89
|
-
end
|
90
|
-
|
91
|
-
it "sets the property and works lte (lesss than or equal to) predicate" do
|
92
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "Test 2"}, {id: 3, value: ""}, {id: 4, value: "Test 4"}]
|
93
|
-
finder = new_finder entity, id_lte: 3 do
|
94
|
-
property :id, type: Types::String
|
95
|
-
end
|
96
|
-
|
97
|
-
expect(finder.result.map { |n| n[:id] }).to eq [1, 2, 3]
|
98
|
-
end
|
99
|
-
|
100
|
-
it "sets the property and works lte (lesss than or equal to) predicate" do
|
101
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "Test 2"}, {id: 3, value: ""}, {id: 4, value: "Test 4"}]
|
102
|
-
finder = new_finder entity, id_ltse: 3 do
|
103
|
-
property :id, type: Types::String
|
104
|
-
end
|
105
|
-
|
106
|
-
expect(finder.result.map { |n| n[:id] }).to eq [1, 2, 3, 4]
|
107
|
-
end
|
108
|
-
|
109
|
-
it "sets the property and works with cont predicate" do
|
110
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "None 2"}, {id: 3, value: "Test 3"}]
|
111
|
-
finder = new_finder entity, value_cont: "Test" do
|
112
|
-
property :value, type: Types::String
|
113
|
-
end
|
114
|
-
|
115
|
-
expect(finder.result.map { |n| n[:value] }).to eq ["Test 1", "Test 3"]
|
116
|
-
end
|
117
|
-
|
118
|
-
it "sets the property and works with not_cont predicate" do
|
119
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "None 2"}, {id: 3, value: "Test 3"}]
|
120
|
-
finder = new_finder entity, value_not_cont: "Test" do
|
121
|
-
property :value, type: Types::String
|
122
|
-
end
|
123
|
-
|
124
|
-
expect(finder.result.map { |n| n[:value] }).to eq ["None 2"]
|
125
|
-
end
|
126
|
-
|
127
|
-
it "sets the property and works with sw predicate" do
|
128
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "None 2"}, {id: 3, value: "Testical 3"}]
|
129
|
-
finder = new_finder entity, value_sw: "Test" do
|
130
|
-
property :value, type: Types::String
|
131
|
-
end
|
132
|
-
|
133
|
-
expect(finder.result.map { |n| n[:value] }).to eq ["Test 1", "Testical 3"]
|
134
|
-
end
|
135
|
-
|
136
|
-
it "sets the property and works with not_sw predicate" do
|
137
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "None 2"}, {id: 3, value: "ATest 3"}]
|
138
|
-
finder = new_finder entity, value_not_sw: "Test" do
|
139
|
-
property :value, type: Types::String
|
140
|
-
end
|
141
|
-
|
142
|
-
expect(finder.result.map { |n| n[:value] }).to eq ["None 2", "ATest 3"]
|
143
|
-
end
|
144
|
-
|
145
|
-
it "sets the property and works with ew predicate" do
|
146
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "None 1"}, {id: 3, value: "Test 3"}]
|
147
|
-
finder = new_finder entity, value_ew: "1" do
|
148
|
-
property :value, type: Types::String
|
149
|
-
end
|
150
|
-
|
151
|
-
expect(finder.result.map { |n| n[:value] }).to eq ["Test 1", "None 1"]
|
152
|
-
end
|
153
|
-
|
154
|
-
it "sets the property and works with not_ew predicate" do
|
155
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "None 1"}, {id: 3, value: "ATest 3"}]
|
156
|
-
finder = new_finder entity, value_not_ew: "1" do
|
157
|
-
property :value, type: Types::String
|
158
|
-
end
|
159
|
-
|
160
|
-
expect(finder.result.map { |n| n[:value] }).to eq ["ATest 3"]
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
describe "#paging" do
|
165
|
-
it "sets the paging values and shows only the first page results" do
|
166
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "Test 2"}, {id: 3, value: "Test 3"}]
|
167
|
-
finder = new_finder entity do
|
168
|
-
paging per_page: 2, min_per_page: 1, max_per_page: 5
|
169
|
-
property :value, type: Types::String
|
170
|
-
end
|
171
|
-
|
172
|
-
expect(finder.result.map { |n| n[:value] }).to eq ["Test 1", "Test 2"]
|
173
|
-
end
|
174
|
-
|
175
|
-
it "accepts per_page as a parameter" do
|
176
|
-
entity = [
|
177
|
-
{id: 1, value: "Test 1"}, {id: 2, value: "Test 2"}, {id: 3, value: "Test 3"}, {id: 4, value: "Test 4"},
|
178
|
-
{id: 5, value: "Test 5"}, {id: 6, value: "Test 6"}, {id: 7, value: "Test 7"}, {id: 8, value: "Test 8"}, {id: 9, value: "Test 9"}, {id: 10, value: "Test 10"}
|
179
|
-
]
|
180
|
-
finder = new_finder entity, page: 2, per_page: 4 do
|
181
|
-
paging per_page: 5, min_per_page: 2, max_per_page: 8
|
182
|
-
end
|
183
|
-
|
184
|
-
expect(finder.result.first.id).to eq 5
|
185
|
-
expect(finder.result.map(&:id)).to eq [5, 6, 7, 8]
|
186
|
-
end
|
187
|
-
|
188
|
-
it "uses max_per_page in finder as maximum per_page" do
|
189
|
-
entity = [
|
190
|
-
{id: 1, value: "Test 1"}, {id: 2, value: "Test 2"}, {id: 3, value: "Test 3"}, {id: 4, value: "Test 4"},
|
191
|
-
{id: 5, value: "Test 5"}, {id: 6, value: "Test 6"}, {id: 7, value: "Test 7"}, {id: 8, value: "Test 8"}, {id: 9, value: "Test 9"}, {id: 10, value: "Test 10"}
|
192
|
-
]
|
193
|
-
finder = new_finder entity, page: 2, per_page: 9 do
|
194
|
-
paging per_page: 5, min_per_page: 2, max_per_page: 8
|
195
|
-
end
|
196
|
-
|
197
|
-
expect(finder.result.first.id).to eq 9
|
198
|
-
expect(finder.result.map(&:id)).to eq [9, 10]
|
199
|
-
end
|
200
|
-
|
201
|
-
it "uses min_per_page in finder as minimum per_page" do
|
202
|
-
entity = [
|
203
|
-
{id: 1, value: "Test 1"}, {id: 2, value: "Test 2"}, {id: 3, value: "Test 3"}, {id: 4, value: "Test 4"},
|
204
|
-
{id: 5, value: "Test 5"}, {id: 6, value: "Test 6"}, {id: 7, value: "Test 7"}, {id: 8, value: "Test 8"}, {id: 9, value: "Test 9"}, {id: 10, value: "Test 10"}
|
205
|
-
]
|
206
|
-
finder = new_finder entity, page: 2, per_page: 1 do
|
207
|
-
paging per_page: 5, min_per_page: 2, max_per_page: 8
|
208
|
-
end
|
209
|
-
|
210
|
-
expect(finder.result.first.id).to eq 3
|
211
|
-
expect(finder.result.map(&:id)).to eq [3, 4]
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
describe "#sorting" do
|
216
|
-
it "sets the property and works with eq predicate and sorting" do
|
217
|
-
entity = [{id: 1, value: "Test 1"}, {id: 2, value: "Test 1"}, {id: 3, value: "Test 3"}]
|
218
|
-
finder = new_finder entity, value_eq: "Test 1", sort: "id desc" do
|
219
|
-
property :value, type: Types::String, sortable: true
|
220
|
-
property :id, type: Types::Integer, sortable: true
|
221
|
-
end
|
222
|
-
|
223
|
-
expect(finder.result.map { |n| n[:id] }).to eq [2, 1]
|
224
|
-
end
|
225
|
-
|
226
|
-
it "sets the property and works with eq predicate and sorting by multiple columns" do
|
227
|
-
entity = [{id: 1, value: "Test 1", slug: 4}, {id: 2, value: "Test 2", slug: 1}, {id: 3, value: "Test 1", slug: 2}]
|
228
|
-
finder = new_finder entity, value_eq: "Test 1", sort: "slug asc, id desc" do
|
229
|
-
property :value, type: Types::String, sortable: true
|
230
|
-
property :id, type: Types::Integer, sortable: true
|
231
|
-
end
|
232
|
-
|
233
|
-
expect(finder.result.map { |n| n[:id] }).to eq [3, 1]
|
234
|
-
end
|
235
|
-
end
|
236
|
-
|
237
|
-
describe "Predicates, Paging and Sorting together" do
|
238
|
-
it "sets the property and works with eq predicate and paging" do
|
239
|
-
entity = [
|
240
|
-
{id: 1, value: "Test 1"},
|
241
|
-
{id: 2, value: "Test 2"},
|
242
|
-
{id: 3, value: "Test 1"},
|
243
|
-
{id: 4, value: "Test 4"},
|
244
|
-
{id: 5, value: "Test 1"}
|
245
|
-
]
|
246
|
-
|
247
|
-
finder = new_finder entity, value_eq: "Test 1", sort: "id desc", page: 2 do
|
248
|
-
paging per_page: 2, min_per_page: 1, max_per_page: 5
|
249
|
-
property :value, type: Types::String, sortable: true
|
250
|
-
property :id, type: Types::Integer, sortable: true
|
251
|
-
end
|
252
|
-
|
253
|
-
expect(finder.result.map { |n| n[:id] }).to eq [1]
|
254
|
-
expect(finder.count).to eq 1
|
255
|
-
end
|
256
|
-
end
|
257
|
-
end
|
258
|
-
end
|
259
|
-
end
|
260
|
-
end
|
@@ -1,115 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper_active_record"
|
4
|
-
require "kaminari"
|
5
|
-
require "kaminari/activerecord"
|
6
|
-
|
7
|
-
module Trailblazer
|
8
|
-
class Finder
|
9
|
-
module Adapters
|
10
|
-
describe Kaminari do
|
11
|
-
after do
|
12
|
-
Product.delete_all
|
13
|
-
Product.reset_pk_sequence
|
14
|
-
end
|
15
|
-
|
16
|
-
def define_finder_class(&block)
|
17
|
-
Class.new(Trailblazer::Finder) do
|
18
|
-
# adapters ActiveRecord, Kaminari
|
19
|
-
entity { Product }
|
20
|
-
|
21
|
-
class_eval(&block)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def finder_class(&block)
|
26
|
-
define_finder_class do
|
27
|
-
class_eval(&block) unless block.nil?
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def new_finder(filter = {}, &block)
|
32
|
-
finder_class(&block).new params: filter
|
33
|
-
end
|
34
|
-
|
35
|
-
describe "#adapter" do
|
36
|
-
it "cannot use kaminari without an actual orm" do
|
37
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
38
|
-
finder = new_finder do
|
39
|
-
paginator :Kaminari
|
40
|
-
paging per_page: 2, min_per_page: 1, max_per_page: 5
|
41
|
-
end
|
42
|
-
|
43
|
-
expect(finder.errors).to eq [{paginator: "Can't use paginator Kaminari without using an ORM like ActiveRecord or Sequel"}]
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
describe "#paging" do
|
48
|
-
it "sets the paging values and shows only the first page results" do
|
49
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
50
|
-
finder = new_finder do
|
51
|
-
adapter :ActiveRecord
|
52
|
-
paginator "Kaminari"
|
53
|
-
paging per_page: 2, min_per_page: 1, max_per_page: 5
|
54
|
-
end
|
55
|
-
|
56
|
-
expect(finder.result.map { |n| n[:name] }).to eq %w[product_0 product_1]
|
57
|
-
end
|
58
|
-
|
59
|
-
it "accepts per_page as a parameter" do
|
60
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
61
|
-
finder = new_finder page: 2, per_page: 4 do
|
62
|
-
adapter "ActiveRecord"
|
63
|
-
paginator "Kaminari"
|
64
|
-
paging per_page: 5, min_per_page: 2, max_per_page: 8
|
65
|
-
end
|
66
|
-
|
67
|
-
expect(finder.result.first.id).to eq 5
|
68
|
-
expect(finder.result.map(&:id)).to eq [5, 6, 7, 8]
|
69
|
-
end
|
70
|
-
|
71
|
-
it "uses max_per_page in finder as maximum per_page" do
|
72
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
73
|
-
finder = new_finder page: 2, per_page: 9 do
|
74
|
-
adapter "ActiveRecord"
|
75
|
-
paginator "Kaminari"
|
76
|
-
paging per_page: 5, min_per_page: 2, max_per_page: 8
|
77
|
-
end
|
78
|
-
|
79
|
-
expect(finder.result.first.id).to eq 9
|
80
|
-
expect(finder.result.map(&:id)).to eq [9, 10]
|
81
|
-
end
|
82
|
-
|
83
|
-
it "uses min_per_page in finder as minimum per_page" do
|
84
|
-
10.times { |i| Product.create name: "product_#{i}" }
|
85
|
-
finder = new_finder page: 2, per_page: 1 do
|
86
|
-
adapter "ActiveRecord"
|
87
|
-
paginator "Kaminari"
|
88
|
-
paging per_page: 5, min_per_page: 2, max_per_page: 8
|
89
|
-
end
|
90
|
-
|
91
|
-
expect(finder.result.first.id).to eq 3
|
92
|
-
expect(finder.result.map(&:id)).to eq [3, 4]
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
describe "Predicates, Paging and Sorting together" do
|
97
|
-
it "sets the property and works with eq predicate and paging" do
|
98
|
-
5.times { |i| Product.create name: "product_#{i}" }
|
99
|
-
5.times { |_i| Product.create name: "product" }
|
100
|
-
finder = new_finder name_eq: "product", sort: "id desc", page: 2 do
|
101
|
-
adapter "ActiveRecord"
|
102
|
-
paginator "Kaminari"
|
103
|
-
paging per_page: 2, min_per_page: 1, max_per_page: 5
|
104
|
-
property :name, type: Types::String
|
105
|
-
property :id, type: Types::Integer, sortable: true
|
106
|
-
end
|
107
|
-
|
108
|
-
expect(finder.result.map(&:id)).to eq [8, 7]
|
109
|
-
expect(finder.count).to eq 2
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|