import { Given , Then } from "@cucumber/cucumber" ; import DataTable from "@cucumber/cucumber/lib/models/data_table" ; import { TransactionType , TypeDBSession , TypeDBTransaction } from "../../../../dist" ; import { assertThrows , assertThrowsWithMessage } from "../../util/Util" ; import { optionSetters , sessions , sessionsToTransactions , transactionOptions } from "../ConnectionStepsBase" ; import assert = require ( "assert" ) ; async function forEachSessionOpenTransactionsOfType ( transactionTypes ) { for ( const session of sessions ) { const transactions = [ ] ; for ( const transactionType of transactionTypes ) { const transaction = await session . transaction ( transactionType , transactionOptions ) ; transactions . push ( transaction ) ; } sessionsToTransactions . set ( session , transactions ) ; } } Then ( '(for each )session(,) open(s) transaction(s) of type: {transaction_type}' , async function ( transactionType ) { await forEachSessionOpenTransactionsOfType ( [ transactionType ] ) ; } ) ; Then ( '(for each )session(,) open(s) transaction(s) of type:' , async function ( transactionTypeTable ) { const transactionTypes = dataTableToTransactionTypes ( transactionTypeTable ) ; await forEachSessionOpenTransactionsOfType ( transactionTypes ) ; } ) ; Then ( '(for each )session(,) open transaction(s) of type; throws exception: {transaction_type}' , async function ( transactionType ) { for ( const session of sessions ) { await assertThrows ( async ( ) => await session . transaction ( transactionType ) ) ; } } ) ; Then ( '(for each )session(,) open transaction(s) of type; throws exception' , async function ( transactionTypeTable ) { const typeArray = dataTableToTransactionTypes ( transactionTypeTable ) ; for ( const session of sessions ) { if ( ! sessionsToTransactions . has ( session ) ) sessionsToTransactions . set ( session , [ ] ) ; for ( const transactionType of typeArray ) { await assertThrows ( async ( ) => await session . transaction ( transactionType ) ) ; } } } ) ; Then ( '(for each )session(,) transaction(s)( in parallel) is/are null: {bool}' , function ( isNull ) { for ( const session of sessions ) assert . ok ( sessionsToTransactions . has ( session ) !== isNull ) ; } ) ; Then ( '(for each )session(,) transaction(s)( in parallel) is/are open: {bool}' , function ( isOpen ) { for ( const session of sessions ) { assert . ok ( sessionsToTransactions . has ( session ) ) ; for ( const transaction of sessionsToTransactions . get ( session ) ) { assert . ok ( transaction . isOpen ( ) === isOpen ) ; } } } ) ; Then ( 'transaction commits' , async function ( ) { await sessionsToTransactions . get ( sessions [ 0 ] ) [ 0 ] . commit ( ) ; } ) ; Then ( 'transaction commits; throws exception' , async function ( ) { await assertThrows ( async ( ) => await sessionsToTransactions . get ( sessions [ 0 ] ) [ 0 ] . commit ( ) ) ; } ) ; Then ( 'transaction commits; throws exception containing {string}' , async function ( error ) { await assertThrowsWithMessage ( async ( ) => await sessionsToTransactions . get ( sessions [ 0 ] ) [ 0 ] . commit ( ) , error ) ; } ) ; Then ( '(for each )session(,) transaction(s) commit(s)' , async function ( ) { for ( const session of sessions ) { for ( const transaction of sessionsToTransactions . get ( session ) ) { await transaction . commit ( ) ; } } } ) ; Then ( '(for each )session(,) transaction(s) commit(s); throws exception' , async function ( ) { for ( const session of sessions ) { for ( const transaction of sessionsToTransactions . get ( session ) ) { await assertThrows ( async ( ) => await transaction . commit ( ) ) ; } } } ) ; Then ( '(for each )session(,) transaction(s) close(s)' , async function ( ) { for ( const session of sessions ) { for ( const transaction of sessionsToTransactions . get ( session ) ) { await transaction . close ( ) ; } } } ) ; Then ( '(for each )session(,) transaction(s)( in parallel) has/have type: {transaction_type}' , function ( type ) { for ( const session of sessions ) { for ( const transaction of sessionsToTransactions . get ( session ) ) { assert ( transaction . type === type ) ; } } } ) ; Then ( '(for each )session(,) transaction(s)( in parallel) has/have type(s):' , function ( transactionTypeTable ) { const typeArray = dataTableToTransactionTypes ( transactionTypeTable ) ; for ( const session of sessions ) { const transactionArray = sessionsToTransactions . get ( session ) ; for ( let i = 0 ; i < transactionArray . length ; i ++ ) { assert ( transactionArray [ i ] . type === typeArray [ i ] ) ; } } } ) ; Then ( '(for each )session(,) open transaction(s) in parallel of type:' , async function ( transactionTypeTable ) { const typeArray = dataTableToTransactionTypes ( transactionTypeTable ) ; const openings = [ ] ; const sessionList = [ ] ; for ( const type of typeArray ) { for ( const session of sessions ) { openings . push ( session . transaction ( type ) ) ; sessionList . push ( session ) ; } } const newTransactions = await Promise . all ( openings ) ; for ( let i = 0 ; i < newTransactions . length ; i ++ ) { if ( ! sessionsToTransactions . has ( sessionList [ i ] ) ) sessionsToTransactions . set ( sessionList [ i ] , [ ] ) ; sessionsToTransactions . get ( sessionList [ i ] ) . push ( newTransactions [ i ] ) ; } } ) ; Given ( 'set transaction option {word} to: {word}' , async function ( option , value ) { if ( ! ( option in optionSetters ) ) { throw ( "Unrecognised option: " + option ) ; } else { optionSetters [ option ] ( transactionOptions , value ) ; } } ) ; function dataTableToTransactionTypes ( transactionTypeTable ) { const typeArray = [ ] ; for ( const transactionTypeRow of transactionTypeTable . raw ( ) ) { switch ( transactionTypeRow [ 0 ] ) { case "write" : typeArray . push ( TransactionType . WRITE ) ; break ; case "read" : typeArray . push ( TransactionType . READ ) ; break ; default : throw "Behaviour asked for unrecognised Transaction Type. This is a problem with the feature file, not the client or server." ; } } return typeArray ; }