Example #1
0
        public void Test_CheckCsvFileContent()
        {
            var csvHelper = new CsvHelper();
            // negative test empty
            const string fileContentEmpty = "";
            var          actualForEmpty   = csvHelper.CheckCsvFileContent(fileContentEmpty);

            Assert.False(actualForEmpty);
            // positive test 1
            const string fileContentTest_Positive_1 = "Name, Class, Dorm Address, Room, GPA\r\nSally Whittaker,2018,McCarren House,312,3.75\r\nBelinda Jameson,2017,Cushing House,148,3.52\r\nJeff Smith,2018,Prescott House,17-D,3.20";
            var          actualForPositiveTest_1    = csvHelper.CheckCsvFileContent(fileContentTest_Positive_1);

            Assert.True(actualForPositiveTest_1);
            // positive test 2
            const string fileContentTest_Positive_2 = "Name, Class, Dorm Address, Room, GPA\nSally Whittaker,2018,McCarren House,312,3.75\nBelinda Jameson,2017,Cushing House,148,3.52\nJeff Smith,2018,Prescott House,17-D,3.20";
            var          actualForPositiveTest_2    = csvHelper.CheckCsvFileContent(fileContentTest_Positive_2);

            Assert.True(actualForPositiveTest_2);
            // negative test case 1
            const string fileContentTest_Negative_1 = "Name, Class, Dorm Address, Room, GPA, Sally Whittaker,2018,McCarren House,312,3.75, Belinda Jameson,2017,Cushing House,148,3.52, Jeff Smith,2018,Prescott House,17-D,3.20";
            var          actualForNegativeTest_1    = csvHelper.CheckCsvFileContent(fileContentTest_Negative_1);

            Assert.False(actualForNegativeTest_1);
        }
Example #2
0
        public IActionResult Upload(string fileContent, bool hasHeader)
        {
            const int MaxCharsInput = 800;
            var       viewModel     = new IndexViewModel();
            var       csvHelper     = new CsvHelper();

            if (csvHelper.CheckCsvFileLength(fileContent, MaxCharsInput) == true)
            {
                if (csvHelper.CheckCsvFileContent(fileContent) == true)
                {
                    var fields = csvHelper.GetMappingFields(fileContent, hasHeader, MaxCharsInput);
                    var dict   = csvHelper.GetRowData(fileContent, hasHeader, MaxCharsInput);
                    //
                    viewModel.FileHasHeaders = hasHeader;
                    if (hasHeader == true)
                    {
                        viewModel.FileHeaderCount = fields.Count;
                    }
                    else
                    {
                        viewModel.FileHeaderCount = 0;
                    }
                    viewModel.MappedItems  = fields;
                    viewModel.DataSet      = dict;
                    viewModel.ErrorMessage = string.Empty;
                }
                else
                {
                    viewModel.ErrorMessage = "Csv Input File content is not correct, please have a check on the file uploaded.";
                }
            }
            else
            {
                viewModel.ErrorMessage = string.Concat("Csv Input File Contains too much characters, over than ", MaxCharsInput, "!");
            }
            viewModel.MappingFieldList = GetExampleConfigurationDataSet();

            return(Json(viewModel));
        }