case.test.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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['Case expression builder base class'] =
  26. beforeEach: ->
  27. @func = squel.case
  28. @inst = @func()
  29. # manual return,
  30. # otherwise @inst will be treated a promise because it has a then() method
  31. return
  32. 'extends BaseBuilder': ->
  33. assert.ok (@inst instanceof squel.cls.BaseBuilder)
  34. 'toString() returns NULL': ->
  35. assert.same "NULL", @inst.toString()
  36. 'options':
  37. 'default options': ->
  38. assert.same squel.cls.DefaultQueryBuilderOptions, @inst.options
  39. 'custom options': ->
  40. e = @func({
  41. separator: ',asdf'
  42. })
  43. expected = _.extend({}, squel.cls.DefaultQueryBuilderOptions, {
  44. separator: ',asdf'
  45. })
  46. assert.same expected, e.options
  47. 'build expression':
  48. '>> when().then()':
  49. beforeEach: ->
  50. @inst.when('?', 'foo').then('bar')
  51. # manual return,
  52. # otherwise @inst will be treated a promise because it has a then() method
  53. return
  54. toString: ->
  55. assert.same @inst.toString(), 'CASE WHEN (\'foo\') THEN \'bar\' ELSE NULL END'
  56. toParam: ->
  57. assert.same @inst.toParam(), {
  58. text: 'CASE WHEN (?) THEN \'bar\' ELSE NULL END',
  59. values: ['foo']
  60. }
  61. '>> when().then().else()':
  62. beforeEach: ->
  63. @inst.when('?', 'foo').then('bar').else('foobar')
  64. # manual return,
  65. # otherwise @inst will be treated a promise because it has a then() method
  66. return
  67. toString: ->
  68. assert.same @inst.toString(), 'CASE WHEN (\'foo\') THEN \'bar\' ELSE \'foobar\' END'
  69. toParam: ->
  70. assert.same @inst.toParam(), {
  71. text: 'CASE WHEN (?) THEN \'bar\' ELSE \'foobar\' END',
  72. values: ['foo']
  73. }
  74. 'field case':
  75. beforeEach: ->
  76. @inst = @func('name').when('?', 'foo').then('bar')
  77. # manual return,
  78. # otherwise @inst will be treated a promise because it has a then() method
  79. return
  80. toString: ->
  81. assert.same @inst.toString(), 'CASE name WHEN (\'foo\') THEN \'bar\' ELSE NULL END'
  82. toParam: ->
  83. assert.same @inst.toParam(), {
  84. text: 'CASE name WHEN (?) THEN \'bar\' ELSE NULL END',
  85. values: ['foo']
  86. }
  87. module?.exports[require('path').basename(__filename)] = test