Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Add test cases for getQuerySelectTop #117

Merged
merged 6 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion spec/db.spec.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/db/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down