public void ParseCsvFileTestReturnsNotHeadedModelForNotHeadedFile() { // Arrange var filePath = "InputData.csv"; TestParserHelper parserHelper = new TestParserHelper(); TestFileHelper tstHelper = new TestFileHelper(filePath, parserHelper.notHeadedContent); CsvFileHandler csvFileHandler = new CsvFileHandler( new ValidationService(), new FileService(tstHelper.config.Object), new ParsingService()); // Expected result var expectedResult = new CsvHandleResult(); expectedResult.Success = true; expectedResult.ParsedCsvContent = parserHelper.expectedResultNoHeader; // Act var actualResult = csvFileHandler.ParseCsvFile(tstHelper.fileMock.Object, false); // Assert Assert.IsNotNull(actualResult); Assert.IsNotNull(actualResult.Result); Assert.AreEqual(expectedResult, actualResult.Result); }
public void TestParseCsvFile() { ICsvFileHandler _csvFileHandler = new CsvFileHandler(new ParsingService(), new ValidationService(), new FileService()); string csvFileContent = File.ReadAllText(HeaderFileName); CsvTable csvTable = new CsvTable(); string[] lines = csvFileContent.Split( new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries ); for (int i = 0; i < lines.Length; i++) { var row = lines[i]; if (i == 0) { csvTable.HeaderRow = ParseRow(row); } else { csvTable.Rows.Add(ParseRow(row)); } } CsvHandleResult csvHandleResult = new CsvHandleResult(); csvHandleResult.Success = true; csvHandleResult.ParsedCsvContent = csvTable; // todo mock HttpPostedFileBase // Assert(_csvFileHandler.ParseCsvFile }
public void PostTestReturnsHeadedModelForHeadedFile() { // Arrange var filePath = "InputData.csv"; TestParserHelper parserHelper = new TestParserHelper(); TestFileHelper tstHelper = new TestFileHelper(filePath, parserHelper.headedContent); FilesController fileController = new FilesController(tstHelper.config.Object, new CsvFileHandler(new ValidationService(), new FileService(tstHelper.config.Object), new ParsingService())); // Expected result var expectedCsvResult = new CsvHandleResult(); expectedCsvResult.Success = true; expectedCsvResult.ParsedCsvContent = parserHelper.headedExpectedResult; var expectedResult = new OkObjectResult(new { isHeadedFiles = true, resultList = new List <CsvHandleResult>() { expectedCsvResult } }); // Act var actualResult = fileController.Post(new List <IFormFile>() { tstHelper.fileMock.Object }, true); // Assert Assert.IsNotNull(actualResult); Assert.IsNotNull(actualResult.Result); Assert.AreEqual(expectedResult.StatusCode, ((OkObjectResult)actualResult.Result).StatusCode); Assert.AreEqual(expectedResult.Value.ToString(), ((OkObjectResult)actualResult.Result).Value.ToString()); }
/// <summary> /// Takes in an input HttpPostedFileBase and returns a parsed CsvTable object /// </summary> /// <param name="inputFile"></param> /// <param name="containsHeader"></param> /// <returns></returns> public CsvHandleResult ParseCsvFile(HttpPostedFileBase inputFile, bool containsHeader) { var result = new CsvHandleResult(); if (!_validationService.IsCsvFile(inputFile.FileName)) { result.ErrorMessage = $"Selected file, {inputFile.FileName}, does not have supported format CSV. Nothing has been uploaded"; return(result); } var uploadedFilePath = _fileService.StoreFile(inputFile); if (string.IsNullOrWhiteSpace(uploadedFilePath)) { result.ErrorMessage = "File failed to save to server"; return(result); } var fileContent = _fileService.ReadFile(uploadedFilePath); if (string.IsNullOrWhiteSpace(fileContent)) { result.ErrorMessage = "File had no content"; return(result); } var parsedFileContent = _parsingService.ParseCsv(fileContent, containsHeader); if (parsedFileContent == null) { result.ErrorMessage = "Failed to parse file content"; return(result); } result.Success = true; result.ParsedCsvContent = parsedFileContent; return(result); }
public void ParseCsvFileTestWrongExtensionReturnsErrorMessage() { //Arrange var txtFileName = "Data.txt"; TestFileHelper tstHelper = new TestFileHelper(txtFileName, ""); CsvFileHandler csvFileHandler = new CsvFileHandler( new ValidationService(), new FileService(tstHelper.config.Object), new ParsingService()); // Expected result var expectedResult = new CsvHandleResult(); expectedResult.ErrorMessage = $"Selected file, {txtFileName}, does not have supported format CSV"; //Act var actualResult = csvFileHandler.ParseCsvFile(tstHelper.fileMock.Object, true); //Assert Assert.IsNotNull(actualResult); Assert.IsNotNull(actualResult.Result); Assert.AreEqual(expectedResult, actualResult.Result); }
/// <summary> /// Validate, store and parse Csv File /// </summary> /// <param name="inputFile">IFormFile</param> /// <param name="isHeadedFile">bool</param> /// <returns>Successfully parsed data or error message - CsvHandleResult task</returns> public async Task <CsvHandleResult> ParseCsvFile(IFormFile inputFile, bool isHeadedFile) { var result = new CsvHandleResult(); // Check file is valid if (!_validationService.IsCsvFile(inputFile.FileName)) { result.ErrorMessage = $"Selected file, {inputFile.FileName}, does not have supported format CSV"; return(result); } // Store the file var uploadedFilePath = await _fileService.StoreFile(inputFile); if (string.IsNullOrWhiteSpace(uploadedFilePath)) { result.ErrorMessage = "File failed to save to server"; return(result); } // Parse the file var parsedFileContent = _parsingService.ParseCsv( uploadedFilePath, isHeadedFile); if (parsedFileContent == null) { result.ErrorMessage = "Failed to parse file content"; return(result); } result.Success = true; result.ParsedCsvContent = parsedFileContent; return(result); }