MCPcopy Index your code
hub / github.com/upper/db / TestPaginate

Function TestPaginate

internal/sqlbuilder/builder_test.go:1482–1561  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1480}
1481
1482func TestPaginate(t *testing.T) {
1483 b := &sqlBuilder{t: newTemplateWithUtils(&testTemplate)}
1484 assert := assert.New(t)
1485
1486 // Limit, offset
1487 assert.Equal(
1488 `SELECT * FROM "artist" LIMIT 10`,
1489 b.Select().From("artist").Paginate(10).Page(1).String(),
1490 )
1491
1492 assert.Equal(
1493 `SELECT * FROM "artist" LIMIT 10 OFFSET 10`,
1494 b.Select().From("artist").Paginate(10).Page(2).String(),
1495 )
1496
1497 assert.Equal(
1498 `SELECT * FROM "artist" LIMIT 5 OFFSET 110`,
1499 b.Select().From("artist").Paginate(5).Page(23).String(),
1500 )
1501
1502 // Cursor
1503 assert.Equal(
1504 `SELECT * FROM "artist" ORDER BY "id" ASC LIMIT 10`,
1505 b.Select().From("artist").Paginate(10).Cursor("id").String(),
1506 )
1507
1508 {
1509 q := b.Select().From("artist").Paginate(10).Cursor("id").NextPage(3)
1510 assert.Equal(
1511 `SELECT * FROM "artist" WHERE ("id" > $1) ORDER BY "id" ASC LIMIT 10`,
1512 q.String(),
1513 )
1514 assert.Equal(
1515 []interface{}{3},
1516 q.Arguments(),
1517 )
1518 }
1519
1520 {
1521 q := b.Select().From("artist").Paginate(10).Cursor("id").PrevPage(30)
1522 assert.Equal(
1523 `SELECT * FROM (SELECT * FROM "artist" WHERE ("id" < $1) ORDER BY "id" DESC LIMIT 10) AS p0 ORDER BY "id" ASC`,
1524 q.String(),
1525 )
1526 assert.Equal(
1527 []interface{}{30},
1528 q.Arguments(),
1529 )
1530 }
1531
1532 // Cursor reversed
1533 assert.Equal(
1534 `SELECT * FROM "artist" ORDER BY "id" DESC LIMIT 10`,
1535 b.Select().From("artist").Paginate(10).Cursor("-id").String(),
1536 )
1537
1538 {
1539 q := b.Select().From("artist").Paginate(10).Cursor("-id").NextPage(3)

Callers

nothing calls this directly

Calls 11

SelectMethod · 0.95
newTemplateWithUtilsFunction · 0.85
NewMethod · 0.65
StringMethod · 0.65
PageMethod · 0.65
PaginateMethod · 0.65
FromMethod · 0.65
CursorMethod · 0.65
NextPageMethod · 0.65
ArgumentsMethod · 0.65
PrevPageMethod · 0.65

Tested by

no test coverage detected