public void Generate_Datatable_from_DataCollections() { const string userId = "GA37493"; var dataCollectionParties = Builder <DataCollectionParty> .CreateListOfSize(5) .TheFirst(1) .With(q => q.Party = Builder <Party> .CreateNew().With(r => r.UserId = userId).Build()) .And(q => q.Relationship = DataCollectionRelationshipType.Manager) .TheLast(4) .With(q => q.Party = Builder <Party> .CreateNew().Build()) .And(q => q.Relationship = PickHelper.RandomEnumExcept(DataCollectionRelationshipType.None, DataCollectionRelationshipType.Manager)) .Build(); var forCodes = Builder <DataCollectionFieldOfResearch> .CreateListOfSize(5) .All() .With(f => f.FieldOfResearch = Builder <FieldOfResearch> .CreateNew().Build()) .Build(); var seoCodes = Builder <DataCollectionSocioEconomicObjective> .CreateListOfSize(5) .All() .With(f => f.SocioEconomicObjective = Builder <SocioEconomicObjective> .CreateNew().Build()) .Build(); var dataCollections = Builder <DataCollection> .CreateListOfSize(5) .All() .Do(d => d.Parties.AddRange(dataCollectionParties)) .And(d => d.FieldsOfResearch.AddRange(forCodes)) .And(d => d.SocioEconomicObjectives.AddRange(seoCodes)) .Build(); var dataCollectionTable = _csvHelper.DataCollectionsToDataTable(dataCollections); Assert.That(dataCollectionTable, Is.Not.Null); Assert.That(dataCollectionTable.Columns.Count, Is.EqualTo(DataCollectionColumnCount)); Assert.That(dataCollectionTable.Rows.Count, Is.EqualTo(5)); for (var index = 0; index < dataCollections.Count; index++) { var dataRow = dataCollectionTable.Rows[index]; Assert.That(dataRow["Title"], Is.EqualTo(dataCollections[index].Title)); Assert.That(dataRow["Type"], Is.EqualTo(dataCollections[index].Type.ToString())); Assert.That(dataRow["StartDate"], Is.EqualTo(dataCollections[index].StartDate)); Assert.That(dataRow["DataLicensingRights"], Is.EqualTo(dataCollections[index].DataLicensingRights.ToString())); Assert.That(dataRow["ShareAccess"], Is.EqualTo(dataCollections[index].ShareAccess.ToString())); var parties = dataRow["Parties"].ToString().Split('%'); Assert.That(parties.Length, Is.EqualTo(dataCollections[index].Parties.Count)); var seoCodeStrings = dataRow["SocioEconomicObjectives"].ToString().Split('%'); Assert.That(seoCodeStrings.Length, Is.EqualTo(dataCollections[index].SocioEconomicObjectives.Count)); var forCodeStrings = dataRow["FieldsOfResearch"].ToString().Split('%'); Assert.That(forCodeStrings.Length, Is.EqualTo(dataCollections[index].FieldsOfResearch.Count)); } }
public ActionResult AllDataCollectionsToCsv(CsvDumpViewModel model) { var dataCollections = _dataCollectionRepository.GetAll(); if (dataCollections.Count != 0) { var dataCollectionTable = _csvHelper.DataCollectionsToDataTable(dataCollections); var reponse = this.ControllerContext.RequestContext.HttpContext.Response; reponse.AddHeader("Content-Disposition", "attachment;filename=DataCollections_" + DateTime.Now.ToShortDateString() + ".csv"); return(File(Encoding.UTF8.GetBytes(_csvHelper.ExportToCsv(dataCollectionTable, _appSettingsService.CsvSeparator)), "text/csv")); } return(RedirectToAction("Index")); }
public void Render_CSV_file_for_post_to_DataCollectionsToCsv() { var vm = new CsvDumpViewModel { DataCollections = Builder <CollectionListViewModel> .CreateListOfSize(3).Build() }; var dataCollections = Builder <DataCollection> .CreateListOfSize(5) .All() .Build(); _dataCollectionRepository.GetAll().Returns(dataCollections); _csvHelper.ExportToCsv(Arg.Any <DataTable>()).Returns(""); _csvHelper.DataCollectionsToDataTable(Arg.Any <IList <DataCollection> >()).Returns(new DataTable()); _controller.WithCallTo(c => c.AllDataCollectionsToCsv(vm)).ShouldRenderFile("text/csv"); _csvHelper.Received().ExportToCsv(Arg.Any <DataTable>(), Arg.Any <string>()); _csvHelper.Received().DataCollectionsToDataTable(Arg.Any <IList <DataCollection> >()); _context.HttpContext.Response.Received().AddHeader(Arg.Any <string>(), Arg.Any <string>()); }