Example #1
0
        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. ");
        }
Example #2
0
        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");
        }