|
| 1 | +require "../../spec_helper" |
| 2 | + |
| 3 | +# Cursor-based pagination tests |
| 4 | +# Tests after_cursor, before_cursor, and paginate_by methods |
| 5 | + |
| 6 | +CursorPaginationDB = CQL::Schema.define( |
| 7 | + :cursor_pagination_db, |
| 8 | + adapter: CQL::Adapter::SQLite, |
| 9 | + uri: "sqlite3://./spec/support/db/cursor_pagination.db" |
| 10 | +) do |
| 11 | + table :cursor_test_records do |
| 12 | + primary :id, Int64, auto_increment: true |
| 13 | + text :name |
| 14 | + bigint :sort_order |
| 15 | + timestamp :created_at |
| 16 | + end |
| 17 | +end |
| 18 | + |
| 19 | +class CursorTestRecord |
| 20 | + include CQL::ActiveRecord::Model(Int64) |
| 21 | + |
| 22 | + db_context schema: CursorPaginationDB, table: :cursor_test_records |
| 23 | + |
| 24 | + property name : String = "" |
| 25 | + property sort_order : Int64 = 0_i64 |
| 26 | + property created_at : Time? |
| 27 | +end |
| 28 | + |
| 29 | +describe "Cursor-based Pagination" do |
| 30 | + before_all do |
| 31 | + CursorPaginationDB.cursor_test_records.drop! |
| 32 | + CursorPaginationDB.cursor_test_records.create! |
| 33 | + end |
| 34 | + |
| 35 | + after_all do |
| 36 | + CursorPaginationDB.cursor_test_records.drop! |
| 37 | + end |
| 38 | + |
| 39 | + before_each do |
| 40 | + CursorTestRecord.delete_all |
| 41 | + # Create test records with sequential IDs |
| 42 | + 10.times do |i| |
| 43 | + CursorTestRecord.create!( |
| 44 | + name: "Record #{i + 1}", |
| 45 | + sort_order: (i + 1).to_i64 * 10, |
| 46 | + created_at: Time.utc - (10 - i).hours |
| 47 | + ) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + describe ".after_cursor" do |
| 52 | + it "returns records after the given cursor ID" do |
| 53 | + all_records = CursorTestRecord.query.order(:id).all |
| 54 | + cursor_id = all_records[4].id! # 5th record |
| 55 | + |
| 56 | + results = CursorTestRecord.after_cursor(cursor_id, limit: 3) |
| 57 | + |
| 58 | + results.size.should eq(3) |
| 59 | + results.all? { |r| r.id! > cursor_id }.should be_true |
| 60 | + end |
| 61 | + |
| 62 | + it "returns empty array when cursor is at the end" do |
| 63 | + all_records = CursorTestRecord.query.order(:id).all |
| 64 | + last_id = all_records.last.id! |
| 65 | + |
| 66 | + results = CursorTestRecord.after_cursor(last_id, limit: 10) |
| 67 | + |
| 68 | + results.should be_empty |
| 69 | + end |
| 70 | + |
| 71 | + it "returns records in ascending order by ID" do |
| 72 | + all_records = CursorTestRecord.query.order(:id).all |
| 73 | + first_id = all_records.first.id! |
| 74 | + |
| 75 | + results = CursorTestRecord.after_cursor(first_id, limit: 5) |
| 76 | + |
| 77 | + results.size.should eq(5) |
| 78 | + ids = results.map(&.id!) |
| 79 | + ids.should eq(ids.sort) |
| 80 | + end |
| 81 | + |
| 82 | + it "respects the limit parameter" do |
| 83 | + all_records = CursorTestRecord.query.order(:id).all |
| 84 | + first_id = all_records.first.id! |
| 85 | + |
| 86 | + results = CursorTestRecord.after_cursor(first_id, limit: 2) |
| 87 | + |
| 88 | + results.size.should eq(2) |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + describe ".before_cursor" do |
| 93 | + it "returns records before the given cursor ID" do |
| 94 | + all_records = CursorTestRecord.query.order(:id).all |
| 95 | + cursor_id = all_records[5].id! # 6th record |
| 96 | + |
| 97 | + results = CursorTestRecord.before_cursor(cursor_id, limit: 3) |
| 98 | + |
| 99 | + results.size.should eq(3) |
| 100 | + results.all? { |r| r.id! < cursor_id }.should be_true |
| 101 | + end |
| 102 | + |
| 103 | + it "returns empty array when cursor is at the beginning" do |
| 104 | + all_records = CursorTestRecord.query.order(:id).all |
| 105 | + first_id = all_records.first.id! |
| 106 | + |
| 107 | + results = CursorTestRecord.before_cursor(first_id, limit: 10) |
| 108 | + |
| 109 | + results.should be_empty |
| 110 | + end |
| 111 | + |
| 112 | + it "returns records in ascending order (reversed from query)" do |
| 113 | + all_records = CursorTestRecord.query.order(:id).all |
| 114 | + last_id = all_records.last.id! |
| 115 | + |
| 116 | + results = CursorTestRecord.before_cursor(last_id, limit: 5) |
| 117 | + |
| 118 | + results.size.should eq(5) |
| 119 | + ids = results.map(&.id!) |
| 120 | + ids.should eq(ids.sort) |
| 121 | + end |
| 122 | + |
| 123 | + it "respects the limit parameter" do |
| 124 | + all_records = CursorTestRecord.query.order(:id).all |
| 125 | + last_id = all_records.last.id! |
| 126 | + |
| 127 | + results = CursorTestRecord.before_cursor(last_id, limit: 2) |
| 128 | + |
| 129 | + results.size.should eq(2) |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + describe ".paginate_by" do |
| 134 | + it "paginates by a custom column" do |
| 135 | + results = CursorTestRecord.paginate_by(:sort_order, 30_i64, limit: 3) |
| 136 | + |
| 137 | + results.size.should eq(3) |
| 138 | + results.all? { |r| r.sort_order > 30 }.should be_true |
| 139 | + end |
| 140 | + |
| 141 | + it "returns records in order of the specified column" do |
| 142 | + results = CursorTestRecord.paginate_by(:sort_order, 0_i64, limit: 5) |
| 143 | + |
| 144 | + sort_orders = results.map(&.sort_order) |
| 145 | + sort_orders.should eq(sort_orders.sort) |
| 146 | + end |
| 147 | + |
| 148 | + it "returns empty when no records match" do |
| 149 | + results = CursorTestRecord.paginate_by(:sort_order, 1000_i64, limit: 10) |
| 150 | + |
| 151 | + results.should be_empty |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + describe "pagination workflow" do |
| 156 | + it "supports forward pagination through all records" do |
| 157 | + collected_ids = [] of Int64 |
| 158 | + cursor_id = 0_i64 |
| 159 | + |
| 160 | + loop do |
| 161 | + page = CursorTestRecord.after_cursor(cursor_id, limit: 3) |
| 162 | + break if page.empty? |
| 163 | + |
| 164 | + page.each { |r| collected_ids << r.id! } |
| 165 | + cursor_id = page.last.id! |
| 166 | + end |
| 167 | + |
| 168 | + collected_ids.size.should eq(10) |
| 169 | + collected_ids.should eq(collected_ids.sort) |
| 170 | + end |
| 171 | + |
| 172 | + it "supports backward pagination through all records" do |
| 173 | + all_records = CursorTestRecord.query.order(:id).all |
| 174 | + cursor_id = all_records.last.id! + 1 # Start after last record |
| 175 | + |
| 176 | + collected_ids = [] of Int64 |
| 177 | + |
| 178 | + loop do |
| 179 | + page = CursorTestRecord.before_cursor(cursor_id, limit: 3) |
| 180 | + break if page.empty? |
| 181 | + |
| 182 | + page.reverse_each { |r| collected_ids.unshift(r.id!) } |
| 183 | + cursor_id = page.first.id! |
| 184 | + end |
| 185 | + |
| 186 | + collected_ids.size.should eq(10) |
| 187 | + collected_ids.should eq(collected_ids.sort) |
| 188 | + end |
| 189 | + end |
| 190 | +end |
0 commit comments