@@ -294,6 +294,58 @@ describe('@tryghost/bookshelf-pagination', function () {
294294 assert . equal ( modelState . rawCalls [ 0 ] , 'count(distinct posts.id) as aggregate' ) ;
295295 } ) ;
296296
297+ it ( 'useSmartCount supports SQL fragments returned as an array' , async function ( ) {
298+ const { bookshelf, modelState} = createBookshelf ( { countRows : [ { aggregate : 1 } ] } ) ;
299+ const model = new bookshelf . Model ( ) ;
300+
301+ const originalQuery = model . query ;
302+ model . query = function ( ) {
303+ const qb = originalQuery . apply ( this , arguments ) ;
304+ if ( arguments . length === 0 ) {
305+ qb . toSQL = ( ) => [
306+ { sql : 'select *' } ,
307+ { } ,
308+ { sql : 'from `posts`, `tags` where `posts`.`id` = `tags`.`post_id`' }
309+ ] ;
310+ }
311+ return qb ;
312+ } ;
313+
314+ await model . fetchPage ( { page : 1 , limit : 10 , useSmartCount : true } ) ;
315+
316+ assert . equal ( modelState . rawCalls [ 0 ] , 'count(distinct posts.id) as aggregate' ) ;
317+ } ) ;
318+
319+ it ( 'useSmartCount falls back safely when compiled SQL is missing' , async function ( ) {
320+ const { bookshelf, modelState} = createBookshelf ( { countRows : [ { aggregate : 1 } ] } ) ;
321+ const model = new bookshelf . Model ( ) ;
322+
323+ const originalQuery = model . query ;
324+ model . query = function ( ) {
325+ const qb = originalQuery . apply ( this , arguments ) ;
326+ if ( arguments . length === 0 ) {
327+ qb . toSQL = ( ) => ( { } ) ;
328+ }
329+ return qb ;
330+ } ;
331+
332+ await model . fetchPage ( { page : 1 , limit : 10 , useSmartCount : true } ) ;
333+
334+ assert . equal ( modelState . rawCalls [ 0 ] , 'count(*) as aggregate' ) ;
335+ } ) ;
336+
337+ it ( 'handleRelation does not duplicate entries and ignores same-table properties' , function ( ) {
338+ const { bookshelf} = createBookshelf ( ) ;
339+ const model = new bookshelf . Model ( ) ;
340+ model . eagerLoad = [ 'authors' ] ;
341+
342+ paginationPlugin . paginationUtils . handleRelation ( model , 'posts.id' ) ;
343+ paginationPlugin . paginationUtils . handleRelation ( model , 'title' ) ;
344+ paginationPlugin . paginationUtils . handleRelation ( model , 'authors.name' ) ;
345+
346+ assert . deepEqual ( model . eagerLoad , [ 'authors' ] ) ;
347+ } ) ;
348+
297349 it ( 'falls back to zero total when aggregate row is missing' , async function ( ) {
298350 const { bookshelf } = createBookshelf ( { countRows : [ ] } ) ;
299351 const model = new bookshelf . Model ( ) ;
0 commit comments