/**
 * 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.
 */ import * as shortid from 'shortid' ; import { selectResultsTab , assertSQLLabResultsAreEqual } from './sqllab.helper' ; function parseClockStr ( node ) { return Number . parseFloat ( node . text ( ) . replace ( /:/g , '' ) ) ; } describe ( 'SqlLab query panel' , ( ) => { beforeEach ( ( ) => { cy . login ( ) ; cy . visit ( '/superset/sqllab' ) ; } ) ; it . skip ( 'supports entering and running a query' , ( ) => { let clockTime = 0 ; const sampleResponse = { status : 'success' , data : [ { '?column?' : 1 } ] , columns : [ { name : '?column?' , type : 'INT' , is_date : false } ] , selected_columns : [ { name : '?column?' , type : 'INT' , is_date : false } ] , expanded_columns : [ ] , } ; cy . intercept ( { method : 'POST' , url : '/superset/sql_json/' , delay : 1000 , response : ( ) => sampleResponse , } ) . as ( 'mockSQLResponse' ) ; cy . get ( '.TableSelector .Select:eq(0)' ) . click ( ) ; cy . get ( '.TableSelector .Select:eq(0) input[type=text]' ) . focus ( ) . type ( '{enter}' ) ; cy . get ( '#brace-editor textarea' ) . focus ( ) . clear ( ) . type ( `{selectall}{backspace}SELECT 1` ) ; cy . get ( '#js-sql-toolbar button:eq(0)' ) . eq ( 0 ) . click ( ) ; cy . wait ( 300 ) ; cy . get ( '.sql-toolbar .label-success' ) . then ( node => { clockTime = parseClockStr ( node ) ; expect ( clockTime ) . greaterThan ( 0.2 ) ; } ) ; cy . wait ( '@mockSQLResponse' ) ; cy . get ( '.sql-toolbar .label-success' ) . then ( node => { const newClockTime = parseClockStr ( node ) ; expect ( newClockTime ) . greaterThan ( 0.9 ) ; clockTime = newClockTime ; } ) ; cy . get ( '#js-sql-toolbar button:eq(0)' ) . eq ( 0 ) . click ( ) ; cy . get ( '.sql-toolbar .label-success' ) . contains ( '00:00:00' ) ; cy . wait ( '@mockSQLResponse' ) ; cy . get ( '.sql-toolbar .label-success' ) . then ( node => { expect ( parseClockStr ( node ) ) . greaterThan ( 0.9 ) ; } ) ; } ) ; it . skip ( 'successfully saves a query' , ( ) => { cy . intercept ( 'superset/tables/**' ) . as ( 'getTables' ) ; cy . intercept ( 'savedqueryviewapi/**' ) . as ( 'getSavedQuery' ) ; const query = 'SELECT ds, gender, name, num FROM main.birth_names ORDER BY name LIMIT 3' ; const savedQueryTitle = `CYPRESS TEST QUERY ${ shortid . generate ( ) }` ; let initialResultsTable = null ; let savedQueryResultsTable = null ; cy . get ( '#brace-editor textarea' ) . clear ( { force : true } ) . type ( `{selectall}{backspace}${ query }` , { force : true } ) . focus ( ) . blur ( ) ; cy . get ( '#brace-editor textarea' ) . type ( '{ctrl}r' , { force : true } ) ; cy . wait ( '@sqlLabQuery' ) ; selectResultsTab ( ) . then ( resultsA => { initialResultsTable = resultsA [ 0 ] ; } ) ; cy . get ( '#js-sql-toolbar button' ) . eq ( 1 ) . click ( ) ; cy . get ( '.modal-sm input' ) . clear ( { force : true } ) . type ( `{selectall}{backspace}${ savedQueryTitle }` , { force : true , } ) ; cy . get ( '.modal-sm .modal-body button' ) . eq ( 0 ) . click ( ) ; cy . visit ( '/sqllab/my_queries/' ) ; cy . get ( 'table tr:first-child a[href*="savedQueryId"' ) . click ( ) ; cy . wait ( [ '@getSavedQuery' , '@getTables' ] ) ; cy . get ( '#js-sql-toolbar button' ) . eq ( 0 ) . click ( ) ; cy . wait ( '@sqlLabQuery' ) ; selectResultsTab ( ) . then ( resultsB => { savedQueryResultsTable = resultsB [ 0 ] ; assertSQLLabResultsAreEqual ( initialResultsTable , savedQueryResultsTable ) ; } ) ; } ) ; } ) ;