PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/wheels/tests/model/associations/hasMany.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 209 lines | 191 code | 18 blank | 0 comment | 2 complexity | e945df2bf031642f85f3895fff68bb32 MD5 | raw file
  1<cfcomponent extends="wheelsMapping.Test">
  2
  3	<cffunction name="test_getting_children">
  4		<cfset loc.author = model("author").findOne(order="id")>
  5		<cfset loc.dynamicResult = loc.author.posts()>
  6		<cfset loc.coreResult = model("post").findAll(where="authorId=#loc.author.id#")>
  7		<cfset assert("loc.dynamicResult['title'][1] IS loc.coreResult['title'][1]")>
  8	</cffunction>
  9	
 10	<cffunction name="test_getting_children_with_include">
 11		<cfset loc.author = model("author").findOne(order="id", include="posts")>
 12		<cfset assert("IsObject(loc.author) && ArrayLen(loc.author.posts) eq 3")>
 13		<cfset loc.author = model("author").findOne(order="id", include="posts", returnAs="query")>
 14		<cfset assert("loc.author.recordcount eq 3")>
 15	</cffunction>
 16
 17	<cffunction name="test_counting_children">
 18		<cfset loc.author = model("author").findOne(order="id")>
 19		<cfset loc.dynamicResult = loc.author.postCount()>
 20		<cfset loc.coreResult = model("post").count(where="authorId=#loc.author.id#")>
 21		<cfset assert("loc.dynamicResult IS loc.coreResult")>
 22	</cffunction>
 23
 24	<cffunction name="test_checking_if_children_exist">
 25		<cfset loc.author = model("author").findOne(order="id")>
 26		<cfset loc.dynamicResult = loc.author.hasPosts()>
 27		<cfset loc.coreResult = model("post").exists(where="authorId=#loc.author.id#")>
 28		<cfset assert("loc.dynamicResult IS loc.coreResult")>
 29	</cffunction>
 30
 31	<cffunction name="test_getting_one_child">
 32		<cfset loc.author = model("author").findOne(order="id")>
 33		<cfset loc.dynamicResult = loc.author.findOnePost()>
 34		<cfset loc.coreResult = model("post").findOne(where="authorId=#loc.author.id#")>
 35		<cfset assert("loc.dynamicResult.title IS loc.coreResult.title")>
 36	</cffunction>
 37
 38	<cffunction name="test_adding_child_by_setting_foreign_key">
 39		<cfset loc.author = model("author").findOne(order="id")>
 40		<cfset loc.post = model("post").findOne(order="id DESC")>
 41		<cftransaction>
 42			<cfset loc.author.addPost(post=loc.post, transaction="none")>
 43			<!--- we need to test if authorId is set on the loc.post object as well and not just in the database! --->
 44			<cfset loc.post.reload()>
 45			<cftransaction action="rollback" />
 46		</cftransaction>		
 47		<cfset assert("loc.author.id IS loc.post.authorId")>
 48		<cfset loc.post.reload()>
 49		<cftransaction>
 50			<cfset loc.author.addPost(key=loc.post.id, transaction="none")>
 51			<cfset loc.post.reload()>
 52			<cftransaction action="rollback" />
 53		</cftransaction>		
 54		<cfset assert("loc.author.id IS loc.post.authorId")>
 55		<cfset loc.post.reload()>
 56		<cftransaction>
 57			<cfset model("post").updateByKey(key=loc.post.id, authorId=loc.author.id, transaction="none")>
 58			<cfset loc.post.reload()>
 59			<cftransaction action="rollback" />
 60		</cftransaction>		
 61		<cfset assert("loc.author.id IS loc.post.authorId")>
 62	</cffunction>
 63
 64	<cffunction name="test_removing_child_by_nullifying_foreign_key">
 65		<cfset loc.author = model("author").findOne(order="id")>
 66		<cfset loc.post = model("post").findOne(order="id DESC")>
 67		<cftransaction>
 68			<cfset loc.author.removePost(post=loc.post, transaction="none")>
 69			<!--- we need to test if authorId is set to blank on the loc.post object as well and not just in the database! --->
 70			<cfset loc.post.reload()>
 71			<cftransaction action="rollback" />
 72		</cftransaction>		
 73		<cfset assert("loc.post.authorId IS ''")>
 74		<cfset loc.post.reload()>
 75		<cftransaction>
 76			<cfset loc.author.removePost(key=loc.post.id, transaction="none")>
 77			<cfset loc.post.reload()>
 78			<cftransaction action="rollback" />
 79		</cftransaction>
 80		<cfset assert("loc.post.authorId IS ''")>
 81		<cfset loc.post.reload()>
 82		<cftransaction>
 83			<cfset model("post").updateByKey(key=loc.post.id, authorId="", transaction="none")>
 84			<cfset loc.post.reload()>
 85			<cftransaction action="rollback" />
 86		</cftransaction>		
 87		<cfset assert("loc.post.authorId IS ''")>
 88	</cffunction>
 89
 90	<cffunction name="test_deleting_child">
 91		<cfset loc.author = model("author").findOne(order="id")>
 92		<cfset loc.post = model("post").findOne(order="id DESC")>
 93		<cftransaction>
 94			<cfset loc.author.deletePost(post=loc.post, transaction="none")>
 95			<!--- should we also set loc.post to false here? --->
 96			<cfset assert("NOT model('post').exists(loc.post.id)")>
 97			<cftransaction action="rollback" />
 98		</cftransaction>
 99		<cftransaction>
100			<cfset loc.author.deletePost(key=loc.post.id, transaction="none")>
101			<cfset assert("NOT model('post').exists(loc.post.id)")>
102			<cftransaction action="rollback" />
103		</cftransaction>		
104		<cftransaction>
105			<cfset model("post").deleteByKey(key=loc.post.id, transaction="none")>
106			<cfset assert("NOT model('post').exists(loc.post.id)")>
107			<cftransaction action="rollback" />
108		</cftransaction>		
109	</cffunction>
110
111	<cffunction name="test_removing_all_children_by_nullifying_foreign_keys">
112		<cfset loc.author = model("author").findOne(order="id")>
113		<cftransaction>
114			<cfset loc.author.removeAllPosts(transaction="none")>
115			<cfset loc.dynamicResult = loc.author.postCount()>
116			<cfset loc.remainingCount = model("post").count()>
117			<cftransaction action="rollback" />
118		</cftransaction>
119		<cftransaction>
120			<cfset model("post").updateAll(authorId="", where="authorId=#loc.author.id#", transaction="none")>
121			<cfset loc.coreResult = loc.author.postCount()>
122			<cftransaction action="rollback" />
123		</cftransaction>		
124		<cfset assert("loc.dynamicResult IS 0 AND loc.coreResult IS 0 AND loc.remainingCount IS 4")>
125	</cffunction>
126
127	<cffunction name="test_deleting_all_children">
128		<cfset loc.author = model("author").findOne(order="id")>
129		<cftransaction>
130			<cfset loc.author.deleteAllPosts(transaction="none")>
131			<cfset loc.dynamicResult = loc.author.postCount()>
132			<cfset loc.remainingCount = model("post").count()>
133			<cftransaction action="rollback" />
134		</cftransaction>
135		<cftransaction>
136			<cfset model("post").deleteAll(where="authorId=#loc.author.id#", transaction="none")>
137			<cfset loc.coreResult = loc.author.postCount()>
138			<cftransaction action="rollback" />
139		</cftransaction>		
140		<cfset assert("loc.dynamicResult IS 0 AND loc.coreResult IS 0 AND loc.remainingCount IS 1")>
141	</cffunction>
142
143	<cffunction name="test_creating_new_child">
144		<cfset loc.author = model("author").findOne(order="id")>
145		<cfset loc.newPost = loc.author.newPost(title="New Title")>
146		<cfset loc.dynamicResult = loc.newPost.authorId>
147		<cfset loc.newPost = model("post").new(authorId=loc.author.id, title="New Title")>
148		<cfset loc.coreResult = loc.newPost.authorId>
149		<cfset assert("loc.dynamicResult IS loc.coreResult")>
150	</cffunction>
151
152	<cffunction name="test_creating_new_child_and_saving_it">
153		<cfset loc.author = model("author").findOne(order="id")>
154		<cftransaction>
155			<cfset loc.newPost = loc.author.createPost(title="New Title", body="New Body", transaction="none")>
156			<cfset loc.dynamicResult = loc.newPost.authorId>
157			<cftransaction action="rollback" />
158		</cftransaction>
159		<cftransaction>
160			<cfset loc.newPost = model("post").create(authorId=loc.author.id, title="New Title", body="New Body", transaction="none")>
161			<cfset loc.coreResult = loc.newPost.authorId>
162			<cftransaction action="rollback" />
163		</cftransaction>
164		<cfset assert("loc.dynamicResult IS loc.coreResult")>
165	</cffunction>
166	
167	<cffunction name="test_dependency_delete">
168		<cftransaction>
169			<cfset loc.postWithAuthor = model("post").findOne(order="id")>
170			<cfset loc.author = model("author").findByKey(key=loc.postWithAuthor.authorId)>
171			<cfset loc.author.hasMany(name="posts", dependent="delete")>
172			<cfset loc.author.delete()>
173			<cfset loc.posts = model("post").findAll(where="authorId=#loc.author.id#")>
174			<cftransaction action="rollback" />
175		</cftransaction>
176		<cfset assert("loc.posts.recordcount eq 0")>
177	</cffunction>
178
179	<cffunction name="test_dependency_deleteAll">
180		<cftransaction>
181			<cfset loc.postWithAuthor = model("post").findOne(order="id")>
182			<cfset loc.author = model("author").findByKey(key=loc.postWithAuthor.authorId)>
183			<cfset loc.author.hasMany(name="posts", dependent="deleteAll")>
184			<cfset loc.author.delete()>
185			<cfset loc.posts = model("post").findAll(where="authorId=#loc.author.id#")>
186			<cftransaction action="rollback" />
187		</cftransaction>
188		<cfset assert("loc.posts.recordcount eq 0")>
189	</cffunction>
190
191	<cffunction name="test_dependency_removeAll">
192		<cftransaction>
193			<cfset loc.postWithAuthor = model("post").findOne(order="id")>
194			<cfset loc.author = model("author").findByKey(key=loc.postWithAuthor.authorId)>
195			<cfset loc.author.hasMany(name="posts", dependent="removeAll")>
196			<cfset loc.author.delete()>
197			<cfset loc.posts = model("post").findAll(where="authorId=#loc.author.id#")>
198			<cftransaction action="rollback" />
199		</cftransaction>
200		<cfset assert("loc.posts.recordcount eq 0")>
201	</cffunction>
202
203	<cffunction name="test_getting_children_with_join_key">
204		<cfset loc.obj = model("user").findOne(order="id", include="authors")>
205		<cfset assert('loc.obj.firstName eq loc.obj.authors[1].firstName')>
206	</cffunction>
207
208
209</cfcomponent>