delete.test.coffee 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ###
  2. Copyright (c) 2014 Ramesh Nair (hiddentao.com)
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. ###
  22. squel = require "../dist/squel-basic"
  23. {_, testCreator, assert, expect, should} = require './testbase'
  24. test = testCreator()
  25. test['DELETE builder'] =
  26. beforeEach: ->
  27. @func = squel.delete
  28. @inst = @func()
  29. 'instanceof QueryBuilder': ->
  30. assert.instanceOf @inst, squel.cls.QueryBuilder
  31. 'constructor':
  32. 'override options': ->
  33. @inst = squel.update
  34. usingValuePlaceholders: true
  35. dummy: true
  36. expectedOptions = _.extend {}, squel.cls.DefaultQueryBuilderOptions,
  37. usingValuePlaceholders: true
  38. dummy: true
  39. for block in @inst.blocks
  40. if (block instanceof squel.cls.WhereBlock)
  41. assert.same _.extend({}, expectedOptions, { verb: 'WHERE'}), block.options
  42. else
  43. assert.same expectedOptions, block.options
  44. 'override blocks': ->
  45. block = new squel.cls.StringBlock('SELECT')
  46. @inst = @func {}, [block]
  47. assert.same [block], @inst.blocks
  48. 'build query':
  49. 'no need to call from()': ->
  50. @inst.toString()
  51. '>> from(table)':
  52. beforeEach: -> @inst.from('table')
  53. toString: ->
  54. assert.same @inst.toString(), 'DELETE FROM table'
  55. '>> table(table2, t2)':
  56. beforeEach: -> @inst.from('table2', 't2')
  57. toString: ->
  58. assert.same @inst.toString(), 'DELETE FROM table2 `t2`'
  59. '>> where(a = 1)':
  60. beforeEach: -> @inst.where('a = 1')
  61. toString: ->
  62. assert.same @inst.toString(), 'DELETE FROM table2 `t2` WHERE (a = 1)'
  63. '>> join(other_table)':
  64. beforeEach: -> @inst.join('other_table', 'o', 'o.id = t2.id')
  65. toString: ->
  66. assert.same @inst.toString(), 'DELETE FROM table2 `t2` INNER JOIN other_table `o` ON (o.id = t2.id) WHERE (a = 1)'
  67. '>> order(a, true)':
  68. beforeEach: -> @inst.order('a', true)
  69. toString: ->
  70. assert.same @inst.toString(), 'DELETE FROM table2 `t2` INNER JOIN other_table `o` ON (o.id = t2.id) WHERE (a = 1) ORDER BY a ASC'
  71. '>> limit(2)':
  72. beforeEach: -> @inst.limit(2)
  73. toString: ->
  74. assert.same @inst.toString(), 'DELETE FROM table2 `t2` INNER JOIN other_table `o` ON (o.id = t2.id) WHERE (a = 1) ORDER BY a ASC LIMIT 2'
  75. '>> target(table1).from(table1).left_join(table2, null, "table1.a = table2.b")':
  76. beforeEach: ->
  77. @inst.target('table1').from('table1').left_join('table2', null, 'table1.a = table2.b').where('c = ?', 3)
  78. toString: ->
  79. assert.same @inst.toString(),
  80. 'DELETE table1 FROM table1 LEFT JOIN table2 ON (table1.a = table2.b) WHERE (c = 3)'
  81. toParam: ->
  82. assert.same @inst.toParam(),
  83. {
  84. text: 'DELETE table1 FROM table1 LEFT JOIN table2 ON (table1.a = table2.b) WHERE (c = ?)',
  85. values: [3]
  86. }
  87. '>> target(table2)':
  88. beforeEach: ->
  89. @inst.target('table2')
  90. toString: ->
  91. assert.same @inst.toString(),
  92. 'DELETE table1, table2 FROM table1 LEFT JOIN table2 ON (table1.a = table2.b) WHERE (c = 3)'
  93. toParam: ->
  94. assert.same @inst.toParam(),
  95. {
  96. text: 'DELETE table1, table2 FROM table1 LEFT JOIN table2 ON (table1.a = table2.b) WHERE (c = ?)',
  97. values: [3]
  98. }
  99. '>> from(table1).left_join(table2, null, "table1.a = table2.b")':
  100. beforeEach: ->
  101. @inst.from('table1').left_join('table2', null, 'table1.a = table2.b').where('c = ?', 3)
  102. toString: ->
  103. assert.same @inst.toString(),
  104. 'DELETE FROM table1 LEFT JOIN table2 ON (table1.a = table2.b) WHERE (c = 3)'
  105. toParam: ->
  106. assert.same @inst.toParam(),
  107. {
  108. text: 'DELETE FROM table1 LEFT JOIN table2 ON (table1.a = table2.b) WHERE (c = ?)',
  109. values: [3]
  110. }
  111. 'cloning': ->
  112. newinst = @inst.from('students').limit(10).clone()
  113. newinst.limit(20)
  114. assert.same 'DELETE FROM students LIMIT 10', @inst.toString()
  115. assert.same 'DELETE FROM students LIMIT 20', newinst.toString()
  116. module?.exports[require('path').basename(__filename)] = test