Ejemplo n.º 1
0
		public void GetRecordGenericTest()
		{
			var parser = new ParserMock
			{
				{ "Id", "Name" },
				{ "a", "b" },
				null
			};

			var reader = new CsvReader( parser );
			reader.Read();
			try
			{
				reader.GetRecord<Simple>();
				Assert.Fail();
			}
			catch( CsvTypeConverterException ex )
			{
				var expected = "Row: '2' (1 based)\r\n" +
				        "Type: 'CsvHelper.Tests.Exceptions.ExceptionMessageTests+Simple'\r\n" +
				        "Field Index: '0' (0 based)\r\n" +
				        "Field Name: 'Id'\r\n" +
				        "Field Value: 'a'\r\n";
				Assert.AreEqual( expected, ex.Data["CsvHelper"] );

			}
		}
 public void ReadTest()
 {
     var queue = new Queue<string[]>();
     queue.Enqueue( new[] { "Id", "Names" } );
     queue.Enqueue( new[] { "1", "one" } );
     queue.Enqueue( null );
     var parserMock = new ParserMock( queue );
     var csv = new CsvReader( parserMock );
     csv.Read();
     var record = csv.GetRecord<Test>();
     Assert.IsNull( record.Names );
 }
        public void TryGetDoesNotThrowWhenWillThrowOnMissingFieldIsEnabled()
        {
            var data = new[] { "1" };
            var queue = new Queue<string[]>();
            queue.Enqueue( data );
            queue.Enqueue( null );
            var parserMock = new ParserMock( queue );

            var reader = new CsvReader( parserMock );
            reader.Configuration.WillThrowOnMissingField = true;
            reader.Read();
            string field;
            Assert.IsFalse( reader.TryGetField( "test", out field ) );
        }
        public void GetRecordTest()
        {
            var data = new List<string[]>
            {
                new[] { "Id", "Name" },
                new[] { "1", "one" },
                new[] { "2", "two" },
                null
            };

            var parserMock = new ParserMock( new Queue<string[]>( data ) );

            var csvReader = new MyCsvReader( parserMock );
            csvReader.GetRecords<Test>().ToList();
        }
Ejemplo n.º 5
0
        public void ReadHeaderDoesNotReadRowTest()
        {
            var rows = new Queue<string[]>();
            rows.Enqueue( new[] { "Id", "Name" } );
            rows.Enqueue( new[] { "1", "One" } );
            rows.Enqueue( new[] { "2", "two" } );
            rows.Enqueue( null );
            var parser = new ParserMock( rows );

            var csv = new CsvReader( parser );
            csv.Read();
            csv.ReadHeader();

            Assert.IsNull( csv.CurrentRecord );
        }
Ejemplo n.º 6
0
		public void SkipEmptyRowTest()
		{
			var rows = new Queue<string[]>();
			rows.Enqueue( new[] { "First,Second" } );
			rows.Enqueue( new[] { " " } );
			rows.Enqueue( new[] { "1", "2" } );
			var parser = new ParserMock( rows );

			var csv = new CsvReader( parser );
			csv.Configuration.ShouldSkipRecord = row => row.All( string.IsNullOrWhiteSpace );

			csv.Read();
			Assert.AreEqual( "1", csv.GetField( 0 ) );
			Assert.AreEqual( "2", csv.GetField( 1 ) );
		}
        public void ReferenceMappingTest()
        {
            var queue = new Queue<string[]>();
            queue.Enqueue( new[]
            {
                "FirstName",
                "LastName",
                "HomeStreet",
                "HomeCity",
                "HomeState",
                "HomeZip",
                "WorkStreet",
                "WorkCity",
                "WorkState",
                "WorkZip"
            } );
            var row = new[]
            {
                "John",
                "Doe",
                "1234 Home St",
                "Home Town",
                "Home State",
                "12345",
                "5678 Work Rd",
                "Work City",
                "Work State",
                "67890"
            };
            queue.Enqueue( row );
            queue.Enqueue( null );
            var parserMock = new ParserMock( queue );

            var reader = new CsvReader( parserMock );
            reader.Read();
            var person = reader.GetRecord<Person>();

            Assert.AreEqual( "John", person.FirstName );
            Assert.AreEqual( "Doe", person.LastName );
            Assert.AreEqual( "1234 Home St", person.HomeAddress.Street );
            Assert.AreEqual( "Home Town", person.HomeAddress.City );
            Assert.AreEqual( "Home State", person.HomeAddress.State );
            Assert.AreEqual( "12345", person.HomeAddress.Zip );
            Assert.AreEqual( "5678 Work Rd", person.WorkAddress.Street );
            Assert.AreEqual( "Work City", person.WorkAddress.City );
            Assert.AreEqual( "Work State", person.WorkAddress.State );
            Assert.AreEqual( "67890", person.WorkAddress.Zip );
        }
Ejemplo n.º 8
0
        public void ReadHeaderReadsHeaderTest()
        {
            var rows = new Queue<string[]>();
            rows.Enqueue( new[] { "Id", "Name" } );
            rows.Enqueue( new[] { "1", "One" } );
            rows.Enqueue( new[] { "2", "two" } );
            rows.Enqueue( null );
            var parser = new ParserMock( rows );

            var csv = new CsvReader( parser );
            csv.Read();
            csv.ReadHeader();

            Assert.IsNotNull( csv.FieldHeaders );
            Assert.AreEqual( "Id", csv.FieldHeaders[0] );
            Assert.AreEqual( "Name", csv.FieldHeaders[1] );
        }
        public void TryGetFieldInvalidIndexTest()
        {
            var data1 = new[] { "One", "Two" };
            var data2 = new[] { "one", "two" };
            var queue = new Queue<string[]>();
            queue.Enqueue( data1 );
            queue.Enqueue( data2 );
            queue.Enqueue( null );
            var parserMock = new ParserMock( queue );

            var reader = new CsvReader( parserMock );
            reader.Read();

            int field;
            var got = reader.TryGetField( 0, out field );
            Assert.IsFalse( got );
            Assert.AreEqual( default( int ), field );
        }
Ejemplo n.º 10
0
		public void TryGetFieldStrictTest()
		{
			var data1 = new[] { "One", "Two" };
			var data2 = new[] { "1", "2" };
			var queue = new Queue<string[]>();
			queue.Enqueue( data1 );
			queue.Enqueue( data2 );
			queue.Enqueue( null );
			var parserMock = new ParserMock( queue );

			var reader = new CsvReader( parserMock ) { Configuration = { WillThrowOnMissingField = true } };
			reader.Read();

			int field;
			var got = reader.TryGetField( "One", out field );
			Assert.IsTrue( got );
			Assert.AreEqual( 1, field );
		}
Ejemplo n.º 11
0
		public void TryGetFieldTest()
		{
			var data1 = new[] { "One", "Two" };
			var data2 = new[] { "1", "2" };
			var queue = new Queue<string[]>();
			queue.Enqueue( data1 );
			queue.Enqueue( data2 );
			queue.Enqueue( null );
			var parserMock = new ParserMock( queue );

			var reader = new CsvReader( parserMock );
			reader.Read();

			int field;
			var got = reader.TryGetField( 0, out field );
			Assert.IsTrue( got );
			Assert.AreEqual( 1, field );
		}
Ejemplo n.º 12
0
		public void ReadTest()
		{
			var queue = new Queue<string[]>();
			queue.Enqueue( new[] { "Names" } );
			queue.Enqueue( new[] { "one" } );
			queue.Enqueue( null );
			var parserMock = new ParserMock( queue );
			var csv = new CsvReader( parserMock );
			csv.Read();
			try
			{
				csv.GetRecord<Test>();
				Assert.Fail();
			}
			catch( CsvTypeConverterException )
			{
			}
		}
        public void TryGetFieldIndexTest()
        {
            var parserMock = new ParserMock
            {
                { "One", "Two", "Two" },
                { "1", "2", "3" }
            };
            var reader = new CsvReader( parserMock );
            reader.Read();

            int field;
            var got = reader.TryGetField( "Two", 0, out field );
            Assert.IsTrue( got );
            Assert.AreEqual( 2, field );

            got = reader.TryGetField( "Two", 1, out field );
            Assert.IsTrue( got );
            Assert.AreEqual( 3, field );
        }
Ejemplo n.º 14
0
        public void ShouldSkipWithEmptyRows()
        {
            var rows = new Queue<string[]>();
            rows.Enqueue( new[] { "First,Second" } );
            rows.Enqueue( new[] { "skipme," } );
            rows.Enqueue( new[] { "" } );
            rows.Enqueue( new[] { "1", "2" } );

            var parser = new ParserMock( rows );

            var csv = new CsvReader( parser );
            csv.Configuration.ShouldSkipRecord = row => row[0].StartsWith( "skipme" );
            csv.Configuration.SkipEmptyRecords = true;

            csv.Read();
            csv.Read();
            Assert.AreEqual( "1", csv.GetField( 0 ) );
            Assert.AreEqual( "2", csv.GetField( 1 ) );
        }
Ejemplo n.º 15
0
        public void GettingFieldHeadersFailsWhenHeaderNotReadTest()
        {
            var rows = new Queue<string[]>();
            rows.Enqueue( new[] { "Id", "Name" } );
            rows.Enqueue( new[] { "1", "One" } );
            rows.Enqueue( new[] { "2", "two" } );
            rows.Enqueue( null );
            var parser = new ParserMock( rows );

            var csv = new CsvReader( parser );

            try
            {
                var x = csv.FieldHeaders;
                Assert.Fail();
            }
            catch( CsvReaderException )
            {
            }
        }
        public void TryGetFieldEmptyDate()
        {
            // DateTimeConverter.IsValid() doesn't work correctly
            // so we need to test and make sure that the conversion
            // fails for an emptry string for a date.
            var data = new[] { " " };
            var queue = new Queue<string[]>();
            queue.Enqueue( data );
            queue.Enqueue( null );
            var parserMock = new ParserMock( queue );

            var reader = new CsvReader( parserMock );
            reader.Configuration.HasHeaderRecord = false;
            reader.Read();

            DateTime field;
            var got = reader.TryGetField( 0, out field );

            Assert.IsFalse( got );
            Assert.AreEqual( DateTime.MinValue, field );
        }
Ejemplo n.º 17
0
        public void GetMissingFieldTest()
        {
            var parser = new ParserMock
            {
                { "Id", "Name" },
                { "a", "b" },
                null
            };

            var reader = new CsvReader( parser );
            reader.Read();
            reader.Read();
            try
            {
                reader.GetField( 2 );
                Assert.Fail();
            }
            catch( CsvMissingFieldException ex )
            {
                Assert.AreEqual( 2, ex.Row );
                Assert.AreEqual( 2, ex.FieldIndex );
            }
        }
        public void GetMissingFieldTest()
        {
            var parser = new ParserMock
            {
                { "Id", "Name" },
                { "a", "b" },
                null
            };

            var reader = new CsvReader( parser );
            reader.Read();
            try
            {
                reader.GetField( 2 );
                Assert.Fail();
            }
            catch( CsvMissingFieldException ex )
            {
                var expected = "Row: '2' (1 based)\r\n" +
                               "Type: 'System.String'\r\n" +
                               "Field Index: '2' (0 based)\r\n";
                Assert.AreEqual( expected, ex.Data["CsvHelper"] );
            }
        }
Ejemplo n.º 19
0
        public void GetGenericMissingFieldWithTypeTest()
        {
            var parser = new ParserMock
            {
                { "Id", "Name" },
                { "a", "b" },
                null
            };

            var reader = new CsvReader( parser );
            reader.Read();
            reader.Read();
            try
            {
                reader.GetField<int>( 2 );
                Assert.Fail();
            }
            catch( CsvMissingFieldException ex )
            {
                Assert.AreEqual( 2, ex.Row );
                Assert.AreEqual( ex.Type, typeof( int ) );
                Assert.AreEqual( 2, ex.FieldIndex );
            }
        }
Ejemplo n.º 20
0
        public void GetRecordEmptyFileWithHeaderOnTest()
        {
            var queue = new Queue<string[]>();
            queue.Enqueue( null );
            var parserMock = new ParserMock( queue );

            var csvReader = new CsvReader( parserMock );
            try
            {
                csvReader.Read();
                Assert.Fail();
            }
            catch( CsvReaderException )
            {
            }
        }
Ejemplo n.º 21
0
        public void GetRecordNoAttributesTest()
        {
            var headerData = new[]
            {
                "IntColumn",
                "StringColumn",
                "GuidColumn",
                "CustomTypeColumn",
            };
            var recordData = new[]
            {
                "1",
                "string column",
                Guid.NewGuid().ToString(),
                "blah",
            };
            var queue = new Queue<string[]>();
            queue.Enqueue( headerData );
            queue.Enqueue( recordData );
            queue.Enqueue( null );
            var parserMock = new ParserMock( queue );

            var csv = new CsvReader( parserMock );
            csv.Configuration.IsStrictMode = false;
            csv.Read();
            var record = csv.GetRecord<TestRecordNoAttributes>();

            Assert.AreEqual( Convert.ToInt32( recordData[0] ), record.IntColumn );
            Assert.AreEqual( recordData[1], record.StringColumn );
            Assert.AreEqual( default( string ), record.IgnoredColumn );
            Assert.AreEqual( default( string ), record.TypeConvertedColumn );
            Assert.AreEqual( default( int ),record.FirstColumn );
            Assert.AreEqual( new Guid( recordData[2] ), record.GuidColumn );
            Assert.AreEqual( default( int ), record.NoMatchingFields );
            Assert.AreEqual( default( TestRecord ), record.CustomTypeColumn );
        }
Ejemplo n.º 22
0
        public void GetRecordsWithDuplicateHeaderNames()
        {
            var headerData = new[]
            {
                "Column",
                "Column",
                "Column"
            };

            var queue = new Queue<string[]>();
            queue.Enqueue( headerData );
            queue.Enqueue( new[] { "one", "two", "three" } );
            queue.Enqueue( new[] { "one", "two", "three" } );
            queue.Enqueue( null );
            var csvParserMock = new ParserMock( queue );

            var csv = new CsvReader( csvParserMock );
            csv.Configuration.IsStrictMode = true;
            var records = csv.GetRecords<TestRecordDuplicateHeaderNames>().ToList();

            Assert.AreEqual( 2, records.Count );

            for( var i = 1; i <= records.Count; i++ )
            {
                var record = records[i - 1];
                Assert.AreEqual( "one", record.Column1 );
                Assert.AreEqual( "two", record.Column2 );
                Assert.AreEqual( "three", record.Column3 );
            }
        }
Ejemplo n.º 23
0
        public void GetRecordsTest()
        {
            var headerData = new[]
            {
                "IntColumn",
                "String Column",
                "GuidColumn",
            };
            var guid = Guid.NewGuid();
            var queue = new Queue<string[]>();
            queue.Enqueue( headerData );
            queue.Enqueue( new[] { "1", "string column 1", guid.ToString() } );
            queue.Enqueue( new[] { "2", "string column 2", guid.ToString() } );
            queue.Enqueue( null );
            var csvParserMock = new ParserMock( queue );

            var csv = new CsvReader( csvParserMock );
            csv.Configuration.IsStrictMode = false;
            var records = csv.GetRecords( typeof( TestRecord ) ).ToList();

            Assert.AreEqual( 2, records.Count );

            for( var i = 1; i <= records.Count; i++ )
            {
                var record = (TestRecord)records[i - 1];
                Assert.AreEqual( i, record.IntColumn );
                Assert.AreEqual( "string column " + i, record.StringColumn );
                Assert.AreEqual( "test", record.TypeConvertedColumn );
                Assert.AreEqual( i, record.FirstColumn );
                Assert.AreEqual( guid, record.GuidColumn );
            }
        }
Ejemplo n.º 24
0
        public void SkipEmptyRecordsTest()
        {
            var queue = new Queue<string[]>();
            queue.Enqueue( new[] { "1", "2", "3" } );
            queue.Enqueue( new[] { "", "", "" } );
            queue.Enqueue( new[] { "4", "5", "6" } );
            queue.Enqueue( null );

            var parserMock = new ParserMock( queue );

            var reader = new CsvReader( parserMock );
            reader.Configuration.HasHeaderRecord = false;
            reader.Configuration.SkipEmptyRecords = true;

            reader.Read();
            Assert.AreEqual( "1",  reader.CurrentRecord[0] );
            Assert.AreEqual( "2",  reader.CurrentRecord[1] );
            Assert.AreEqual( "3",  reader.CurrentRecord[2] );

            reader.Read();
            Assert.AreEqual( "4", reader.CurrentRecord[0] );
            Assert.AreEqual( "5", reader.CurrentRecord[1] );
            Assert.AreEqual( "6", reader.CurrentRecord[2] );

            Assert.IsFalse( reader.Read() );
        }
Ejemplo n.º 25
0
        public void GetRecordTest()
        {
            var headerData = new[]
            {
                "IntColumn",
                "String Column",
                "GuidColumn",
            };
            var recordData = new[]
            {
                "1",
                "string column",
                Guid.NewGuid().ToString(),
            };
            var queue = new Queue<string[]>();
            queue.Enqueue( headerData );
            queue.Enqueue( recordData );
            queue.Enqueue( null );
            var csvParserMock = new ParserMock( queue );

            var csv = new CsvReader( csvParserMock );
            csv.Configuration.IsStrictMode = false;
            csv.Read();
            var record = (TestRecord)csv.GetRecord( typeof( TestRecord ));

            Assert.AreEqual( Convert.ToInt32( recordData[0] ), record.IntColumn );
            Assert.AreEqual( recordData[1], record.StringColumn );
            Assert.AreEqual( "test", record.TypeConvertedColumn );
            Assert.AreEqual( Convert.ToInt32( recordData[0] ), record.FirstColumn );
            Assert.AreEqual( new Guid( recordData[2] ), record.GuidColumn );
        }
Ejemplo n.º 26
0
        public void InvalidateRecordsCacheTest()
        {
            var parserMock = new ParserMock( new Queue<string[]>() );
            var csvReader = new CsvReader( parserMock );
            csvReader.Configuration.AttributeMapping<TestRecord>();

            Assert.IsTrue( csvReader.Configuration.Properties.Count > 0 );

            csvReader.InvalidateRecordCache<TestRecord>();

            Assert.AreEqual( 0, csvReader.Configuration.Properties.Count );

            csvReader.Configuration.AttributeMapping<TestRecordNoAttributes>();

            Assert.IsTrue( csvReader.Configuration.Properties.Count > 0 );
        }
Ejemplo n.º 27
0
        public void HasHeaderRecordTest()
        {
            var data1 = new[] { "One", "Two" };
            var data2 = new[] { "1", "2" };
            var queue = new Queue<string[]>();
            queue.Enqueue( data1 );
            queue.Enqueue( data2 );

            var parserMock = new ParserMock( queue );

            var reader = new CsvReader( parserMock );
            reader.Read();

            // Check to see if the header record and first record are set properly.
            Assert.AreEqual( Convert.ToInt32( data2[0] ), reader.GetField<int>( "One" ) );
            Assert.AreEqual( Convert.ToInt32( data2[1] ), reader.GetField<int>( "Two" ) );
            Assert.AreEqual( Convert.ToInt32( data2[0] ), reader.GetField<int>( 0 ) );
            Assert.AreEqual( Convert.ToInt32( data2[1] ), reader.GetField<int>( 1 ) );
        }
Ejemplo n.º 28
0
        public void HasHeaderRecordNotReadExceptionTest()
        {
            var parserMock = new ParserMock( new Queue<string[]>() );
            var reader = new CsvReader( parserMock );

            try
            {
                reader.GetField<int>( 0 );
                Assert.Fail();
            }
            catch( CsvReaderException )
            {
            }
        }
Ejemplo n.º 29
0
        public void GetTypeTest()
        {
            var data = new[]
            {
                "1",
                "blah",
                DateTime.Now.ToString(),
                "true",
                "c",
                "",
                Guid.NewGuid().ToString(),
            };
            var queue = new Queue<string[]>();
            queue.Enqueue( data );
            queue.Enqueue( data );
            queue.Enqueue( null );

            var parserMock = new ParserMock( queue );

            var reader = new CsvReader( parserMock );
            reader.Read();

            Assert.AreEqual( Convert.ToInt16( data[0] ), reader.GetField<short>( 0 ) );
            Assert.AreEqual( Convert.ToInt16( data[0] ), reader.GetField<short?>( 0 ) );
            Assert.AreEqual( null, reader.GetField<short?>( 5 ) );
            Assert.AreEqual( Convert.ToInt32( data[0] ), reader.GetField<int>( 0 ) );
            Assert.AreEqual( Convert.ToInt32( data[0] ), reader.GetField<int?>( 0 ) );
            Assert.AreEqual( null, reader.GetField<int?>( 5 ) );
            Assert.AreEqual( Convert.ToInt64( data[0] ), reader.GetField<long>( 0 ) );
            Assert.AreEqual( Convert.ToInt64( data[0] ), reader.GetField<long?>( 0 ) );
            Assert.AreEqual( null, reader.GetField<long?>( 5 ) );
            Assert.AreEqual( Convert.ToDecimal( data[0] ), reader.GetField<decimal>( 0 ) );
            Assert.AreEqual( Convert.ToDecimal( data[0] ), reader.GetField<decimal?>( 0 ) );
            Assert.AreEqual( null, reader.GetField<decimal?>( 5 ) );
            Assert.AreEqual( Convert.ToSingle( data[0] ), reader.GetField<float>( 0 ) );
            Assert.AreEqual( Convert.ToSingle( data[0] ), reader.GetField<float?>( 0 ) );
            Assert.AreEqual( null, reader.GetField<float?>( 5 ) );
            Assert.AreEqual( Convert.ToDouble( data[0] ), reader.GetField<double>( 0 ) );
            Assert.AreEqual( Convert.ToDouble( data[0] ), reader.GetField<double?>( 0 ) );
            Assert.AreEqual( null, reader.GetField<double?>( 5 ) );
            Assert.AreEqual( data[1], reader.GetField<string>( 1 ) );
            Assert.AreEqual( string.Empty, reader.GetField<string>( 5 ) );
            Assert.AreEqual( Convert.ToDateTime( data[2] ), reader.GetField<DateTime>( 2 ) );
            Assert.AreEqual( Convert.ToDateTime( data[2] ), reader.GetField<DateTime?>( 2 ) );
            Assert.AreEqual( null, reader.GetField<DateTime?>( 5 ) );
            Assert.AreEqual( Convert.ToBoolean( data[3] ), reader.GetField<bool>( 3 ) );
            Assert.AreEqual( Convert.ToBoolean( data[3] ), reader.GetField<bool?>( 3 ) );
            Assert.AreEqual( null, reader.GetField<bool?>( 5 ) );
            Assert.AreEqual( Convert.ToChar( data[4] ), reader.GetField<char>( 4 ) );
            Assert.AreEqual( Convert.ToChar( data[4] ), reader.GetField<char?>( 4 ) );
            Assert.AreEqual( null, reader.GetField<char?>( 5 ) );
            Assert.AreEqual( new Guid( data[6] ), reader.GetField<Guid>( 6 ) );
            Assert.AreEqual( null, reader.GetField<Guid?>( 5 ) );
        }
Ejemplo n.º 30
0
        public void GetRecordWithDuplicateHeaderFields()
        {
            var data = new[] { "Field1", "Field1" };
            var queue = new Queue<string[]>();
            queue.Enqueue( data );
            queue.Enqueue( data );
            var parserMock = new ParserMock( queue );

            var reader = new CsvReader( parserMock );
            reader.Configuration.IsStrictMode = true;
            reader.Read();
        }