PageRenderTime 83ms CodeModel.GetById 41ms app.highlight 2ms RepoModel.GetById 18ms app.codeStats 0ms

/wheels/tests/model/crud/finders.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 214 lines | 177 code | 37 blank | 0 comment | 1 complexity | e6286d6648f950e32f5e9eb28baed2ed MD5 | raw file
  1<cfcomponent extends="wheelsMapping.Test">
  2
  3	<cffunction name="setup">
  4		<cfset loc.user = model("user")>
  5	</cffunction>
  6
  7	<cffunction name="test_select_distinct_addresses">
  8		<cfset loc.q = loc.user.findAll(select="address", distinct="true", order="address")>
  9		<cfset assert('loc.q.recordcount eq 4')>
 10		<cfset loc.e = "123 Petruzzi St.|456 Peters Dr.|789 Djurner Ave.|987 Riera Blvd.">
 11		<cfset loc.r = valuelist(loc.q.address, "|")>
 12		<cfset assert('loc.e eq loc.r')>
 13	</cffunction>
 14
 15	<cffunction name="test_select_users_groupby_address">
 16		<cfset loc.q = loc.user.findAll(select="address", group="address", order="address", result="loc.result")>
 17		<cfset assert('loc.q.recordcount eq 4')>
 18		<cfset loc.e = "123 Petruzzi St.|456 Peters Dr.|789 Djurner Ave.|987 Riera Blvd.">
 19		<cfset loc.r = valuelist(loc.q.address, "|")>
 20		<cfset assert('loc.e eq loc.r')>
 21	</cffunction>
 22
 23 	<cffunction name="test_findByKey">
 24		<cfset loc.e = loc.user.findOne(where="lastname = 'Petruzzi'")>
 25		<cfset loc.q = loc.user.findByKey(loc.e.id)>
 26		<cfset assert('loc.q.id eq loc.e.id')>
 27	</cffunction>
 28
 29 	<cffunction name="test_findByKey_returns_false_when_record_not_found">
 30		<cfset loc.q = loc.user.findByKey(999999999)>
 31		<cfset assert('loc.q eq false')>
 32	</cffunction>
 33
 34 	<cffunction name="test_findByKey_returns_false_when_passed_blank_string">
 35		<cfset loc.q = loc.user.findByKey("")>
 36		<cfset assert('loc.q eq false')>
 37	</cffunction>
 38
 39 	<cffunction name="test_findByKey_returns_empty_query_when_record_not_found_with_return_as_equal_query">
 40		<cfset loc.q = loc.user.findByKey(key=999999999, returnAs="query")>
 41		<cfset assert('loc.q.RecordCount eq false')>
 42	</cffunction>
 43
 44 	<cffunction name="test_findOne">
 45		<cfset loc.e = loc.user.findOne(where="lastname = 'Petruzzi'")>
 46		<cfset assert('isobject(loc.e)')>
 47	</cffunction>
 48
 49 	<cffunction name="test_findOne_returns_false_when_record_not_found">
 50		<cfset loc.e = loc.user.findOne(where="lastname = 'somenamenotfound'")>
 51		<cfset assert('loc.e eq false')>
 52	</cffunction>
 53
 54 	<cffunction name="test_findOne_returns_empty_query_when_record_not_found_with_return_as_equal_query">
 55		<cfset loc.e = loc.user.findOne(where="lastname = 'somenamenotfound'", returnAs="query")>
 56		<cfset assert('loc.e.RecordCount eq false')>
 57	</cffunction>
 58
 59	<cffunction name="test_findOne_returns_false_when_record_not_found_with_inner_join_include">
 60		<cfset loc.e = loc.user.findOne(where="lastname= = 'somenamenotfound'", include="galleries") />
 61		<cfset assert('loc.e eq false')>
 62	</cffunction>
 63
 64	<cffunction name="test_findOne_returns_false_when_record_not_found_with_outer_join_include">
 65		<cfset loc.e = loc.user.findOne(where="lastname= = 'somenamenotfound'", include="outerjoinphotogalleries") />
 66		<cfset assert('loc.e eq false')>
 67	</cffunction>
 68
 69 	<cffunction name="test_findAll">
 70		<cfset loc.q = loc.user.findAll()>
 71		<cfset assert('loc.q.recordcount eq 5')>
 72		<cfset loc.q = loc.user.findAll(where="lastname = 'Petruzzi' OR lastname = 'Peters'", order="lastname")>
 73		<cfset assert('loc.q.recordcount eq 2')>
 74		<cfset assert('valuelist(loc.q.lastname) eq "peters,Petruzzi"')>
 75	</cffunction>
 76
 77	<cffunction name="test_findAllByXXX">
 78		<cfset loc.q = loc.user.findAllByZipcode(value="22222", order="id")>
 79		<cfset assert('loc.q.recordcount eq 2')>
 80		<cfset loc.q = loc.user.findAllByZipcode(value="11111", order="id")>
 81		<cfset assert('loc.q.recordcount eq 1')>
 82	</cffunction>
 83
 84	<cffunction name="test_findByKey_norecords_returns_correct_type">
 85		<cfset loc.q = loc.user.findByKey("0")>
 86		<cfset debug('loc.q', false)>
 87		<cfset assert('isboolean(loc.q) and loc.q eq false')>
 88
 89		<cfset loc.q = loc.user.findByKey(key="0", returnas="query")>
 90		<cfset debug('loc.q', false)>
 91		<cfset assert('isquery(loc.q) and loc.q.recordcount eq 0')>
 92
 93		<cfset loc.q = loc.user.findByKey(key="0", returnas="object")>
 94		<cfset debug('loc.q', false)>
 95		<cfset assert('isboolean(loc.q) and loc.q eq false')>
 96
 97		<!--- readd when we have implemented the code to throw an error when an incorrect returnAs value is passed in
 98		<cfset loc.q = raised('loc.user.findByKey(key="0", returnas="objects")')>
 99		<cfset loc.r = "Wheels.IncorrectArgumentValue">
100		<cfset debug('loc.q', false)>
101		<cfset assert('loc.q eq loc.r')> --->
102	</cffunction>
103
104	<cffunction name="test_findOne_norecords_returns_correct_type">
105		<cfset loc.q = loc.user.findOne(where="id = 0")>
106		<cfset debug('loc.q', false)>
107		<cfset assert('isboolean(loc.q) and loc.q eq false')>
108
109		<cfset loc.q = loc.user.findOne(where="id = 0", returnas="query")>
110		<cfset debug('loc.q', false)>
111		<cfset assert('isquery(loc.q) and loc.q.recordcount eq 0')>
112
113		<cfset loc.q = loc.user.findOne(where="id = 0", returnas="object")>
114		<cfset debug('loc.q', false)>
115		<cfset assert('isboolean(loc.q) and loc.q eq false')>
116
117		<!--- readd when we have implemented the code to throw an error when an incorrect returnAs value is passed in
118		<cfset loc.q = raised('loc.user.findOne(where="id = 0", returnas="objects")')>
119		<cfset loc.r = "Wheels.IncorrectArgumentValue">
120		<cfset debug('loc.q', false)>
121		<cfset assert('loc.q eq loc.r')> --->
122	</cffunction>
123
124	<cffunction name="test_findAll_returnAs_query_noRecords_returns_correct_type">
125		<cfset loc.q = loc.user.findAll(where="id = 0", returnas="query")>
126		<cfset debug('loc.q', false)>
127		<cfset assert('isquery(loc.q) and loc.q.recordcount eq 0')>
128	</cffunction>
129
130	<cffunction name="test_findAll_returnAs_structs_noRecords_returns_correct_type">
131		<cfset loc.q = loc.user.findAll(where="id = 0", returnAs="structs")>
132		<cfset debug('loc.q', false)>
133		<cfset assert('isarray(loc.q) and arrayisempty(loc.q)')>
134	</cffunction>
135
136	<cffunction name="test_findAll_returnAs_objects_noRecords_returns_correct_type">
137		<cfset loc.q = loc.user.findAll(where="id = 0", returnas="objects")>
138		<cfset debug('loc.q', false)>
139		<cfset assert('isarray(loc.q) and arrayisempty(loc.q)')>
140	</cffunction>
141
142	<cffunction name="test_findAll_returnAs_invalid_throws_error">
143		<cfset loc.q = raised('loc.user.findAll(where="id = 1", returnas="notvalid")')>
144		<cfset loc.r = "Wheels.IncorrectArgumentValue">
145		<cfset debug('loc.q', false)>
146		<cfset assert('loc.q eq loc.r')>
147	</cffunction>
148
149	<cffunction name="test_exists_by_key_valid">
150		<cfset loc.e = loc.user.findOne(where="lastname = 'Petruzzi'")>
151		<cfset loc.r = loc.user.exists(loc.e.id)>
152		<cfset assert('loc.r eq true')>
153	</cffunction>
154
155	<cffunction name="test_exists_by_key_invalid">
156		<cfset loc.r = loc.user.exists(0)>
157		<cfset assert('loc.r eq false')>
158	</cffunction>
159
160	<cffunction name="test_exists_by_where_one_record_valid">
161		<cfset loc.r = loc.user.exists(where="lastname = 'Petruzzi'")>
162		<cfset assert('loc.r eq true')>
163	</cffunction>
164
165	<cffunction name="test_exists_by_where_one_record_invalid">
166		<cfset loc.r = loc.user.exists(where="lastname = 'someoneelse'")>
167		<cfset assert('loc.r eq false')>
168	</cffunction>
169
170	<cffunction name="test_exists_by_where_two_records_valid">
171		<cfset loc.r = loc.user.exists(where="zipcode = '22222'")>
172		<cfset assert('loc.r eq true')>
173	</cffunction>
174
175	<cffunction name="test_allow_negative_values_in_where_clause">
176		<cfset loc.r = loc.user.exists(where="id = -1")>
177		<cfset assert('loc.r eq false')>
178	</cffunction>
179
180	<cffunction name="test_findByKey_with_include_soft_deletes">
181		<cftransaction action="begin">
182			<cfset loc.post1 = model("Post").findOne()>
183			<cfset loc.post1.delete(transaction="none")>
184			<cfset loc.post2 = model("Post").findByKey(key=loc.post1.id, includeSoftDeletes=true)>
185			<cftransaction action="rollback" />
186		</cftransaction>
187		<cfset assert('IsObject(loc.post2) is true')>
188	</cffunction>
189
190	<cffunction name="test_findOne_with_include_soft_deletes">
191		<cftransaction action="begin">
192			<cfset loc.post1 = model("Post").findOne()>
193			<cfset loc.post1.delete(transaction="none")>
194			<cfset loc.post2 = model("Post").findOne(where="id=#loc.post1.id#", includeSoftDeletes=true)>
195			<cftransaction action="rollback" />
196		</cftransaction>
197		<cfset assert('IsObject(loc.post2) is true')>
198	</cffunction>
199
200	<cffunction name="test_findAll_with_include_soft_deletes">
201		<cftransaction action="begin">
202			<cfset model("Post").deleteAll()>
203			<cfset loc.allPosts = model("Post").findAll(includeSoftDeletes=true)>
204			<cftransaction action="rollback" />
205		</cftransaction>
206		<cfset assert('loc.allPosts.recordcount eq 4')>
207	</cffunction>
208
209	<cffunction name="test_findOne_returns_empty_array_for_included_model_when_none_exist">
210		<cfset loc.e = model("author").findOne(where="lastname = 'Bellenie'", include="posts")>
211		<cfset assert('IsArray(loc.e.posts) && ArrayIsEmpty(loc.e.posts)')>
212	</cffunction>
213
214</cfcomponent>