import 'reflect-metadata' ; import { CosmosContainerClient , CosmosOperationResponse } from 'azure-services' ; import { BatchPoolLoadSnapshot , ItemType , PartitionKey } from 'storage-documents' ; import { IMock , Mock , Times } from 'typemoq' ; import { BatchPoolLoadSnapshotProvider } from './batch-pool-load-snapshot-provider' ; let batchPoolLoadSnapshotProvider ; let cosmosContainerClientMock ; beforeEach ( ( ) => { cosmosContainerClientMock = Mock . ofType < CosmosContainerClient > ( ) ; batchPoolLoadSnapshotProvider = new BatchPoolLoadSnapshotProvider ( cosmosContainerClientMock . object ) ; } ) ; describe ( BatchPoolLoadSnapshotProvider , ( ) => { it ( 'write batch pool load snapshot to a Cosmos DB' , async ( ) => { const batchPoolLoadSnapshot = { id : undefined as string , itemType : undefined as string , partitionKey : undefined as string , batchAccountName : 'batchAccountName-1' , poolId : 'poolId-1' , } ; const batchPoolLoadSnapshotDocument = { ... batchPoolLoadSnapshot , id : `${ batchPoolLoadSnapshot . batchAccountName }.${ batchPoolLoadSnapshot . poolId }` , itemType : ItemType . batchPoolLoadSnapshot , partitionKey : PartitionKey . batchPoolLoadSnapshots , } ; let document ; cosmosContainerClientMock . setup ( async ( o ) => o . writeDocument ( batchPoolLoadSnapshotDocument ) ) . callback ( async ( d ) => ( document = d ) ) . verifiable ( Times . once ( ) ) ; await batchPoolLoadSnapshotProvider . writeBatchPoolLoadSnapshot ( < BatchPoolLoadSnapshot > ( < unknown > batchPoolLoadSnapshot ) ) ; expect ( document ) . toEqual ( batchPoolLoadSnapshotDocument ) ; cosmosContainerClientMock . verifyAll ( ) ; } ) ; it ( 'read batch pool load snapshot from a Cosmos DB' , async ( ) => { const batchAccountName = 'batchAccountName-1' ; const poolId = 'poolId-1' ; const id = `${ batchAccountName }.${ poolId }` ; const batchPoolLoadSnapshot = { id : id , } ; cosmosContainerClientMock . setup ( async ( o ) => o . readDocument ( id , PartitionKey . batchPoolLoadSnapshots ) ) . returns ( async ( ) => Promise . resolve ( < CosmosOperationResponse < BatchPoolLoadSnapshot > > ( < unknown > { item : batchPoolLoadSnapshot } ) ) ) . verifiable ( Times . once ( ) ) ; const batchPoolLoadSnapshotResult = await batchPoolLoadSnapshotProvider . readBatchPoolLoadSnapshot ( batchAccountName , poolId ) ; expect ( batchPoolLoadSnapshotResult ) . toEqual ( batchPoolLoadSnapshot ) ; cosmosContainerClientMock . verifyAll ( ) ; } ) ; } ) ;