static void Main(string[] args) { var serviceProvider = new ServiceCollection() .AddSingleton <ICSVFileMerge, CSVFileMerge>() .AddSingleton <ICsvReader, CsvReader>() .AddSingleton <ICsvWriter, CsvWriter>() .AddSingleton <IConfiguration, Configuration>() .AddSingleton <IFileManager, FileManager>() .BuildServiceProvider(); var request = new FileMergeRequest( fileNameOne: args[0], fileNameTwo: args[1], outputFileName: args[2], joinColumnName: args[3]); var fileMergeTool = serviceProvider.GetService <ICSVFileMerge>(); var task = Task.Run(() => fileMergeTool.GetMergedFile(request)); task.Wait(); if (task.IsFaulted) { Console.WriteLine($"An error occurred while performing the merge"); } else { Console.WriteLine($"Result file created"); } }
public void MergerMustReturnOk() { mockConfiguration .Setup(f => f.GetInputPath()) .Returns("C:\\csv\\"); mockConfiguration .Setup(c => c.GetOutputPath()) .Returns("C:\\csv\\output\\"); mockReader .SetupSequence(r => r.ConvertCSVtoDataTable(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(GetMockedCsv1()) .Returns(GetMockedCsv2()); mockWritter .Setup(w => w.WriteCsv(It.IsAny <DataTable>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(Task.CompletedTask); var request = new FileMergeRequest("first.csv", "second.csv", "CSV_Files_Merged.csv", "IdCity"); var task = Task.Run(() => csvFileMerge.GetMergedFile(request)); task.Wait(); Assert.AreEqual(task.Status, TaskStatus.RanToCompletion); mockWritter.Verify(w => w.WriteCsv(It.IsAny <DataTable>(), It.IsAny <string>(), It.IsAny <string>()), Times.Once); }
public void MergerMustReturnError() { mockConfiguration .Setup(f => f.GetInputPath()) .Returns(string.Empty); mockConfiguration .Setup(c => c.GetOutputPath()) .Returns("C:\\csv\\output\\"); mockReader .Setup(r => r.ConvertCSVtoDataTable(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Throws <Exception>(); var request = new FileMergeRequest("first.csv", "second.csv", "CSV_Files_Merged.csv", "IdCity"); var task = Task.Run(() => csvFileMerge.GetMergedFile(request)); Assert.Throws <AggregateException>(() => task.Wait()); mockWritter.Verify(w => w.WriteCsv(It.IsAny <DataTable>(), It.IsAny <string>(), It.IsAny <string>()), Times.Never); }
public async Task GetMergedFile(FileMergeRequest request) { try { Console.WriteLine(StartLabel); var file1 = $"{config.GetInputPath()}{request.FileNameOne}"; var file2 = $"{config.GetInputPath()}{request.FileNameTwo}"; var output = $"{config.GetOutputPath()}{request.OutputFileName}"; var dt1 = reader.ConvertCSVtoDataTable("1", file1, request.JoinColumnName); var dt2 = reader.ConvertCSVtoDataTable("2", file2, request.JoinColumnName); dt1.Merge(dt2, false, MissingSchemaAction.AddWithKey); Console.WriteLine(MergedLabel); await writer.WriteCsv(dt1, request.JoinColumnName, output); } catch (Exception ex) { Console.WriteLine(ex.Message, ex); await Task.FromException(ex); } }