import { DirtyCheckContext , Persister } from "../src/persister" ; import { ResultCallback } from "../src/core/callback" ; import { MappingModel } from "../src/mapping/mappingModel" ; import { Batch } from "../src/batch" ; import { ChangeTrackingType } from "../src/mapping/mappingModel" ; import { IdentityGenerator } from "../src/config/configuration" ; import { EntityMapping } from "../src/mapping/entityMapping" ; import { Callback } from "../src/core/callback" ; import { QueryDefinition } from "../src/query/queryDefinition" ; import { Observer } from "../src/observer" ; import { Property } from "../src/mapping/property" ; import { InternalSession } from "../src/session" ; export class MockPersister implements Persister { private _mapping : EntityMapping ; private _session : InternalSession ; changeTracking : ChangeTrackingType ; identity : IdentityGenerator ; insertCalled = 0 ; inserted : any [ ] = [ ] ; insertError : Error ; dirtyCheckCalled = 0 ; dirtyChecked : any [ ] = [ ] ; removeCalled = 0 ; removed : any [ ] = [ ] ; executeQueryCalled = 0 ; constructor ( session , mapping ) { this . _mapping = mapping ; this . _session = session ; this . changeTracking = ( < EntityMapping > mapping . inheritanceRoot ) . changeTracking ; this . identity = ( < EntityMapping > mapping . inheritanceRoot ) . identity ; } areDocumentsEqual ( context , entity , originalDocument ) { if ( this . onAreDocumentsEqual ) { return this . onAreDocumentsEqual ( context , entity , originalDocument ) ; } return true ; } onAreDocumentsEqual : ( context : DirtyCheckContext , entity : Object , originalDocument : Object ) => boolean ; dirtyCheck ( batch , entity , originalDocument ) { this . dirtyCheckCalled ++ ; if ( ! this . wasDirtyChecked ( entity ) ) { this . dirtyChecked . push ( entity ) ; } return originalDocument ; } wasDirtyChecked ( entity ) { return this . dirtyChecked . indexOf ( entity ) !== - 1 ; } addInsert ( batch , entity ) { if ( this . insertError ) { batch . error = this . insertError ; return null ; } this . insertCalled ++ ; if ( ! this . wasInserted ( entity ) ) { this . inserted . push ( entity ) ; } return { } ; } wasInserted ( entity ) { return this . inserted . indexOf ( entity ) !== - 1 ; } addRemove ( batch , entity ) { this . removeCalled ++ ; if ( ! this . wasRemoved ( entity ) ) { this . removed . push ( entity ) ; } } wasRemoved ( entity ) { return this . removed . indexOf ( entity ) !== - 1 ; } postUpdate ( entity , callback ) { callback ( ) ; } postInsert ( entity , callback ) { callback ( ) ; } postRemove ( entity , callback ) { callback ( ) ; } refresh ( entity , callback ) { if ( this . onRefresh ) { process . nextTick ( ( ) => this . onRefresh ( entity , callback ) ) ; } } onRefresh : ( entity : any , callback : Callback ) => void ; watch ( value , observer ) { this . _mapping . watchEntity ( value , observer ) ; } fetch ( entity , path , callback ) { if ( this . onFetch ) { process . nextTick ( ( ) => this . onFetch ( entity , path , callback ) ) ; } else { this . _mapping . fetch ( this . _session , undefined , entity , path . split ( "." ) , 0 , callback ) ; } } onFetch : ( entity : any , path : string , callback : Callback ) => void ; fetchPropertyValue ( entity , property , callback ) { if ( this . onFetchPropertyValue ) { process . nextTick ( ( ) => this . onFetchPropertyValue ( entity , property , callback ) ) ; } } onFetchPropertyValue : ( entity : any , property : Property , callback : ResultCallback < any > ) => void ; findAll ( criteria , callback ? ) { } findOneById ( id , callback ) { if ( this . onFindOneById ) { process . nextTick ( ( ) => this . onFindOneById ( id , callback ) ) ; } } onFindOneById : ( id : any , callback : ResultCallback < any > ) => void ; findOne ( criteria , callback ) { } findInverseOf ( entity , path , callback ) { } findOneInverseOf ( entity , path , callback ) { } walk ( entity , flags , entities , embedded , callback ) { if ( entities . indexOf ( entity ) == - 1 ) { entities . push ( entity ) ; } process . nextTick ( ( ) => callback ( null ) ) ; } executeQuery ( query , callback ) { this . executeQueryCalled ++ ; if ( this . onExecuteQuery ) { process . nextTick ( ( ) => this . onExecuteQuery ( query , callback ) ) ; } else { process . nextTick ( ( ) => callback ( null ) ) ; } } onExecuteQuery : ( query : QueryDefinition , callback : ResultCallback < any > ) => void ; }