123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- var Sequelizer = require('../../../index')({ dialect: 'postgres' }).sequelizer;
- var analyze = require('../../support/analyze');
- var assert = require('assert');
- describe('Sequelizer ::', function() {
- describe('JOIN statements', function() {
- it('should generate a query when a JOIN statement is added', function() {
- var tree = analyze({
- select: ['users.id', 'contacts.phone'],
- from: 'users',
- join: [
- {
- from: 'contacts',
- on: {
- users: 'id',
- contacts: 'user_id'
- }
- }
- ]
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select "users"."id", "contacts"."phone" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id"');
- });
- it('should generate a query when a multiple JOIN statements are added', function() {
- var tree = analyze({
- select: ['users.id', 'contacts.phone', 'carriers.name'],
- from: 'users',
- join: [
- {
- from: 'contacts',
- on: {
- users: 'id',
- contacts: 'user_id'
- }
- },
- {
- from: 'carriers',
- on: {
- users: 'id',
- carriers: 'user_id'
- }
- }
- ]
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select "users"."id", "contacts"."phone", "carriers"."name" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id" inner join "carriers" on "users"."id" = "carriers"."user_id"');
- });
- it('should generate a query when an INNERJOIN statement is added', function() {
- var tree = analyze({
- select: ['users.id', 'contacts.phone'],
- from: 'users',
- innerJoin: [
- {
- from: 'contacts',
- on: {
- users: 'id',
- contacts: 'user_id'
- }
- }
- ]
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select "users"."id", "contacts"."phone" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id"');
- });
- it('should generate a query when an OUTERJOIN statement is added', function() {
- var tree = analyze({
- select: ['users.id', 'contacts.phone'],
- from: 'users',
- outerJoin: [
- {
- from: 'contacts',
- on: {
- users: 'id',
- contacts: 'user_id'
- }
- }
- ]
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select "users"."id", "contacts"."phone" from "users" outer join "contacts" on "users"."id" = "contacts"."user_id"');
- });
- it('should generate a query when grouped OR joins are added', function() {
- var tree = analyze({
- select: ['*'],
- from: 'users',
- join: [
- {
- from: 'accounts',
- on: {
- or: [
- {
- accounts: 'id',
- users: 'account_id'
- },
- {
- accounts: 'owner_id',
- users: 'id'
- }
- ]
- }
- }
- ]
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select * from "users" inner join "accounts" on "accounts"."id" = "users"."account_id" or "accounts"."owner_id" = "users"."id"');
- });
- it('should generate a query when grouped AND joins are added', function() {
- var tree = analyze({
- select: ['*'],
- from: 'users',
- join: [
- {
- from: 'accounts',
- on: [
- {
- accounts: 'id',
- users: 'account_id'
- },
- {
- accounts: 'owner_id',
- users: 'id'
- }
- ]
- }
- ]
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select * from "users" inner join "accounts" on "accounts"."id" = "users"."account_id" and "accounts"."owner_id" = "users"."id"');
- });
- it('should generate a query when multiple grouped OR joins are added', function() {
- var tree = analyze({
- select: ['*'],
- from: 'users',
- join: [
- {
- from: 'accounts',
- on: {
- or: [
- {
- accounts: 'id',
- users: 'account_id'
- },
- {
- accounts: 'owner_id',
- users: 'id'
- }
- ]
- }
- },
- {
- from: 'carriers',
- on: {
- or: [
- {
- carriers: 'id',
- users: 'account_id'
- },
- {
- carriers: 'owner_id',
- users: 'id'
- }
- ]
- }
- }
- ]
- });
- var result = Sequelizer(tree);
- assert.equal(result.sql, 'select * from "users" inner join "accounts" on "accounts"."id" = "users"."account_id" or "accounts"."owner_id" = "users"."id" inner join "carriers" on "carriers"."id" = "users"."account_id" or "carriers"."owner_id" = "users"."id"');
- });
- });
- });
|