Skip to content

Commit 405b4d2

Browse files
committed
reorganization and renaming of test helpers
1 parent 459edaa commit 405b4d2

29 files changed

Lines changed: 440 additions & 517 deletions
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class MockCollection < Array
2+
3+
def initialize(arr=Array(1..1000))
4+
super
5+
@collection = self.clone
6+
end
7+
8+
def offset(value)
9+
@collection = self[value..-1]
10+
self
11+
end
12+
13+
def limit(value)
14+
@collection[0, value]
15+
end
16+
17+
def count(*)
18+
size
19+
end
20+
21+
class Grouped < MockCollection
22+
23+
def count(*)
24+
Hash[@collection.map { |value| [value, value + 1] }]
25+
end
26+
27+
end
28+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# encoding: utf-8
2+
# frozen_string_literal: true
3+
4+
require_relative 'collection'
5+
6+
class MockController
7+
include Pagy::Backend
8+
# we ned to explicitly include this because Pagy::Backend
9+
# does not include it when the test loads this module witout the headers
10+
include Pagy::Helpers
11+
12+
attr_reader :params
13+
14+
def initialize(params={a: 'a', page: 3}, url='https://example.com:8080/foo?page=3')
15+
@params = params
16+
@url = url
17+
end
18+
19+
def request
20+
@request ||= Rack::Request.new(Rack::MockRequest.env_for(@url))
21+
end
22+
23+
def response
24+
@response ||= Rack::Response.new
25+
end
26+
27+
end
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'pagy/extras/elasticsearch_rails'
22

3-
module ElasticsearchRailsTest
3+
module MockElasticsearchRails
44

55
RESULTS = { 'a' => ('a-1'..'a-1000').to_a,
66
'b' => ('b-1'..'b-1000').to_a }
@@ -34,14 +34,14 @@ def count
3434
end
3535

3636
end
37-
end
3837

39-
class ElasticsearchRailsModel
38+
class Model
4039

41-
def self.search(*args)
42-
ElasticsearchRailsTest::Response.new(*args)
43-
end
40+
def self.search(*args)
41+
Response.new(*args)
42+
end
4443

45-
extend Pagy::Search
44+
extend Pagy::Search
45+
end
4646

4747
end
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'pagy/extras/searchkick'
22

3-
module SearchkickTest
3+
module MockSearchkick
44

55
RESULTS = { 'a' => ('a-1'..'a-1000').to_a,
66
'b' => ('b-1'..'b-1000').to_a }
@@ -28,13 +28,15 @@ def total_count
2828
end
2929

3030
end
31-
end
3231

33-
class SearchkickModel
32+
class Model
3433

35-
def self.search(*args, &block)
36-
SearchkickTest::Results.new(*args, &block)
37-
end
34+
def self.search(*args, &block)
35+
Results.new(*args, &block)
36+
end
3837

39-
extend Pagy::Search
38+
extend Pagy::Search
39+
end
4040
end
41+
42+

β€Žtest/mock_helpers/view.rbβ€Ž

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# encoding: utf-8
2+
# frozen_string_literal: true
3+
4+
class MockView
5+
include Pagy::Frontend
6+
7+
def initialize(url='http://example.com:3000/foo?page=2')
8+
@url = url
9+
end
10+
11+
def request
12+
Rack::Request.new(Rack::MockRequest.env_for(@url))
13+
end
14+
15+
class Overridden < MockView
16+
def pagy_get_params(params)
17+
params.except(:a).merge!(k: 'k')
18+
end
19+
end
20+
end
21+
22+
23+
class Hash
24+
def except!(*keys)
25+
keys.each { |key| delete(key) }
26+
self
27+
end
28+
29+
def except(*keys)
30+
dup.except!(*keys)
31+
end
32+
end

β€Žtest/pagy/backend_test.rbβ€Ž

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@
55

66
describe Pagy::Backend do
77

8-
let(:backend) { TestController.new }
8+
let(:controller) { MockController.new }
99

1010
describe "#pagy" do
1111

1212
before do
13-
@collection = TestCollection.new((1..1000).to_a)
13+
@collection = MockCollection.new
1414
end
1515

1616
it 'paginates with defaults' do
17-
pagy, records = backend.send(:pagy, @collection)
17+
pagy, records = controller.send(:pagy, @collection)
1818
pagy.must_be_instance_of Pagy
1919
pagy.count.must_equal 1000
2020
pagy.items.must_equal Pagy::VARS[:items]
21-
pagy.page.must_equal backend.params[:page]
21+
pagy.page.must_equal controller.params[:page]
2222
records.count.must_equal Pagy::VARS[:items]
2323
records.must_equal [41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]
2424
end
2525

2626
it 'paginates with vars' do
27-
pagy, records = backend.send(:pagy, @collection, page: 2, items: 10, link_extra: 'X')
27+
pagy, records = controller.send(:pagy, @collection, page: 2, items: 10, link_extra: 'X')
2828
pagy.must_be_instance_of Pagy
2929
pagy.count.must_equal 1000
3030
pagy.items.must_equal 10
@@ -39,12 +39,12 @@
3939
describe "#pagy_get_vars" do
4040

4141
before do
42-
@collection = TestCollection.new((1..1000).to_a)
42+
@collection = MockCollection.new
4343
end
4444

4545
it 'gets defaults' do
4646
vars = {}
47-
merged = backend.send :pagy_get_vars, @collection, vars
47+
merged = controller.send :pagy_get_vars, @collection, vars
4848
merged.keys.must_include :count
4949
merged.keys.must_include :page
5050
merged[:count].must_equal 1000
@@ -53,7 +53,7 @@
5353

5454
it 'gets vars' do
5555
vars = {page: 2, items: 10, link_extra: 'X'}
56-
merged = backend.send :pagy_get_vars, @collection, vars
56+
merged = controller.send :pagy_get_vars, @collection, vars
5757
merged.keys.must_include :count
5858
merged.keys.must_include :page
5959
merged.keys.must_include :items
@@ -65,9 +65,9 @@
6565
end
6666

6767
it 'works with grouped collections' do
68-
@collection = TestGroupedCollection.new((1..1000).to_a)
68+
@collection = MockCollection::Grouped.new((1..1000).to_a)
6969
vars = {page: 2, items: 10, link_extra: 'X'}
70-
merged = backend.send :pagy_get_vars, @collection, vars
70+
merged = controller.send :pagy_get_vars, @collection, vars
7171
merged.keys.must_include :count
7272
merged.keys.must_include :page
7373
merged.keys.must_include :items
@@ -80,7 +80,7 @@
8080

8181
it 'overrides count and page' do
8282
vars = {count: 10, page: 32}
83-
merged = backend.send :pagy_get_vars, @collection, vars
83+
merged = controller.send :pagy_get_vars, @collection, vars
8484
merged.keys.must_include :count
8585
merged[:count].must_equal 10
8686
merged.keys.must_include :page
@@ -92,9 +92,9 @@
9292
describe "#pagy_get_items" do
9393

9494
it 'gets items' do
95-
collection = TestCollection.new((1..1000).to_a)
95+
collection = MockCollection.new
9696
pagy = Pagy.new count: 1000
97-
items = backend.send :pagy_get_items, collection, pagy
97+
items = controller.send :pagy_get_items, collection, pagy
9898
items.must_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
9999
end
100100

β€Žtest/pagy/countless_test.rbβ€Ž

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
describe Pagy::Countless do
88

9-
let(:backend) { TestController.new } # page = 3, items = 20
9+
let(:controller) { MockController.new } # page = 3, items = 20
1010

1111
describe "#finalize" do
1212

1313
before do
14-
@empty_collection = TestCollection.new([])
15-
@collection = TestCollection.new(Array(1..59))
14+
@empty_collection = MockCollection.new([])
15+
@collection = MockCollection.new(Array(1..59))
1616
end
1717

1818
let(:last_page) { 3 }
@@ -22,7 +22,7 @@
2222
end
2323

2424
it 'initializes empty collection' do
25-
pagy, _ = backend.send(:pagy_countless, @empty_collection, page: 1)
25+
pagy, _ = controller.send(:pagy_countless, @empty_collection, page: 1)
2626
pagy.items.must_equal 20
2727
pagy.pages.must_equal 1
2828
pagy.last.must_equal 1
@@ -33,7 +33,7 @@
3333
end
3434

3535
it 'initializes first page' do
36-
pagy, _ = backend.send(:pagy_countless, @collection, page: 1)
36+
pagy, _ = controller.send(:pagy_countless, @collection, page: 1)
3737
pagy.must_be_instance_of Pagy::Countless
3838
pagy.items.must_equal 20
3939
pagy.last.must_equal 2
@@ -45,7 +45,7 @@
4545
end
4646

4747
it 'initializes single full page' do
48-
pagy, _ = backend.send(:pagy_countless, TestCollection.new(Array(1..20)), page: 1)
48+
pagy, _ = controller.send(:pagy_countless, MockCollection.new(Array(1..20)), page: 1)
4949
pagy.items.must_equal 20
5050
pagy.pages.must_equal 1
5151
pagy.from.must_equal 1
@@ -55,7 +55,7 @@
5555
end
5656

5757
it 'initialize single partial page' do
58-
pagy, _ = backend.send(:pagy_countless, TestCollection.new(Array(1..4)), page: 1)
58+
pagy, _ = controller.send(:pagy_countless, MockCollection.new(Array(1..4)), page: 1)
5959
pagy.items.must_equal 4
6060
pagy.pages.must_equal 1
6161
pagy.from.must_equal 1
@@ -66,7 +66,7 @@
6666
end
6767

6868
it 'initializes last partial page' do
69-
pagy, _ = backend.send(:pagy_countless, @collection, page: last_page)
69+
pagy, _ = controller.send(:pagy_countless, @collection, page: last_page)
7070
pagy.items.must_equal 19
7171
pagy.pages.must_equal last_page
7272
pagy.from.must_equal 41
@@ -76,7 +76,7 @@
7676
end
7777

7878
it 'handles the :cycle variable' do
79-
pagy, _ = backend.send(:pagy_countless, @collection, page: last_page, cycle: true)
79+
pagy, _ = controller.send(:pagy_countless, @collection, page: last_page, cycle: true)
8080
pagy.items.must_equal 19
8181
pagy.pages.must_equal last_page
8282
pagy.from.must_equal 41

β€Žtest/pagy/extras/array_test.rbβ€Ž

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
describe Pagy::Backend do
88

9-
let(:backend) { TestController.new }
9+
let(:controller) { MockController.new }
1010

1111
describe "#pagy_array" do
1212

@@ -15,17 +15,17 @@
1515
end
1616

1717
it 'paginates with defaults' do
18-
pagy, items = backend.send(:pagy_array, @collection)
18+
pagy, items = controller.send(:pagy_array, @collection)
1919
pagy.must_be_instance_of Pagy
2020
pagy.count.must_equal 1000
2121
pagy.items.must_equal Pagy::VARS[:items]
22-
pagy.page.must_equal backend.params[:page]
22+
pagy.page.must_equal controller.params[:page]
2323
items.count.must_equal Pagy::VARS[:items]
2424
items.must_equal [41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]
2525
end
2626

2727
it 'paginates with vars' do
28-
pagy, items = backend.send(:pagy_array, @collection, page: 2, items: 10, link_extra: 'X')
28+
pagy, items = controller.send(:pagy_array, @collection, page: 2, items: 10, link_extra: 'X')
2929
pagy.must_be_instance_of Pagy
3030
pagy.count.must_equal 1000
3131
pagy.items.must_equal 10
@@ -45,7 +45,7 @@
4545

4646
it 'gets defaults' do
4747
vars = {}
48-
merged = backend.send :pagy_array_get_vars, @collection, vars
48+
merged = controller.send :pagy_array_get_vars, @collection, vars
4949
merged.keys.must_include :count
5050
merged.keys.must_include :page
5151
merged[:count].must_equal 1000
@@ -54,7 +54,7 @@
5454

5555
it 'gets vars' do
5656
vars = {page: 2, items: 10, link_extra: 'X'}
57-
merged = backend.send :pagy_array_get_vars, @collection, vars
57+
merged = controller.send :pagy_array_get_vars, @collection, vars
5858
merged.keys.must_include :count
5959
merged.keys.must_include :page
6060
merged.keys.must_include :items

0 commit comments

Comments
 (0)