import * as mongodb from "mongodb" ; import { InternalSession , Session } from "../src/session" ; import { Configuration } from "../src/config/configuration" ; import { AnnotationMappingProvider } from "../src/mapping/providers/annotationMappingProvider" ; import { Cat } from "../tests/fixtures/cat" ; import { SessionFactory , SessionFactoryImpl } from "../src/sessionFactory" ; import { DirtyCheckContext , Persister } from "../src/persister" ; import { IdentityGenerator } from "../src/config/configuration" ; import { Batch } from "../src/batch" ; import { MappingModel , ChangeTrackingType } from "../src/mapping/mappingModel" ; import { MappingRegistry } from "../src/mapping/mappingRegistry" ; import { ResultCallback , Callback } from "../src/core/callback" ; import { EntityMapping } from "../src/mapping/entityMapping" ; import { QueryDefinition } from "../src/query/queryDefinition" ; import { Observer } from "../src/observer" ; import { MockDb } from "../tests/driver/mockDb" ; import { Property } from "../src/mapping/property" ; import { MockMongoClient } from "../tests/driver/mockMongoClient" ; suite ( "SessionImpl" , ( ) => { var session ; var count = 0 ; var cats = [ ] ; var i = 0 , saved = 0 ; before ( ( done ) => { var config = new Configuration ( ) ; config . createIndexes = true ; config . addMapping ( new AnnotationMappingProvider ( Cat ) ) ; mongodb . MongoClient . connect ( "mongodb://localhost:27017" , ( err , connection ) => { if ( err ) return done ( err ) ; config . createSessionFactory ( connection , "test" , ( err , sessionFactory ) => { if ( err ) return done ( err ) ; session = sessionFactory . createSession ( ) ; session . query ( Cat ) . removeAll ( { } , done ) ; } ) ; } ) ; for ( var i = 0 ; i < 1000000 ; i ++ ) { cats . push ( new Cat ( "cat" + count ++ ) ) ; } } ) ; after ( ( done ) => { for ( var j = 0 ; j < saved ; j ++ ) { session . remove ( cats [ j ] ) ; } session . flush ( done ) ; } ) ; beforeEach ( ( ) => { i = 0 ; } ) ; test ( "save x 1000" , ( done ) => { for ( var j = 0 ; j < 1000 ; j ++ ) { if ( ! cats [ i ] ) return done ( new Error ( "ran out of cats at " + i ) ) ; session . save ( cats [ i ++ ] ) ; saved = i ; } session . flush ( done ) ; } ) ; test ( "findOne" , ( done ) => { session . query ( Cat ) . findOne ( { name : 'cat' + ( i ++ ) } , done ) ; } ) ; test ( "findAll" , ( done ) => { session . query ( Cat ) . findAll ( { name : /^cat/ } , done ) ; } ) ; test ( "edit x 1000" , ( done ) => { for ( var j = 0 ; j < 1000 ; j ++ ) { if ( ! cats [ i ] ) return done ( new Error ( "ran out of cats at " + i ) ) ; cats [ i ] . name = 'kitty' + ( i ++ ) ; } session . flush ( done ) ; } ) ; } ) ; class DummySessionFactory extends SessionFactoryImpl { constructor ( mappingRegistry ) { super ( new MockMongoClient ( ) , null , mappingRegistry ) ; } createPersister ( session , mapping ) { return new DummyPersister ( mapping ) ; } } class DummyPersister implements Persister { changeTracking : ChangeTrackingType ; identity : IdentityGenerator ; constructor ( mapping ) { this . changeTracking = ( < EntityMapping > mapping . inheritanceRoot ) . changeTracking ; this . identity = ( < EntityMapping > mapping . inheritanceRoot ) . identity ; } areDocumentsEqual ( context , entity , originalDocument ) { return true ; } dirtyCheck ( batch , entity , originalDocument ) { return originalDocument ; } addInsert ( batch , entity ) { return { } ; } addRemove ( batch , entity ) { } postUpdate ( entity , callback ) { callback ( ) ; } postInsert ( entity , callback ) { callback ( ) ; } postRemove ( entity , callback ) { callback ( ) ; } refresh ( entity , callback ) { } watch ( value , observer ) { } fetch ( entity , path , callback ) { } fetchPropertyValue ( entity , property , callback ) { } findAll ( criteria , callback ? ) { } findOneById ( id , callback ) { } findOne ( criteria , callback ) { } findInverseOf ( entity , path , callback ) { } findOneInverseOf ( entity , path , callback ) { } walk ( entity , flags , entities , embedded , callback ) { } executeQuery ( query , callback ) { } }