Skip to content

Commit

Permalink
Updated tests for new functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
JuiceBawks committed Mar 9, 2022
1 parent ec6349c commit a24fb62
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions __tests__/frontend/lib/queries.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
window.require = ((str: string) => str) as any
import * as queries from '../../../frontend/lib/queries';
import type { QueryData } from '../../../frontend/types';
import type { QueryData, ExplainJson } from '../../../frontend/types';

const first: Partial<QueryData> = {
label: 'firstQuery',
db: 'firstDb',
group: 'group1',
sqlString: 'select * from tests',
executionPlan: {
Plan: {
'Node Type': 'Seq Scan',
'Relation Name': 'users',
Alias: 'users',
'Startup Cost': 0,
'Total Cost': 0,
'Plan Rows': 0,
'Plan Width': 0,
'Actual Startup Time': 0,
'Actual Total Time': 0,
'Actual Rows': 0,
'Actual Loops': 0,
},
'Planning Time': 1,
'Execution Time': 1,
},
};

const second: Partial<QueryData> = {
Expand All @@ -15,15 +34,16 @@ const second: Partial<QueryData> = {

describe('key generation', () => {
it('should create key from label and db given as params', () => {
expect(queries.keyFromData('LABEL', 'DB')).toEqual('label:LABEL db:DB');
expect(queries.keyFromData('LABEL', 'DB', 'GROUP')).toEqual('label:LABEL db:DB group:GROUP');
});

it('should create key from query object', () => {
const query = {
label: 'query1',
db: 'db1',
group: 'group1'
};
expect(queries.key(query as QueryData)).toEqual('label:query1 db:db1');
expect(queries.key(query as QueryData)).toEqual('label:query1 db:db1 group:group1');
});
});

Expand Down Expand Up @@ -137,23 +157,28 @@ describe('setCompare', () => {

it('should not mutate original collection', () => {
expect(Object.keys(collection).length).toBe(0);
const newCollection = queries.setCompare({}, first as QueryData, true);
const newCollection = queries.setCompare({}, {}, first as QueryData, true);
expect(Object.keys(collection).length).toBe(0);
expect(newCollection).not.toBe(collection);
});

it('should add query to new collection if given true for isCompared', () => {
expect(Object.keys(collection).length).toBe(0);
const newCollection = queries.setCompare({}, first as QueryData, true);
const newCollection = queries.setCompare({}, {}, first as QueryData, true);
expect(Object.keys(newCollection).length).toBe(1);
expect(newCollection[queries.key(first as QueryData)]).toEqual(first);
});

it('should remove query from new collection if given false for isCompared', () => {
it('should set execution time to 0 if given false for isCompared', () => {
let qs:any = { [`${queries.key(first as QueryData)}`]: first };
expect(Object.keys(collection).length).toBe(0);
const newCollection = queries.setCompare({}, first as QueryData, true);
const newCollection = queries.setCompare({}, qs, first as QueryData, true);
expect(Object.keys(newCollection).length).toBe(1);
const clearedCollection = queries.setCompare({}, first as QueryData, false);
expect(Object.keys(clearedCollection).length).toBe(0);
expect(newCollection[queries.key(first as QueryData)].executionPlan['Planning Time']).toBe(1);
expect(newCollection[queries.key(first as QueryData)].executionPlan['Execution Time']).toBe(1);
const newSetCollection = queries.setCompare(newCollection, qs, first as QueryData, false);
expect(Object.keys(newSetCollection).length).toBe(1);
expect(newSetCollection[queries.key(first as QueryData)].executionPlan['Planning Time']).toBe(0);
expect(newSetCollection[queries.key(first as QueryData)].executionPlan['Execution Time']).toBe(0);
});
});

0 comments on commit a24fb62

Please sign in to comment.