PageRenderTime 384ms CodeModel.GetById 202ms app.highlight 1ms RepoModel.GetById 180ms app.codeStats 0ms

/wheels/tests/model/associations/hasOne.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 100 lines | 91 code | 9 blank | 0 comment | 0 complexity | b39ab9f2f4295701606ae1af41769403 MD5 | raw file
  1<cfcomponent extends="wheelsMapping.Test">
  2
  3	<cffunction name="test_getting_child">
  4		<cfset loc.author = model("author").findOne(order="id")>
  5		<cfset loc.dynamicResult = loc.author.profile()>
  6		<cfset loc.coreResult = model("profile").findOne(where="authorId=#loc.author.id#")>
  7		<cfset assert("loc.dynamicResult.bio IS loc.coreResult.bio")>
  8	</cffunction>
  9
 10	<cffunction name="test_checking_if_child_exist">
 11		<cfset loc.author = model("author").findOne(order="id")>
 12		<cfset loc.dynamicResult = loc.author.hasProfile()>
 13		<cfset loc.coreResult = model("profile").exists(where="authorId=#loc.author.id#")>
 14		<cfset assert("loc.dynamicResult IS loc.coreResult")>
 15	</cffunction>
 16
 17	<cffunction name="test_adding_child_by_setting_foreign_key">
 18		<cfset loc.author = model("author").findOne(order="id DESC")>
 19		<cfset loc.profile = model("profile").findOne(order="id")>
 20		<cftransaction>
 21			<cfset loc.author.setProfile(profile=loc.profile, transaction="none")>
 22			<cfset loc.profile.reload()>
 23			<cftransaction action="rollback" />
 24		</cftransaction>		
 25		<cfset assert("loc.author.id IS loc.profile.authorId")>
 26		<cfset loc.profile.reload()>
 27		<cftransaction>
 28			<cfset loc.author.setProfile(key=loc.profile.id, transaction="none")>
 29			<cfset loc.profile.reload()>
 30			<cftransaction action="rollback" />
 31		</cftransaction>		
 32		<cfset assert("loc.author.id IS loc.profile.authorId")>
 33		<cfset loc.profile.reload()>
 34		<cftransaction>
 35			<cfset model("profile").updateByKey(key=loc.profile.id, authorId=loc.author.id, transaction="none")>
 36			<cfset loc.profile.reload()>
 37			<cftransaction action="rollback" />
 38		</cftransaction>		
 39		<cfset assert("loc.author.id IS loc.profile.authorId")>
 40	</cffunction>
 41
 42	<cffunction name="test_removing_child_by_nullifying_foreign_key">
 43		<cfset loc.author = model("author").findOne(order="id")>
 44		<cftransaction>
 45			<cfset loc.author.removeProfile(transaction="none")>
 46			<cfset assert("model('profile').findOne().authorId IS ''")>
 47			<cftransaction action="rollback" />
 48		</cftransaction>		
 49		<cftransaction>
 50			<cfset model("profile").updateOne(authorId="", where="authorId=#loc.author.id#", transaction="none")>
 51			<cfset assert("model('profile').findOne().authorId IS ''")>
 52			<cftransaction action="rollback" />
 53		</cftransaction>		
 54	</cffunction>
 55
 56	<cffunction name="test_deleting_child">
 57		<cfset loc.author = model("author").findOne(order="id")>
 58		<cfset loc.profileCount = model("profile").count() />
 59		<cftransaction>
 60			<cfset loc.author.deleteProfile(transaction="none")>
 61			<cfset assert("model('profile').count() eq (loc.profileCount - 1)")>
 62			<cftransaction action="rollback" />
 63		</cftransaction>		
 64		<cftransaction>
 65			<cfset model("profile").deleteOne(where="authorId=#loc.author.id#", transaction="none")>
 66			<cfset assert("model('profile').count() eq (loc.profileCount - 1)")>
 67			<cftransaction action="rollback" />
 68		</cftransaction>		
 69	</cffunction>
 70
 71	<cffunction name="test_creating_new_child">
 72		<cfset loc.author = model("author").findOne(order="id")>
 73		<cfset loc.newProfile = loc.author.newProfile(dateOfBirth="17/12/1981")>
 74		<cfset loc.dynamicResult = loc.newProfile.authorId>
 75		<cfset loc.newProfile = model("profile").new(authorId=loc.author.id, dateOfBirth="17/12/1981")>
 76		<cfset loc.coreResult = loc.newProfile.authorId>
 77		<cfset assert("loc.dynamicResult IS loc.coreResult")>
 78	</cffunction>
 79
 80	<cffunction name="test_creating_new_child_and_saving_it">
 81		<cfset loc.author = model("author").findOne(order="id")>
 82		<cftransaction>
 83			<cfset loc.newProfile = loc.author.createProfile(dateOfBirth="17/12/1981", transaction="none")>
 84			<cfset loc.dynamicResult = loc.newProfile.authorId>
 85			<cftransaction action="rollback" />
 86		</cftransaction>
 87		<cftransaction>
 88			<cfset loc.newProfile = model("profile").create(authorId=loc.author.id, dateOfBirth="17/12/1981", transaction="none")>
 89			<cfset loc.coreResult = loc.newProfile.authorId>
 90			<cftransaction action="rollback" />
 91		</cftransaction>
 92		<cfset assert("loc.dynamicResult IS loc.coreResult")>
 93	</cffunction>
 94
 95	<cffunction name="test_getting_child_with_join_key">
 96		<cfset loc.obj = model("user").findOne(order="id", include="author")>
 97		<cfset assert('loc.obj.firstName eq loc.obj.author.firstName')>
 98	</cffunction>
 99
100</cfcomponent>