Skip to content

Commit

Permalink
#4 Tests and code for search pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicWatson committed Sep 3, 2012
1 parent 6cc91a6 commit cf84233
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
27 changes: 23 additions & 4 deletions cfelasticsearch/api/Wrapper.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,15 @@
</cffunction>

<cffunction name="search" access="public" returntype="struct" output="false">
<cfargument name="q" type="string" required="true" />
<cfargument name="index" type="string" required="false" />
<cfargument name="type" type="string" required="false" />
<cfargument name="q" type="string" required="true" />
<cfargument name="index" type="string" required="false" />
<cfargument name="type" type="string" required="false" />
<cfargument name="page" type="numeric" required="false" default="1" />
<cfargument name="pageSize" type="numeric" required="false" default="10" />

<cfscript>
var uri = _getIndexAndTypeUri( args=arguments ) & "/_search?q=#q#";
var from = _calculateStartRecordFromPageInfo( page, pageSize );
var uri = _getIndexAndTypeUri( args=arguments ) & "/_search?q=#q#&from=#from#&size=#pageSize#";

return _call(
uri = uri
Expand Down Expand Up @@ -223,6 +226,22 @@
</cfscript>
</cffunction>

<cffunction name="_calculateStartRecordFromPageInfo" access="private" returntype="numeric" output="false">
<cfargument name="page" type="numeric" required="true" />
<cfargument name="pageSize" type="numeric" required="true" />

<cfscript>
if ( page lte 0 ) {
throw(
type = "cfelasticsearch.search.invalidPage"
, message = "Page number must be greater than zero. Page number supplied was '#page#'."
);
}

return ((page-1) * pageSize) + 1;
</cfscript>
</cffunction>

<!--- accessors --->
<cffunction name="_getEndpoint" access="private" returntype="string" output="false">
<cfreturn _endpoint>
Expand Down
71 changes: 69 additions & 2 deletions tests/integration/WrapperTest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@
super.assertEquals( 1, result.items[i].index._version, "Item should have been at version 1 but wasn't." );
}
}

debug( result );
</cfscript>
</cffunction>

Expand Down Expand Up @@ -445,6 +443,75 @@
</cfscript>
</cffunction>

<cffunction name="t21_search_shouldReturnPaginatedResults" returntype="void">
<cfscript>
var result = "";
var result2 = "";
var nDocsExpected = 0;

nDocsExpected += _addABunchOfDocs( "index1", "type1" );
nDocsExpected += _addABunchOfDocs( "index1", "type2" );
nDocsExpected += _addABunchOfDocs( "index1", "type3" );
nDocsExpected += _addABunchOfDocs( "index1", "type4" );


wrapper.refresh();

result = wrapper.search( q="*", page=3, pageSize=1 );
result2 = wrapper.search( q="*", page=4, pageSize=1 );

super.assert( IsStruct( result ), "Result not in expected format" );
super.assertEquals( nDocsExpected, result.hits.total );
super.assertEquals( 1, ArrayLen( result.hits.hits ) );

super.assert( IsStruct( result2 ), "Result not in expected format" );
super.assertEquals( nDocsExpected, result2.hits.total );
super.assertEquals( 1, ArrayLen( result2.hits.hits ) );

super.assertNotEquals( result2.hits.hits[1]._source, result.hits.hits[1]._source );
</cfscript>
</cffunction>

<cffunction name="t22_search_shouldThrowError_whenPageNumberIsSubZero" returntype="void">
<cfscript>
var failed = false;

_addABunchOfDocs( "index1", "type1" );
_addABunchOfDocs( "index1", "type2" );
_addABunchOfDocs( "index1", "type3" );
_addABunchOfDocs( "index1", "type4" );

wrapper.refresh();

try {
wrapper.search( q="*", page=-2 );
} catch ( "cfelasticsearch.search.invalidPage" e ) {
failed = true;
} catch ( any e ) {}

assert( failed, "The search method did not throw an appropriate error when the page number was zero or less" );
</cfscript>
</cffunction>

<cffunction name="t23_search_shouldReturnNoResults_whenPageNumberIsGreaterThanNumberOfPagesForQuery" returntype="void">
<cfscript>
var result = "";

_addABunchOfDocs( "index1", "type1" );
_addABunchOfDocs( "index1", "type2" );
_addABunchOfDocs( "index1", "type3" );
_addABunchOfDocs( "index1", "type4" );

wrapper.refresh();

result = wrapper.search( q="*", page=1000, pageSize=100 );

super.assert( IsStruct( result ), "Result not in expected format" );
super.assert( result.hits.total );
super.assertEquals( 0, ArrayLen( result.hits.hits ) );
</cfscript>
</cffunction>

<!--- private --->
<cffunction name="_teardownTestIndexes" access="private" returntype="void" output="false">
<cftry>
Expand Down

0 comments on commit cf84233

Please sign in to comment.