Now that we can insert posts, it is possible to update, select, delete, and search for them. To me, any
one of these would be a valid place to go next. However, since I want to keep the database as unchanged
as possible, I'll start with
test_deletePost()
. This way, as posts are inserted for
testing, we can easily delete them.
Here is the code I wrote in xorblog/cfcs/tests/test_PostEntity:
<cffunction name="test_deletePost"
access="public"
returnType="void"
output="false"
>
<cfset var local = structNew()>
<cfset local.newID=_thePostEntity.insertPost(name="blah"
, meat="blah"
, originalDate="1/1/1900"
, author="yoda"
)>
<cfset local.wasDeleted = _thePostEntity.deletePost(local.newID)>
<cfset assertTrue(condition=local.wasDeleted, message="The post was not deleted."
)>
<cfquery name="local.post"
datasource="#_datasource#"
>
select id from post where id = <cfqueryparam cfsqltype="cf_sql_integer"
value="#local.newID#"
>
</cfquery>
<cfset assertEquals(actual = local.post.recordcount, expected = 0)>
</cffunction>
And the corresponding code for
deletePost()
:
<cffunction name="deletePost"
output="false"
returntype="boolean"
access="public"
>
<cfargument name="id"
required="true"
type="numeric"
>
<cfset var local = structNew()>
<cfset local.result = false>
<cftry>
<cfquery name="local.del"
datasource="#_datasource#"
>
delete from post where id = <cfqueryparam cfsqltype="cf_sql_integer"
value="#id#"
>
</cfquery>
<cfset local.result=true>
<cfcatch>
</cfcatch>
</cftry>
<cfreturn local.result>
</cffunction>
Originally, I just left the test as asserting that
local.wasDeleted
was true. However, writing just
enough of
deletePost()
in xorblog/cfcs/tests/PostEntity to get the test to pass resulted in the
simple line
<cfreturn true>
. Since that would always pass, I also added a check that
the inserted post no longer existed.
Now that we have some duplicate code, its definitely time to do some refactoring. More on that next time.
(To be continued...)
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
There are no comments for this entry yet.
Leave a comment