123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- var Sequelizer = require('../../../index')({ dialect: 'postgres' }).sequelizer;
- var analyze = require('../../support/analyze');
- var assert = require('assert');
- describe('Sequelizer ::', function() {
- describe('Grouping statements with OR', function() {
- it('should generate a query when an OR statement is used', function() {
- var tree = analyze({
- select: ['*'],
- where: {
- or: [
- {
- id: { '>': 10 }
- },
- {
- name: 'Tester'
- }
- ]
- },
- from: 'users'
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select * from "users" where "id" > $1 or "name" = $2');
- assert.deepEqual(result.bindings, ['10', 'Tester']);
- });
- it('should generate a query when nested OR statements are used', function() {
- var tree = analyze({
- select: ['*'],
- where: {
- or: [
- {
- or: [
- { id: 1 },
- { id: { '>': 10 } }
- ]
- },
- {
- name: 'Tester'
- }
- ]
- },
- from: 'users'
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select * from "users" where ("id" = $1 or "id" > $2) or "name" = $3');
- assert.deepEqual(result.bindings, ['1', '10', 'Tester']);
- });
- it('should generate a query when complex OR statements are used', function() {
- var tree = analyze({
- select: ['*'],
- where: {
- or: [
- {
- and: [
- {
- firstName: {
- like: '%user0%'
- }
- },
- {
- type: 'or test'
- }
- ]
- },
- {
- and: [
- {
- firstName: {
- like: '%user1'
- }
- },
- {
- age: {
- '>': 0
- }
- },
- {
- type: 'or test'
- }
- ]
- }
- ]
- },
- from: 'users'
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select * from "users" where ("firstName" like $1 and "type" = $2) or ("firstName" like $3 and "age" > $4 and "type" = $5)');
- assert.deepEqual(result.bindings, ['%user0%', 'or test', '%user1', '0', 'or test']);
- });
- it('should generate a query when complex OR statements are used with IN', function() {
- var tree = analyze({
- select: ['*'],
- where: {
- and: [{
- inviteStatus: 'pending'
- }, {
- or: [{
- entityId: 1,
- inviteType: 'globalAdmin'
- }, {
- entityId: { in : [1] },
- inviteType: 'localAdmin'
- }]
- }]
- },
- from: 'users'
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select * from "users" where "inviteStatus" = $1 and (("entityId" = $2 and "inviteType" = $3) or ("entityId" in ($4) and "inviteType" = $5))');
- assert.deepEqual(result.bindings, ['pending', '1', 'globalAdmin', '1', 'localAdmin']);
- });
- it('should generate a query when complex OR statements are used with NOT IN', function() {
- var tree = analyze({
- select: ['*'],
- where: {
- and: [{
- inviteStatus: 'pending'
- }, {
- or: [{
- entityId: 1,
- inviteType: 'globalAdmin'
- }, {
- entityId: { nin : [1] },
- inviteType: 'localAdmin'
- }]
- }]
- },
- from: 'users'
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select * from "users" where "inviteStatus" = $1 and (("entityId" = $2 and "inviteType" = $3) or ("entityId" not in ($4) and "inviteType" = $5))');
- assert.deepEqual(result.bindings, ['pending', '1', 'globalAdmin', '1', 'localAdmin']);
- });
- it('should generate a query when complex multi-level nesting OR statements are used', function() {
- var tree = analyze({
- select: ['*'],
- where: {
- or: [
- {
- and: [
- {
- lastName: 'smith'
- },
- {
- or: [
- {
- age: {
- '<=': 7
- }
- },
- {
- type: 'even'
- }
- ]
- }
- ]
- },
- {
- and: [
- {
- lastName: 'jones'
- },
- {
- or: [
- {
- type: 'odd'
- },
- {
- firstName: {
- like: '%6%'
- }
- }
- ]
- }
- ]
- }
- ]
- },
- from: 'users'
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select * from "users" where ("lastName" = $1 and ("age" <= $2 or "type" = $3)) or ("lastName" = $4 and ("type" = $5 or "firstName" like $6))');
- assert.deepEqual(result.bindings, ['smith', 7, 'even', 'jones', 'odd', '%6%']);
- });
- it('should generate a query when complex even more multi-level nesting OR statements are used', function() {
- var tree = analyze({
- select: ['*'],
- where: {
- or: [
- {
- and: [
- {
- lastName: 'smith'
- },
- {
- or: [
- { age: { '<=': 7 } },
- { type: 'even' }
- ]
- }
- ]
- },
- {
- and: [
- {
- lastName: 'jones'
- },
- {
- or: [
- {
- type: 'odd'
- },
- {
- and: [
- {
- firstName: { like: '%6%' }
- },
- {
- firstName: { like: '%nested' }
- }
- ]
- }
- ]
- }
- ]
- }
- ]
- },
- from: 'users'
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select * from "users" where ("lastName" = $1 and ("age" <= $2 or "type" = $3)) or ("lastName" = $4 and ("type" = $5 or ("firstName" like $6 and "firstName" like $7)))');
- assert.deepEqual(result.bindings, ['smith', 7, 'even', 'jones', 'odd', '%6%', '%nested']);
- });
- it('should generate a query when complex even more super duper multi-level nesting OR statements are used', function() {
- var tree = analyze({
- select: ['*'],
- where: {
- or: [
- {
- and: [
- {
- lastName: 'smith'
- },
- {
- or: [
- { age: { '<=': 7 } },
- { type: 'even' }
- ]
- }
- ]
- },
- {
- and: [
- {
- lastName: 'jones'
- },
- {
- or: [
- {
- type: 'odd'
- },
- {
- and: [
- {
- firstName: {
- like: '%6%'
- }
- },
- {
- or: [
- { age: 1 },
- { age: { '<': 2 } }
- ]
- }
- ]
- }
- ]
- }
- ]
- }
- ]
- },
- from: 'users'
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select * from "users" where ("lastName" = $1 and ("age" <= $2 or "type" = $3)) or ("lastName" = $4 and ("type" = $5 or ("firstName" like $6 and ("age" = $7 or "age" < $8))))');
- assert.deepEqual(result.bindings, ['smith', 7, 'even', 'jones', 'odd', '%6%', 1, 2]);
- });
- });
- });
|