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