Well, to hopefully stir up some discussion, I am attaching what I have done thus far for a DAO unit test.  I am not particularly happy with it as it is one big test for 4 different methods in the dao (create, read, update, delete).  So, it is open for suggestions ....
 
-- Jeff
 
 
 
-- DAO Object -------------------------------------------------------------
 
<cfcomponent displayname="propertyDAO_MySQL" output="false" extends="propertyDAO"
 hint="mySQL data access object which models a property">
 
 <!--- -------------------------------------------------------------------------------------- --->
 <!--- CRUD Methods --->
 
 <!--- create new property --->
 <cffunction name="create" returnType="void" access="public" output="false"
  hint="create a new property based upon the receieved property object" >
  
  <cfargument name="oProperty" type="property" required="true" hint="property object from which to create a record" />
 
  <cfset var propertyInsert = '' />
  
  <cftransaction>
   
   <!--- insert record into database --->
   <cfquery name="propertyInsert" datasource="#variables.dsn#">
    INSERT INTO property (
      pName,
      pDataType,
      pRequired,
      pDefaultValue,
      pCurrentValue
     )
    VALUES (
      <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oProperty.getName()#" maxlength="#arguments.oProperty.getConfigValue('name').maxlength#" null="#arguments.oProperty.isNullName()#" />,
      <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oProperty.getDataType()#" maxlength="#arguments.oProperty.getConfigValue('dataType').maxlength#" null="#arguments.oProperty.isNullDataType()#" />,
      <cfqueryparam cfsqltype="cf_sql_bit" value="#arguments.oProperty.getRequired()#" null="#arguments.oProperty.isNullRequired()#" />,
      <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oProperty.getDefaultValue()#" maxlength="#arguments.oProperty.getConfigValue('defaultValue').maxlength#" null="#arguments.oProperty.isNullDefaultValue()#" />,
      <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oProperty.getCurrentValue()#" maxlength="#arguments.oProperty.getConfigValue('currentValue').maxlength#" null="#arguments.oProperty.isNullCurrentValue()#" />
     );
   </cfquery>
   
   <!--- capture the new property id --->
   <cfquery name="propertyInsert" datasource="#variables.dsn#">   
    SELECT LAST_INSERT_ID() AS pID;     
   </cfquery>
 
   <!--- update property object with captured id --->
   <cfset arguments.oProperty.setID(propertyInsert.pID) />
 
  </cftransaction>
 </cffunction> <!--- end function: create() --->
 
 <!--- read property --->
 <cffunction name="read" returnType="void" access="public" output="true"
  hint="retrieve property based upon the receieved property id" >
  
  <cfargument name="sPropertyID" type="string" required="true" hint="id of property to return" />
  <cfargument name="oProperty" type="property" required="true" hint="property object to return" />
 
  <cfset var propertySelect = '' />
  
  <!--- retrieve record from database --->
  <cfquery name="propertySelect" datasource="#variables.dsn#">
   SELECT pID,
     pName,
     pDataType,
     pRequired,
     pDefaultValue,
     pCurrentValue
   FROM property
   WHERE pID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.sPropertyID#" maxlength="#arguments.oProperty.getConfigValue('id').maxlength#" null="false" />
  </cfquery>
  
  <!--- if a record was found --->
  <cfif propertySelect.recordCount>
   <cfscript>
    arguments.oProperty.setID(propertySelect.pID, propertySelect.pID EQ '', false);
    arguments.oProperty.setName(propertySelect.pName, propertySelect.pName EQ '', false);
    arguments.oProperty.setDataType(propertySelect.pDataType, propertySelect.pDataType EQ '', false);
    arguments.oProperty.setRequired(propertySelect.pRequired, propertySelect.pRequired EQ '', false);
    arguments.oProperty.setDefaultValue(propertySelect.pDefaultValue, propertySelect.pDefaultValue EQ '', false);
    arguments.oProperty.setCurrentValue(propertySelect.pCurrentValue, propertySelect.pCurrentValue EQ '', false);
   </cfscript>  
  <cfelse>
   <cfthrow type="propertyDAO" errorcode="propertyDAO.propertyNotFound" message="Unable to find property by the primary key given. (#arguments.sPropertyID#)" />
  </cfif>
 </cffunction> <!--- end function: read() --->
 
 <!--- update property --->
 <cffunction name="update" returnType="void" access="public" output="false"
  hint="update property based upon the receieved property object" >
  
  <cfargument name="oProperty" type="property" required="true" hint="property object to update" />
 
  <cfset var propertyUpdate = '' />
  
  <!--- update record in database --->
  <cfquery name="propertyUpdate" datasource="#variables.dsn#">
   UPDATE property
   SET  pName = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oProperty.getName()#" maxlength="#arguments.oProperty.getConfigValue('name').maxlength#" null="#arguments.oProperty.isNullName()#" />,
     pDataType = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oProperty.getDataType()#" maxlength="#arguments.oProperty.getConfigValue('dataType').maxlength#" null="#arguments.oProperty.isNullDataType()#" />,
     pRequired = <cfqueryparam cfsqltype="cf_sql_bit" value="#arguments.oProperty.getRequired()#" null="#arguments.oProperty.isNullRequired()#" />,
     pDefaultValue = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oProperty.getDefaultValue()#" maxlength="#arguments.oProperty.getConfigValue('defaultValue').maxlength#" null="#arguments.oProperty.isNullDefaultValue()#" />,
     pCurrentValue = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oProperty.getCurrentValue()#" maxlength="#arguments.oProperty.getConfigValue('currentValue').maxlength#" null="#arguments.oProperty.isNullCurrentValue()#" />
   WHERE pID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oProperty.getID()#" maxlength="#arguments.oProperty.getConfigValue('id').maxlength#" null="false" />
  </cfquery>
 </cffunction> <!--- end function: update() --->
 
 <!--- delete property --->
 <cffunction name="delete" returnType="void" access="public" output="false"
  hint="delete property based upon the receieved property object" >
  
  <cfargument name="oProperty" type="property" required="true" hint="property object to delete" />
 
  <cfset var propertyDelete = '' />
  
  <!--- delete record from database --->
  <cfquery name="propertyDelete" datasource="#variables.dsn#">
   DELETE
   FROM property
   WHERE pID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.oProperty.getID()#" maxlength="#arguments.oProperty.getConfigValue('id').maxlength#" null="false" />
  </cfquery>
 </cffunction> <!--- end function: delete() --->
 
</cfcomponent>
 
 
 
-- Unit Test -------------------------------------------------------------
 
<cfcomponent displayname="test_propertyDAO_MySQL" output="false" extends="org.cfcunit.framework.TestCase"
 hint="unit test for propertyDAO_MySQL component">
 
 <!--- define base parameters --->
 <cfset variables.dsn = 'listManager_mySQL' />
 
 <!--- define bean data structure --->
 <cfset variables.testData = structNew() />
 <cfset variables.testData.id = '' />
 <cfset variables.testData.name = 'Test Property'/>
 <cfset variables.testData.dataType = 'String' />
 <cfset variables.testData.required = true />
 <cfset variables.testData.defaultValue = '' />
 <cfset variables.testData.currentValue = '' />
 
 <!--- create the property and propertyDAO objects --->
 <cfset variables.oPropertyDAO = createObject('component', 'propertyDAO_MySQL').init(variables.dsn) />
 <cfset variables.oProperty = createObject('component', 'property').init() />
 <cfset variables.oProperty.setInstance(variables.testData) />
 
 <!--- -------------------------------------------------------------------------------------- --->
 <!--- setup the unit test --->
 <cffunction name="setUp" returntype="void" access="public">
 </cffunction> <!--- end function: setUp() --->
 
 <!--- tear down the unit test upon completion --->
 <cffunction name="tearDown" returntype="void" access="package">
 </cffunction> <!--- end function: tearDown() --->
 
 <!--- -------------------------------------------------------------------------------------- --->
 <!--- test functions --->
 <cffunction name="testCRUD" returntype="void" access="public">
 
  <!--- call create method, passing property object --->
  <cfset variables.oPropertyDAO.create(variables.oProperty) />
  
  <!--- check if id value was set --->
  <cfset assertFalse(variables.oProperty.isNullID(), 'Create Failure - Failed to return new property id') />
  
  <!--- store property ID --->
  <cfset variables.testData.id = variables.oProperty.getID() />
  
  <!--- call read method to retrieve property object --->
  <cfset variables.oPropertyDAO.read(variables.testData.id, variables.oProperty) />
  
  <!--- check result --->
  <cfset assertEqualsString(variables.testData.id, variables.oProperty.getID(), 'Create/Read Failure - Failed to set property id') />
  <cfset assertEqualsString(variables.testData.name, variables.oProperty.getName(), 'Create/Read Failure - Failed to set property name') />
  <cfset assertEqualsString(variables.testData.dataType, variables.oProperty.getDataType(), 'Create/Read Failure - Failed to set property dataType') />
  <cfset assertEqualsBoolean(variables.testData.required, variables.oProperty.getRequired(), 'Create/Read Failure - Failed to set property required') />
  <cfset assertTrue(variables.oProperty.isNullDefaultValue(), 'Create/Read Failure - Failed to set property defaultValue') />
  <cfset assertTrue(variables.oProperty.isNullCurrentValue(), 'Create/Read Failure - Failed to set property currentValue') />
 
  <!--- call update method, passing property object --->
  <cfset variables.oPropertyDAO.update(variables.oProperty) />
  
  <!--- call read method to retrieve property object --->
  <cfset variables.oPropertyDAO.read(variables.testData.id, variables.oProperty) />
  
  <!--- check result --->
  <cfset assertEqualsString(variables.testData.id, variables.oProperty.getID(), 'Update/Read Failure - Failed to set property id') />
  <cfset assertEqualsString(variables.testData.name, variables.oProperty.getName(), 'Update/Read Failure - Failed to set property name') />
  <cfset assertEqualsString(variables.testData.dataType, variables.oProperty.getDataType(), 'Update/Read Failure - Failed to set property dataType') />
  <cfset assertEqualsBoolean(variables.testData.required, variables.oProperty.getRequired(), 'Update/Read Failure - Failed to set property required') />
  <cfset assertTrue(variables.oProperty.isNullDefaultValue(), 'Update/Read Failure - Failed to set property defaultValue') />
  <cfset assertTrue(variables.oProperty.isNullCurrentValue(), 'Update/Read Failure - Failed to set property currentValue') />
 
  <!--- call delete method, passing property object --->
  <cfset variables.oPropertyDAO.delete(variables.oProperty) />
 
  <!--- call read method, attempting to retrieve deleted property --->
  <cftry>
   <cfset variables.oPropertyDAO.read(variables.testData.id, variables.oProperty) />
   <cfset fail("Property was found - Delete method failure") />
  
   <cfcatch type="propertyDAO"></cfcatch>
  </cftry>
 </cffunction> <!--- end function: testCRUD() --->
 
</cfcomponent>
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com).

An archive of the CFCDev list is available at www.mail-archive.com/[email protected]

Reply via email to