public async Task Read_TextFileWithoutHeaders_ReturnsFileDataWithCustomHeaders()
        {
            var parseOptions = new FileParseOptionsDto()
            {
                FullName   = TAB_TEXTFILE_PATH,
                Delimiter  = "\t",
                HasHeaders = false
            };

            var expectedData = new FileDataDto()
            {
                FullName  = TAB_TEXTFILE_PATH,
                Delimiter = "\t",
                Headers   = new List <string>()
                {
                    "col_0", "col_1", "col_2"
                },
                Rows = new List <string[]>()
                {
                    new string[] { "Name", "Site", "Email" },
                    new string[] { "Betty J. Brown", "OEMJobs.com", "*****@*****.**" },
                    new string[] { "John D. Ginter", "keepclicker.com", "*****@*****.**" },
                    new string[] { "Roger I. Clinton", "tastrons.com", "*****@*****.**" }
                }
            };

            var resultData = await _fileProcessor.Read(parseOptions);

            Assert.IsTrue(IsFileDataDtoEquals(resultData, expectedData));
        }
        public async Task GetData_InvalidDto_ThrowValidationException(FileParseOptionsDto value)
        {
            var fileService = CreateFileService(TEST_ROOT_FOLDER_PATH);

            Func <Task <FileDataDto> > act = () => fileService.GetData(value);

            await Assert.ThrowsExceptionAsync <ValidationException>(act);
        }
        public async Task <FileDataDto> GetData(FileParseOptionsDto parseOptions)
        {
            FileParseOptionsDtoValidator.ValidateAndThrow(parseOptions);

            CheckRootPath(parseOptions.FullName);
            var fileProcessor = GetFileProcessor(parseOptions.FullName);

            return(await fileProcessor.Read(parseOptions));
        }
        public async Task Update_TextFile_OverwriteFileAndReturnsTask()
        {
            string tempFile = Path.Combine(Path.GetTempPath(), $"temp_file_{Guid.NewGuid().ToString()}.txt");

            File.WriteAllText(tempFile, string.Empty);

            var firstDataToWrite = new FileDataDto()
            {
                FullName  = tempFile,
                Delimiter = "\t",
                Headers   = new List <string>()
                {
                    "1", "2"
                },
                Rows = new List <string[]>()
                {
                    new string[] { "A", "B" }
                }
            };

            var secondDataToWrite = new FileDataDto()
            {
                FullName  = tempFile,
                Delimiter = "\t",
                Headers   = new List <string>()
                {
                    "COL_1", "COL_2"
                },
                Rows = new List <string[]>()
                {
                    new string[] { "A", "B" },
                    new string[] { "A1", "B1" }
                }
            };

            var parseOptions = new FileParseOptionsDto()
            {
                FullName   = tempFile,
                Delimiter  = "\t",
                HasHeaders = true
            };

            await _fileProcessor.Update(firstDataToWrite);

            await _fileProcessor.Update(secondDataToWrite);

            var resultData = await _fileProcessor.Read(parseOptions);

            File.Delete(tempFile);

            Assert.IsTrue(IsFileDataDtoEquals(resultData, secondDataToWrite));
        }
        public async Task Read_NonexistentTextFile_ThrowFileNotFoundException()
        {
            var parseOptions = new FileParseOptionsDto()
            {
                FullName   = Guid.NewGuid().ToString(),
                Delimiter  = " ",
                HasHeaders = false
            };

            Func <Task> act = () => _fileProcessor.Read(parseOptions);

            await Assert.ThrowsExceptionAsync <FileNotFoundException>(act);
        }
        public async Task GetData_InvalidRootPath_ThrowBusinessLogicException()
        {
            var parseOptions = new FileParseOptionsDto()
            {
                FullName = @"C:\f2\text.txt",
            };

            var fileService = CreateFileService(TEST_ROOT_FOLDER_PATH);

            Func <Task <FileDataDto> > act = () => fileService.GetData(parseOptions);

            await Assert.ThrowsExceptionAsync <BusinessLogicException>(act);
        }
        public async Task Read_EmptyTextFile_ReturnsEmptyFileData()
        {
            var parseOptions = new FileParseOptionsDto()
            {
                FullName   = EMPTY_TEXTFILE_PATH,
                Delimiter  = " ",
                HasHeaders = false
            };

            var expectedData = new FileDataDto()
            {
                FullName  = EMPTY_TEXTFILE_PATH,
                Delimiter = " "
            };

            var resultData = await _fileProcessor.Read(parseOptions);

            Assert.IsTrue(IsFileDataDtoEquals(resultData, expectedData));
        }
示例#8
0
        public async Task <FileDataDto> Read(FileParseOptionsDto parseOptions)
        {
            CheckFileExist(parseOptions.FullName);

            var result = new FileDataDto()
            {
                FullName  = parseOptions.FullName,
                Delimiter = parseOptions.Delimiter
            };

            using (var stream = new FileStream(parseOptions.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, DefaultOptions))
                using (var reader = new StreamReader(stream, Encoding.Default))
                {
                    string line;
                    while ((line = await reader.ReadLineAsync()) != null)
                    {
                        result.Rows.Add(line.Split(new string[] { parseOptions.Delimiter }, StringSplitOptions.None));
                    }
                }

            if (result.Rows.Count == 0)
            {
                return(result);
            }

            if (parseOptions.HasHeaders)
            {
                result.Headers.AddRange(result.Rows[0]);
                result.Rows.RemoveAt(0);
            }
            else
            {
                var maxColumnsCount = result.Rows.Max(x => x.Length);
                result.Headers.AddRange(Enumerable.Range(0, maxColumnsCount).Select(x => $"col_{x}"));
            }

            return(result);
        }
        public async Task GetData_ValidFile_ReturnsFileData()
        {
            var parseOptions = new FileParseOptionsDto()
            {
                FullName = $"{TEST_ROOT_FOLDER_PATH}\\text.txt"
            };

            var expectedData = new FileDataDto()
            {
                FullName = $"{TEST_ROOT_FOLDER_PATH}\\text.txt"
            };

            var fileProcFactory = new Mock <IFileProcessorFactory>();
            var fileProcessor   = new Mock <IFileProcessor>();

            fileProcessor.Setup(x => x.Read(parseOptions)).Returns(Task.FromResult(expectedData));
            fileProcFactory.Setup(x => x.Create("txt")).Returns(fileProcessor.Object);

            var fileService = CreateFileService(TEST_ROOT_FOLDER_PATH, fileProcFactory.Object);

            var resultFileData = await fileService.GetData(parseOptions);

            Assert.ReferenceEquals(resultFileData, expectedData);
        }
 public async Task <FileDataDto> GetFileData([FromBody] FileParseOptionsDto parseOptions)
 {
     return(await _fileService.GetData(parseOptions));
 }