Last active
September 10, 2018 18:13
-
-
Save cfvonner/6674b99fbec6644d4c06f90461163a3f to your computer and use it in GitHub Desktop.
Testing a service with dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component displayname="GIS Plant Asset Service" extends="app.model.services.baseService" accessors="true" { | |
property assetGateway; | |
public array function list() { | |
return populateBeans( "assetBean", assetGateway.select() ); | |
} | |
public array function getByAssetID ( required string assetid ) { | |
var data = assetGateway.select( { assetid : assetid } ); | |
return populateBeans ( "assetBean", data ); | |
} | |
public array function getByAssetNumber ( required string assetno ) { | |
var data = assetGateway.select( { assetno : assetno } ); | |
return populateBeans ( "assetBean", data ); | |
} | |
public array function getByLegacyId ( required string legacyid ) { | |
var data = assetGateway.select( { legacyid : legacyid } ); | |
return populateBeans ( "assetBean", data ); | |
} | |
public array function getByKeywords( required string keywords ) { | |
var data = assetGateway.select( { keywords : keywords } ); | |
return populateBeans ( "assetBean", data ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component extends="testbox.system.BaseSpec"{ | |
/*********************************** LIFE CYCLE Methods ***********************************/ | |
// executes before all suites+specs in the run() method | |
function beforeAll(){ | |
mockBox = getMockBox(); | |
mockQuery = mockBox.querySim("ObjectId, AssetId, LegacyID, manufacturer, modelNumber | |
57119 | 10 | 10000 HOB | null | null | |
59932 | 520 | 25110 Influent Pump 1 | Worthington | 42MNF46"); | |
assetGateway = mockBox.createEmptyMock( 'plant.model.gis.gateways.asset' ); | |
assetGateway.$( 'select' ).$results ( mockQuery ); | |
mockBeans = [ | |
{ "ObjectId" : 57119, "AssetId" : 10, "LegacyID" : "10000 HOB", "manufacturer" : "", modelNumber : "" }, | |
{ "ObjectId" : 59932, "AssetId" : 520, "LegacyID" : "25110 Influent Pump 1", "manufacturer" : "Worthington", modelNumber : "42MNF46" } | |
]; | |
} | |
// executes after all suites+specs in the run() method | |
function afterAll(){ | |
} | |
/*********************************** BDD SUITES ***********************************/ | |
function run(){ | |
describe( "Given a plant asset service", function(){ | |
beforeEach( function( currentSpec ){ | |
cut = createMock( 'plant.model.gis.services.asset' ); | |
cut.$( 'populateBeans', mockBeans ); | |
cut.setAssetGateway( assetGateway ); | |
}); | |
it( "should have its injected properties set", function(){ | |
expect( cut.getAssetGateway() ).toBe( assetGateway ); | |
}); | |
describe( "when calling list()", function(){ | |
it( "should call select on the assetGateway with no arguments", function(){ | |
var selectLog = assetGateway.$callLog().select; | |
expect( assetGateway.$count( 'select') ).toBe( 0 ); | |
var result = cut.list(); | |
selectLog = assetGateway.$callLog().select; | |
expect( assetGateway.$count( 'select') ).toBe( 1 ); | |
}); | |
}); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component displayname="Base Service" accessors="true" output="false" { | |
property beanFactory; | |
public function init() { | |
return this; | |
} | |
private any function populate ( any bean, struct properties, any beanFactory ) { | |
/*writeLog( text="#GetMetaData( this ).name#-->#getFunctionCalledName()#() arguments: #SerializeJSON( arguments )#", | |
file="geofusion", type="information" );*/ | |
if ( isSimpleValue( bean ) ) { | |
if ( beanFactory.containsBean( bean ) ) bean = beanFactory.getBean( bean ); | |
else bean = beanFactory.construct( bean ); | |
} | |
//TODO: add a test for whether property exists in the bean before trying to push the data in | |
for ( var property in properties ) { | |
if ( !isNull( properties[ property ] ) && | |
( ( isSimpleValue( properties[ property ] ) && Len( properties[ property ] ) ) || | |
!isSimpleValue( properties[ property ] ) ) ) { | |
var args = { }; | |
args[ property ] = properties[ property ]; | |
evaluate( 'bean.set#property#( argumentCollection = args )' ); | |
} | |
} | |
return bean; | |
} | |
private array function populateBeans( required string beanName, required any recordSet ) { | |
var beanArray = []; | |
if ( ( isQuery( recordSet) && recordSet.RecordCount ) || | |
( isArray( recordSet ) && recordSet.len() ) ) { | |
for ( var record in recordSet ) { | |
/*writeLog( text="#GetMetaData( this ).name#-->#getFunctionCalledName()#() for loop: #SerializeJSON( record )#", | |
file="geofusion", type="information" );*/ | |
var bean = populate( beanName, record ); | |
beanArray.append( bean ); | |
} | |
} | |
return beanArray; | |
} | |
private query function queryDeleteColumn( required query qry, required string column ) { | |
var newColumnList = qry.ColumnList; | |
/*writeLog( text="#GetMetaData( this ).name#-->#getFunctionCalledName()#() arguments: #SerializeJSON( arguments )#", | |
file="geofusion", type="information" );*/ | |
if ( !ListFindNoCase( newColumnList, column ) ) { | |
throw ( message="Invalid column name [#column#] passed in the 'column' argument.", type="custom" ); | |
return; | |
} | |
newColumnList = ListDeleteAt( newColumnList, ListFindNoCase( newColumnList, column ) ); | |
return queryExecute( "SELECT " & newColumnList & " FROM qry", {}, { dbtype="query" } ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment