sequelizer.insert.test.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var Sequelizer = require('../../../index')({ dialect: 'postgres' }).sequelizer;
  2. var analyze = require('../../support/analyze');
  3. var assert = require('assert');
  4. describe('Sequelizer ::', function() {
  5. describe('INSERT statements', function() {
  6. it('should generate a simple query with an INSERT statement', function() {
  7. var tree = analyze({
  8. insert: {
  9. title: 'Slaughterhouse Five'
  10. },
  11. into: 'books'
  12. });
  13. var result = Sequelizer(tree);
  14. assert.equal(result.sql, 'insert into "books" ("title") values ($1)');
  15. assert.deepEqual(result.bindings, ['Slaughterhouse Five']);
  16. });
  17. it('should generate a query with multiple values being inserted', function() {
  18. var tree = analyze({
  19. insert: {
  20. title: 'Slaughterhouse Five',
  21. author: 'Kurt Vonnegut'
  22. },
  23. into: 'books'
  24. });
  25. var result = Sequelizer(tree);
  26. assert.equal(result.sql, 'insert into "books" ("author", "title") values ($1, $2)');
  27. assert.deepEqual(result.bindings, ['Kurt Vonnegut', 'Slaughterhouse Five']);
  28. });
  29. it('should generate a query with an array of values being inserted', function() {
  30. var tree = analyze({
  31. insert: [
  32. {
  33. title: 'Slaughterhouse Five',
  34. author: 'Kurt Vonnegut'
  35. },
  36. {
  37. title: 'The Great Gatsby',
  38. author: 'F. Scott Fitzgerald'
  39. }
  40. ],
  41. into: 'books'
  42. });
  43. var result = Sequelizer(tree);
  44. assert.equal(result.sql, 'insert into "books" ("author", "title") values ($1, $2), ($3, $4)');
  45. assert.deepEqual(result.bindings, ['Kurt Vonnegut', 'Slaughterhouse Five', 'F. Scott Fitzgerald', 'The Great Gatsby']);
  46. });
  47. });
  48. });