sequelizer.and.test.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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('Grouping statements with AND', function() {
  6. it('should generate a query when AND is used as an array', function() {
  7. var tree = analyze({
  8. select: '*',
  9. from: 'users',
  10. where: {
  11. and: [
  12. {
  13. firstName: 'foo'
  14. },
  15. {
  16. lastName: 'bar'
  17. }
  18. ]
  19. }
  20. });
  21. var result = Sequelizer(tree);
  22. assert.equal(result.sql, 'select * from "users" where "firstName" = $1 and "lastName" = $2');
  23. assert.deepEqual(result.bindings, ['foo', 'bar']);
  24. });
  25. it('should generate a query when nested OR statements are used', function() {
  26. var tree = analyze({
  27. select: ['*'],
  28. from: 'users',
  29. where: {
  30. and: [
  31. {
  32. or: [
  33. {
  34. firstName: 'John'
  35. },
  36. {
  37. lastName: 'Smith'
  38. }
  39. ]
  40. },
  41. {
  42. or: [
  43. {
  44. qty: {
  45. '>': 100
  46. }
  47. },
  48. {
  49. price: {
  50. '<': 10.01
  51. }
  52. }
  53. ]
  54. }
  55. ]
  56. }
  57. });
  58. var result = Sequelizer(tree);
  59. assert.equal(result.sql, 'select * from "users" where ("firstName" = $1 or "lastName" = $2) and ("qty" > $3 or "price" < $4)');
  60. assert.deepEqual(result.bindings, ['John', 'Smith', '100', '10.01']);
  61. });
  62. });
  63. });