expressions.test.coffee 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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['Expression builder base class'] =
  26. beforeEach: ->
  27. @inst = squel.expr()
  28. 'extends BaseBuilder': ->
  29. assert.ok (@inst instanceof squel.cls.BaseBuilder)
  30. 'toString() returns empty': ->
  31. assert.same "", @inst.toString()
  32. 'options':
  33. 'default options': ->
  34. assert.same squel.cls.DefaultQueryBuilderOptions, @inst.options
  35. 'custom options': ->
  36. e = squel.expr({
  37. separator: ',asdf'
  38. })
  39. expected = _.extend({}, squel.cls.DefaultQueryBuilderOptions, {
  40. separator: ',asdf'
  41. })
  42. assert.same expected, e.options
  43. 'and()':
  44. 'without an argument throws an error': ->
  45. assert.throws (=> @inst.and()), 'expression must be a string or Expression instance'
  46. 'with an array throws an error': ->
  47. assert.throws (=> @inst.and([1])), 'expression must be a string or Expression instance'
  48. 'with an object throws an error': ->
  49. assert.throws (=> @inst.and(new Object)), 'expression must be a string or Expression instance'
  50. 'with a function throws an error': ->
  51. assert.throws (=> @inst.and(-> 1)), 'expression must be a string or Expression instance'
  52. 'with an Expression returns object instance': ->
  53. assert.same @inst, @inst.and(squel.expr())
  54. 'with a string returns object instance': ->
  55. assert.same @inst, @inst.and('bla')
  56. 'or()':
  57. 'without an argument throws an error': ->
  58. assert.throws (=> @inst.or()), 'expression must be a string or Expression instance'
  59. 'with an array throws an error': ->
  60. assert.throws (=> @inst.or([1])), 'expression must be a string or Expression instance'
  61. 'with an object throws an error': ->
  62. assert.throws (=> @inst.or(new Object)), 'expression must be a string or Expression instance'
  63. 'with a function throws an error': ->
  64. assert.throws (=> @inst.or(-> 1)), 'expression must be a string or Expression instance'
  65. 'with an Expression returns object instance': ->
  66. assert.same @inst, @inst.or(squel.expr())
  67. 'with a string returns object instance': ->
  68. assert.same @inst, @inst.or('bla')
  69. 'and("test = 3")':
  70. beforeEach: ->
  71. @inst.and("test = 3")
  72. '>> toString()': ->
  73. assert.same @inst.toString(), 'test = 3'
  74. '>> toParam()': ->
  75. assert.same @inst.toParam(), {
  76. text: 'test = 3',
  77. values: []
  78. }
  79. '>> and("flight = \'4\'")':
  80. beforeEach: ->
  81. @inst.and("flight = '4'")
  82. '>> toString()': ->
  83. assert.same @inst.toString(), "test = 3 AND flight = '4'"
  84. '>> toParam()': ->
  85. assert.same @inst.toParam(), {
  86. text: "test = 3 AND flight = '4'",
  87. values: []
  88. }
  89. '>> or("dummy IN (1,2,3)")':
  90. beforeEach: ->
  91. @inst.or("dummy IN (1,2,3)")
  92. '>> toString()': ->
  93. assert.same @inst.toString(), "test = 3 AND flight = '4' OR dummy IN (1,2,3)"
  94. '>> toParam()': ->
  95. assert.same @inst.toParam(), {
  96. text: "test = 3 AND flight = '4' OR dummy IN (1,2,3)",
  97. values: [],
  98. }
  99. 'and("test = ?", null)':
  100. beforeEach: ->
  101. @inst.and("test = ?", null)
  102. '>> toString()': ->
  103. assert.same @inst.toString(), 'test = NULL'
  104. '>> toParam()': ->
  105. assert.same @inst.toParam(), {
  106. text: 'test = ?'
  107. values: [null]
  108. }
  109. 'and("test = ?", 3)':
  110. beforeEach: ->
  111. @inst.and("test = ?", 3)
  112. '>> toString()': ->
  113. assert.same @inst.toString(), 'test = 3'
  114. '>> toParam()': ->
  115. assert.same @inst.toParam(), {
  116. text: 'test = ?'
  117. values: [3]
  118. }
  119. '>> and("flight = ?", "4")':
  120. beforeEach: ->
  121. @inst.and("flight = ?", '4')
  122. '>> toString()': ->
  123. assert.same @inst.toString(), "test = 3 AND flight = '4'"
  124. '>> toParam()': ->
  125. assert.same @inst.toParam(), {
  126. text: "test = ? AND flight = ?"
  127. values: [3, '4']
  128. }
  129. '>> or("dummy IN ?", [false, 2, null, "str"])':
  130. beforeEach: ->
  131. @inst.or("dummy IN ?", [false,2,null,"str"])
  132. '>> toString()': ->
  133. assert.same @inst.toString(), "test = 3 AND flight = '4' OR dummy IN (FALSE, 2, NULL, 'str')"
  134. '>> toParam()': ->
  135. assert.same @inst.toParam(), {
  136. text: "test = ? AND flight = ? OR dummy IN (?, ?, ?, ?)"
  137. values: [3, '4', false, 2, null, 'str']
  138. }
  139. 'or("test = 3")':
  140. beforeEach: ->
  141. @inst.or("test = 3")
  142. '>> toString()': ->
  143. assert.same @inst.toString(), 'test = 3'
  144. '>> toParam()': ->
  145. assert.same @inst.toParam(), {
  146. text: 'test = 3',
  147. values: [],
  148. }
  149. '>> or("flight = \'4\'")':
  150. beforeEach: ->
  151. @inst.or("flight = '4'")
  152. '>> toString()': ->
  153. assert.same @inst.toString(), "test = 3 OR flight = '4'"
  154. '>> toString()': ->
  155. assert.same @inst.toParam(), {
  156. text: "test = 3 OR flight = '4'",
  157. values: [],
  158. }
  159. '>> and("dummy IN (1,2,3)")':
  160. beforeEach: ->
  161. @inst.and("dummy IN (1,2,3)")
  162. '>> toString()': ->
  163. assert.same @inst.toString(), "test = 3 OR flight = '4' AND dummy IN (1,2,3)"
  164. '>> toParam()': ->
  165. assert.same @inst.toParam(), {
  166. text: "test = 3 OR flight = '4' AND dummy IN (1,2,3)",
  167. values: [],
  168. }
  169. 'or("test = ?", 3)':
  170. beforeEach: ->
  171. @inst.or("test = ?", 3)
  172. '>> toString()': ->
  173. assert.same @inst.toString(), 'test = 3'
  174. '>> toParam()': ->
  175. assert.same @inst.toParam(), {
  176. text: 'test = ?'
  177. values: [3]
  178. }
  179. '>> or("flight = ?", "4")':
  180. beforeEach: ->
  181. @inst.or("flight = ?", "4")
  182. '>> toString()': ->
  183. assert.same @inst.toString(), "test = 3 OR flight = '4'"
  184. '>> toParam()': ->
  185. assert.same @inst.toParam(), {
  186. text: "test = ? OR flight = ?"
  187. values: [3, '4']
  188. }
  189. '>> and("dummy IN ?", [false, 2, null, "str"])':
  190. beforeEach: ->
  191. @inst.and("dummy IN ?", [false, 2, null, "str"])
  192. '>> toString()': ->
  193. assert.same @inst.toString(), "test = 3 OR flight = '4' AND dummy IN (FALSE, 2, NULL, 'str')"
  194. '>> toParam()': ->
  195. assert.same @inst.toParam(), {
  196. text: "test = ? OR flight = ? AND dummy IN (?, ?, ?, ?)"
  197. values: [3, '4', false, 2, null, 'str']
  198. }
  199. 'or("test = ?", 4)':
  200. beforeEach: -> @inst.or("test = ?", 4)
  201. '>> and(expr().or("inner = ?", 1))':
  202. beforeEach: -> @inst.and( squel.expr().or('inner = ?', 1) )
  203. '>> toString()': ->
  204. assert.same @inst.toString(), "test = 4 AND (inner = 1)"
  205. '>> toParam()': ->
  206. assert.same @inst.toParam(), {
  207. text: "test = ? AND (inner = ?)"
  208. values: [4, 1]
  209. }
  210. '>> and(expr().or("inner = ?", 1).or(expr().and("another = ?", 34)))':
  211. beforeEach: ->
  212. @inst.and( squel.expr().or('inner = ?', 1).or(squel.expr().and("another = ?", 34)) )
  213. '>> toString()': ->
  214. assert.same @inst.toString(), "test = 4 AND (inner = 1 OR (another = 34))"
  215. '>> toParam()': ->
  216. assert.same @inst.toParam(), {
  217. text: "test = ? AND (inner = ? OR (another = ?))"
  218. values: [4, 1, 34]
  219. }
  220. 'custom parameter character: @@':
  221. beforeEach: ->
  222. @inst.options.parameterCharacter = '@@'
  223. 'and("test = @@", 3).and("flight = @@", "4").or("dummy IN @@", [false, 2, null, "str"])':
  224. beforeEach: ->
  225. @inst
  226. .and("test = @@", 3)
  227. .and("flight = @@", '4')
  228. .or("dummy IN @@", [false,2,null,"str"])
  229. '>> toString()': ->
  230. assert.same @inst.toString(), "test = 3 AND flight = '4' OR dummy IN (FALSE, 2, NULL, 'str')"
  231. '>> toParam()': ->
  232. assert.same @inst.toParam(), {
  233. text: "test = @@ AND flight = @@ OR dummy IN (@@, @@, @@, @@)"
  234. values: [3, '4', false, 2, null, 'str']
  235. }
  236. 'cloning': ->
  237. newinst = @inst.or("test = 4").or("inner = 1").or("inner = 2").clone()
  238. newinst.or('inner = 3')
  239. assert.same @inst.toString(), 'test = 4 OR inner = 1 OR inner = 2'
  240. assert.same newinst.toString(), 'test = 4 OR inner = 1 OR inner = 2 OR inner = 3'
  241. 'custom array prototype methods (Issue #210)': ->
  242. Array.prototype.last = () ->
  243. this[this.length - 1]
  244. @inst.or("foo = ?", "bar")
  245. delete Array.prototype.last
  246. module?.exports[require('path').basename(__filename)] = test