Skip to content

Commit

Permalink
Add keyset test
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jul 9, 2024
1 parent be0da1a commit 53414fa
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
10 changes: 7 additions & 3 deletions .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ SimpleCov.formatter = if ENV['COVERAGE_REPORT'] == 'true'

SimpleCov.start do
command_name "Task##{$PROCESS_ID}" # best way to get a different id for the specific task
merge_timeout 60
merge_timeout 120
enable_coverage :branch

add_group 'All Extras', %w[gem/lib/pagy/extras]
add_group 'Core', %w[gem/lib/pagy.rb
gem/lib/pagy/b64.rb
gem/lib/pagy/backend.rb
gem/lib/pagy/console.rb
gem/lib/pagy/countless.rb
gem/lib/pagy/init_vars.rb
gem/lib/pagy/exceptions.rb
gem/lib/pagy/frontend.rb
gem/lib/pagy/i18n.rb
gem/lib/pagy/url_helpers.rb]
add_group 'Countless', %w[gem/lib/pagy/countless.rb
gem/lib/pagy/extras/countless.rb]
add_group 'Calendar', %w[gem/lib/pagy/extras/calendar.rb
gem/lib/pagy/calendar]
add_group 'Calendar', %w[gem/lib/pagy/calendar
gem/lib/pagy/extras/calendar.rb]
add_group 'Keyset', %w[gem/lib/pagy/keyset.rb
gem/lib/pagy/extras/keyset.rb]
add_group 'Tests', %w[test]
end
Empty file added test/files/db/keyset.sqlite3
Empty file.
Empty file.
73 changes: 73 additions & 0 deletions test/pagy/keyset_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../files/models/pets'
require_relative '../../gem/lib/pagy/b64'

require 'pagy/keyset'

describe 'Pagy::Keyset' do
describe '#initialize' do
it 'raises ArgumentError without arguments' do
err = assert_raises(ArgumentError) { Pagy::Keyset.new }
assert_match(/missing keyword: :scope/, err.message)
end
it 'raises ArgumentError without arguments' do
err = assert_raises(Pagy::InternalError) { Pagy::Keyset.new(scope: Pet.all) }
assert_match(/The :scope must be ordered/, err.message)
end
it 'is an instance of Pagy::Keyset' do
_(Pagy::Keyset.new(scope: Pet.order(:id))).must_be_instance_of Pagy::Keyset
end
it 'raises Pagy::InternalError for inconsistent page/cursor' do
page_animal_id = Pagy::B64.urlsafe_encode({animal: 'dog', id: 23}.to_json)
err = assert_raises(Pagy::InternalError) do
Pagy::Keyset.new(scope: Pet.order(:id), items: 10, page: page_animal_id)
end
assert_match(/Order and page cursor are not consistent/, err.message)
end
end
describe '#setup_order' do
it 'extracts the scope order' do
pagy = Pagy::Keyset.new(scope: Pet.order(:id))
_(pagy.instance_variable_get(:@order)).must_equal({id: :asc})
pagy = Pagy::Keyset.new(scope: Pet.order(id: :desc))
_(pagy.instance_variable_get(:@order)).must_equal({id: :desc})
pagy = Pagy::Keyset.new(scope: Pet.order(:id, animal: :desc))
_(pagy.instance_variable_get(:@order)).must_equal({id: :asc, animal: :desc})
end
end
describe 'handles the page/cursor' do
it 'handles the page/cursor for the first page' do
pagy = Pagy::Keyset.new(scope: Pet.order(:id), items: 10)
_(pagy.cursor).must_be_nil
_(pagy.next).must_equal "eyJpZCI6MTB9"
end
it 'handles the page/cursor for the second page' do
pagy = Pagy::Keyset.new(scope: Pet.order(:id), items: 10, page: "eyJpZCI6MTB9")
_(pagy.cursor).must_equal({id: 10})
_(pagy.records.first.id).must_equal 11
_(pagy.next).must_equal "eyJpZCI6MjB9"
end
it 'handles the page/cursor for the last page' do
pagy = Pagy::Keyset.new(scope: Pet.order(:id), items: 10, page: "eyJpZCI6NDB9")
_(pagy.next).must_be_nil
end
end
describe 'other requirements' do
it 'adds the required columns to the selected values' do
pagy = Pagy::Keyset.new(scope: Pet.order(:animal, :name, :id).select(:name), items: 10)
pagy.records
_(pagy.instance_variable_get(:@scope).select_values.sort).must_equal %i[animal name id].sort
end
it 'use the :row_comparison' do
pagy = Pagy::Keyset.new(scope: Pet.order(:animal, :name, :id),
page: "eyJhbmltYWwiOiJjYXQiLCJuYW1lIjoiRWxsYSIsImlkIjoxOH0",
items: 10,
row_comparison: true)
records = pagy.records
_(records.size).must_equal 10
_(records.first.id).must_equal 13
end
end
end

0 comments on commit 53414fa

Please sign in to comment.