public void Load_StreamIsNull_ThrowsArgumentNullException()
        {
            Stream      stream   = null;
            CsvSettings settings = null;

            Assert.Throws <ArgumentNullException>(() => { CsvImporter <TestClassBase> .Load(stream, settings); });
        }
        public void Load_DataConversionSucceeds_ResultIsAsExpected()
        {
            String content =
                "Label,Enabled,Number,Currency\r\n" +
                "Label-1,true,42,\"1,234\"\r\n" +
                "Label-2,true,42,\"1,234\"\r\n";

            CsvSettings settings = new CsvSettings();

            List <TestClass4> expected = new List <TestClass4>()
            {
                new TestClass4()
                {
                    Label = "Label-1", Enabled = true, Number = 42, Currency = 1.234m,
                },
                new TestClass4()
                {
                    Label = "Label-2", Enabled = true, Number = 42, Currency = 1.234m,
                },
            };

            using (MemoryStream stream = new MemoryStream(settings.Encoding.GetBytes(content)))
            {
                List <TestClass4> actual = CsvImporter <TestClass4> .Load(stream, settings).ToList();

                Assert.AreEqual(expected, actual);
            }
        }
        public void Load_FileDoesNotExist_ThrowsFileNotFoundException()
        {
            String      filename = @"c:\temp\missing-file.csv";
            CsvSettings settings = null;

            Assert.Throws <FileNotFoundException>(() => { CsvImporter <TestClassBase> .Load(filename, settings); });
        }
 public void Load_SettingsAreNull_ThrowsArgumentNullException()
 {
     using (Stream stream = new MemoryStream())
     {
         CsvSettings settings = null;
         Assert.Throws <ArgumentNullException>(() => { CsvImporter <TestClassBase> .Load(stream, settings); });
     }
 }
        public void Load_NotExactlyButColumnCountMismatch_ThrowsFormatException()
        {
            String content =
                "Label,Enabled,Number,Currency\r\n" +
                "Label-1,true,42,\"1,234\"\r\n" +
                "Label-2,true,42\r\n";

            CsvSettings settings = new CsvSettings();

            using (MemoryStream stream = new MemoryStream(settings.Encoding.GetBytes(content)))
            {
                Assert.Throws <FormatException>(() => { CsvImporter <TestClass1> .Load(stream, settings); });
            }
        }
        public void Load_DataConversionFails_ThrowsFormatException()
        {
            String content =
                "Label,Enabled,Number,Currency\r\n" +
                "Label-1,true,42,\"1,234\"\r\n" +
                "Label-2,true,Hello,\"1,234\"\r\n";

            CsvSettings settings = new CsvSettings()
            {
                Exactly = true, Heading = true,
            };

            using (MemoryStream stream = new MemoryStream(settings.Encoding.GetBytes(content)))
            {
                Assert.Throws <FormatException>(() => { CsvImporter <TestClass1> .Load(stream, settings); });
            }
        }
        public void Load_DefaultConstructorFails_ThrowsInvalidOperationException()
        {
            String content =
                "Label,Enabled,Number,Currency\r\n" +
                "Label-1,true,42,\"1,234\"\r\n" +
                "Label-2,true,42,\"1,234\"\r\n";

            CsvSettings settings = new CsvSettings()
            {
                Exactly = true, Heading = true,
            };

            using (MemoryStream stream = new MemoryStream(settings.Encoding.GetBytes(content)))
            {
                Assert.Throws <InvalidOperationException>(() => { CsvImporter <TestClass3> .Load(stream, settings); });
            }
        }
        public void Load_StreamCanWriteOnly_ThrowsArgumentException()
        {
            String      filename = Path.GetTempFileName();
            Stream      stream   = null;
            CsvSettings settings = null;

            try
            {
                stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);

                Assert.Throws <ArgumentException>(() => { CsvImporter <TestClassBase> .Load(stream, settings); });
                this.CleanUp(stream, filename);
            }
            catch
            {
                this.CleanUp(stream, filename);
                throw;
            }
        }
        public void Load_InvalidFilename_ThrowsArgumentException(String filename)
        {
            CsvSettings settings = null;

            Assert.Throws <ArgumentException>(() => { CsvImporter <TestClassBase> .Load(filename, settings); });
        }
        public void Load_FullIntegrationTest_ResultIsAsExpected()
        {
            List <CsvCustomer> expected = new List <CsvCustomer>
            {
                new CsvCustomer {
                    LastName     = "Marley",
                    FirstName    = "Bob",
                    ExternalId   = 1001,
                    EntryDate    = new DateTime(2007, 5, 3),
                    SalesAverage = 1234.56m,
                    IsActive     = false,
                    Description  = "Have a short note here."
                },
                new CsvCustomer {
                    LastName     = "Monroe",
                    FirstName    = "Marilyn",
                    ExternalId   = 1002,
                    EntryDate    = new DateTime(2008, 6, 5),
                    SalesAverage = 1234.56m,
                    IsActive     = false,
                    Description  = null
                },
                new CsvCustomer {
                    LastName     = "Snipes",
                    FirstName    = "Wesley",
                    ExternalId   = 1003,
                    EntryDate    = new DateTime(2009, 7, 6),
                    SalesAverage = 1234.56m,
                    IsActive     = true,
                    Description  = "Have a short note here."
                },
                new CsvCustomer {
                    LastName     = "Hurley",
                    FirstName    = "Elizabeth",
                    ExternalId   = 1004,
                    EntryDate    = new DateTime(2005, 8, 8),
                    SalesAverage = 1234.56m,
                    IsActive     = true,
                    Description  = "Have a short note here."
                },
            };

            String content =
                "Surname,    Forename,      Identifier, Date,       Sales,        Active, Notes\r\n" +
                "\"Marley\", \"Bob\",       1001,       2007-05-03, \"1,234.56\", nope,   \"Have a short note here.\"\r\n" +
                "\"Monroe\", \"Marilyn\",   1002,       2008-06-05, \"1,234.56\", nope,   \"\"\r\n" +
                "\"Snipes\", \"Wesley\",    1003,       2009-07-06, \"1,234.56\", yeah,   \"Have a short note here.\"\r\n" +
                "\"Hurley\", \"Elizabeth\", 1004,       2005-08-08, \"1,234.56\", yeah,   \"Have a short note here.\"\r\n";

            CsvSettings settings = new CsvSettings
            {
                Culture  = CultureInfo.GetCultureInfo("en-US"),
                Mappings = new CsvMappings
                {
                    TrueValues = new List <String> {
                        "yeah"
                    },
                    FalseValues = new List <String> {
                        "nope"
                    },
                },
            };

            List <CsvCustomer> actual = null;

            using (MemoryStream stream = new MemoryStream(settings.Encoding.GetBytes(content)))
            {
                actual = CsvImporter <CsvCustomer> .Load(stream, settings).ToList();
            }

            Assert.AreEqual(expected, actual);
        }