PageRenderTime 7ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/wheels/tests/model/crud/transactions.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 198 lines | 171 code | 27 blank | 0 comment | 0 complexity | 782e1b924b23a59b3ccfc0c714bfda7a MD5 | raw file
  1<cfcomponent extends="wheelsMapping.Test">
  2
  3	<cffunction name="setup">
  4		<cfset application.wheels.transactionMode = "commit">
  5	</cffunction>
  6
  7	<cffunction name="teardown">
  8		<cfset application.wheels.transactionMode = "none">
  9	</cffunction>
 10
 11	<cffunction name="test_create_rollbacks_when_callback_returns_false">
 12		<cfset loc.tag = model("tagFalseCallbacks").create(name="Kermit", description="The Frog")>
 13		<cfset loc.tag = model("tagFalseCallbacks").findOne(where="name='Kermit'")>
 14		<cfset assert("loc.tag IS false")>
 15	</cffunction>
 16
 17	<cffunction name="test_update_rollbacks_when_callback_returns_false">
 18		<cfset loc.tag = model("tagFalseCallbacks").findOne(where="description='testdesc'")>
 19		<cfset loc.tag.update(name="Kermit")>
 20		<cfset loc.tag = model("tagFalseCallbacks").findOne(where="description='testdesc'")>
 21		<cfset assert("loc.tag.name IS 'releases'")>
 22	</cffunction>
 23
 24	<cffunction name="test_save_rollbacks_when_callback_returns_false">
 25		<cfset loc.tag = model("tagFalseCallbacks").findOne(where="description='testdesc'")>
 26		<cfset loc.tag.name = "Kermit">
 27		<cfset loc.tag.save()>
 28		<cfset loc.tag = model("tagFalseCallbacks").findOne(where="description='testdesc'")>
 29		<cfset assert("loc.tag.name IS 'releases'")>
 30	</cffunction>
 31
 32	<cffunction name="test_delete_rollbacks_when_callback_returns_false">
 33		<cfset loc.tag = model("tagFalseCallbacks").findOne(where="description='testdesc'")>
 34		<cfset loc.tag.delete()>
 35		<cfset loc.tag = model("tagFalseCallbacks").findOne(where="description='testdesc'")>
 36		<cfset assert("IsObject(loc.tag)")>
 37	</cffunction>
 38
 39	<cffunction name="test_deleteAll_with_instantiate_rollbacks_when_callback_returns_false">
 40		<cfset model("tagFalseCallbacks").deleteAll(instantiate=true)>
 41		<cfset loc.results = model("tagFalseCallbacks").findAll()>
 42		<cfset assert("loc.results.recordcount IS 8")>
 43	</cffunction>
 44
 45	<cffunction name="test_updateAll_with_instantiate_rollbacks_when_callback_returns_false">
 46		<cfset model("tagFalseCallbacks").updateAll(name="Kermit", instantiate=true)>
 47		<cfset loc.results = model("tagFalseCallbacks").findAll(where="name = 'Kermit'")>
 48		<cfset assert("loc.results.recordcount IS 0")>
 49	</cffunction>
 50
 51	<cffunction name="test_create_with_rollback">
 52		<cfset loc.tag = model("tag").create(name="Kermit", description="The Frog", transaction="rollback")>
 53		<cfset loc.tag = model("tag").findOne(where="name='Kermit'")>
 54		<cfset assert("not IsObject(loc.tag)")>
 55	</cffunction>
 56
 57	<cffunction name="test_update_with_rollback">
 58		<cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
 59		<cfset loc.tag.update(name="Kermit", transaction="rollback")>
 60		<cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
 61		<cfset assert("loc.tag.name IS 'releases'")>
 62	</cffunction>
 63
 64	<cffunction name="test_save_with_rollback">
 65		<cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
 66		<cfset loc.tag.name = "Kermit">
 67		<cfset loc.tag.save(transaction="rollback")>
 68		<cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
 69		<cfset assert("loc.tag.name IS 'releases'")>
 70	</cffunction>
 71
 72	<cffunction name="test_delete_with_rollback">
 73		<cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
 74		<cfset loc.tag.delete(transaction="rollback")>
 75		<cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
 76		<cfset assert("IsObject(loc.tag)")>
 77	</cffunction>
 78
 79	<cffunction name="test_deleteAll_with_rollback">
 80		<cfset model("tag").deleteAll(instantiate=true, transaction="rollback")>
 81		<cfset loc.results = model("tag").findAll()>
 82		<cfset assert("loc.results.recordcount IS 8")>
 83	</cffunction>
 84
 85	<cffunction name="test_updateAll_with_rollback">
 86		<cfset model("tag").updateAll(name="Kermit", instantiate=true, transaction="rollback")>
 87		<cfset loc.results = model("tag").findAll(where="name = 'Kermit'")>
 88		<cfset assert("loc.results.recordcount IS 0")>
 89	</cffunction>
 90
 91	<cffunction name="test_create_with_transactions_disabled">
 92		<cftransaction>
 93			<cfset loc.tag = model("tag").create(name="Kermit", description="The Frog", transaction="none")>
 94			<cfset loc.tag = model("tag").findOne(where="name='Kermit'")>
 95			<cfset assert("IsObject(loc.tag)")>
 96			<cftransaction action="rollback" />
 97		</cftransaction>
 98	</cffunction>
 99
100	<cffunction name="test_update_with_transactions_disabled">
101		<cftransaction>
102			<cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
103			<cfset loc.tag.update(name="Kermit", transaction="none")>
104			<cfset loc.tag.reload()>
105			<cfset assert("loc.tag.name IS 'Kermit'")>
106			<cftransaction action="rollback" />
107		</cftransaction>
108	</cffunction>
109
110	<cffunction name="test_save_with_transactions_disabled">
111		<cftransaction>
112			<cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
113			<cfset loc.tag.name = "Kermit">
114			<cfset loc.tag.save(transaction="none")>
115			<cfset loc.tag.reload()>
116			<cfset assert("loc.tag.name IS 'Kermit'")>
117			<cftransaction action="rollback" />
118		</cftransaction>
119	</cffunction>
120
121	<cffunction name="test_delete_with_transactions_disabled">
122		<cftransaction>
123			<cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
124			<cfset loc.tag.delete(transaction="none")>
125			<cfset loc.tag = model("tag").findOne(where="description='testdesc'")>
126			<cfset assert("not IsObject(loc.tag)")>
127			<cftransaction action="rollback" />
128		</cftransaction>
129	</cffunction>
130
131	<cffunction name="test_deleteAll_with_transactions_disabled">
132		<cftransaction>
133			<cfset model("tag").deleteAll(instantiate=true, transaction="none")>
134			<cfset loc.results = model("tag").findAll()>
135			<cfset assert("loc.results.recordcount IS 0")>
136			<cftransaction action="rollback" />
137		</cftransaction>
138	</cffunction>
139
140	<cffunction name="test_updateAll_with_transactions_disabled">
141		<cftransaction>
142			<cfset model("tag").updateAll(name="Kermit", instantiate=true, transaction="none")>
143			<cfset loc.results = model("tag").findAll(where="name = 'Kermit'")>
144			<cfset assert("loc.results.recordcount IS 8")>
145			<cftransaction action="rollback" />
146		</cftransaction>
147	</cffunction>
148
149	<cffunction name="test_nested_transaction_within_callback">
150		<cfset loc.tag = model("tagWithDataCallbacks").create(name="Kermit", description="The Frog", transaction="rollback")>
151		<cfset assert("IsObject(loc.tag)")>
152	</cffunction>
153
154	<cffunction name="test_nested_transaction_within_callback_with_transactions_disabled">
155		<cftransaction>
156			<cfset loc.tag = model("tagWithDataCallbacks").create(name="Kermit", description="The Frog", transaction="none")>
157			<cfset assert("IsObject(loc.tag)")>
158			<cftransaction action="rollback" />
159		</cftransaction>
160	</cffunction>
161		
162	<cffunction name="test_transaction_closed_after_rollback">
163		<cfset loc.hash = model("tag").$hashedConnectionArgs()>
164		<cfset loc.tag = model("tagWithDataCallbacks").create(name="Kermit", description="The Frog", transaction="rollback")>
165		<cfset assert('request.wheels.transactions[loc.hash] eq false')>
166	</cffunction>
167	
168	<cffunction name="test_transaction_closed_after_none">
169		<cfset loc.hash = model("tag").$hashedConnectionArgs()>
170		<cftransaction>
171			<cfset loc.tag = model("tagWithDataCallbacks").create(name="Kermit", description="The Frog", transaction="none")>
172			<cftransaction action="rollback" />
173		</cftransaction>
174		<cfset assert('request.wheels.transactions[loc.hash] eq false')>
175	</cffunction>
176
177	<cffunction name="test_transaction_closed_when_error_raised">
178		<cfset loc.hash = model("tag").$hashedConnectionArgs()>
179		<cftry>
180			<cfset loc.tag = model("tag").create(id="", name="Kermit", description="The Frog", transaction="rollback")>
181			<cfcatch type="any"></cfcatch>
182		</cftry>
183		<cfset assert('request.wheels.transactions[loc.hash] eq false')>
184	</cffunction>
185
186	<cffunction name="test_rollback_when_error_raised">
187		<cfset loc.tag = Duplicate(model("tagWithDataCallbacks").new(name="Kermit", description="The Frog"))>
188		<cfset loc.tag.afterSave(methods="crashMe")>
189		<cftry>
190			<cfset loc.tag.save()>
191			<cfcatch>
192				<cfset loc.results = model("tag").findAll(where="name = 'Kermit'")>
193			</cfcatch>
194		</cftry>
195		<cfset assert("loc.results.recordcount IS 0")>
196	</cffunction>
197
198</cfcomponent>