PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wheels/tests/model/nestedproperties/one_to_one.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 108 lines | 98 code | 10 blank | 0 comment | 0 complexity | 96eff2a3a84654844808d621ea434904 MD5 | raw file
Possible License(s): Apache-2.0, CPL-1.0
  1. <cfcomponent extends="wheelsMapping.Test">
  2. <cffunction name="setup">
  3. <cfset loc.author = model("author")>
  4. <cfset loc.profile = model("profile")>
  5. <cfset $setTestObjects()>
  6. <cfset loc.testParamsStruct = $setTestParamsStruct()>
  7. </cffunction>
  8. <cffunction name="test_add_entire_data_set_via_create_and_struct" hint="Simulates adding an `author` and its child `profile` through a single structure passed into `author.create()`, much like what's normally done with the `params` struct.">
  9. <cftransaction>
  10. <!--- Should return `true` on successful create --->
  11. <cfset loc.author = loc.author.create(loc.testParamsStruct.author) />
  12. <cfset assert('IsObject(loc.author)')>
  13. <cftransaction action="rollback" />
  14. </cftransaction>
  15. <!--- Test whether profile was transformed into an object --->
  16. <cfset assert("IsObject(loc.author.profile)")>
  17. <!--- Test generated primary keys --->
  18. <cfset assert("IsNumeric(loc.author.id) and IsNumeric(loc.author.profile.id)")>
  19. </cffunction>
  20. <cffunction name="test_add_entire_data_set_via_new_and_struct" hint="Simulates adding an `author` and its child `profile` through a single structure passed into `author.new()` and saved with `author.save()`, much like what's normally done with the `params` struct.">
  21. <cfset loc.author = loc.author.new(loc.testParamsStruct.author)>
  22. <cftransaction>
  23. <!--- Should return `true` on successful create --->
  24. <cfset assert("loc.author.save()")>
  25. <cftransaction action="rollback" />
  26. </cftransaction>
  27. <!--- Test whether profile was transformed into an object --->
  28. <cfset assert("IsObject(loc.author.profile)")>
  29. <!--- Test generated primary keys --->
  30. <cfset assert("IsNumeric(loc.author.id) and IsNumeric(loc.author.profile.id)")>
  31. </cffunction>
  32. <cffunction name="test_add_child_via_object" hint="Loads an existing `author` and sets its child `profile` as an object before saving.">
  33. <cftransaction>
  34. <cfset assert("loc.testAuthor.save()")>
  35. <cfset loc.p = loc.profile.findByKey(loc.testAuthor.profile.id)>
  36. <cftransaction action="rollback" />
  37. </cftransaction>
  38. <cfset assert("IsObject(loc.p)")>
  39. </cffunction>
  40. <cffunction name="test_add_child_via_struct" hint="Loads an existing `author` and sets its child `profile` as a struct before saving.">
  41. <cftransaction>
  42. <cfset assert("loc.testAuthor.save()")>
  43. <cfset loc.testAuthor.profile = {dateOfBirth="10/02/1980 18:00:00", bio=loc.bioText}>
  44. <cfset assert("loc.testAuthor.save()")>
  45. <cfset assert("IsObject(loc.testAuthor.profile)")>
  46. <cfset loc.p = loc.profile.findByKey(loc.testAuthor.profile.id)>
  47. <cftransaction action="rollback" />
  48. </cftransaction>
  49. <cfset assert("IsObject(loc.p)")>
  50. </cffunction>
  51. <cffunction name="test_delete_child_through_object_property" hint="Loads an existing `author` and deletes its child `profile` by setting the `_delete` property to `true`.">
  52. <cftransaction>
  53. <cfset loc.testAuthor.save()>
  54. <!--- Delete profile through nested property --->
  55. <cfset loc.testAuthor.profile._delete = true>
  56. <cfset loc.profileID = loc.testAuthor.profile.id>
  57. <cfset assert("loc.testAuthor.save()")>
  58. <!--- Should return `false` because the record is now deleted --->
  59. <cfset loc.missingProfile = loc.profile.findByKey(key=loc.profileId, reload=true)>
  60. <cftransaction action="rollback" />
  61. </cftransaction>
  62. <cfset assert("IsBoolean(loc.missingProfile) and not loc.missingProfile")>
  63. </cffunction>
  64. <cffunction name="test_delete_child_through_struct" hint="Loads an existing `author` and deletes its child `property` by passing in a struct through `update()`.">
  65. <cftransaction>
  66. <!--- Save test author with child profile and grab new profile's ID --->
  67. <cfset loc.testAuthor.save()>
  68. <cfset loc.profileID = loc.testAuthor.profile.id>
  69. <!--- Delete profile through nested property --->
  70. <cfset loc.updateStruct.profile._delete = true>
  71. <cfset assert("loc.testAuthor.update(properties=loc.updateStruct)")>
  72. <!--- Should return `false` because the record is now deleted --->
  73. <cfset loc.missingProfile = loc.profile.findByKey(key=loc.profileId, reload=true)>
  74. <cftransaction action="rollback"/>
  75. </cftransaction>
  76. <cfset assert("IsBoolean(loc.missingProfile) and not loc.missingProfile")>
  77. </cffunction>
  78. <cffunction name="$setTestObjects" access="private" hint="Sets up test `author` and `profile` objects.">
  79. <cfset loc.testAuthor = loc.author.findOneByLastName(value="Peters", include="profile")>
  80. <cfset loc.bioText = "Loves learning how to write tests." />
  81. <cfset loc.testAuthor.profile = model("profile").new(dateOfBirth="10/02/1980 18:00:00", bio=loc.bioText)>
  82. </cffunction>
  83. <cffunction name="$setTestParamsStruct" access="private" hint="Sets up test `author` struct reminiscent of what would be passed through a form. The `author` represented here also includes a nested child `profile` struct.">
  84. <cfset
  85. loc.testParams = {
  86. author = {
  87. firstName="Brian",
  88. lastName="Meloche",
  89. profile = {
  90. dateOfBirth="10/02/1970 18:01:00",
  91. bio="Host of CFConversations, the best ColdFusion podcast out there."
  92. }
  93. }
  94. }
  95. >
  96. <cfreturn loc.testParams>
  97. </cffunction>
  98. </cfcomponent>