PageRenderTime 404ms CodeModel.GetById 141ms app.highlight 2ms RepoModel.GetById 259ms app.codeStats 1ms

/wheels/tests/model/nestedproperties/one_to_one.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 108 lines | 98 code | 10 blank | 0 comment | 0 complexity | 96eff2a3a84654844808d621ea434904 MD5 | raw file
  1<cfcomponent extends="wheelsMapping.Test">
  2
  3	<cffunction name="setup">
  4		<cfset loc.author = model("author")>
  5		<cfset loc.profile = model("profile")>
  6		<cfset $setTestObjects()>
  7		<cfset loc.testParamsStruct = $setTestParamsStruct()>
  8	</cffunction>
  9	
 10	<cffunction name="test_add_entire_data_set_via_create_and_struct" hint="Simulates adding an `author` and its child `profile` through a single structure passed into `author.create()`, much like what's normally done with the `params` struct.">
 11		<cftransaction>
 12			<!--- Should return `true` on successful create --->
 13			<cfset loc.author = loc.author.create(loc.testParamsStruct.author) />
 14			<cfset assert('IsObject(loc.author)')>
 15			<cftransaction action="rollback" />
 16		</cftransaction>
 17		<!--- Test whether profile was transformed into an object --->
 18		<cfset assert("IsObject(loc.author.profile)")>
 19		<!--- Test generated primary keys --->
 20		<cfset assert("IsNumeric(loc.author.id) and IsNumeric(loc.author.profile.id)")>
 21	</cffunction>
 22	
 23	<cffunction name="test_add_entire_data_set_via_new_and_struct" hint="Simulates adding an `author` and its child `profile` through a single structure passed into `author.new()` and saved with `author.save()`, much like what's normally done with the `params` struct.">
 24		<cfset loc.author = loc.author.new(loc.testParamsStruct.author)>
 25		<cftransaction>
 26			<!--- Should return `true` on successful create --->
 27			<cfset assert("loc.author.save()")>
 28			<cftransaction action="rollback" />
 29		</cftransaction>
 30		<!--- Test whether profile was transformed into an object --->
 31		<cfset assert("IsObject(loc.author.profile)")>
 32		<!--- Test generated primary keys --->
 33		<cfset assert("IsNumeric(loc.author.id) and IsNumeric(loc.author.profile.id)")>
 34	</cffunction>
 35
 36	<cffunction name="test_add_child_via_object" hint="Loads an existing `author` and sets its child `profile` as an object before saving.">
 37		<cftransaction>
 38			<cfset assert("loc.testAuthor.save()")>
 39			<cfset loc.p = loc.profile.findByKey(loc.testAuthor.profile.id)>
 40			<cftransaction action="rollback" />
 41		</cftransaction>
 42		<cfset assert("IsObject(loc.p)")>
 43	</cffunction>
 44	
 45	<cffunction name="test_add_child_via_struct" hint="Loads an existing `author` and sets its child `profile` as a struct before saving.">
 46		<cftransaction>
 47			<cfset assert("loc.testAuthor.save()")>
 48			<cfset loc.testAuthor.profile = {dateOfBirth="10/02/1980 18:00:00", bio=loc.bioText}>
 49			<cfset assert("loc.testAuthor.save()")>
 50			<cfset assert("IsObject(loc.testAuthor.profile)")>
 51			<cfset loc.p = loc.profile.findByKey(loc.testAuthor.profile.id)>
 52			<cftransaction action="rollback" />
 53		</cftransaction>
 54		<cfset assert("IsObject(loc.p)")>
 55	</cffunction>
 56	
 57	<cffunction name="test_delete_child_through_object_property" hint="Loads an existing `author` and deletes its child `profile` by setting the `_delete` property to `true`.">
 58		<cftransaction>
 59			<cfset loc.testAuthor.save()>
 60			<!--- Delete profile through nested property --->
 61			<cfset loc.testAuthor.profile._delete = true>
 62			<cfset loc.profileID = loc.testAuthor.profile.id>
 63			<cfset assert("loc.testAuthor.save()")>
 64			<!--- Should return `false` because the record is now deleted --->
 65			<cfset loc.missingProfile = loc.profile.findByKey(key=loc.profileId, reload=true)>
 66			<cftransaction action="rollback" />
 67		</cftransaction>
 68		<cfset assert("IsBoolean(loc.missingProfile) and not loc.missingProfile")>
 69	</cffunction>
 70
 71	<cffunction name="test_delete_child_through_struct" hint="Loads an existing `author` and deletes its child `property` by passing in a struct through `update()`.">
 72		<cftransaction>
 73			<!--- Save test author with child profile and grab new profile's ID --->
 74			<cfset loc.testAuthor.save()>
 75			<cfset loc.profileID = loc.testAuthor.profile.id>
 76			<!--- Delete profile through nested property --->
 77			<cfset loc.updateStruct.profile._delete = true>
 78			<cfset assert("loc.testAuthor.update(properties=loc.updateStruct)")>
 79			<!--- Should return `false` because the record is now deleted --->
 80			<cfset loc.missingProfile = loc.profile.findByKey(key=loc.profileId, reload=true)>
 81			<cftransaction action="rollback"/>
 82		</cftransaction>
 83		<cfset assert("IsBoolean(loc.missingProfile) and not loc.missingProfile")>
 84	</cffunction>
 85
 86	<cffunction name="$setTestObjects" access="private" hint="Sets up test `author` and `profile` objects.">
 87		<cfset loc.testAuthor = loc.author.findOneByLastName(value="Peters", include="profile")>
 88		<cfset loc.bioText = "Loves learning how to write tests." />
 89		<cfset loc.testAuthor.profile = model("profile").new(dateOfBirth="10/02/1980 18:00:00", bio=loc.bioText)>
 90	</cffunction>
 91	
 92	<cffunction name="$setTestParamsStruct" access="private" hint="Sets up test `author` struct reminiscent of what would be passed through a form. The `author` represented here also includes a nested child `profile` struct.">
 93		<cfset
 94			loc.testParams = {
 95				author = {
 96					firstName="Brian",
 97					lastName="Meloche",
 98					profile = {
 99						dateOfBirth="10/02/1970 18:01:00",
100						bio="Host of CFConversations, the best ColdFusion podcast out there."
101					}
102				}
103			}
104		>
105		<cfreturn loc.testParams>
106	</cffunction>
107
108</cfcomponent>