import { EventEmitter } from "events" ; import { InternalSession } from "../src/session" ; import { InternalSessionFactory } from "../src/sessionFactory" ; import { MockQueryObject , MockQueryBuilder } from "./query/mockQueryBuilder" ; import { ResultCallback , Callback } from "../src/core/callback" ; import { generateId } from "./helpers" ; import { Constructor } from "../src/index" ; import { FindOneQuery , QueryBuilder } from "../src/query/queryBuilder" ; import { Persister } from "../src/persister" ; import { EntityMapping } from "../src/mapping/entityMapping" ; import { QueryDefinition } from "../src/query/queryDefinition" ; export class MockInternalSession extends EventEmitter implements InternalSession { factory : InternalSessionFactory ; executeQueryCalled = 0 ; onExecuteQuery : ( query : MockQueryObject , callback : ResultCallback < any > ) => void ; saved : any [ ] = [ ] ; fetches : FetchDescription [ ] = [ ] ; private _collection : any [ ] = [ ] ; constructor ( factory ) { super ( ) ; this . factory = factory ; } add ( item ) { if ( ! this . contains ( item ) ) { this . _collection . push ( item ) ; } } save ( obj , callback ? ) { this . saved . push ( obj ) ; if ( ! ( < any > obj ) . _id ) { ( < any > obj ) . _id = generateId ( ) ; } this . add ( obj ) ; if ( callback ) { process . nextTick ( callback ) ; } } remove ( obj , callback ? ) { if ( callback ) { process . nextTick ( callback ) ; } } detach ( obj , callback ? ) { if ( callback ) { process . nextTick ( callback ) ; } } refresh ( obj , callback ) { process . nextTick ( callback ) ; } flush ( callback ? ) { if ( callback ) { process . nextTick ( callback ) ; } } clear ( callback ? ) { if ( callback ) { process . nextTick ( callback ) ; } } find < T > ( ctr , id , callback ? ) { if ( id ) { var entity = this . _find ( id ) ; if ( entity ) { process . nextTick ( ( ) => { callback ( null , entity ) ; } ) ; return ; } } return this . query ( ctr ) . findOneById ( id , callback ) ; } getReference < T > ( ctr , id ) { var entity = this . _find ( id ) ; if ( entity ) { return entity ; } throw new Error ( "Not implemented" ) ; } fetch < T > ( obj , pathsOrCallback , callback ? ) { var paths ; if ( typeof pathsOrCallback === "function" ) { callback = pathsOrCallback ; } else if ( typeof pathsOrCallback === "string" ) { paths = [ pathsOrCallback ] ; } else { paths = pathsOrCallback ; } this . fetches . push ( { obj , paths } ) ; process . nextTick ( ( ) => callback ( null , obj ) ) ; } query < T > ( ctr ) { return new MockQueryBuilder < T > ( this , ctr ) ; } wait ( callback ? ) { if ( callback ) { process . nextTick ( callback ) ; } } close ( callback ? ) { if ( callback ) { process . nextTick ( callback ) ; } } contains ( obj ) { for ( let i = 0 ; i < this . _collection . length ; i ++ ) { if ( this . _collection [ i ] == obj ) { return true ; } } return false ; } exists < T > ( ctr , id , callback ) { for ( let i = 0 ; i < this . _collection . length ; i ++ ) { if ( this . _collection [ i ] . id == id ) { process . nextTick ( ( ) => callback ( null , true ) ) ; return ; } } process . nextTick ( ( ) => callback ( null , false ) ) ; } executeQuery ( query , callback ) { this . executeQueryCalled ++ ; if ( this . onExecuteQuery ) { process . nextTick ( ( ) => this . onExecuteQuery ( < any > query , callback ) ) ; } else { process . nextTick ( ( ) => callback ( null ) ) ; } } toDocument ( obj , callback ? ) { return undefined ; } isDirty ( obj , callback ? ) { if ( callback ) { callback ( null , false ) ; } return false ; } getVersion ( obj ) { return undefined ; } getObject ( id ) { return undefined ; } registerManaged ( persister , entity , document ) { } notifyRemoved ( entity ) { } getPersister ( mapping ) { return undefined ; } getReferenceInternal ( mapping , id ) { return undefined ; } fetchInternal ( entity , paths , callback ) { this . fetch ( entity , paths , callback ) ; } findFetchedPaths ( obj ) { for ( var i = 0 ; i < this . fetches . length ; i ++ ) { var fetch = this . fetches [ i ] ; if ( fetch . obj == obj ) { return fetch . paths ; } } return null ; } getEntities ( ) { return [ ] ; } private _find ( id ) { for ( let i = 0 ; i < this . _collection . length ; i ++ ) { let item = this . _collection [ i ] ; if ( item . id === id ) { return item ; } } return null ; } } export interface FetchDescription { obj : any ; paths : string [ ] ; }