protected void AppendCsvFragment(string temporaryFilePath, IEnumerable <ExpandoObject> csvRows, bool outputHeaders) { string csv = _csvUtils.AsCsv(csvRows, outputHeaders); _fileSystemAccess.Append(temporaryFilePath, csv) .GetAwaiter() .GetResult(); }
public void SetUp() { _calculationResultsRepository = Substitute.For <ICalculationResultsRepository>(); _blobClient = Substitute.For <IBlobClient>(); _csvUtils = Substitute.For <ICsvUtils>(); _transformation = Substitute.For <IProviderResultsToCsvRowsTransformation>(); _cloudBlob = Substitute.For <ICloudBlob>(); _fileSystemAccess = Substitute.For <IFileSystemAccess>(); _fileSystemCacheSettings = Substitute.For <IFileSystemCacheSettings>(); _jobManagement = Substitute.For <IJobManagement>(); _calcsApiClient = Substitute.For <ICalculationsApiClient>(); _specsApiClient = Substitute.For <ISpecificationsApiClient>(); _service = new ProviderResultsCsvGeneratorService(Substitute.For <ILogger>(), _blobClient, _calcsApiClient, _specsApiClient, _calculationResultsRepository, new ResiliencePolicies { BlobClient = Policy.NoOpAsync(), CalculationsApiClient = Policy.NoOpAsync(), SpecificationsApiClient = Policy.NoOpAsync(), ResultsRepository = Policy.NoOpAsync() }, _csvUtils, _transformation, _fileSystemAccess, _fileSystemCacheSettings, _jobManagement); _message = new Message(); _rootPath = NewRandomString(); _fileSystemCacheSettings.Path .Returns(_rootPath); _fileSystemAccess .Append(Arg.Any <string>(), Arg.Any <Stream>()) .Returns(Task.CompletedTask); _blobProperties = new BlobProperties(); _cloudBlob .Properties .Returns(_blobProperties); }
public async Task TransformsProviderResultsForSpecificationInBatchesAndCreatesCsvWithResults() { string specificationId = NewRandomString(); string fundingStream = NewRandomString(); string specificationName = NewRandomString(); string expectedInterimFilePath = Path.Combine(_rootPath, $"calculation-results-{specificationId}.csv"); List <ProviderResult> providerResultsOne = new List <ProviderResult>(); List <ProviderResult> providerResultsTwo = new List <ProviderResult>(); ExpandoObject[] transformedRowsOne = { new ExpandoObject(), new ExpandoObject(), new ExpandoObject(), new ExpandoObject(), }; ExpandoObject[] transformedRowsTwo = { new ExpandoObject(), new ExpandoObject(), new ExpandoObject(), new ExpandoObject(), }; string expectedCsvOne = NewRandomString(); string expectedCsvTwo = NewRandomString(); StreamWriter streamWriterOne = new StreamWriter(new MemoryStream(expectedCsvOne.AsUTF8Bytes())); StreamWriter streamWriterTwo = new StreamWriter(new MemoryStream(expectedCsvTwo.AsUTF8Bytes())); MemoryStream incrementalFileStream = new MemoryStream(); GivenSpecification(specificationId, fundingStream); GivenTheCsvRowTransformation(providerResultsOne, transformedRowsOne, expectedCsvOne, true); AndTheCsvRowTransformation(providerResultsTwo, transformedRowsTwo, expectedCsvTwo, false); AndTheMessageProperties(("specification-id", specificationId)); AndTheMessageProperties(("specification-name", specificationName)); AndTheCloudBlobForSpecificationId(specificationId); AndTheFileStream(expectedInterimFilePath, incrementalFileStream); AndTheFileExists(expectedInterimFilePath); _calculationResultsRepository .When(_ => _.ProviderResultsBatchProcessing(Arg.Is(specificationId), Arg.Any <Func <List <ProviderResult>, Task> >(), Arg.Is(100))) .Do(info => { Func <List <ProviderResult>, Task> batchProcessingCallBack = info.ArgAt <Func <List <ProviderResult>, Task> >(1); batchProcessingCallBack(providerResultsOne) .GetAwaiter() .GetResult(); batchProcessingCallBack(providerResultsTwo) .GetAwaiter() .GetResult(); }); await WhenTheCsvIsGenerated(); _fileSystemAccess .Received(1) .Delete(expectedInterimFilePath); await _fileSystemAccess .Append(expectedInterimFilePath, streamWriterOne.BaseStream); await _fileSystemAccess .Append(expectedInterimFilePath, streamWriterTwo.BaseStream); await _blobClient .Received(1) .UploadAsync(_cloudBlob, incrementalFileStream); await _blobClient .Received(1) .AddMetadataAsync( _cloudBlob, Arg.Is <IDictionary <string, string> >(_ => _["specification-id"] == specificationId && _["specification-name"] == specificationName && _["file-name"].StartsWith($"Calculation Results {specificationName} {DateTimeOffset.UtcNow:yyyy-MM-dd}") && _["job-type"] == "CalcResult") ); }