PageRenderTime 237ms CodeModel.GetById 120ms app.highlight 2ms RepoModel.GetById 113ms app.codeStats 0ms

/wheels/tests/model/crud/changes.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 155 lines | 140 code | 15 blank | 0 comment | 0 complexity | 38cb4cd69ca1bb3375f34e7723c8ef48 MD5 | raw file
  1<cfcomponent extends="wheelsMapping.Test">
  2
  3	<cffunction name="test_comparing_existing_properties_only">
  4		<cfset loc.author = model("author").findOne(select="firstName")>
  5		<cfset loc.result = loc.author.hasChanged()>
  6		<cfset assert("NOT loc.result")>
  7		<cfset loc.result = loc.author.hasChanged("firstName")>
  8		<cfset assert("NOT loc.result")>
  9		<cfset loc.author = model("author").findOne()>
 10		<cfset StructDelete(loc.author, "firstName")>
 11		<cfset loc.result = loc.author.hasChanged()>
 12		<cfset assert("NOT loc.result")>
 13		<cfset loc.result = loc.author.hasChanged("firstName")>
 14		<cfset assert("NOT loc.result")>
 15		<cfset loc.result = loc.author.hasChanged("somethingThatDoesNotExist")>
 16		<cfset assert("NOT loc.result")>
 17	</cffunction>
 18
 19	<cffunction name="test_allChanges">
 20		<cfset loc.author = model("author").findOne(order="id")>
 21		<cfset loc.author.firstName = "a">
 22		<cfset loc.author.lastName = "b">
 23		<cfset loc.compareWith.firstName.changedFrom = "Per">
 24		<cfset loc.compareWith.firstName.changedTo = "a">
 25		<cfset loc.compareWith.lastName.changedFrom = "Djurner">
 26		<cfset loc.compareWith.lastName.changedTo = "b">
 27		<cfset loc.result = loc.author.allChanges()>
 28		<cfset assert("loc.result.toString() IS loc.compareWith.toString()")>
 29	</cffunction>
 30
 31	<cffunction name="test_changedProperties">
 32		<cfset loc.author = model("author").findOne()>
 33		<cfset loc.author.firstName = "a">
 34		<cfset loc.author.lastName = "b">
 35		<cfset loc.result = listSort(loc.author.changedProperties(), "textnocase")>
 36		<cfset assert("loc.result IS 'firstName,lastName'")>
 37	</cffunction>
 38
 39	<cffunction name="test_changedProperties_without_changes">
 40		<cfset loc.author = model("author").findOne()>
 41		<cfset loc.result = loc.author.changedProperties()>
 42		<cfset assert("loc.result IS ''")>
 43	</cffunction>
 44
 45	<cffunction name="test_changedProperties_change_and_back">
 46		<cfset loc.author = model("author").findOne()>
 47		<cfset loc.author.oldFirstName = loc.author.firstName>
 48		<cfset loc.author.firstName = "a">
 49		<cfset loc.result = loc.author.changedProperties()>
 50		<cfset assert("loc.result IS 'firstName'")>
 51		<cfset loc.author.firstName = loc.author.oldFirstName>
 52		<cfset loc.result = loc.author.changedProperties()>
 53		<cfset assert("loc.result IS ''")>
 54	</cffunction>
 55
 56	<cffunction name="test_isNew">
 57		<cftransaction>
 58			<cfset loc.author = model("author").new(firstName="Per", lastName="Djurner")>
 59			<cfset loc.result = loc.author.isNew()>
 60			<cfset assert("loc.result IS true")>
 61			<cfset loc.author.save(transaction="none")>
 62			<cfset loc.result = loc.author.isNew()>
 63			<cfset assert("loc.result IS false")>
 64			<cftransaction action="rollback" />
 65		</cftransaction>
 66	</cffunction>
 67
 68	<cffunction name="test_isNew_with_find">
 69		<cfset loc.author = model("author").findOne()>
 70		<cfset loc.result = loc.author.isNew()>
 71		<cfset assert("loc.result IS false")>
 72	</cffunction>
 73
 74	<cffunction name="test_hasChanged">
 75		<cfset loc.author = model("author").findOne(where="lastName = 'Djurner'")>
 76		<cfset loc.result = loc.author.hasChanged()>
 77		<cfset assert("loc.result IS false")>
 78		<cfset loc.author.lastName = "Petruzzi">
 79		<cfset loc.result = loc.author.hasChanged()>
 80		<cfset assert("loc.result IS true")>
 81		<cfset loc.author.lastName = "Djurner">
 82		<cfset loc.result = loc.author.hasChanged()>
 83		<cfset assert("loc.result IS false")>
 84	</cffunction>
 85
 86	<cffunction name="test_hasChanged_with_new">
 87		<cftransaction>
 88			<cfset loc.author = model("author").new()>
 89			<cfset loc.result = loc.author.hasChanged()>
 90			<cfset assert("loc.result IS true")>
 91			<cfset loc.author.firstName = "Per">
 92			<cfset loc.author.lastName = "Djurner">
 93			<cfset loc.author.save(transaction="none")>
 94			<cfset loc.result = loc.author.hasChanged()>
 95			<cfset assert("loc.result IS false")>
 96			<cfset loc.author.lastName = "Petruzzi">
 97			<cfset loc.result = loc.author.hasChanged()>
 98			<cfset assert("loc.result IS true")>
 99			<cfset loc.author.save(transaction="none")>
100			<cfset loc.result = loc.author.hasChanged()>
101			<cfset assert("loc.result IS false")>
102			<cftransaction action="rollback" />
103		</cftransaction>
104	</cffunction>
105
106	<cffunction name="test_XXXHasChanged">
107		<cfset loc.author = model("author").findOne(where="lastName = 'Djurner'")>
108		<cfset loc.author.lastName = "Petruzzi">
109		<cfset loc.result = loc.author.lastNameHasChanged()>
110		<cfset assert("loc.result IS true")>
111		<cfset loc.result = loc.author.firstNameHasChanged()>
112		<cfset assert("loc.result IS false")>
113	</cffunction>
114
115	<cffunction name="test_changedFrom">
116		<cfset loc.author = model("author").findOne(where="lastName = 'Djurner'")>
117		<cfset loc.author.lastName = "Petruzzi">
118		<cfset loc.result = loc.author.changedFrom(property="lastName")>
119		<cfset assert("loc.result IS 'Djurner'")>
120	</cffunction>
121
122	<cffunction name="test_XXXChangedFrom">
123		<cfset loc.author = model("author").findOne(where="lastName = 'Djurner'")>
124		<cfset loc.author.lastName = "Petruzzi">
125		<cfset loc.result = loc.author.lastNameChangedFrom(property="lastName")>
126		<cfset assert("loc.result IS 'Djurner'")>
127	</cffunction>
128
129	<cffunction name="test_date_compare">
130		<cfset loc.user = model("user").findOne(where="username = 'tonyp'")>
131		<cfset loc.user.birthday = "11/01/1975 12:00 AM">
132		<cfset loc.e = loc.user.hasChanged("birthday")>
133		<cfset assert('loc.e eq false')>
134	</cffunction>
135
136	<cffunction name="test_binary_compare">
137		<cftransaction>
138			<cfset loc.photo = model("photo").findOne(order=model("photo").primaryKey())>
139			<cfset assert("NOT loc.photo.hasChanged('fileData')")>
140			<cffile action="readbinary" file="#expandpath('wheels/tests/_assets/files/cfwheels-logo.png')#" variable="loc.binaryData">
141			<cfset loc.photo.fileData = loc.binaryData>
142			<cfset assert("loc.photo.hasChanged('fileData')")>
143			<cfset loc.photo.galleryid = 99>
144			<cfset loc.photo.save()>
145			<cfset assert("NOT loc.photo.hasChanged('fileData')")>
146			<cfset loc.photo = model("photo").findOne(where="galleryid=99")>
147			<cfset assert("NOT loc.photo.hasChanged('fileData')")>
148			<cffile action="readbinary" file="#expandpath('wheels/tests/_assets/files/cfwheels-logo.txt')#" variable="loc.binaryData">
149			<cfset loc.photo.fileData = loc.binaryData>
150			<cfset assert("loc.photo.hasChanged('fileData')")>
151			<cftransaction action="rollback" />
152		</cftransaction>
153	</cffunction>
154
155</cfcomponent>