diff --git a/spec/db.spec.js b/spec/db.spec.js index 6e21d09..7931b2b 100644 --- a/spec/db.spec.js +++ b/spec/db.spec.js @@ -1,9 +1,11 @@ import chai, { expect } from 'chai'; import chaiAsPromised from 'chai-as-promised'; -import { db } from '../src'; +import { stub } from 'sinon'; import config from './databases/config'; import setupSQLite from './databases/sqlite/setup'; import setupCassandra from './databases/cassandra/setup'; +import { db, config as srcConfig } from '../src'; +import { clearLimitSelect } from '../src/db/client'; import { versionCompare } from '../src/utils'; chai.use(chaiAsPromised); @@ -632,6 +634,68 @@ describe('db', () => { }); }); + describe('.getQuerySelectTop', () => { + let stubObj; + + beforeEach(() => { + stubObj = stub(srcConfig, 'get'); + }); + + afterEach(() => { + stubObj.restore(); + clearLimitSelect(); + }); + + it('should return select with default limit', async () => { + stubObj.returns({}); + const sql = await dbConn.getQuerySelectTop('test_table'); + if (mysqlClients.includes(dbClient)) { + expect(sql).to.eql('SELECT * FROM `test_table` LIMIT 1000'); + } else if (postgresClients.includes(dbClient)) { + expect(sql).to.eql('SELECT * FROM "public"."test_table" LIMIT 1000'); + } else if (dbClient === 'sqlite' || dbClient === 'cassandra') { + expect(sql).to.eql('SELECT * FROM "test_table" LIMIT 1000'); + } else if (dbClient === 'sqlserver') { + expect(sql).to.eql('SELECT TOP 1000 * FROM [test_table]'); + } else { + throw new Error('Invalid db client'); + } + }); + + it('should return select with limit from config', async () => { + stubObj.returns({ + limitQueryDefaultSelectTop: 125, + }); + const sql = await dbConn.getQuerySelectTop('test_table'); + if (mysqlClients.includes(dbClient)) { + expect(sql).to.eql('SELECT * FROM `test_table` LIMIT 125'); + } else if (postgresClients.includes(dbClient)) { + expect(sql).to.eql('SELECT * FROM "public"."test_table" LIMIT 125'); + } else if (dbClient === 'sqlite' || dbClient === 'cassandra') { + expect(sql).to.eql('SELECT * FROM "test_table" LIMIT 125'); + } else if (dbClient === 'sqlserver') { + expect(sql).to.eql('SELECT TOP 125 * FROM [test_table]'); + } else { + throw new Error('Invalid db client'); + } + }); + + it('should return select with limit from parameters', async () => { + const sql = await dbConn.getQuerySelectTop('test_table', 'public', 222); + if (mysqlClients.includes(dbClient)) { + expect(sql).to.eql('SELECT * FROM `test_table` LIMIT 222'); + } else if (postgresClients.includes(dbClient)) { + expect(sql).to.eql('SELECT * FROM "public"."test_table" LIMIT 222'); + } else if (dbClient === 'sqlite' || dbClient === 'cassandra') { + expect(sql).to.eql('SELECT * FROM "test_table" LIMIT 222'); + } else if (dbClient === 'sqlserver') { + expect(sql).to.eql('SELECT TOP 222 * FROM [test_table]'); + } else { + throw new Error('Invalid db client'); + } + }); + }); + if (dbClient !== 'cassandra') { describe('.query', function () { // eslint-disable-line func-names this.timeout(15000); diff --git a/src/db/client.js b/src/db/client.js index 25ff856..d0c58a4 100644 --- a/src/db/client.js +++ b/src/db/client.js @@ -45,6 +45,11 @@ export function createConnection(server, database) { } +export function clearLimitSelect() { + limitSelect = null; +} + + async function connect(server, database) { /* eslint no-param-reassign: 0 */ if (database.connecting) { @@ -197,7 +202,7 @@ async function getQuerySelectTop(server, database, table, schema, limit) { let limitValue = limit; if (typeof limit === 'undefined') { await loadConfigLimit(); - limitValue = typeof limitSelect !== 'undefined' ? limitSelect : DEFAULT_LIMIT; + limitValue = (typeof limitSelect !== 'undefined' && limitSelect !== null) ? limitSelect : DEFAULT_LIMIT; } return database.connection.getQuerySelectTop(table, limitValue, schema); } @@ -279,7 +284,7 @@ function wrap(database, identifier) { } async function loadConfigLimit() { - if (limitSelect === null) { + if (typeof limitSelect === 'undefined' || limitSelect === null) { const { limitQueryDefaultSelectTop } = await config.get(); limitSelect = limitQueryDefaultSelectTop; }