insert.test.coffee 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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['INSERT builder'] =
  26. beforeEach: ->
  27. @func = squel.insert
  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. 'need to call into() first': ->
  50. assert.throws (=> @inst.toString()), 'into() needs to be called'
  51. 'when set() not called': ->
  52. assert.same 'INSERT INTO table', @inst.into('table').toString()
  53. '>> into(table).set(field, null)':
  54. beforeEach: -> @inst.into('table').set('field', null)
  55. toString: ->
  56. assert.same @inst.toString(), 'INSERT INTO table (field) VALUES (NULL)'
  57. toParam: ->
  58. assert.same @inst.toParam(), { text: 'INSERT INTO table (field) VALUES (?)', values: [null] }
  59. '>> into(table)':
  60. beforeEach: -> @inst.into('table')
  61. '>> set(field, 1)':
  62. beforeEach: -> @inst.set('field', 1)
  63. toString: ->
  64. assert.same @inst.toString(), 'INSERT INTO table (field) VALUES (1)'
  65. '>> set(field2, 1.2)':
  66. beforeEach: -> @inst.set('field2', 1.2)
  67. toString: ->
  68. assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, 1.2)'
  69. '>> set(field2, "str")':
  70. beforeEach: -> @inst.set('field2', 'str')
  71. toString: ->
  72. assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, \'str\')'
  73. toParam: ->
  74. assert.same @inst.toParam(), {
  75. text: 'INSERT INTO table (field, field2) VALUES (?, ?)'
  76. values: [ 1, 'str' ]
  77. }
  78. '>> set(field2, "str", { dontQuote: true } )':
  79. beforeEach: -> @inst.set('field2', 'str', dontQuote: true)
  80. toString: ->
  81. assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, str)'
  82. toParam: ->
  83. assert.same @inst.toParam(), {
  84. text: 'INSERT INTO table (field, field2) VALUES (?, ?)'
  85. values: [ 1, 'str' ]
  86. }
  87. '>> set(field2, true)':
  88. beforeEach: -> @inst.set('field2', true)
  89. toString: ->
  90. assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, TRUE)'
  91. '>> set(field2, null)':
  92. beforeEach: -> @inst.set('field2', null)
  93. toString: ->
  94. assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, NULL)'
  95. '>> set(field, query builder)':
  96. beforeEach: ->
  97. @subQuery = squel.select().field('MAX(score)').from('scores')
  98. @inst.set( 'field', @subQuery )
  99. toString: ->
  100. assert.same @inst.toString(), 'INSERT INTO table (field) VALUES ((SELECT MAX(score) FROM scores))'
  101. toParam: ->
  102. parameterized = @inst.toParam()
  103. assert.same parameterized.text, 'INSERT INTO table (field) VALUES ((SELECT MAX(score) FROM scores))'
  104. assert.same parameterized.values, []
  105. '>> setFields({field2: \'value2\', field3: true })':
  106. beforeEach: -> @inst.setFields({field2: 'value2', field3: true })
  107. toString: ->
  108. assert.same @inst.toString(), 'INSERT INTO table (field, field2, field3) VALUES (1, \'value2\', TRUE)'
  109. toParam: ->
  110. parameterized = @inst.toParam()
  111. assert.same parameterized.text, 'INSERT INTO table (field, field2, field3) VALUES (?, ?, ?)'
  112. assert.same parameterized.values, [1,'value2',true]
  113. '>> setFields({field2: \'value2\', field: true })':
  114. beforeEach: -> @inst.setFields({field2: 'value2', field: true })
  115. toString: ->
  116. assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (TRUE, \'value2\')'
  117. toParam: ->
  118. parameterized = @inst.toParam()
  119. assert.same parameterized.text, 'INSERT INTO table (field, field2) VALUES (?, ?)'
  120. assert.same parameterized.values, [true, 'value2']
  121. '>> setFields(custom value type)':
  122. beforeEach: ->
  123. class MyClass
  124. @inst.registerValueHandler MyClass, -> 'abcd'
  125. @inst.setFields({ field: new MyClass() })
  126. toString: ->
  127. assert.same @inst.toString(), 'INSERT INTO table (field) VALUES ((abcd))'
  128. toParam: ->
  129. parameterized = @inst.toParam()
  130. assert.same parameterized.text, 'INSERT INTO table (field) VALUES (?)'
  131. assert.same parameterized.values, ['abcd']
  132. '>> setFieldsRows([{field: \'value2\', field2: true },{field: \'value3\', field2: 13 }]])':
  133. beforeEach: -> @inst.setFieldsRows([{field: 'value2', field2: true },{field: 'value3', field2: 13 }])
  134. toString: ->
  135. assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (\'value2\', TRUE), (\'value3\', 13)'
  136. toParam: ->
  137. parameterized = @inst.toParam()
  138. assert.same parameterized.text, 'INSERT INTO table (field, field2) VALUES (?, ?), (?, ?)'
  139. assert.same parameterized.values, ['value2',true, 'value3',13]
  140. 'Function values':
  141. beforeEach: -> @inst.set('field', squel.str('GETDATE(?, ?)', 2014, 'feb'))
  142. toString: ->
  143. assert.same 'INSERT INTO table (field) VALUES ((GETDATE(2014, \'feb\')))', @inst.toString()
  144. toParam: ->
  145. assert.same { text: 'INSERT INTO table (field) VALUES ((GETDATE(?, ?)))', values: [2014, 'feb'] }, @inst.toParam()
  146. '>> fromQuery([field1, field2], select query)':
  147. beforeEach: -> @inst.fromQuery(
  148. ['field1', 'field2'],
  149. squel.select().from('students').where('a = ?', 2)
  150. )
  151. toString: ->
  152. assert.same @inst.toString(), 'INSERT INTO table (field1, field2) (SELECT * FROM students WHERE (a = 2))'
  153. toParam: ->
  154. parameterized = @inst.toParam()
  155. assert.same parameterized.text, 'INSERT INTO table (field1, field2) (SELECT * FROM students WHERE (a = ?))'
  156. assert.same parameterized.values, [ 2 ]
  157. '>> setFieldsRows([{field1: 13, field2: \'value2\'},{field1: true, field3: \'value4\'}])': ->
  158. assert.throws (=> @inst.setFieldsRows([{field1: 13, field2: 'value2'},{field1: true, field3: 'value4'}]).toString()), 'All fields in subsequent rows must match the fields in the first row'
  159. 'dontQuote and replaceSingleQuotes set(field2, "ISNULL(\'str\', str)", { dontQuote: true })':
  160. beforeEach: ->
  161. @inst = squel.insert replaceSingleQuotes: true
  162. @inst.into('table').set('field', 1)
  163. @inst.set('field2', "ISNULL('str', str)", dontQuote: true)
  164. toString: ->
  165. assert.same @inst.toString(), 'INSERT INTO table (field, field2) VALUES (1, ISNULL(\'str\', str))'
  166. toParam: ->
  167. assert.same @inst.toParam(), {
  168. text: 'INSERT INTO table (field, field2) VALUES (?, ?)'
  169. values: [1, "ISNULL('str', str)"]
  170. }
  171. 'fix for #225 - autoquoting field names': ->
  172. @inst = squel.insert(autoQuoteFieldNames: true)
  173. .into('users')
  174. .set('active', 1)
  175. .set('regular', 0)
  176. .set('moderator',1)
  177. assert.same @inst.toParam(), {
  178. text: 'INSERT INTO users (`active`, `regular`, `moderator`) VALUES (?, ?, ?)',
  179. values: [1, 0, 1],
  180. }
  181. 'cloning': ->
  182. newinst = @inst.into('students').set('field', 1).clone()
  183. newinst.set('field', 2).set('field2', true)
  184. assert.same 'INSERT INTO students (field) VALUES (1)', @inst.toString()
  185. assert.same 'INSERT INTO students (field, field2) VALUES (2, TRUE)', newinst.toString()
  186. module?.exports[require('path').basename(__filename)] = test