[Search] Fix sequential prev/next buttons

Apparently this broke with the upgrade to rails 7, see cae6599631
The overwritten records method is at fault.
Added some tests to confirm that both elastic and active record
return the correct result to prevent breakage in the future.
This commit is contained in:
Earlopain 2023-05-29 20:12:14 +02:00
parent 85206a4023
commit 9035f845ee
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
3 changed files with 46 additions and 51 deletions

View File

@ -26,7 +26,8 @@ module Danbooru
when :sequential_before
false
when :sequential_after
records.size <= records_per_page
load
@records.size <= records_per_page
end
end
@ -35,7 +36,8 @@ module Danbooru
when :numbered
current_page >= total_pages
when :sequential_before
records.size <= records_per_page
load
@records.size <= records_per_page
when :sequential_after
false
end

View File

@ -1,34 +1,52 @@
require 'test_helper'
require "test_helper"
class PaginatorTest < ActiveSupport::TestCase
setup do
@posts = create_list(:post, 5)
def assert_paginated(expected_records:, is_first_page:, is_last_page:, &)
records = yield
assert_equal(expected_records.map(&:id), records.map(&:id))
assert_equal(is_first_page, records.is_first_page?, "is_first_page")
assert_equal(is_last_page, records.is_last_page?, "is_last_page")
end
context "sequential pagination (before)" do
should "return the correct set of records" do
expected_posts = Post.limit(3).order(id: :desc)
posts = Post.paginate("b9999999", limit: 3)
{ active_record: Blip, elasticsearch: Post }.each do |name, model| # rubocop:disable Metrics/BlockLength
context name do
context "sequential pagination (before)" do
should "return the correct set of records" do
@records = create_list(model.name.underscore, 4)
assert_paginated(expected_records: [], is_first_page: false, is_last_page: true) { model.paginate("b#{@records[0].id}", limit: 2) }
assert_paginated(expected_records: [@records[0]], is_first_page: false, is_last_page: true) { model.paginate("b#{@records[1].id}", limit: 2) }
assert_paginated(expected_records: [@records[1], @records[0]], is_first_page: false, is_last_page: true) { model.paginate("b#{@records[2].id}", limit: 2) }
assert_paginated(expected_records: [@records[2], @records[1]], is_first_page: false, is_last_page: false) { model.paginate("b#{@records[3].id}", limit: 2) }
assert_paginated(expected_records: [@records[3], @records[2]], is_first_page: false, is_last_page: false) { model.paginate("b999999999", limit: 2) }
end
end
assert_equal(expected_posts.map(&:id), posts.map(&:id))
end
end
context "sequential pagination (after)" do
should "return the correct set of records" do
@records = create_list(model.name.underscore, 4)
assert_paginated(expected_records: [@records[1], @records[0]], is_first_page: false, is_last_page: false) { model.paginate("a0", limit: 2) }
assert_paginated(expected_records: [@records[2], @records[1]], is_first_page: false, is_last_page: false) { model.paginate("a#{@records[0].id}", limit: 2) }
assert_paginated(expected_records: [@records[3], @records[2]], is_first_page: true, is_last_page: false) { model.paginate("a#{@records[1].id}", limit: 2) }
assert_paginated(expected_records: [@records[3]], is_first_page: true, is_last_page: false) { model.paginate("a#{@records[2].id}", limit: 2) }
assert_paginated(expected_records: [], is_first_page: true, is_last_page: false) { model.paginate("a#{@records[3].id}", limit: 2) }
end
end
context "sequential pagination (after)" do
should "return the correct set of records" do
expected_posts = Post.limit(3).order(id: :asc).reverse
posts = Post.paginate("a0", limit: 3)
context "numbered pagination" do
should "return the correct set of records" do
@records = create_list(model.name.underscore, 4)
assert_paginated(expected_records: [@records[0]], is_first_page: true, is_last_page: false) { model.paginate("1", limit: 1) }
assert_paginated(expected_records: [@records[1]], is_first_page: false, is_last_page: false) { model.paginate("2", limit: 1) }
assert_paginated(expected_records: [@records[2]], is_first_page: false, is_last_page: false) { model.paginate("3", limit: 1) }
assert_paginated(expected_records: [@records[3]], is_first_page: false, is_last_page: true) { model.paginate("4", limit: 1) }
assert_paginated(expected_records: [], is_first_page: false, is_last_page: true) { model.paginate("5", limit: 1) }
end
assert_equal(expected_posts.map(&:id), posts.map(&:id))
end
end
context "numbered pagination" do
should "return the correct set of records" do
expected_posts = Post.limit(3).order(id: :desc)
posts = Post.order(id: :desc).paginate("1", limit: 3)
assert_equal(expected_posts.map(&:id), posts.map(&:id))
should "return the correct set of records when only one result exists" do
@records = create_list(model.name.underscore, 2)
assert_paginated(expected_records: [@records[0], @records[1]], is_first_page: true, is_last_page: true) { model.paginate("1", limit: 2) }
end
end
end
end
end

View File

@ -44,11 +44,6 @@ module PostSets
should "return the second most recent element" do
assert_equal(@post_1.id, @set.posts.first.id)
end
should_eventually "know what page it's on" do
refute(@set.posts.is_first_page?)
refute(@set.posts.is_last_page?)
end
end
end
@ -62,11 +57,6 @@ module PostSets
should "return the third most recent element" do
assert_equal(@post_2.id, @set.posts.first.id)
end
should_eventually "know what page it's on" do
refute(@set.posts.is_first_page?)
assert(@set.posts.is_last_page?)
end
end
end
@ -80,11 +70,6 @@ module PostSets
should "return the most recent element" do
assert_equal(@post_3.id, @set.posts.first.id)
end
should "know what page it's on" do
assert(@set.posts.is_first_page?)
refute(@set.posts.is_last_page?)
end
end
end
@ -97,11 +82,6 @@ module PostSets
should "return the second most recent element" do
assert_equal(@post_1.id, @set.posts.first.id)
end
should "know what page it's on" do
refute(@set.posts.is_first_page?)
refute(@set.posts.is_last_page?)
end
end
end
@ -113,11 +93,6 @@ module PostSets
should "return the most recent element" do
assert_equal(@post_3.id, @set.posts.first.id)
end
should "know what page it's on" do
assert(@set.posts.is_first_page?)
refute(@set.posts.is_last_page?)
end
end
end
end