不要怂,就是干,撸起袖子干!

query.test.js 27.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
'use strict';

const { expect } = require('chai');
const Support = require('../support');
const Sequelize = Support.Sequelize;
const DataTypes = Support.Sequelize.DataTypes;
const dialect = Support.getTestDialect();
const sinon = require('sinon');
const moment = require('moment');

const qq = str => {
  if (dialect === 'postgres' || dialect === 'mssql') {
    return `"${str}"`;
  }
  if (dialect === 'mysql' || dialect === 'mariadb' || dialect === 'sqlite') {
    return `\`${str}\``;
  }
  return str;
};

describe(Support.getTestDialectTeaser('Sequelize'), () => {
  describe('query', () => {
    afterEach(function() {
      this.sequelize.options.quoteIdentifiers = true;
      console.log.restore && console.log.restore();
    });

    beforeEach(async function() {
      this.User = this.sequelize.define('User', {
        username: {
          type: Sequelize.STRING,
          unique: true
        },
        emailAddress: {
          type: Sequelize.STRING,
          field: 'email_address'
        }
      });

      this.insertQuery = `INSERT INTO ${qq(this.User.tableName)} (username, email_address, ${
        qq('createdAt')  }, ${qq('updatedAt')
      }) VALUES ('john', 'john@gmail.com', '2012-01-01 10:10:10', '2012-01-01 10:10:10')`;

      await this.User.sync({ force: true });
    });

    it('executes a query the internal way', async function() {
      await this.sequelize.query(this.insertQuery, { raw: true });
    });

    it('executes a query if only the sql is passed', async function() {
      await this.sequelize.query(this.insertQuery);
    });

    it('executes a query if a placeholder value is an array', async function() {
      await this.sequelize.query(`INSERT INTO ${qq(this.User.tableName)} (username, email_address, ` +
        `${qq('createdAt')}, ${qq('updatedAt')}) VALUES ?;`, {
        replacements: [[
          ['john', 'john@gmail.com', '2012-01-01 10:10:10', '2012-01-01 10:10:10'],
          ['michael', 'michael@gmail.com', '2012-01-01 10:10:10', '2012-01-01 10:10:10']
        ]]
      });

      const rows = await this.sequelize.query(`SELECT * FROM ${qq(this.User.tableName)};`, {
        type: this.sequelize.QueryTypes.SELECT
      });

      expect(rows).to.be.lengthOf(2);
      expect(rows[0].username).to.be.equal('john');
      expect(rows[1].username).to.be.equal('michael');
    });

    describe('QueryTypes', () => {
      it('RAW', async function() {
        await this.sequelize.query(this.insertQuery, {
          type: Sequelize.QueryTypes.RAW
        });

        const [rows, count] = await this.sequelize.query(`SELECT * FROM ${qq(this.User.tableName)};`, {
          type: Sequelize.QueryTypes.RAW
        });

        expect(rows).to.be.an.instanceof(Array);
        expect(count).to.be.ok;
      });
    });

    describe('retry',  () => {
      it('properly bind parameters on extra retries', async function() {
        const payload = {
          username: 'test',
          createdAt: '2010-10-10 00:00:00',
          updatedAt: '2010-10-10 00:00:00'
        };

        const spy = sinon.spy();

        await this.User.create(payload);

        await expect(this.sequelize.query(`
          INSERT INTO ${qq(this.User.tableName)} (username,${qq('createdAt')},${qq('updatedAt')}) VALUES ($username,$createdAt,$updatedAt);
        `, {
          bind: payload,
          logging: spy,
          retry: {
            max: 3,
            match: [
              /Validation/
            ]
          }
        })).to.be.rejectedWith(Sequelize.UniqueConstraintError);

        expect(spy.callCount).to.eql(3);
      });
    });

    describe('logging', () => {
      it('executes a query with global benchmarking option and custom logger', async () => {
        const logger = sinon.spy();
        const sequelize = Support.createSequelizeInstance({
          logging: logger,
          benchmark: true
        });

        await sequelize.query('select 1;');
        expect(logger.calledOnce).to.be.true;
        expect(logger.args[0][0]).to.be.match(/Executed \((\d*|default)\): select 1/);
        expect(typeof logger.args[0][1] === 'number').to.be.true;
      });

      it('executes a query with benchmarking option and custom logger', async function() {
        const logger = sinon.spy();

        await this.sequelize.query('select 1;', {
          logging: logger,
          benchmark: true
        });

        expect(logger.calledOnce).to.be.true;
        expect(logger.args[0][0]).to.be.match(/Executed \(\d*|default\): select 1;/);
        expect(typeof logger.args[0][1] === 'number').to.be.true;
      });

      describe('with logQueryParameters', () => {
        beforeEach(async function() {
          this.sequelize = Support.createSequelizeInstance({
            benchmark: true,
            logQueryParameters: true
          });
          this.User = this.sequelize.define('User', {
            id: {
              type: DataTypes.INTEGER,
              primaryKey: true,
              autoIncrement: true
            },
            username: {
              type: DataTypes.STRING
            },
            emailAddress: {
              type: DataTypes.STRING
            }
          }, {
            timestamps: false
          });

          await this.User.sync({ force: true });
        });

        it('add parameters in log sql', async function() {
          let createSql, updateSql;

          const user = await this.User.create({
            username: 'john',
            emailAddress: 'john@gmail.com'
          }, {
            logging: s =>{
              createSql = s;
            }
          });

          user.username = 'li';

          await user.save({
            logging: s =>{
              updateSql = s;
            }
          });

          expect(createSql).to.match(/; ("john", "john@gmail.com"|{"(\$1|0)":"john","(\$2|1)":"john@gmail.com"})/);
          expect(updateSql).to.match(/; ("li", 1|{"(\$1|0)":"li","(\$2|1)":1})/);
        });

        it('add parameters in log sql when use bind value', async function() {
          let logSql;
          const typeCast = dialect === 'postgres' ? '::text' : '';
          await this.sequelize.query(`select $1${typeCast} as foo, $2${typeCast} as bar`, { bind: ['foo', 'bar'], logging: s=>logSql = s });
          expect(logSql).to.match(/; ("foo", "bar"|{"(\$1|0)":"foo","(\$2|1)":"bar"})/);
        });
      });
    });

    it('executes select queries correctly', async function() {
      await this.sequelize.query(this.insertQuery);
      const [users] = await this.sequelize.query(`select * from ${qq(this.User.tableName)}`);
      expect(users.map(u => { return u.username; })).to.include('john');
    });

    it('executes select queries correctly when quoteIdentifiers is false', async function() {
      const seq = Object.create(this.sequelize);

      seq.options.quoteIdentifiers = false;
      await seq.query(this.insertQuery);
      const [users] = await seq.query(`select * from ${qq(this.User.tableName)}`);
      expect(users.map(u => { return u.username; })).to.include('john');
    });

    it('executes select query with dot notation results', async function() {
      await this.sequelize.query(`DELETE FROM ${qq(this.User.tableName)}`);
      await this.sequelize.query(this.insertQuery);
      const [users] = await this.sequelize.query(`select username as ${qq('user.username')} from ${qq(this.User.tableName)}`);
      expect(users).to.deep.equal([{ 'user.username': 'john' }]);
    });

    it('executes select query with dot notation results and nest it', async function() {
      await this.sequelize.query(`DELETE FROM ${qq(this.User.tableName)}`);
      await this.sequelize.query(this.insertQuery);
      const users = await this.sequelize.query(`select username as ${qq('user.username')} from ${qq(this.User.tableName)}`, { raw: true, nest: true });
      expect(users.map(u => { return u.user; })).to.deep.equal([{ 'username': 'john' }]);
    });

    if (dialect === 'mysql') {
      it('executes stored procedures', async function() {
        await this.sequelize.query(this.insertQuery);
        await this.sequelize.query('DROP PROCEDURE IF EXISTS foo');

        await this.sequelize.query(
          `CREATE PROCEDURE foo()\nSELECT * FROM ${this.User.tableName};`
        );

        const users = await this.sequelize.query('CALL foo()');
        expect(users.map(u => { return u.username; })).to.include('john');
      });
    } else {
      console.log('FIXME: I want to be supported in this dialect as well :-(');
    }

    it('uses the passed model', async function() {
      await this.sequelize.query(this.insertQuery);

      const users = await this.sequelize.query(`SELECT * FROM ${qq(this.User.tableName)};`, {
        model: this.User
      });

      expect(users[0]).to.be.instanceof(this.User);
    });

    it('maps the field names to attributes based on the passed model', async function() {
      await this.sequelize.query(this.insertQuery);

      const users = await this.sequelize.query(`SELECT * FROM ${qq(this.User.tableName)};`, {
        model: this.User,
        mapToModel: true
      });

      expect(users[0].emailAddress).to.be.equal('john@gmail.com');
    });

    it('arbitrarily map the field names', async function() {
      await this.sequelize.query(this.insertQuery);

      const users = await this.sequelize.query(`SELECT * FROM ${qq(this.User.tableName)};`, {
        type: 'SELECT',
        fieldMap: { username: 'userName', email_address: 'email' }
      });

      expect(users[0].userName).to.be.equal('john');
      expect(users[0].email).to.be.equal('john@gmail.com');
    });

    it('keeps field names that are mapped to the same name', async function() {
      await this.sequelize.query(this.insertQuery);

      const users = await this.sequelize.query(`SELECT * FROM ${qq(this.User.tableName)};`, {
        type: 'SELECT',
        fieldMap: { username: 'username', email_address: 'email' }
      });

      expect(users[0].username).to.be.equal('john');
      expect(users[0].email).to.be.equal('john@gmail.com');
    });

    describe('rejections', () => {
      it('reject if `values` and `options.replacements` are both passed', async function() {
        await this.sequelize.query({ query: 'select ? as foo, ? as bar', values: [1, 2] }, { raw: true, replacements: [1, 2] })
          .should.be.rejectedWith(Error, 'Both `sql.values` and `options.replacements` cannot be set at the same time');
      });

      it('reject if `sql.bind` and `options.bind` are both passed', async function() {
        await this.sequelize.query({ query: 'select $1 + ? as foo, $2 + ? as bar', bind: [1, 2] }, { raw: true, bind: [1, 2] })
          .should.be.rejectedWith(Error, 'Both `sql.bind` and `options.bind` cannot be set at the same time');
      });

      it('reject if `options.replacements` and `options.bind` are both passed', async function() {
        await this.sequelize.query('select $1 + ? as foo, $2 + ? as bar', { raw: true, bind: [1, 2], replacements: [1, 2] })
          .should.be.rejectedWith(Error, 'Both `replacements` and `bind` cannot be set at the same time');
      });

      it('reject if `sql.bind` and `sql.values` are both passed', async function() {
        await this.sequelize.query({ query: 'select $1 + ? as foo, $2 + ? as bar', bind: [1, 2], values: [1, 2] }, { raw: true })
          .should.be.rejectedWith(Error, 'Both `replacements` and `bind` cannot be set at the same time');
      });

      it('reject if `sql.bind` and `options.replacements`` are both passed', async function() {
        await this.sequelize.query({ query: 'select $1 + ? as foo, $2 + ? as bar', bind: [1, 2] }, { raw: true, replacements: [1, 2] })
          .should.be.rejectedWith(Error, 'Both `replacements` and `bind` cannot be set at the same time');
      });

      it('reject if `options.bind` and `sql.replacements` are both passed', async function() {
        await this.sequelize.query({ query: 'select $1 + ? as foo, $1 _ ? as bar', values: [1, 2] }, { raw: true, bind: [1, 2] })
          .should.be.rejectedWith(Error, 'Both `replacements` and `bind` cannot be set at the same time');
      });

      it('reject when key is missing in the passed object', async function() {
        await this.sequelize.query('select :one as foo, :two as bar, :three as baz', { raw: true, replacements: { one: 1, two: 2 } })
          .should.be.rejectedWith(Error, /Named parameter ":\w+" has no value in the given object\./g);
      });

      it('reject with the passed number', async function() {
        await this.sequelize.query('select :one as foo, :two as bar', { raw: true, replacements: 2 })
          .should.be.rejectedWith(Error, /Named parameter ":\w+" has no value in the given object\./g);
      });

      it('reject with the passed empty object', async function() {
        await this.sequelize.query('select :one as foo, :two as bar', { raw: true, replacements: {} })
          .should.be.rejectedWith(Error, /Named parameter ":\w+" has no value in the given object\./g);
      });

      it('reject with the passed string', async function() {
        await this.sequelize.query('select :one as foo, :two as bar', { raw: true, replacements: 'foobar' })
          .should.be.rejectedWith(Error, /Named parameter ":\w+" has no value in the given object\./g);
      });

      it('reject with the passed date', async function() {
        await this.sequelize.query('select :one as foo, :two as bar', { raw: true, replacements: new Date() })
          .should.be.rejectedWith(Error, /Named parameter ":\w+" has no value in the given object\./g);
      });

      it('reject when binds passed with object and numeric $1 is also present', async function() {
        const typeCast = dialect === 'postgres' ? '::int' : '';

        await this.sequelize.query(`select $one${typeCast} as foo, $two${typeCast} as bar, '$1' as baz`, {  raw: true, bind: { one: 1, two: 2 } })
          .should.be.rejectedWith(Error, /Named bind parameter "\$\w+" has no value in the given object\./g);
      });

      it('reject when binds passed as array and $alpha is also present', async function() {
        const typeCast = dialect === 'postgres' ? '::int' : '';

        await this.sequelize.query(`select $1${typeCast} as foo, $2${typeCast} as bar, '$foo' as baz`, { raw: true, bind: [1, 2] })
          .should.be.rejectedWith(Error, /Named bind parameter "\$\w+" has no value in the given object\./g);
      });

      it('reject when bind key is $0 with the passed array', async function() {
        await this.sequelize.query('select $1 as foo, $0 as bar, $3 as baz', { raw: true, bind: [1, 2] })
          .should.be.rejectedWith(Error, /Named bind parameter "\$\w+" has no value in the given object\./g);
      });

      it('reject when bind key is $01 with the passed array', async function() {
        await this.sequelize.query('select $1 as foo, $01 as bar, $3 as baz', { raw: true, bind: [1, 2] })
          .should.be.rejectedWith(Error, /Named bind parameter "\$\w+" has no value in the given object\./g);
      });

      it('reject when bind key is missing in the passed array', async function() {
        await this.sequelize.query('select $1 as foo, $2 as bar, $3 as baz', { raw: true, bind: [1, 2] })
          .should.be.rejectedWith(Error, /Named bind parameter "\$\w+" has no value in the given object\./g);
      });

      it('reject when bind key is missing in the passed object', async function() {
        await this.sequelize.query('select $one as foo, $two as bar, $three as baz', { raw: true, bind: { one: 1, two: 2 } })
          .should.be.rejectedWith(Error, /Named bind parameter "\$\w+" has no value in the given object\./g);
      });

      it('reject with the passed number for bind', async function() {
        await this.sequelize.query('select $one as foo, $two as bar', { raw: true, bind: 2 })
          .should.be.rejectedWith(Error, /Named bind parameter "\$\w+" has no value in the given object\./g);
      });

      it('reject with the passed empty object for bind', async function() {
        await this.sequelize.query('select $one as foo, $two as bar', { raw: true, bind: {} })
          .should.be.rejectedWith(Error, /Named bind parameter "\$\w+" has no value in the given object\./g);
      });

      it('reject with the passed string for bind', async function() {
        await this.sequelize.query('select $one as foo, $two as bar', { raw: true, bind: 'foobar' })
          .should.be.rejectedWith(Error, /Named bind parameter "\$\w+" has no value in the given object\./g);
      });

      it('reject with the passed date for bind', async function() {
        await this.sequelize.query('select $one as foo, $two as bar', { raw: true, bind: new Date() })
          .should.be.rejectedWith(Error, /Named bind parameter "\$\w+" has no value in the given object\./g);
      });
    });

    it('properly adds and escapes replacement value', async function() {
      let logSql;
      const number  = 1,
        date = new Date(),
        string = 't\'e"st',
        boolean = true,
        buffer = Buffer.from('t\'e"st');

      date.setMilliseconds(0);

      const result = await this.sequelize.query({
        query: 'select ? as number, ? as date,? as string,? as boolean,? as buffer',
        values: [number, date, string, boolean, buffer]
      }, {
        type: this.sequelize.QueryTypes.SELECT,
        logging(s) {
          logSql = s;
        }
      });

      const res = result[0] || {};
      res.date = res.date && new Date(res.date);
      res.boolean = res.boolean && true;
      if (typeof res.buffer === 'string' && res.buffer.startsWith('\\x')) {
        res.buffer = Buffer.from(res.buffer.substring(2), 'hex');
      }
      expect(res).to.deep.equal({
        number,
        date,
        string,
        boolean,
        buffer
      });
      expect(logSql).to.not.include('?');
    });

    it('it allows to pass custom class instances', async function() {
      let logSql;
      class SQLStatement {
        constructor() {
          this.values = [1, 2];
        }
        get query() {
          return 'select ? as foo, ? as bar';
        }
      }
      const result = await this.sequelize.query(new SQLStatement(), { type: this.sequelize.QueryTypes.SELECT, logging: s => logSql = s } );
      expect(result).to.deep.equal([{ foo: 1, bar: 2 }]);
      expect(logSql).to.not.include('?');
    });

    it('uses properties `query` and `values` if query is tagged', async function() {
      let logSql;
      const result = await this.sequelize.query({ query: 'select ? as foo, ? as bar', values: [1, 2] }, { type: this.sequelize.QueryTypes.SELECT, logging(s) { logSql = s; } });
      expect(result).to.deep.equal([{ foo: 1, bar: 2 }]);
      expect(logSql).to.not.include('?');
    });

    it('uses properties `query` and `bind` if query is tagged', async function() {
      const typeCast = dialect === 'postgres' ? '::int' : '';
      let logSql;
      const result = await this.sequelize.query({ query: `select $1${typeCast} as foo, $2${typeCast} as bar`, bind: [1, 2] }, { type: this.sequelize.QueryTypes.SELECT, logging(s) { logSql = s; } });
      expect(result).to.deep.equal([{ foo: 1, bar: 2 }]);
      if (dialect === 'postgres' || dialect === 'sqlite') {
        expect(logSql).to.include('$1');
        expect(logSql).to.include('$2');
      } else if (dialect === 'mssql') {
        expect(logSql).to.include('@0');
        expect(logSql).to.include('@1');
      } else if (dialect === 'mysql') {
        expect(logSql.match(/\?/g).length).to.equal(2);
      }
    });

    it('dot separated attributes when doing a raw query without nest', async function() {
      const tickChar = dialect === 'postgres' || dialect === 'mssql' ? '"' : '`',
        sql = `select 1 as ${Sequelize.Utils.addTicks('foo.bar.baz', tickChar)}`;

      await expect(this.sequelize.query(sql, { raw: true, nest: false }).then(obj => obj[0])).to.eventually.deep.equal([{ 'foo.bar.baz': 1 }]);
    });

    it('destructs dot separated attributes when doing a raw query using nest', async function() {
      const tickChar = dialect === 'postgres' || dialect === 'mssql' ? '"' : '`',
        sql = `select 1 as ${Sequelize.Utils.addTicks('foo.bar.baz', tickChar)}`;

      const result = await this.sequelize.query(sql, { raw: true, nest: true });
      expect(result).to.deep.equal([{ foo: { bar: { baz: 1 } } }]);
    });

    it('replaces token with the passed array', async function() {
      const result = await this.sequelize.query('select ? as foo, ? as bar', { type: this.sequelize.QueryTypes.SELECT, replacements: [1, 2] });
      expect(result).to.deep.equal([{ foo: 1, bar: 2 }]);
    });

    it('replaces named parameters with the passed object', async function() {
      await expect(this.sequelize.query('select :one as foo, :two as bar', { raw: true, replacements: { one: 1, two: 2 } }).then(obj => obj[0]))
        .to.eventually.deep.equal([{ foo: 1, bar: 2 }]);
    });

    it('replaces named parameters with the passed object and ignore those which does not qualify', async function() {
      await expect(this.sequelize.query('select :one as foo, :two as bar, \'00:00\' as baz', { raw: true, replacements: { one: 1, two: 2 } }).then(obj => obj[0]))
        .to.eventually.deep.equal([{ foo: 1, bar: 2, baz: '00:00' }]);
    });

    it('replaces named parameters with the passed object using the same key twice', async function() {
      await expect(this.sequelize.query('select :one as foo, :two as bar, :one as baz', { raw: true, replacements: { one: 1, two: 2 } }).then(obj => obj[0]))
        .to.eventually.deep.equal([{ foo: 1, bar: 2, baz: 1 }]);
    });

    it('replaces named parameters with the passed object having a null property', async function() {
      await expect(this.sequelize.query('select :one as foo, :two as bar', { raw: true, replacements: { one: 1, two: null } }).then(obj => obj[0]))
        .to.eventually.deep.equal([{ foo: 1, bar: null }]);
    });

    it('binds token with the passed array', async function() {
      const typeCast = dialect === 'postgres' ? '::int' : '';
      let logSql;
      const result = await this.sequelize.query(`select $1${typeCast} as foo, $2${typeCast} as bar`, { type: this.sequelize.QueryTypes.SELECT, bind: [1, 2], logging(s) { logSql = s;} });
      expect(result).to.deep.equal([{ foo: 1, bar: 2 }]);
      if (dialect === 'postgres' || dialect === 'sqlite') {
        expect(logSql).to.include('$1');
      }
    });

    it('binds named parameters with the passed object', async function() {
      const typeCast = dialect === 'postgres' ? '::int' : '';
      let logSql;
      const result = await this.sequelize.query(`select $one${typeCast} as foo, $two${typeCast} as bar`, { raw: true, bind: { one: 1, two: 2 }, logging(s) { logSql = s; } });
      expect(result[0]).to.deep.equal([{ foo: 1, bar: 2 }]);
      if (dialect === 'postgres') {
        expect(logSql).to.include('$1');
      }
      if (dialect === 'sqlite') {
        expect(logSql).to.include('$one');
      }
    });

    it('binds named parameters with the passed object using the same key twice', async function() {
      const typeCast = dialect === 'postgres' ? '::int' : '';
      let logSql;
      const result = await this.sequelize.query(`select $one${typeCast} as foo, $two${typeCast} as bar, $one${typeCast} as baz`, { raw: true, bind: { one: 1, two: 2 }, logging(s) { logSql = s; } });
      expect(result[0]).to.deep.equal([{ foo: 1, bar: 2, baz: 1 }]);
      if (dialect === 'postgres') {
        expect(logSql).to.include('$1');
        expect(logSql).to.include('$2');
        expect(logSql).to.not.include('$3');
      }
    });

    it('binds named parameters with the passed object having a null property', async function() {
      const typeCast = dialect === 'postgres' ? '::int' : '';
      const result = await this.sequelize.query(`select $one${typeCast} as foo, $two${typeCast} as bar`, { raw: true, bind: { one: 1, two: null } });
      expect(result[0]).to.deep.equal([{ foo: 1, bar: null }]);
    });

    it('binds named parameters array handles escaped $$', async function() {
      const typeCast = dialect === 'postgres' ? '::int' : '';
      let logSql;
      const result = await this.sequelize.query(`select $1${typeCast} as foo, '$$ / $$1' as bar`, { raw: true, bind: [1], logging(s) { logSql = s;} });
      expect(result[0]).to.deep.equal([{ foo: 1, bar: '$ / $1' }]);
      if (dialect === 'postgres' || dialect === 'sqlite') {
        expect(logSql).to.include('$1');
      }
    });

    it('binds named parameters object handles escaped $$', async function() {
      const typeCast = dialect === 'postgres' ? '::int' : '';
      const result = await this.sequelize.query(`select $one${typeCast} as foo, '$$ / $$one' as bar`, { raw: true, bind: { one: 1 } });
      expect(result[0]).to.deep.equal([{ foo: 1, bar: '$ / $one' }]);
    });

    it('escape where has $ on the middle of characters', async function() {
      const typeCast = dialect === 'postgres' ? '::int' : '';
      const result = await this.sequelize.query(`select $one${typeCast} as foo$bar`, { raw: true, bind: { one: 1 } });
      expect(result[0]).to.deep.equal([{ foo$bar: 1 }]);
    });

    if (dialect === 'postgres' || dialect === 'sqlite' || dialect === 'mssql') {
      it('does not improperly escape arrays of strings bound to named parameters', async function() {
        const result = await this.sequelize.query('select :stringArray as foo', { raw: true, replacements: { stringArray: ['"string"'] } });
        expect(result[0]).to.deep.equal([{ foo: '"string"' }]);
      });
    }

    it('handles AS in conjunction with functions just fine', async function() {
      let datetime = dialect === 'sqlite' ? 'date(\'now\')' : 'NOW()';
      if (dialect === 'mssql') {
        datetime = 'GETDATE()';
      }

      const [result] = await this.sequelize.query(`SELECT ${datetime} AS t`);
      expect(moment(result[0].t).isValid()).to.be.true;
    });

    if (Support.getTestDialect() === 'postgres') {
      it('replaces named parameters with the passed object and ignores casts', async function() {
        await expect(this.sequelize.query('select :one as foo, :two as bar, \'1000\'::integer as baz', { raw: true, replacements: { one: 1, two: 2 } }).then(obj => obj[0]))
          .to.eventually.deep.equal([{ foo: 1, bar: 2, baz: 1000 }]);
      });

      it('supports WITH queries', async function() {
        await expect(this.sequelize.query('WITH RECURSIVE t(n) AS ( VALUES (1) UNION ALL SELECT n+1 FROM t WHERE n < 100) SELECT sum(n) FROM t').then(obj => obj[0]))
          .to.eventually.deep.equal([{ 'sum': '5050' }]);
      });
    }

    if (Support.getTestDialect() === 'sqlite') {
      it('binds array parameters for upsert are replaced. $$ unescapes only once', async function() {
        let logSql;
        await this.sequelize.query('select $1 as foo, $2 as bar, \'$$$$\' as baz', { type: this.sequelize.QueryTypes.UPSERT, bind: [1, 2], logging(s) { logSql = s; } });
        // sqlite.exec does not return a result
        expect(logSql).to.not.include('$one');
        expect(logSql).to.include('\'$$\'');
      });

      it('binds named parameters for upsert are replaced. $$ unescapes only once', async function() {
        let logSql;
        await this.sequelize.query('select $one as foo, $two as bar, \'$$$$\' as baz', { type: this.sequelize.QueryTypes.UPSERT, bind: { one: 1, two: 2 }, logging(s) { logSql = s; } });
        // sqlite.exec does not return a result
        expect(logSql).to.not.include('$one');
        expect(logSql).to.include('\'$$\'');
      });
    }
  });
});