2019-01-15 18:53:27 -05:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2020-05-07 17:18:10 -04:00
|
|
|
import sqlLabReducer from 'src/SqlLab/reducers/sqlLab';
|
|
|
|
import * as actions from 'src/SqlLab/actions/sqlLab';
|
|
|
|
import { now } from 'src/modules/dates';
|
|
|
|
|
2018-11-07 02:12:32 -05:00
|
|
|
import { table, initialState as mockState } from '../fixtures';
|
2018-07-12 14:50:25 -04:00
|
|
|
|
|
|
|
const initialState = mockState.sqlLab;
|
2016-10-18 19:02:29 -04:00
|
|
|
|
|
|
|
describe('sqlLabReducer', () => {
|
2016-10-25 19:44:32 -04:00
|
|
|
describe('Query editors actions', () => {
|
|
|
|
let newState;
|
|
|
|
let defaultQueryEditor;
|
|
|
|
let qe;
|
|
|
|
beforeEach(() => {
|
2018-07-12 14:50:25 -04:00
|
|
|
newState = { ...initialState };
|
2016-10-25 19:44:32 -04:00
|
|
|
defaultQueryEditor = newState.queryEditors[0];
|
2019-11-14 12:44:57 -05:00
|
|
|
const action = {
|
|
|
|
type: actions.ADD_QUERY_EDITOR,
|
|
|
|
queryEditor: { ...initialState.queryEditors[0], id: 'abcd' },
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, action);
|
|
|
|
qe = newState.queryEditors.find(e => e.id === 'abcd');
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should add a query editor', () => {
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.queryEditors).toHaveLength(2);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should remove a query editor', () => {
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.queryEditors).toHaveLength(2);
|
2019-11-14 12:44:57 -05:00
|
|
|
const action = {
|
|
|
|
type: actions.REMOVE_QUERY_EDITOR,
|
|
|
|
queryEditor: qe,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, action);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.queryEditors).toHaveLength(1);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should set q query editor active', () => {
|
2019-11-14 12:44:57 -05:00
|
|
|
const addQueryEditorAction = {
|
|
|
|
type: actions.ADD_QUERY_EDITOR,
|
|
|
|
queryEditor: { ...initialState.queryEditors[0], id: 'abcd' },
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, addQueryEditorAction);
|
|
|
|
const setActiveQueryEditorAction = {
|
|
|
|
type: actions.SET_ACTIVE_QUERY_EDITOR,
|
|
|
|
queryEditor: defaultQueryEditor,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, setActiveQueryEditorAction);
|
2019-11-27 17:02:48 -05:00
|
|
|
expect(newState.tabHistory[newState.tabHistory.length - 1]).toBe(
|
|
|
|
defaultQueryEditor.id,
|
|
|
|
);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should not fail while setting DB', () => {
|
|
|
|
const dbId = 9;
|
2019-11-14 12:44:57 -05:00
|
|
|
const action = {
|
|
|
|
type: actions.QUERY_EDITOR_SETDB,
|
|
|
|
queryEditor: qe,
|
|
|
|
dbId,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, action);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.queryEditors[1].dbId).toBe(dbId);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should not fail while setting schema', () => {
|
|
|
|
const schema = 'foo';
|
2019-11-14 12:44:57 -05:00
|
|
|
const action = {
|
|
|
|
type: actions.QUERY_EDITOR_SET_SCHEMA,
|
|
|
|
queryEditor: qe,
|
|
|
|
schema,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, action);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.queryEditors[1].schema).toBe(schema);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
2021-01-10 20:26:35 -05:00
|
|
|
it('should not fail while setting autorun', () => {
|
2019-11-14 12:44:57 -05:00
|
|
|
const action = {
|
|
|
|
type: actions.QUERY_EDITOR_SET_AUTORUN,
|
|
|
|
queryEditor: qe,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, { ...action, autorun: false });
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.queryEditors[1].autorun).toBe(false);
|
2019-11-14 12:44:57 -05:00
|
|
|
newState = sqlLabReducer(newState, { ...action, autorun: true });
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.queryEditors[1].autorun).toBe(true);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should not fail while setting title', () => {
|
|
|
|
const title = 'a new title';
|
2019-11-14 12:44:57 -05:00
|
|
|
const action = {
|
|
|
|
type: actions.QUERY_EDITOR_SET_TITLE,
|
|
|
|
queryEditor: qe,
|
|
|
|
title,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, action);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.queryEditors[1].title).toBe(title);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should not fail while setting Sql', () => {
|
|
|
|
const sql = 'SELECT nothing from dev_null';
|
2019-11-14 12:44:57 -05:00
|
|
|
const action = {
|
|
|
|
type: actions.QUERY_EDITOR_SET_SQL,
|
|
|
|
queryEditor: qe,
|
|
|
|
sql,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, action);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.queryEditors[1].sql).toBe(sql);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
2018-11-07 18:57:44 -05:00
|
|
|
it('should not fail while setting queryLimit', () => {
|
|
|
|
const queryLimit = 101;
|
2019-11-14 12:44:57 -05:00
|
|
|
const action = {
|
|
|
|
type: actions.QUERY_EDITOR_SET_QUERY_LIMIT,
|
|
|
|
queryEditor: qe,
|
|
|
|
queryLimit,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, action);
|
2018-11-07 18:57:44 -05:00
|
|
|
expect(newState.queryEditors[1].queryLimit).toEqual(queryLimit);
|
|
|
|
});
|
2016-11-01 00:07:46 -04:00
|
|
|
it('should set selectedText', () => {
|
|
|
|
const selectedText = 'TEST';
|
2019-11-14 12:44:57 -05:00
|
|
|
const action = {
|
|
|
|
type: actions.QUERY_EDITOR_SET_SELECTED_TEXT,
|
|
|
|
queryEditor: newState.queryEditors[0],
|
|
|
|
sql: selectedText,
|
|
|
|
};
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.queryEditors[0].selectedText).toBeNull();
|
2019-11-14 12:44:57 -05:00
|
|
|
newState = sqlLabReducer(newState, action);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.queryEditors[0].selectedText).toBe(selectedText);
|
2016-11-01 00:07:46 -04:00
|
|
|
});
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
describe('Tables', () => {
|
|
|
|
let newState;
|
|
|
|
let newTable;
|
|
|
|
beforeEach(() => {
|
2020-04-03 20:05:16 -04:00
|
|
|
newTable = { ...table };
|
2019-11-14 12:44:57 -05:00
|
|
|
const action = {
|
|
|
|
type: actions.MERGE_TABLE,
|
|
|
|
table: newTable,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(initialState, action);
|
2016-10-25 19:44:32 -04:00
|
|
|
newTable = newState.tables[0];
|
|
|
|
});
|
|
|
|
it('should add a table', () => {
|
|
|
|
// Testing that beforeEach actually added the table
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.tables).toHaveLength(1);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should merge the table attributes', () => {
|
|
|
|
// Merging the extra attribute
|
|
|
|
newTable.extra = true;
|
2019-11-14 12:44:57 -05:00
|
|
|
const action = {
|
|
|
|
type: actions.MERGE_TABLE,
|
|
|
|
table: newTable,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, action);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.tables).toHaveLength(1);
|
|
|
|
expect(newState.tables[0].extra).toBe(true);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should expand and collapse a table', () => {
|
2019-11-14 12:44:57 -05:00
|
|
|
const collapseTableAction = {
|
|
|
|
type: actions.COLLAPSE_TABLE,
|
|
|
|
table: newTable,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, collapseTableAction);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.tables[0].expanded).toBe(false);
|
2019-11-14 12:44:57 -05:00
|
|
|
const expandTableAction = {
|
|
|
|
type: actions.EXPAND_TABLE,
|
|
|
|
table: newTable,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, expandTableAction);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.tables[0].expanded).toBe(true);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should remove a table', () => {
|
2019-11-14 12:44:57 -05:00
|
|
|
const action = {
|
|
|
|
type: actions.REMOVE_TABLE,
|
|
|
|
table: newTable,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, action);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(newState.tables).toHaveLength(0);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('Run Query', () => {
|
|
|
|
let newState;
|
|
|
|
let query;
|
|
|
|
beforeEach(() => {
|
2018-07-12 14:50:25 -04:00
|
|
|
newState = { ...initialState };
|
2019-11-14 12:44:57 -05:00
|
|
|
query = {
|
|
|
|
id: 'abcd',
|
|
|
|
progress: 0,
|
|
|
|
startDttm: now(),
|
|
|
|
state: 'running',
|
|
|
|
cached: false,
|
|
|
|
sqlEditorId: 'dfsadfs',
|
|
|
|
};
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should start a query', () => {
|
2019-11-14 12:44:57 -05:00
|
|
|
const action = {
|
|
|
|
type: actions.START_QUERY,
|
|
|
|
query: {
|
|
|
|
id: 'abcd',
|
|
|
|
progress: 0,
|
|
|
|
startDttm: now(),
|
|
|
|
state: 'running',
|
|
|
|
cached: false,
|
|
|
|
sqlEditorId: 'dfsadfs',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, action);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(Object.keys(newState.queries)).toHaveLength(1);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should stop the query', () => {
|
2019-11-14 12:44:57 -05:00
|
|
|
const startQueryAction = {
|
|
|
|
type: actions.START_QUERY,
|
|
|
|
query,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, startQueryAction);
|
|
|
|
const stopQueryAction = {
|
|
|
|
type: actions.STOP_QUERY,
|
|
|
|
query,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, stopQueryAction);
|
2016-10-25 19:44:32 -04:00
|
|
|
const q = newState.queries[Object.keys(newState.queries)[0]];
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(q.state).toBe('stopped');
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should remove a query', () => {
|
2019-11-14 12:44:57 -05:00
|
|
|
const startQueryAction = {
|
|
|
|
type: actions.START_QUERY,
|
|
|
|
query,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, startQueryAction);
|
|
|
|
const removeQueryAction = {
|
|
|
|
type: actions.REMOVE_QUERY,
|
|
|
|
query,
|
|
|
|
};
|
|
|
|
newState = sqlLabReducer(newState, removeQueryAction);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(Object.keys(newState.queries)).toHaveLength(0);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('should refresh queries when polling returns empty', () => {
|
2018-11-07 02:12:32 -05:00
|
|
|
newState = sqlLabReducer(newState, actions.refreshQueries({}));
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
});
|
2016-10-18 19:02:29 -04:00
|
|
|
});
|