/// <summary> /// Convert a Pdb Property quert result into a CSV string. /// </summary> /// <param name="result">The result to convert</param> /// <param name="indicatorId">Indicator Id that represtents this export</param> /// <returns>A string of comma seperated values, with header row and /// escaped with "" around strings</returns> public static string ResultsAsCsv(NycResultsWithMetadata result, string indicatorId) { StringWriter writer = new StringWriter(); List <ClassMapColDefinition> colMap = new List <ClassMapColDefinition>(); // Additional Header properties NycIndicator indicator = GetIndicator(indicatorId); colMap.Add(new ClassMapColDefinition(_csvHeaderText, _csvHeaderText.Replace("<%DATE%>", DateTime.Now.ToShortDateString()), null)); colMap.Add(new ClassMapColDefinition("Indicator", "Indicator", null)); colMap.Add(new ClassMapColDefinition("Description", "Description", null)); // Some hardcoded values need to be added to each row Dictionary <String, String> valuesToStuff = new Dictionary <string, string>(); valuesToStuff.Add("Indicator", result.Indicator); valuesToStuff.Add("Description", indicator.Description); // Create some column mappings by looping through the attributes foreach (AbstractNamedSortable attr in result.Attrs) { // Only add it if we've got something if (attr.Name != null) { string displayName = attr.Name == "Area" ? result.Resolution : attr.Name; colMap.Add(new ClassMapColDefinition(attr.Name, displayName, null)); } } ClassMapping map = new ClassMapping("csv", "csv", colMap, false); DictionaryDao csvDao = new DictionaryDao(new CsvDescriptor(writer, CsvQuoteLevel.QuoteAlways), map); List <CheckedDictionary <string, object> > list = new List <CheckedDictionary <string, object> >(); if (result.ContextRows != null) { // Add context rows AddValuesToDictionary(result, list, result.ContextRows, valuesToStuff); } if (result.Values != null) { // Add value rows AddValuesToDictionary(result, list, result.Values, valuesToStuff); } // If there were no results, just give them nothing if (list.Count == 0) { return(""); } // Add these values to the csv writer csvDao.Insert(list); // Return our result return(writer.ToString()); }
public void TestWriteCsvWithQuotes() { StringWriter writer = new StringWriter(); // This is testing to verify the CsvDescriptor constructor that takes a // Writer and a CVSQuoteLevel. CsvTestObj testObj1 = new CsvTestObj(); CheckedDictionary<string, object> testDict1 = new CheckedDictionary<string, object>(); testObj1.One = 50; testObj1.Two = -1.0; testObj1.Three = "Yo"; testObj1.Four = new DateTime(2001, 1, 1, 1, 1, 1); testObj1.Five = null; testDict1["One"] = testObj1.One; testDict1["Two"] = testObj1.Two; testDict1["Three"] = testObj1.Three; testDict1["Four"] = testObj1.Four; testDict1["Five"] = testObj1.Five; CsvTestObj testObj2 = new CsvTestObj(); CheckedDictionary<string, object> testDict2 = new CheckedDictionary<string, object>(); testObj2.One = int.MaxValue; testObj2.Two = double.MinValue; testObj2.Three = null; testObj2.Four = DateTime.MinValue; testObj2.Five = ""; testDict2["One"] = testObj2.One; testDict2["Two"] = testObj2.Two; testDict2["Three"] = testObj2.Three; testDict2["Four"] = testObj2.Four; testDict2["Five"] = testObj2.Five; ClassMapping mapping1 = MakeMapping("n/a", "WriteOne", true); CsvDescriptor desc1 = new CsvDescriptor(writer, CsvQuoteLevel.QuoteStrings); DictionaryDao dao1 = new DictionaryDao(desc1, mapping1); dao1.Insert(testDict1); dao1.Insert(testDict2); string csv = writer.ToString(); Assert.IsNotEmpty(csv, "The writer should not return an empty string. "); }
public void TestWriteCsv() { CsvTestObj testObj1 = new CsvTestObj(); CheckedDictionary<string, object> testDict1 = new CheckedDictionary<string, object>(); testObj1.One = 50; testObj1.Two = -1.0; testObj1.Three = "Yo"; testObj1.Four = new DateTime(2001, 1, 1, 1, 1, 1); testObj1.Five = null; testDict1["One"] = testObj1.One; testDict1["Two"] = testObj1.Two; testDict1["Three"] = testObj1.Three; testDict1["Four"] = testObj1.Four; testDict1["Five"] = testObj1.Five; CsvTestObj testObj2 = new CsvTestObj(); CheckedDictionary<string, object> testDict2 = new CheckedDictionary<string, object>(); testObj2.One = int.MaxValue; testObj2.Two = double.MinValue; testObj2.Three = null; testObj2.Four = DateTime.MinValue; testObj2.Five = ""; testDict2["One"] = testObj2.One; testDict2["Two"] = testObj2.Two; testDict2["Three"] = testObj2.Three; testDict2["Four"] = testObj2.Four; testDict2["Five"] = testObj2.Five; ClassMapping mapping1 = MakeMapping("n/a", "WriteOne", true); CsvDescriptor desc1 = new CsvDescriptor("..\\..\\Tests"); DictionaryDao dao1 = new DictionaryDao(desc1, mapping1); dao1.Truncate(); dao1.Insert(testDict1); dao1.Insert(testDict2); ClassMapping mapping2 = MakeMapping("Azavea.Open.DAO.CSV.Tests.CsvTestObj,Azavea.Open.DAO.CSV", "Doesn'tMatter", true); CsvDescriptor desc2 = new CsvDescriptor(CsvConnectionType.FileName, "..\\..\\Tests\\WriteTwo.csv"); FastDAO<CsvTestObj> dao2 = new FastDAO<CsvTestObj>(desc2, mapping2); dao2.Truncate(); dao2.Insert(testObj1); dao2.Insert(testObj2); ClassMapping mapping3 = MakeMapping("n/a", "AlsoDoesn'tMatter", true); using (StreamWriter sw = new StreamWriter("..\\..\\Tests\\WriteThree.csv", false)) { CsvDescriptor desc3 = new CsvDescriptor(sw); DictionaryDao dao3 = new DictionaryDao(desc3, mapping3); // Can't truncate this one. dao3.Insert(testDict1); dao3.Insert(testDict2); } ClassMapping mapping4 = MakeMapping("n/a", "WriteFour", false); CsvDescriptor desc4 = new CsvDescriptor("..\\..\\Tests"); DictionaryDao dao4 = new DictionaryDao(desc4, mapping4); dao4.Truncate(); dao4.Insert(testDict1); dao4.Insert(testDict2); ClassMapping mapping5 = MakeMapping("Azavea.Open.DAO.CSV.Tests.CsvTestObj,Azavea.Open.DAO.CSV", "Doesn'tMatter", false); CsvDescriptor desc5 = new CsvDescriptor(CsvConnectionType.FileName, "..\\..\\Tests\\WriteFive.csv"); FastDAO<CsvTestObj> dao5 = new FastDAO<CsvTestObj>(desc5, mapping5); dao5.Truncate(); dao5.Insert(testObj1); dao5.Insert(testObj2); ClassMapping mapping6 = MakeMapping("n/a", "AlsoDoesn'tMatter", false); using (StreamWriter sw = new StreamWriter("..\\..\\Tests\\WriteSix.csv", false)) { CsvDescriptor desc6 = new CsvDescriptor(sw); DictionaryDao dao6 = new DictionaryDao(desc6, mapping6); // Can't truncate this one. dao6.Insert(testDict1); dao6.Insert(testDict2); } // Now, assert they are all correct. 1, 2, and 3 should be the same (they have headers) // and 4, 5, and 6 should be the same (without headers). AssertFileContentsSame("..\\..\\Tests\\WriteOne.csv", "..\\..\\Tests\\WriteTwo.csv"); AssertFileContentsSame("..\\..\\Tests\\WriteOne.csv", "..\\..\\Tests\\WriteThree.csv"); AssertFileGreater("..\\..\\Tests\\WriteOne.csv", "..\\..\\Tests\\WriteFour.csv"); AssertFileContentsSame("..\\..\\Tests\\WriteFour.csv", "..\\..\\Tests\\WriteFive.csv"); AssertFileContentsSame("..\\..\\Tests\\WriteFour.csv", "..\\..\\Tests\\WriteFive.csv"); }