update.test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. var Test = require('../../support/test-runner');
  2. describe('Query Generation ::', function() {
  3. describe('UPDATE statements', function() {
  4. it('should generate an update query', function(done) {
  5. Test({
  6. query: {
  7. update: {
  8. status: 'archived'
  9. },
  10. where: {
  11. and: [
  12. {
  13. publishedDate: {
  14. '>': 2000
  15. }
  16. }
  17. ]
  18. },
  19. using: 'books'
  20. },
  21. outcomes: [
  22. {
  23. dialect: 'postgresql',
  24. sql: 'update "books" set "status" = $1 where "publishedDate" > $2',
  25. bindings: ['archived', 2000]
  26. },
  27. {
  28. dialect: 'mysql',
  29. sql: 'update `books` set `status` = ? where `publishedDate` > ?',
  30. bindings: ['archived', 2000]
  31. },
  32. {
  33. dialect: 'sqlite3',
  34. sql: 'update "books" set "status" = ? where "publishedDate" > ?',
  35. bindings: ['archived', 2000]
  36. },
  37. {
  38. dialect: 'oracle',
  39. sql: 'update "books" set "status" = :1 where "publishedDate" > :2',
  40. bindings: ['archived', 2000]
  41. },
  42. {
  43. dialect: 'mariadb',
  44. sql: 'update `books` set `status` = ? where `publishedDate` > ?',
  45. bindings: ['archived', 2000]
  46. }
  47. ]
  48. }, done);
  49. });
  50. it('should generate an update query where order doesn\'t matter', function(done) {
  51. Test({
  52. query: {
  53. where: {
  54. and: [
  55. {
  56. type: 'test'
  57. }
  58. ]
  59. },
  60. using: 'user',
  61. update: {
  62. age: 10
  63. }
  64. },
  65. outcomes: [
  66. {
  67. dialect: 'postgresql',
  68. sql: 'update "user" set "age" = $1 where "type" = $2',
  69. bindings: [10, 'test']
  70. },
  71. {
  72. dialect: 'mysql',
  73. sql: 'update `user` set `age` = ? where `type` = ?',
  74. bindings: [10, 'test']
  75. },
  76. {
  77. dialect: 'sqlite3',
  78. sql: 'update "user" set "age" = ? where "type" = ?',
  79. bindings: [10, 'test']
  80. },
  81. {
  82. dialect: 'oracle',
  83. sql: 'update "user" set "age" = :1 where "type" = :2',
  84. bindings: [10, 'test']
  85. },
  86. {
  87. dialect: 'mariadb',
  88. sql: 'update `user` set `age` = ? where `type` = ?',
  89. bindings: [10, 'test']
  90. }
  91. ]
  92. }, done);
  93. });
  94. it('should generate an insert query when using multiple values', function(done) {
  95. Test({
  96. query: {
  97. update: {
  98. status: 'archived',
  99. active: false
  100. },
  101. where: {
  102. and: [
  103. {
  104. publishedDate: {
  105. '>': 2000
  106. }
  107. }
  108. ]
  109. },
  110. using: 'books'
  111. },
  112. outcomes: [
  113. {
  114. dialect: 'postgresql',
  115. sql: 'update "books" set "active" = $1, "status" = $2 where "publishedDate" > $3',
  116. bindings: [false, 'archived', 2000]
  117. },
  118. {
  119. dialect: 'mysql',
  120. sql: 'update `books` set `active` = ?, `status` = ? where `publishedDate` > ?',
  121. bindings: [false, 'archived', 2000]
  122. },
  123. {
  124. dialect: 'sqlite3',
  125. sql: 'update "books" set "active" = ?, "status" = ? where "publishedDate" > ?',
  126. bindings: [false, 'archived', 2000]
  127. },
  128. {
  129. dialect: 'oracle',
  130. sql: 'update "books" set "active" = :1, "status" = :2 where "publishedDate" > :3',
  131. bindings: ['0', 'archived', 2000]
  132. },
  133. {
  134. dialect: 'mariadb',
  135. sql: 'update `books` set `active` = ?, `status` = ? where `publishedDate` > ?',
  136. bindings: [false, 'archived', 2000]
  137. }
  138. ]
  139. }, done);
  140. });
  141. });
  142. });