2020-05-27 12:26:29 +03:00
import { QueryType } from './../integration/validators/validators' ;
2019-09-25 18:46:28 +03:00
import { ADMIN_SECRET_HEADER_KEY } from '../../src/constants' ;
2018-07-09 11:04:53 +03:00
export const baseUrl = Cypress . config ( 'baseUrl' ) ;
2020-05-27 12:26:29 +03:00
2021-01-07 12:04:22 +03:00
export const getIndexRoute = ( sourceName = 'default' , schemaName = 'public' ) = >
` /data/ ${ sourceName } /schema/ ${ schemaName } / ` ;
2021-01-13 07:21:49 +03:00
export const createVolatileFunction = ( name : string ) = > {
return {
type : 'run_sql' ,
args : {
sql : ` CREATE OR REPLACE FUNCTION public. ${ name } ()
RETURNS SETOF text_result
LANGUAGE sql
AS $function $
SELECT * FROM text_result ;
$function $ ` ,
cascade : false ,
} ,
} ;
} ;
2018-06-28 07:57:37 +03:00
export const dataTypes = [
'serial' ,
'bigserial' ,
'integer' ,
'bigint' ,
2018-07-18 15:46:04 +03:00
'uuid' ,
2018-06-28 07:57:37 +03:00
'text' ,
'numeric' ,
'date' ,
2019-05-22 14:23:30 +03:00
'timestamptz' ,
'timetz' ,
2018-06-28 07:57:37 +03:00
'boolean' ,
] ;
2020-05-27 12:26:29 +03:00
export const typeDefaults : { [ key : string ] : string } = {
2018-06-28 07:57:37 +03:00
integer : '5555' ,
bigint : '5555555555' ,
2018-07-18 15:46:04 +03:00
uuid : 'gen_random_uuid()' ,
2018-06-28 07:57:37 +03:00
text : 'test-text' ,
numeric : '0.55555' ,
date : 'now()' ,
2019-05-22 14:23:30 +03:00
timestamptz : 'now()' ,
timetz : 'now()' ,
2018-06-28 07:57:37 +03:00
boolean : 'false' ,
} ;
2020-05-27 12:26:29 +03:00
export const queryTypes : QueryType [ ] = [ 'insert' , 'select' , 'update' , 'delete' ] ;
export const getColName = ( i : number ) = > ` Apic_test_column_ ${ i } ` ;
export const getTableName = ( i : number , testName = '' ) = >
2019-05-25 10:42:21 +03:00
` Apic_test_table_ ${ testName } _ ${ i } ` ;
2020-05-27 12:26:29 +03:00
2021-01-07 12:04:22 +03:00
export const getElementFromAlias = ( alias : string ) = > ` [data-test=" ${ alias } "] ` ;
2020-05-27 12:26:29 +03:00
export const getElementFromClassName = ( cn : string ) = > ` . ${ cn } ` ;
export const tableColumnTypeSelector = ( alias : string ) = > {
2019-05-22 14:23:30 +03:00
cy . get ( ` ${ getElementFromAlias ( alias ) } ` )
. children ( 'div' )
. click ( )
. find ( 'input' )
. focus ( ) ;
} ;
2020-05-27 12:26:29 +03:00
2021-01-07 12:04:22 +03:00
export const makeDataAPIUrl = (
dataApiUrl : string ,
queryEndpoint : QueryEndpoint = 'query'
) = > {
if ( queryEndpoint === 'query' ) {
return ` ${ dataApiUrl } /v2/query ` ;
}
return ` ${ dataApiUrl } /v1/metadata ` ;
} ;
2020-05-27 12:26:29 +03:00
interface APIPayload {
[ key : string ] : any ;
}
2021-01-07 12:04:22 +03:00
export type QueryEndpoint = 'query' | 'metadata' ;
2020-05-27 12:26:29 +03:00
export const makeDataAPIOptions = (
dataApiUrl : string ,
key : string ,
2021-01-07 12:04:22 +03:00
body : APIPayload ,
queryType : QueryEndpoint = 'query'
2020-05-27 12:26:29 +03:00
) = > ( {
2018-06-28 07:57:37 +03:00
method : 'POST' ,
2021-01-07 12:04:22 +03:00
url : makeDataAPIUrl ( dataApiUrl , queryType ) ,
2018-06-28 07:57:37 +03:00
headers : {
2019-09-25 18:46:28 +03:00
[ ADMIN_SECRET_HEADER_KEY ] : key ,
2018-06-28 07:57:37 +03:00
} ,
body ,
failOnStatusCode : false ,
} ) ;
2019-01-25 06:31:54 +03:00
2020-05-27 12:26:29 +03:00
export const testCustomFunctionDefinition = (
i : string
) = > ` create function search_posts ${ ` _ ${ i } ` } (search text) returns setof post as $ $ select * from post where title ilike ('%' || search || '%') or content ilike ('%' || search || '%') $ $ language sql stable;
2019-01-25 06:31:54 +03:00
` ;
2020-05-27 12:26:29 +03:00
export const getCustomFunctionName = ( i : number ) = > ` search_posts ${ ` _ ${ i } ` } ` ;
2019-01-25 06:31:54 +03:00
2021-01-07 12:04:22 +03:00
export const getCreateTestFunctionQuery = ( i : number ) = > {
2019-01-25 06:31:54 +03:00
return {
2021-01-07 12:04:22 +03:00
type : 'run_sql' ,
args : {
sql : ` CREATE OR REPLACE FUNCTION public.search_posts_ ${ i } (search text) \ n RETURNS SETOF post \ n LANGUAGE sql \ n STABLE \ nAS $ function $ \ n select * \ n from post \ n where \ n title ilike ('%' || search || '%') or \ n content ilike ('%' || search || '%') \ n $ function $ \ n ` ,
cascade : false ,
} ,
} ;
} ;
export const getTrackTestFunctionQuery = ( i : number ) = > {
return {
type : 'pg_track_function' ,
args : {
function : ` search_posts_ ${ i } ` ,
schema : 'public' ,
source : 'default' ,
} ,
2019-01-25 06:31:54 +03:00
} ;
} ;
2020-06-11 15:38:32 +03:00
export const testCustomFunctionSQLWithSessArg = (
name = 'customFunctionWithSessionArg'
) = > {
return {
2021-01-07 12:04:22 +03:00
type : 'run_sql' ,
args : {
sql : ` CREATE OR REPLACE FUNCTION ${ name } (
2020-06-11 15:38:32 +03:00
hasura_session json , name text
) RETURNS SETOF text_result LANGUAGE sql STABLE AS $ $
SELECT
q . *
FROM
(
VALUES
( hasura_session - >> 'x-hasura-role' )
) q $ $ ` ,
2021-01-07 12:04:22 +03:00
cascade : false ,
} ,
2020-06-11 15:38:32 +03:00
} ;
} ;
2021-01-07 12:04:22 +03:00
2021-01-26 21:12:04 +03:00
export const createUntrackedFunctionSQL = (
fnName : string ,
tableName : string
) = > {
return {
type : 'run_sql' ,
args : {
sql : `
CREATE OR REPLACE FUNCTION $ { fnName } ( table_row "${tableName}" )
RETURNS int
LANGUAGE sql
STABLE
AS $function $
SELECT table_row . id
$function $
` ,
cascade : false ,
} ,
} ;
} ;
export const dropUntrackedFunctionSQL = ( fnName : string ) = > {
return {
type : 'run_sql' ,
args : {
sql : `
DROP FUNCTION public . $ { fnName } ;
` ,
cascade : false ,
} ,
} ;
} ;
2020-06-11 15:38:32 +03:00
export const getTrackFnPayload = ( name = 'customfunctionwithsessionarg' ) = > ( {
2021-01-07 12:04:22 +03:00
type : 'pg_track_function' ,
args : {
function : name ,
source : 'default' ,
schema : 'public' ,
} ,
} ) ;
// has to go to query
export const createFunctionTable = ( ) = > {
return {
type : 'run_sql' ,
args : {
sql : 'create table post (id serial PRIMARY KEY,title TEXT,content TEXT);' ,
cascade : false ,
} ,
} ;
} ;
// has to go to metadata
export const trackCreateFunctionTable = ( ) = > {
return {
type : 'pg_track_table' ,
args : {
table : {
name : 'post' ,
2020-06-11 15:38:32 +03:00
schema : 'public' ,
} ,
} ,
2021-01-07 12:04:22 +03:00
} ;
} ;
2021-01-13 07:21:49 +03:00
export const createSampleTable = ( ) = > {
2019-01-25 06:31:54 +03:00
return {
2021-01-07 12:04:22 +03:00
type : 'run_sql' ,
source : 'default' ,
args : {
sql : ` CREATE TABLE text_result(
result text
) ; ` ,
cascade : false ,
} ,
2019-01-25 06:31:54 +03:00
} ;
} ;
2021-01-07 12:04:22 +03:00
2021-01-13 07:21:49 +03:00
export const getTrackSampleTableQuery = ( ) = > {
2019-01-25 06:31:54 +03:00
return {
2021-01-07 12:04:22 +03:00
type : 'pg_track_table' ,
source : 'default' ,
args : {
table : {
name : 'text_result' ,
schema : 'public' ,
2020-06-11 15:38:32 +03:00
} ,
2021-01-07 12:04:22 +03:00
} ,
2020-06-11 15:38:32 +03:00
} ;
} ;
export const dropTable = ( table = 'post' , cascade = false ) = > {
return {
2021-02-11 19:09:58 +03:00
type : 'run_sql' ,
args : {
sql : ` DROP table "public"." ${ table } " ${ cascade ? ' CASCADE;' : ';' } ` ,
cascade ,
} ,
2019-01-25 06:31:54 +03:00
} ;
} ;
2021-02-11 19:09:58 +03:00
export const dropTableIfExists = (
table : { name : string ; schema : string } ,
source = 'default'
) = > ( {
type : 'run_sql' ,
source ,
args : {
sql : ` DROP TABLE IF EXISTS " ${ table . schema } "." ${ table . name } "; ` ,
cascade : false ,
} ,
} ) ;
2019-01-25 06:31:54 +03:00
export const getSchema = ( ) = > 'public' ;