pgadmin4/web/regression/javascript/pgadmin_utils_spec.js
Aditya Toshniwal f16498a8a7 Optimize Webpack to improve overall performance.
Changes include:
1) Remove underscore-string and sprintf-js packages as we were using only %s. Instead, added a function to do the same. Also changed gettext to behave like sprintf directly.
2) backgrid.sizeable.columns was not used anywhere, removed. @babel/polyfill is deprecated, replaced it with core-js.
3) Moved few css to make sure they get minified and bundled.
4) Added Flask-Compress to send static files as compressed gzip. This will reduce network traffic and improve initial load time for pgAdmin.
5) Split few JS files to make code reusable.
6) Lazy load few modules like leaflet, wkx is required only if geometry viewer is opened. snapsvg loaded only when explain plan is executed. This will improve sqleditor initial opening time.

Reviewed By: Khushboo Vashi
Fixes #4701
2019-10-10 12:05:28 +05:30

171 lines
4.4 KiB
JavaScript

/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2019, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import { getEpoch, getGCD, getMod, quote_ident, parseFuncParams, getRandomInt, sprintf } from 'sources/utils';
describe('getEpoch', function () {
it('should return non zero', function () {
expect(getEpoch()).toBeGreaterThan(0);
});
it('should return epoch for a date passed', function () {
let someDate = new Date('Feb 01 2019 10:20:30 GMT0000'),
someDateEpoch = 1549016430;
expect(getEpoch(new Date(someDate))).toEqual(someDateEpoch);
});
});
describe('getGCD', function () {
it('for two numbers', function () {
let nos = [5, 10];
expect(getGCD(nos)).toEqual(5);
});
it('for more than two numbers', function () {
let nos = [9, 24, 33];
expect(getGCD(nos)).toEqual(3);
});
});
describe('getMod', function () {
it('complete divisible', function () {
expect(getMod(5,5)).toEqual(0);
});
it('incomplete divisible less divisor', function () {
expect(getMod(7,5)).toEqual(2);
});
it('incomplete divisible greater divisor', function () {
expect(getMod(5,7)).toEqual(5);
});
it('negative number', function () {
expect(getMod(-7,5)).toEqual(3);
});
});
describe('quote_ident', function () {
it('normal string', function () {
expect(quote_ident('abcd')).toEqual('abcd');
});
it('contains certain characters string', function () {
expect(quote_ident('Abcd')).toEqual('"Abcd"');
expect(quote_ident('abc$d')).toEqual('"abc$d"');
expect(quote_ident('ab cd')).toEqual('"ab cd"');
});
it('starts with number', function () {
expect(quote_ident('1a')).toEqual('"1a"');
expect(quote_ident('a1')).toEqual('a1');
});
});
describe('parseFuncParams', function () {
let funcLabel = '',
expectedObj = {};
it('function with params', function () {
funcLabel = 'func1(a integer, b text)';
expectedObj = {
'func_name': 'func1',
'param_string': 'a integer, b text',
'params': [
['a', 'integer'],
['b', 'text'],
],
};
expect(parseFuncParams(funcLabel)).toEqual(expectedObj);
});
it('function without params', function () {
funcLabel = 'func1()';
expectedObj = {
'func_name': 'func1',
'param_string': '',
'params': [],
};
expect(parseFuncParams(funcLabel)).toEqual(expectedObj);
});
it('function name special chars', function () {
funcLabel = 'fun(c1(a integer, b text)';
expectedObj = {
'func_name': 'fun(c1',
'param_string': 'a integer, b text',
'params': [
['a', 'integer'],
['b', 'text'],
],
};
expect(parseFuncParams(funcLabel)).toEqual(expectedObj);
});
it('function params special chars', function () {
funcLabel = 'func1("a(b" integer, "a b" text)';
expectedObj = {
'func_name': 'func1',
'param_string': '"a(b" integer, "a b" text',
'params': [
['"a(b"', 'integer'],
['"a b"', 'text'],
],
};
expect(parseFuncParams(funcLabel)).toEqual(expectedObj);
});
it('function params with modes', function () {
funcLabel = 'func1(IN a integer, OUT b text)';
expectedObj = {
'func_name': 'func1',
'param_string': 'IN a integer, OUT b text',
'params': [
['a', 'integer'],
['b', 'text'],
],
};
expect(parseFuncParams(funcLabel)).toEqual(expectedObj);
});
});
describe('getRandomInt', function () {
it('is between', function () {
let id = getRandomInt(1, 9999999);
expect(1 <= id && id <= 9999999).toBeTruthy();
});
});
describe('sprintf', function () {
it('single replace', function () {
expect(
sprintf('This is normal %s for testing.', 'replace')
).toBe(
'This is normal replace for testing.'
);
});
it('multi replace', function () {
expect(
sprintf('This is multi %s for %s testing.', 'positions', 'replace')
).toBe(
'This is multi positions for replace testing.'
);
});
it('text, numbers, empty replace', function () {
expect(
sprintf('This is a number - %s, text - %s, and not repalce - %s.', 4321, 'replace')
).toBe(
'This is a number - 4321, text - replace, and not repalce - %s.'
);
});
});