@@ -18,6 +18,8 @@ import {
1818 when ,
1919} from 'ts-mockito' ;
2020
21+ import { OrderByOrdering } from '../../BasePostgresEntityDatabaseAdapter' ;
22+ import { PaginationStrategy } from '../../PaginationStrategy' ;
2123import { PostgresEntityDatabaseAdapter } from '../../PostgresEntityDatabaseAdapter' ;
2224import {
2325 TestEntity ,
@@ -253,4 +255,124 @@ describe(EntityKnexDataManager, () => {
253255 } ) ;
254256 } ) ;
255257 } ) ;
258+
259+ describe ( 'pagination' , ( ) => {
260+ describe ( 'max page size validation' , ( ) => {
261+ it ( 'should throw when first exceeds maxPageSize' , async ( ) => {
262+ const queryContext = instance ( mock < EntityQueryContext > ( ) ) ;
263+ const databaseAdapterMock = mock <
264+ PostgresEntityDatabaseAdapter < TestFields , 'customIdField' >
265+ > ( PostgresEntityDatabaseAdapter ) ;
266+
267+ // Configure the adapter to return a maxPageSize of 100
268+ when ( databaseAdapterMock . paginationMaxPageSize ) . thenReturn ( 100 ) ;
269+
270+ const entityDataManager = new EntityKnexDataManager (
271+ testEntityConfiguration ,
272+ instance ( databaseAdapterMock ) ,
273+ new NoOpEntityMetricsAdapter ( ) ,
274+ TestEntity . name ,
275+ ) ;
276+
277+ await expect (
278+ entityDataManager . loadPageAsync ( queryContext , {
279+ first : 101 ,
280+ pagination : {
281+ strategy : PaginationStrategy . STANDARD ,
282+ orderBy : [ { fieldName : 'customIdField' , order : OrderByOrdering . ASCENDING } ] ,
283+ } ,
284+ } ) ,
285+ ) . rejects . toThrow ( 'first must not exceed maximum page size of 100' ) ;
286+ } ) ;
287+
288+ it ( 'should throw when last exceeds maxPageSize' , async ( ) => {
289+ const queryContext = instance ( mock < EntityQueryContext > ( ) ) ;
290+ const databaseAdapterMock = mock <
291+ PostgresEntityDatabaseAdapter < TestFields , 'customIdField' >
292+ > ( PostgresEntityDatabaseAdapter ) ;
293+
294+ // Configure the adapter to return a maxPageSize of 100
295+ when ( databaseAdapterMock . paginationMaxPageSize ) . thenReturn ( 100 ) ;
296+
297+ const entityDataManager = new EntityKnexDataManager (
298+ testEntityConfiguration ,
299+ instance ( databaseAdapterMock ) ,
300+ new NoOpEntityMetricsAdapter ( ) ,
301+ TestEntity . name ,
302+ ) ;
303+
304+ await expect (
305+ entityDataManager . loadPageAsync ( queryContext , {
306+ last : 101 ,
307+ pagination : {
308+ strategy : PaginationStrategy . STANDARD ,
309+ orderBy : [ { fieldName : 'customIdField' , order : OrderByOrdering . ASCENDING } ] ,
310+ } ,
311+ } ) ,
312+ ) . rejects . toThrow ( 'last must not exceed maximum page size of 100' ) ;
313+ } ) ;
314+
315+ it ( 'should allow first/last within maxPageSize' , async ( ) => {
316+ const queryContext = instance ( mock < EntityQueryContext > ( ) ) ;
317+ const databaseAdapterMock = mock <
318+ PostgresEntityDatabaseAdapter < TestFields , 'customIdField' >
319+ > ( PostgresEntityDatabaseAdapter ) ;
320+
321+ // Configure the adapter to return a maxPageSize of 100
322+ when ( databaseAdapterMock . paginationMaxPageSize ) . thenReturn ( 100 ) ;
323+ when (
324+ databaseAdapterMock . fetchManyBySQLFragmentAsync ( queryContext , anything ( ) , anything ( ) ) ,
325+ ) . thenResolve ( [ ] ) ;
326+
327+ const entityDataManager = new EntityKnexDataManager (
328+ testEntityConfiguration ,
329+ instance ( databaseAdapterMock ) ,
330+ new NoOpEntityMetricsAdapter ( ) ,
331+ TestEntity . name ,
332+ ) ;
333+
334+ // This should not throw
335+ const result = await entityDataManager . loadPageAsync ( queryContext , {
336+ first : 100 ,
337+ pagination : {
338+ strategy : PaginationStrategy . STANDARD ,
339+ orderBy : [ { fieldName : 'customIdField' , order : OrderByOrdering . ASCENDING } ] ,
340+ } ,
341+ } ) ;
342+
343+ expect ( result . edges ) . toEqual ( [ ] ) ;
344+ } ) ;
345+
346+ it ( 'should allow pagination when maxPageSize is not configured' , async ( ) => {
347+ const queryContext = instance ( mock < EntityQueryContext > ( ) ) ;
348+ const databaseAdapterMock = mock <
349+ PostgresEntityDatabaseAdapter < TestFields , 'customIdField' >
350+ > ( PostgresEntityDatabaseAdapter ) ;
351+
352+ // Configure the adapter to return undefined for maxPageSize
353+ when ( databaseAdapterMock . paginationMaxPageSize ) . thenReturn ( undefined ) ;
354+ when (
355+ databaseAdapterMock . fetchManyBySQLFragmentAsync ( queryContext , anything ( ) , anything ( ) ) ,
356+ ) . thenResolve ( [ ] ) ;
357+
358+ const entityDataManager = new EntityKnexDataManager (
359+ testEntityConfiguration ,
360+ instance ( databaseAdapterMock ) ,
361+ new NoOpEntityMetricsAdapter ( ) ,
362+ TestEntity . name ,
363+ ) ;
364+
365+ // This should not throw even with a large page size
366+ const result = await entityDataManager . loadPageAsync ( queryContext , {
367+ first : 10000 ,
368+ pagination : {
369+ strategy : PaginationStrategy . STANDARD ,
370+ orderBy : [ { fieldName : 'customIdField' , order : OrderByOrdering . ASCENDING } ] ,
371+ } ,
372+ } ) ;
373+
374+ expect ( result . edges ) . toEqual ( [ ] ) ;
375+ } ) ;
376+ } ) ;
377+ } ) ;
256378} ) ;
0 commit comments