PageRenderTime 255ms CodeModel.GetById 150ms app.highlight 5ms RepoModel.GetById 96ms app.codeStats 0ms

/wheels/tests/model/validations/standard_validations.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 401 lines | 328 code | 73 blank | 0 comment | 0 complexity | da14272b04b39a35a381ccc7252f2349 MD5 | raw file
  1<cfcomponent extends="wheelsMapping.Test">
  2
  3	<cffunction name="setup">
  4		<cfset StructDelete(application.wheels.models, "users", false)>
  5        <cfset loc.user = model("users").new()>
  6	</cffunction>
  7
  8	<!--- validatesConfirmationOf --->
  9	<cffunction name="test_validatesConfirmationOf_valid">
 10		<cfset loc.user.password = "hamsterjelly">
 11		<cfset loc.user.passwordConfirmation = "hamsterjelly">
 12		<cfset loc.user.validatesConfirmationOf(property="password")>
 13		<cfset assert('loc.user.valid()')>
 14	</cffunction>
 15
 16	<cffunction name="test_validatesConfirmationOf_invalid">
 17		<cfset loc.user.password = "hamsterjelly">
 18		<cfset loc.user.passwordConfirmation = "hamsterjellysucks">
 19		<cfset loc.user.validatesConfirmationOf(property="password")>
 20		<cfset assert('!loc.user.valid()')>
 21	</cffunction>
 22
 23	<cffunction name="test_validatesConfirmationOf_missing_property_valid">
 24		<cfset loc.user.passwordConfirmation = "hamsterjellysucks">
 25		<cfset loc.user.validatesConfirmationOf(property="password")>
 26		<cfset assert('loc.user.valid()')>
 27	</cffunction>
 28
 29	<cffunction name="test_validatesConfirmationOf_missing_property_confirmation_valid">
 30		<cfset loc.user.password = "hamsterjelly">
 31		<cfset loc.user.validatesConfirmationOf(property="password")>
 32		<cfset assert('loc.user.valid()')>
 33	</cffunction>
 34
 35
 36	<!--- validatesExclusionOf --->
 37	<cffunction name="test_validatesExclusionOf_valid">
 38		<cfset loc.user.firstname = "tony">
 39		<cfset loc.user.validatesExclusionOf(property="firstname", list="per, raul, chris")>
 40		<cfset assert('loc.user.valid()')>
 41	</cffunction>
 42
 43	<cffunction name="test_validatesExclusionOf_invalid">
 44		<cfset loc.user.firstname = "tony">
 45		<cfset loc.user.validatesExclusionOf(property="firstname", list="per, raul, chris, tony")>
 46		<cfset assert('!loc.user.valid()')>
 47	</cffunction>
 48
 49	<cffunction name="test_validatesExclusionOf_missing_property_invalid">
 50		<cfset loc.user.validatesExclusionOf(property="firstname", list="per, raul, chris, tony")>
 51		<cfset assert('!loc.user.valid()')>
 52	</cffunction>
 53
 54	<cffunction name="test_validatesExclusionOf_missing_property_valid">
 55		<cfset loc.user.validatesExclusionOf(property="firstname", list="per, raul, chris, tony", allowblank="true")>
 56		<cfset assert('loc.user.valid()')>
 57	</cffunction>
 58
 59	<cffunction name="test_validatesExclusionOf_allowblank_valid">
 60		<cfset loc.user.firstname = "">
 61		<cfset loc.user.validatesExclusionOf(property="firstname", list="per, raul, chris", allowblank="true")>
 62		<cfset assert('loc.user.valid()')>
 63	</cffunction>
 64
 65	<cffunction name="test_validatesExclusionOf_allowblank_invalid">
 66		<cfset loc.user.firstname = "">
 67		<cfset loc.user.validatesExclusionOf(property="firstname", list="per, raul, chris", allowblank="false")>
 68		<cfset assert('!loc.user.valid()')>
 69	</cffunction>
 70
 71
 72	<!--- validatesFormatOf --->
 73	<cffunction name="test_validatesFormatOf_valid">
 74		<cfset loc.user.phone = "954-555-1212">
 75		<cfset loc.user.validatesFormatOf(property="phone", regex="(\d{3,3}-){2,2}\d{4,4}")>
 76		<cfset assert('loc.user.valid()')>
 77	</cffunction>
 78
 79	<cffunction name="test_validatesFormatOf_invalid">
 80		<cfset loc.user.phone = "(954) 555-1212">
 81		<cfset loc.user.validatesFormatOf(property="phone", regex="(\d{3,3}-){2,2}\d{4,4}")>
 82		<cfset assert('!loc.user.valid()')>
 83	</cffunction>
 84
 85	<cffunction name="test_validatesFormatOf_missing_property_invalid">
 86		<cfset loc.user.validatesFormatOf(property="phone", regex="(\d{3,3}-){2,2}\d{4,4}")>
 87		<cfset assert('!loc.user.valid()')>
 88	</cffunction>
 89
 90	<cffunction name="test_validatesFormatOf_missing_property_valid">
 91		<cfset loc.user.validatesFormatOf(property="phone", regex="(\d{3,3}-){2,2}\d{4,4}", allowBlank="true")>
 92		<cfset assert('loc.user.valid()')>
 93	</cffunction>
 94
 95	<cffunction name="test_validatesFormatOf_allowblank_valid">
 96		<cfset loc.user.phone = "">
 97		<cfset loc.user.validatesFormatOf(property="phone", regex="(\d{3,3}-){2,2}\d{4,4}", allowBlank="true")>
 98		<cfset assert('loc.user.valid()')>
 99	</cffunction>
100
101	<cffunction name="test_validatesFormatOf_allowblank_invalid">
102		<cfset loc.user.phone = "">
103		<cfset loc.user.validatesFormatOf(property="phone", regex="(\d{3,3}-){2,2}\d{4,4}", allowBlank="false")>
104		<cfset assert('!loc.user.valid()')>
105	</cffunction>
106
107
108	<!--- validatesInclusionOf --->
109	<cffunction name="test_validatesInclusionOf_invalid">
110		<cfset loc.user.firstname = "tony">
111		<cfset loc.user.validatesExclusionOf(property="firstname", list="per, raul, chris")>
112		<cfset assert('loc.user.valid()')>
113	</cffunction>
114
115	<cffunction name="test_validatesInclusionOf_valid">
116		<cfset loc.user.firstname = "tony">
117		<cfset loc.user.validatesExclusionOf(property="firstname", list="per, raul, chris, tony")>
118		<cfset assert('!loc.user.valid()')>
119	</cffunction>
120
121	<cffunction name="test_validatesInclusionOf_missing_property_invalid">
122		<cfset loc.user.validatesExclusionOf(property="firstname", list="per, raul, chris, tony")>
123		<cfset assert('!loc.user.valid()')>
124	</cffunction>
125
126	<cffunction name="test_validatesInclusionOf_missing_property_valid">
127		<cfset loc.user.validatesExclusionOf(property="firstname", list="per, raul, chris, tony", allowblank="true")>
128		<cfset assert('loc.user.valid()')>
129	</cffunction>
130
131	<cffunction name="test_validatesInclusionOf_allowblank_valid">
132		<cfset loc.user.firstname = "">
133		<cfset loc.user.validatesExclusionOf(property="firstname", list="per, raul, chris", allowblank="true")>
134		<cfset assert('loc.user.valid()')>
135	</cffunction>
136
137	<cffunction name="test_validatesInclusionOf_allowblank_invalid">
138		<cfset loc.user.firstname = "">
139		<cfset loc.user.validatesExclusionOf(property="firstname", list="per, raul, chris", allowblank="false")>
140		<cfset assert('!loc.user.valid()')>
141	</cffunction>
142
143
144	<!--- validatesLengthOf --->
145	<cffunction name="test_validatesLengthOf_maximum_valid">
146		<cfset loc.user.firstname = "thisisatestagain">
147		<cfset loc.user.validatesLengthOf(property="firstname", maximum="20")>
148		<cfset assert('loc.user.valid()')>
149	</cffunction>
150
151	<cffunction name="test_validatesLengthOf_maximum_invalid">
152		<cfset loc.user.firstname = "thisisatestagain">
153		<cfset loc.user.validatesLengthOf(property="firstname", maximum="15")>
154		<cfset assert('!loc.user.valid()')>
155	</cffunction>
156
157	<cffunction name="test_validatesLengthOf_missing_property_invalid">
158		<cfset loc.user.validatesLengthOf(property="firstname", maximum="15")>
159		<cfset assert('!loc.user.valid()')>
160	</cffunction>
161
162	<cffunction name="test_validatesLengthOf_missing_property_valid">
163		<cfset loc.user.validatesLengthOf(property="firstname", maximum="15", allowblank="true")>
164		<cfset assert('loc.user.valid()')>
165	</cffunction>
166
167	<cffunction name="test_validatesLengthOf_minimum_valid">
168		<cfset loc.user.firstname = "thisisatestagain">
169		<cfset loc.user.validatesLengthOf(property="firstname", minimum="15")>
170		<cfset assert('loc.user.valid()')>
171	</cffunction>
172
173	<cffunction name="test_validatesLengthOf_minimum_invalid">
174		<cfset loc.user.firstname = "thisisatestagain">
175		<cfset loc.user.validatesLengthOf(property="firstname", minimum="20")>
176		<cfset assert('!loc.user.valid()')>
177	</cffunction>
178
179	<cffunction name="test_validatesLengthOf_within_valid">
180		<cfset loc.user.firstname = "thisisatestagain">
181		<cfset loc.user.validatesLengthOf(property="firstname", within="15,20")>
182		<cfset assert('loc.user.valid()')>
183	</cffunction>
184
185	<cffunction name="test_validatesLengthOf_within_invalid">
186		<cfset loc.user.firstname = "thisisatestagain">
187		<cfset loc.user.validatesLengthOf(property="firstname", within="10,15")>
188		<cfset assert('!loc.user.valid()')>
189	</cffunction>
190
191	<cffunction name="test_validatesLengthOf_exactly_valid">
192		<cfset loc.user.firstname = "thisisatestagain">
193		<cfset loc.user.validatesLengthOf(property="firstname", exactly="16")>
194		<cfset assert('loc.user.valid()')>
195	</cffunction>
196
197	<cffunction name="test_validatesLengthOf_exactly_invalid">
198		<cfset loc.user.firstname = "thisisatestagain">
199		<cfset loc.user.validatesLengthOf(property="firstname", exactly="20")>
200		<cfset assert('!loc.user.valid()')>
201	</cffunction>
202
203	<cffunction name="test_validatesLengthOf_allowblank_valid">
204		<cfset loc.user.firstname = "">
205		<cfset loc.user.validatesLengthOf(property="firstname", allowblank="true")>
206		<cfset assert('loc.user.valid()')>
207	</cffunction>
208
209	<cffunction name="test_validatesLengthOf_allowblank_invalid">
210		<cfset loc.user.firstname = "">
211		<cfset loc.user.validatesLengthOf(property="firstname", allowblank="false")>
212		<cfset assert('!loc.user.valid()')>
213	</cffunction>
214
215
216	<!--- validatesNumericalityOf --->
217	<cffunction name="test_validatesNumericalityOf_valid">
218		<cfset loc.user.birthdaymonth = "10">
219		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth")>
220		<cfset assert('loc.user.valid()')>
221	</cffunction>
222
223	<cffunction name="test_validatesNumericalityOf_invalid">
224		<cfset loc.user.birthdaymonth = "1,000.00">
225		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth")>
226		<cfset assert('!loc.user.valid()')>
227	</cffunction>
228
229	<cffunction name="test_validatesNumericalityOf_missing_property_invalid">
230		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", onlyInteger="true")>
231		<cfset assert('!loc.user.valid()')>
232	</cffunction>
233
234	<cffunction name="test_validatesNumericalityOf_missing_property_valid">
235		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", onlyInteger="true", allowblank="true")>
236		<cfset assert('loc.user.valid()')>
237	</cffunction>
238
239	<cffunction name="test_validatesNumericalityOf_onlyInteger_valid">
240		<cfset loc.user.birthdaymonth = "1000">
241		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", onlyInteger="true")>
242		<cfset assert('loc.user.valid()')>
243	</cffunction>
244
245	<cffunction name="test_validatesNumericalityOf_onlyInteger_invalid">
246		<cfset loc.user.birthdaymonth = "1000.25">
247		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", onlyInteger="true")>
248		<cfset assert('!loc.user.valid()')>
249	</cffunction>
250
251	<cffunction name="test_validatesNumericalityOf_allowBlank_valid">
252		<cfset loc.user.birthdaymonth = "">
253		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", allowBlank="true")>
254		<cfset assert('loc.user.valid()')>
255	</cffunction>
256
257	<cffunction name="test_validatesNumericalityOf_allowBlank_invalid">
258		<cfset loc.user.birthdaymonth = "">
259		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", allowBlank="false")>
260		<cfset assert('!loc.user.valid()')>
261	</cffunction>
262
263	<cffunction name="test_validatesNumericalityOf_greaterThan_valid">
264		<cfset loc.user.birthdaymonth = "11">
265		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", greatThan="10")>
266		<cfset assert('loc.user.valid()')>
267	</cffunction>
268
269	<cffunction name="test_validatesNumericalityOf_greaterThan_invalid">
270		<cfset loc.user.birthdaymonth = "10">
271		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", greaterThan="10")>
272		<cfset assert('!loc.user.valid()')>
273	</cffunction>
274
275	<cffunction name="test_validatesNumericalityOf_greaterThanOrEqualTo_valid">
276		<cfset loc.user.birthdaymonth = "10">
277		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", greaterThanOrEqualTo="10")>
278		<cfset assert('loc.user.valid()')>
279	</cffunction>
280
281	<cffunction name="test_validatesNumericalityOf_greaterThanOrEqualTo_invalid">
282		<cfset loc.user.birthdaymonth = "9">
283		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", greaterThanOrEqualTo="10")>
284		<cfset assert('!loc.user.valid()')>
285	</cffunction>
286
287	<cffunction name="test_validatesNumericalityOf_equalTo_valid">
288		<cfset loc.user.birthdaymonth = "10">
289		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", equalTo="10")>
290		<cfset assert('loc.user.valid()')>
291	</cffunction>
292
293	<cffunction name="test_validatesNumericalityOf_equalTo_invalid">
294		<cfset loc.user.birthdaymonth = "9">
295		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", equalTo="10")>
296		<cfset assert('!loc.user.valid()')>
297	</cffunction>
298
299	<cffunction name="test_validatesNumericalityOf_lessThan_valid">
300		<cfset loc.user.birthdaymonth = "9">
301		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", lessThan="10")>
302		<cfset assert('loc.user.valid()')>
303	</cffunction>
304
305	<cffunction name="test_validatesNumericalityOf_lessThan_invalid">
306		<cfset loc.user.birthdaymonth = "10">
307		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", lessThan="10")>
308		<cfset assert('!loc.user.valid()')>
309	</cffunction>
310
311	<cffunction name="test_validatesNumericalityOf_lessThanOrEqualTo_valid">
312		<cfset loc.user.birthdaymonth = "10">
313		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", lessThanOrEqualTo="10")>
314		<cfset assert('loc.user.valid()')>
315	</cffunction>
316
317	<cffunction name="test_validatesNumericalityOf_lessThanOrEqualTo_invalid">
318		<cfset loc.user.birthdaymonth = "11">
319		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", lessThanOrEqualTo="10")>
320		<cfset assert('!loc.user.valid()')>
321	</cffunction>
322
323	<cffunction name="test_validatesNumericalityOf_odd_valid">
324		<cfset loc.user.birthdaymonth = "13">
325		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", odd="true")>
326		<cfset assert('loc.user.valid()')>
327	</cffunction>
328
329	<cffunction name="test_validatesNumericalityOf_odd_invalid">
330		<cfset loc.user.birthdaymonth = "14">
331		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", odd="true")>
332		<cfset assert('!loc.user.valid()')>
333	</cffunction>
334
335	<cffunction name="test_validatesNumericalityOf_even_valid">
336		<cfset loc.user.birthdaymonth = "14">
337		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", even="true")>
338		<cfset assert('loc.user.valid()')>
339	</cffunction>
340
341	<cffunction name="test_validatesNumericalityOf_even_invalid">
342		<cfset loc.user.birthdaymonth = "13">
343		<cfset loc.user.validatesNumericalityOf(property="birthdaymonth", even="true")>
344		<cfset assert('!loc.user.valid()')>
345	</cffunction>
346
347
348	<!--- validatesPresenceOf --->
349	<cffunction name="test_validatesPresenceOf_valid">
350		<cfset loc.user.firstname = "tony">
351		<cfset loc.user.validatesPresenceOf(property="firstname")>
352		<cfset assert('loc.user.valid()')>
353	</cffunction>
354
355	<cffunction name="test_validatesPresenceOf_invalid">
356		<cfset loc.user.validatesPresenceOf(property="firstname")>
357		<cfset assert('!loc.user.valid()')>
358	</cffunction>
359
360	<cffunction name="test_validatesPresenceOf_invalid_when_blank">
361		<cfset loc.user.firstname = "   ">
362		<cfset loc.user.validatesPresenceOf(property="firstname")>
363		<cfset assert('!loc.user.valid()')>
364	</cffunction>
365
366
367	<!--- validatesUniquenessOf --->
368	<cffunction name="test_validatesUniquenessOf_valid">
369		<cfset loc.user.firstname = "Tony">
370		<cfset loc.user.validatesUniquenessOf(property="firstname")>
371		<cfset assert('!loc.user.valid()')>
372	</cffunction>
373	
374	<cffunction name="test_validatesUniquenessOf_valids_when_updating_existing_record">
375		<cfset loc.user = model("users").findOne(where="firstname = 'Tony'")>
376		<cfset loc.user.validatesUniquenessOf(property="firstname")>
377		<cfset assert('loc.user.valid()')>
378	</cffunction>
379
380	<!--- <cffunction name="test_validatesUniquenessOf_with_blank_property_value">
381		<cfset loc.user.firstname = "">
382		<cfset loc.user.validatesUniquenessOf(property="firstname")>
383		<cfset assert('loc.user.valid()')>
384	</cffunction> --->
385
386	<!--- validate --->
387	<cffunction name="test_validate_registering_methods">
388		<cfset loc.user.firstname = "tony">
389		<cfset loc.user.validate(method="fakemethod")>
390		<cfset loc.user.validate(method="fakemethod2", when="onCreate")>
391		<cfset loc.v = loc.user.$classData().validations>
392		<cfset loc.onsave = loc.v["onsave"]>
393		<cfset loc.oncreate = loc.v["oncreate"]>
394		<cfset assert('arraylen(loc.onsave) eq 1')>
395		<cfset assert('loc.onsave[1].method eq "fakemethod"')>
396		<cfset assert('arraylen(loc.oncreate) eq 1')>
397		<cfset assert('loc.oncreate[1].method eq "fakemethod2"')>
398	</cffunction>
399
400
401</cfcomponent>