Skip to content

Commit

Permalink
reorganization and renaming of test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed May 14, 2019
1 parent 459edaa commit 405b4d2
Show file tree
Hide file tree
Showing 29 changed files with 440 additions and 517 deletions.
28 changes: 28 additions & 0 deletions test/mock_helpers/collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class MockCollection < Array

def initialize(arr=Array(1..1000))
super
@collection = self.clone
end

def offset(value)
@collection = self[value..-1]
self
end

def limit(value)
@collection[0, value]
end

def count(*)
size
end

class Grouped < MockCollection

def count(*)
Hash[@collection.map { |value| [value, value + 1] }]
end

end
end
27 changes: 27 additions & 0 deletions test/mock_helpers/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# encoding: utf-8
# frozen_string_literal: true

require_relative 'collection'

class MockController
include Pagy::Backend
# we ned to explicitly include this because Pagy::Backend
# does not include it when the test loads this module witout the headers
include Pagy::Helpers

attr_reader :params

def initialize(params={a: 'a', page: 3}, url='https://example.com:8080/foo?page=3')
@params = params
@url = url
end

def request
@request ||= Rack::Request.new(Rack::MockRequest.env_for(@url))
end

def response
@response ||= Rack::Response.new
end

end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'pagy/extras/elasticsearch_rails'

module ElasticsearchRailsTest
module MockElasticsearchRails

RESULTS = { 'a' => ('a-1'..'a-1000').to_a,
'b' => ('b-1'..'b-1000').to_a }
Expand Down Expand Up @@ -34,14 +34,14 @@ def count
end

end
end

class ElasticsearchRailsModel
class Model

def self.search(*args)
ElasticsearchRailsTest::Response.new(*args)
end
def self.search(*args)
Response.new(*args)
end

extend Pagy::Search
extend Pagy::Search
end

end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'pagy/extras/searchkick'

module SearchkickTest
module MockSearchkick

RESULTS = { 'a' => ('a-1'..'a-1000').to_a,
'b' => ('b-1'..'b-1000').to_a }
Expand Down Expand Up @@ -28,13 +28,15 @@ def total_count
end

end
end

class SearchkickModel
class Model

def self.search(*args, &block)
SearchkickTest::Results.new(*args, &block)
end
def self.search(*args, &block)
Results.new(*args, &block)
end

extend Pagy::Search
extend Pagy::Search
end
end


32 changes: 32 additions & 0 deletions test/mock_helpers/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# encoding: utf-8
# frozen_string_literal: true

class MockView
include Pagy::Frontend

def initialize(url='http://example.com:3000/foo?page=2')
@url = url
end

def request
Rack::Request.new(Rack::MockRequest.env_for(@url))
end

class Overridden < MockView
def pagy_get_params(params)
params.except(:a).merge!(k: 'k')
end
end
end


class Hash
def except!(*keys)
keys.each { |key| delete(key) }
self
end

def except(*keys)
dup.except!(*keys)
end
end
26 changes: 13 additions & 13 deletions test/pagy/backend_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@

describe Pagy::Backend do

let(:backend) { TestController.new }
let(:controller) { MockController.new }

describe "#pagy" do

before do
@collection = TestCollection.new((1..1000).to_a)
@collection = MockCollection.new
end

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

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

before do
@collection = TestCollection.new((1..1000).to_a)
@collection = MockCollection.new
end

it 'gets defaults' do
vars = {}
merged = backend.send :pagy_get_vars, @collection, vars
merged = controller.send :pagy_get_vars, @collection, vars
merged.keys.must_include :count
merged.keys.must_include :page
merged[:count].must_equal 1000
Expand All @@ -53,7 +53,7 @@

it 'gets vars' do
vars = {page: 2, items: 10, link_extra: 'X'}
merged = backend.send :pagy_get_vars, @collection, vars
merged = controller.send :pagy_get_vars, @collection, vars
merged.keys.must_include :count
merged.keys.must_include :page
merged.keys.must_include :items
Expand All @@ -65,9 +65,9 @@
end

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

it 'overrides count and page' do
vars = {count: 10, page: 32}
merged = backend.send :pagy_get_vars, @collection, vars
merged = controller.send :pagy_get_vars, @collection, vars
merged.keys.must_include :count
merged[:count].must_equal 10
merged.keys.must_include :page
Expand All @@ -92,9 +92,9 @@
describe "#pagy_get_items" do

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

Expand Down
18 changes: 9 additions & 9 deletions test/pagy/countless_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

describe Pagy::Countless do

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

describe "#finalize" do

before do
@empty_collection = TestCollection.new([])
@collection = TestCollection.new(Array(1..59))
@empty_collection = MockCollection.new([])
@collection = MockCollection.new(Array(1..59))
end

let(:last_page) { 3 }
Expand All @@ -22,7 +22,7 @@
end

it 'initializes empty collection' do
pagy, _ = backend.send(:pagy_countless, @empty_collection, page: 1)
pagy, _ = controller.send(:pagy_countless, @empty_collection, page: 1)
pagy.items.must_equal 20
pagy.pages.must_equal 1
pagy.last.must_equal 1
Expand All @@ -33,7 +33,7 @@
end

it 'initializes first page' do
pagy, _ = backend.send(:pagy_countless, @collection, page: 1)
pagy, _ = controller.send(:pagy_countless, @collection, page: 1)
pagy.must_be_instance_of Pagy::Countless
pagy.items.must_equal 20
pagy.last.must_equal 2
Expand All @@ -45,7 +45,7 @@
end

it 'initializes single full page' do
pagy, _ = backend.send(:pagy_countless, TestCollection.new(Array(1..20)), page: 1)
pagy, _ = controller.send(:pagy_countless, MockCollection.new(Array(1..20)), page: 1)
pagy.items.must_equal 20
pagy.pages.must_equal 1
pagy.from.must_equal 1
Expand All @@ -55,7 +55,7 @@
end

it 'initialize single partial page' do
pagy, _ = backend.send(:pagy_countless, TestCollection.new(Array(1..4)), page: 1)
pagy, _ = controller.send(:pagy_countless, MockCollection.new(Array(1..4)), page: 1)
pagy.items.must_equal 4
pagy.pages.must_equal 1
pagy.from.must_equal 1
Expand All @@ -66,7 +66,7 @@
end

it 'initializes last partial page' do
pagy, _ = backend.send(:pagy_countless, @collection, page: last_page)
pagy, _ = controller.send(:pagy_countless, @collection, page: last_page)
pagy.items.must_equal 19
pagy.pages.must_equal last_page
pagy.from.must_equal 41
Expand All @@ -76,7 +76,7 @@
end

it 'handles the :cycle variable' do
pagy, _ = backend.send(:pagy_countless, @collection, page: last_page, cycle: true)
pagy, _ = controller.send(:pagy_countless, @collection, page: last_page, cycle: true)
pagy.items.must_equal 19
pagy.pages.must_equal last_page
pagy.from.must_equal 41
Expand Down
12 changes: 6 additions & 6 deletions test/pagy/extras/array_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

describe Pagy::Backend do

let(:backend) { TestController.new }
let(:controller) { MockController.new }

describe "#pagy_array" do

Expand All @@ -15,17 +15,17 @@
end

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

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

it 'gets defaults' do
vars = {}
merged = backend.send :pagy_array_get_vars, @collection, vars
merged = controller.send :pagy_array_get_vars, @collection, vars
merged.keys.must_include :count
merged.keys.must_include :page
merged[:count].must_equal 1000
Expand All @@ -54,7 +54,7 @@

it 'gets vars' do
vars = {page: 2, items: 10, link_extra: 'X'}
merged = backend.send :pagy_array_get_vars, @collection, vars
merged = controller.send :pagy_array_get_vars, @collection, vars
merged.keys.must_include :count
merged.keys.must_include :page
merged.keys.must_include :items
Expand Down
Loading

0 comments on commit 405b4d2

Please sign in to comment.