public ActionResult FileUpload(UploadExcelFileModel uploadFile) { var model = new ServiceUploadModel(); if (!ModelState.IsValid) { foreach (ModelState state in ModelState.Values) { foreach (ModelError error in state.Errors) { model.RowErrors.Add(error.ErrorMessage); } } return View("FileUploadComplete", model); } DataTable dataTable = null; try { dataTable = FileProcessor.ConsumeFile(uploadFile); } catch { model.RowErrors.Add("Corrupted file format. Please redownload the template and try again."); model.ProcessedRowCount = model.SuccessfulRowsCount = 0; return View("FileUploadComplete", model); } model = FileProcessor.Import((EducationSecurityPrincipal)User, TemplatePath, dataTable); return View("FileUploadComplete", model); }
public void GivenIConsumeAValidServiceUploadFile_ThenTheModelHasNoErrors() { var filePath = UploadFileTemplatePath + "HappyPath.xlsx"; var destinationPath = CopyTestFile(filePath); var model = new ServiceUploadModel(); using (FileStream fs = File.Open(destinationPath, FileMode.Open)) { var uploadFile = new UploadExcelFileModel(); uploadFile.File = MockRepository.GenerateMock<HttpPostedFileBase>(); uploadFile.File.Expect(u => u.InputStream).Return(fs); var table = Target.ConsumeFile(uploadFile); Assert.IsNotNull(model); Assert.IsTrue(model.RowErrors.Count == 0); } DestroyTestFile(destinationPath); }
public void GivenIConsumeAValidServiceOfferingUploadFile_ThenTheCorrectDataTableIsReturned() { var filePath = UploadFileTemplatePath + "HappyPath.xlsx"; var destinationPath = CopyTestFile(filePath); var model = new ServiceUploadModel(); DataTable expected = new DataTable(); expected.Columns.Add("blank", typeof(string)); expected.Columns.Add("Id", typeof(string)); expected.Columns.Add("StartDate", typeof(string)); expected.Columns.Add("EndDate", typeof(string)); expected.Columns.Add("Notes", typeof(string)); expected.Columns.Add(new DataColumn()); expected.Columns.Add(new DataColumn()); expected.Columns.Add(new DataColumn()); expected.Columns.Add(new DataColumn()); expected.Columns.Add(new DataColumn()); expected.Columns.Add(new DataColumn()); expected.Columns.Add(new DataColumn()); DataRow row = expected.NewRow(); row["Id"] = "Upload and Assign Students to a Service Offering"; expected.Rows.Add(row); row = expected.NewRow(); row["Id"] = "1"; row["StartDate"] = "YMCA"; row["EndDate"] = "After School"; expected.Rows.Add(row); row = expected.NewRow(); row["Id"] = "STUDENT ID"; row["StartDate"] = "START DATE"; row["EndDate"] = "END DATE"; row["Notes"] = "NOTES"; expected.Rows.Add(row); row = expected.NewRow(); row["Id"] = "10"; row["StartDate"] = "40920"; row["EndDate"] = "40920"; row["Notes"] = "testing"; expected.Rows.Add(row); row = expected.NewRow(); row["Id"] = "20"; row["StartDate"] = "40962"; row["EndDate"] = "40963"; row["Notes"] = "testing 2"; expected.Rows.Add(row); using (FileStream fs = File.Open(destinationPath, FileMode.Open)) { var uploadFile = new UploadExcelFileModel(); uploadFile.File = MockRepository.GenerateMock<HttpPostedFileBase>(); uploadFile.File.Expect(u => u.InputStream).Return(fs); var actual = Target.ConsumeFile(uploadFile); CollectionAssert.AreEqual(expected.Rows[0].ItemArray, actual.Rows[0].ItemArray); CollectionAssert.AreEqual(expected.Rows[1].ItemArray, actual.Rows[1].ItemArray); CollectionAssert.AreEqual(expected.Rows[2].ItemArray, actual.Rows[2].ItemArray); CollectionAssert.AreEqual(expected.Rows[3].ItemArray, actual.Rows[3].ItemArray); CollectionAssert.AreEqual(expected.Rows[4].ItemArray, actual.Rows[4].ItemArray); } DestroyTestFile(destinationPath); }
public void GivenIConsumeAValidServiceAttendanceUploadFile_ThenTheCorrectDataTableIsReturned() { var filePath = UploadFileTemplatePath + "ValidServiceAttendanceUpload.xlsx"; var destinationPath = CopyTestFile(filePath); DataTable expected = new DataTable(); expected.Columns.Add("blank", typeof(string)); expected.Columns.Add("Id", typeof(string)); expected.Columns.Add("DateAttended", typeof(string)); expected.Columns.Add("Subject", typeof(string)); expected.Columns.Add("Duration", typeof(string)); expected.Columns.Add("Notes", typeof(string)); expected.Columns.Add(new DataColumn()); expected.Columns.Add(new DataColumn()); expected.Columns.Add(new DataColumn()); expected.Columns.Add(new DataColumn()); expected.Columns.Add(new DataColumn()); expected.Columns.Add(new DataColumn()); DataRow row = expected.NewRow(); row["Id"] = "Upload Attendance Records for a Service Offering"; expected.Rows.Add(row); row = expected.NewRow(); row["Id"] = "1"; row["DateAttended"] = "YMCA"; row["Subject"] = "After School"; expected.Rows.Add(row); row = expected.NewRow(); row["Id"] = "STUDENT ID"; row["DateAttended"] = "DATE"; row["Subject"] = "SUBJECT"; row["Duration"] = "DURATION"; row["Notes"] = "NOTES"; expected.Rows.Add(row); row = expected.NewRow(); row["Id"] = "10"; row["DateAttended"] = "41390"; row["Subject"] = "test subject 3"; row["Duration"] = "30"; row["Notes"] = "Further testing"; expected.Rows.Add(row); row = expected.NewRow(); row["Id"] = "20"; row["DateAttended"] = "41391"; row["Subject"] = "test subject 4"; row["Duration"] = "40"; row["Notes"] = "Even further testing"; expected.Rows.Add(row); using (FileStream fs = File.Open(destinationPath, FileMode.Open)) { var uploadFile = new UploadExcelFileModel(); uploadFile.File = MockRepository.GenerateMock<HttpPostedFileBase>(); uploadFile.File.Expect(u => u.InputStream).Return(fs); var actual = Target.ConsumeFile(uploadFile); CollectionAssert.AreEqual(expected.Rows[0].ItemArray, actual.Rows[0].ItemArray); CollectionAssert.AreEqual(expected.Rows[1].ItemArray, actual.Rows[1].ItemArray); CollectionAssert.AreEqual(expected.Rows[2].ItemArray, actual.Rows[2].ItemArray); CollectionAssert.AreEqual(expected.Rows[3].ItemArray, actual.Rows[3].ItemArray); CollectionAssert.AreEqual(expected.Rows[4].ItemArray, actual.Rows[4].ItemArray); } DestroyTestFile(destinationPath); }
public void GivenIConsumeAValidServiceAttendanceUploadFile_ThenADataTableIsReturned() { var filePath = UploadFileTemplatePath + "ValidServiceAttendanceUpload.xlsx"; var destinationPath = CopyTestFile(filePath); using (FileStream fs = File.Open(destinationPath, FileMode.Open)) { var uploadFile = new UploadExcelFileModel(); uploadFile.File = MockRepository.GenerateMock<HttpPostedFileBase>(); uploadFile.File.Expect(u => u.InputStream).Return(fs); var table = Target.ConsumeFile(uploadFile); Assert.IsNotNull(table); } DestroyTestFile(destinationPath); }
public DataTable ConsumeFile(UploadExcelFileModel uploadFile) { var memoryStream = new MemoryStream(); uploadFile.File.InputStream.CopyTo(memoryStream); return ExcelParser.ExtractExcelSheetValues(memoryStream, ServiceOfferingSheetName); }