public ActionResult Index(ImportViewModel vm) { if (ModelState.IsValid && Request.Files.Count == 1) { using (var workbook = new XLWorkbook(Request.Files[0].InputStream)) { Planning p; if (!string.IsNullOrWhiteSpace(vm.NewNom)) { p = new Planning() { Date = DateTime.Now, Nom = vm.NewNom }; db.Plannings.Add(p); db.SaveChanges(); } else { p = db.Plannings.First(s => s.Id == vm.PlanningId); } try { ImportExcelUtil.ImportPlanning(p, db, workbook); vm.LastPlanningId = p.Id; } catch (ImportException ex) { ModelState.AddModelError("NewNom", ex.Message); } } } vm.Jours = db.JourEvenements.ToList(); return(View(vm)); }
public void ImportExport() { //Creation des données using (var ctx = new FestivArtsContext()) { Planning p = new Planning() { Date = DateTime.Now, Nom = "TUImportExport" }; Planning p2 = new Planning() { Date = DateTime.Now, Nom = "TUImportExportResult" }; ctx.Plannings.Add(p); ctx.Plannings.Add(p2); ctx.SaveChanges(); int i = 0, j = 0; var benevoles = ctx.Benevoles.ToList(); foreach (var c in ctx.Creneaux.ToList()) { foreach (var b in benevoles) { var a = new Affectation() { BenevoleId = b.Id, PlanningId = p.Id, CreneauId = c.Id }; if (j < c.NbBenevoleMax) { ctx.Affectations.Add(a); } else { break; } j++; } i++; j = 0; ctx.SaveChanges(); } Assert.AreEqual(ctx.Creneaux.Sum(s => s.NbBenevoleMax), p.Affectations.Count); } //Génération exel var workbooks = new Dictionary <int, Stream>(); using (var ctx = new FestivArtsContext()) { Planning p = ctx.Plannings.First(s => s.Nom == "TUImportExport"); foreach (var jour in ctx.JourEvenements) { workbooks.Add(jour.Id, new MemoryStream()); var wb = new XLWorkbook(); ExcelUtils.FillPlanning(wb, ctx, jour, p); wb.SaveAs(workbooks[jour.Id]); workbooks[jour.Id].Position = 0; } } foreach (var k in workbooks.Keys) { //Import using (var ctx = new FestivArtsContext()) { Planning p = ctx.Plannings.First(s => s.Nom == "TUImportExportResult"); var wb = new XLWorkbook(workbooks[k]); ImportExcelUtil.ImportPlanning(p, ctx, wb); } } //Assert using (var ctx = new FestivArtsContext()) { Planning p = ctx.Plannings.Include("Affectations.Creneau.CreneauDef").First(s => s.Nom == "TUImportExport"); Planning pres = ctx.Plannings.Include("Affectations.Creneau.CreneauDef").First(s => s.Nom == "TUImportExportResult"); Dictionary <int, List <Affectation> > affectationByDay = new Dictionary <int, List <Affectation> >(); Dictionary <int, List <Affectation> > affectationResultByDay = new Dictionary <int, List <Affectation> >(); foreach (int i in ctx.JourEvenements.Select(s => s.Id)) { affectationByDay.Add(i, new List <Affectation>()); affectationResultByDay.Add(i, new List <Affectation>()); } p.Affectations.ForEach(s => affectationByDay[s.Creneau.CreneauDef.JourId].Add(s)); pres.Affectations.ForEach(s => affectationResultByDay[s.Creneau.CreneauDef.JourId].Add(s)); foreach (var j in ctx.JourEvenements) { try { Assert.AreEqual(affectationByDay[j.Id].Count, affectationResultByDay[j.Id].Count); } catch (AssertFailedException) { var wb = new XLWorkbook(workbooks[j.Id]); wb.SaveAs(@"D:\testResult\" + j.Nom + ".xlsx"); wb = new XLWorkbook(); ExcelUtils.FillPlanning(wb, ctx, j, pres); wb.SaveAs(@"D:\testResult\" + j.Nom + "_res.xlsx"); throw; } } Assert.AreEqual(p.Affectations.Count, pres.Affectations.Count); foreach (var a in p.Affectations) { Assert.AreEqual(1, pres.Affectations.Count(s => s.BenevoleId == a.BenevoleId && s.CreneauId == a.CreneauId)); } } }