예제 #1
0
		public void ArgumentTestCtor2()
		{
			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(""), 1))
			{
				Assert.AreEqual(1, csv.BufferSize);
			}
		}
예제 #2
0
        public void MissingDelimiterAfterQuotedFieldTest1()
        {
            const string Data = "\"111\",\"222\"\"333\"";

            using (var csv = new DelimitedRecordReader(new StringReader(Data)))
            {
                csv.DynamicColumnCount = false;
                for (int i = 0; i < 3; i++)
                {
                    csv.Columns.Add(new DelimitedRecordColumn(csv.GetDefaultColumnName(i)));
                }

                csv.DoubleQuoteEscapingEnabled = false;
                csv.TrimWhiteSpaces            = true;

                var ex = Assert.Throws <MalformedRecordException>(
                    () =>
                {
                    while (csv.Read() == ReadResult.Success)
                    {
                    }
                });

                Assert.AreEqual(0, ex.CurrentRecordIndex);
                Assert.AreEqual(1, ex.CurrentColumnIndex);
            }
        }
		public static void ReadAll(DelimitedRecordReaderBenchmarkArguments args)
		{
			using (var reader = new DelimitedRecordReader(new StreamReader(args.Path, args.Encoding, true, args.BufferSize), args.BufferSize))
			{
				reader.AdvancedEscapingEnabled = args.AdvancedEscapingEnabled;
				reader.DoubleQuoteEscapingEnabled = args.DoubleQuoteEscapingEnabled;
				reader.SkipEmptyLines = args.SkipEmptyLines;
				reader.TrimWhiteSpaces = args.TrimWhiteSpaces;

				if (args.FieldIndex > -1)
				{
					reader.DynamicColumnCount = false;

					for (int i = 0; i < args.FieldIndex + 1; i++)
						reader.Columns.Add(new DelimitedRecordColumn(reader.GetDefaultColumnName(i)));
				}

				string s;

				while (reader.Read() != ReadResult.EndOfFile)
				{
					for (int i = 0; i < reader.Columns.Count - 1; i++)
						s = reader[i];
				}
			}
		}
예제 #4
0
 public void FieldCountTest1()
 {
     using (var csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
     {
         Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());
         DelimitedRecordReaderTestData.CheckSampleData1(csv, true, true);
     }
 }
예제 #5
0
 public void ParsingTest_SpecificBufferSize([Random(1, 1024, 1000)] int bufferSize)
 {
     using (DelimitedRecordReader reader = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1), bufferSize))
     {
         Assert.AreEqual(ReadResult.Success, reader.ReadColumnHeaders());
         DelimitedRecordReaderTestData.CheckSampleData1(reader, true, true);
     }
 }
예제 #6
0
 public void MoveToTest_AfterLastRecord()
 {
     using (var csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
     {
         Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());
         Assert.AreEqual(ReadResult.EndOfFile, csv.MoveTo(DelimitedRecordReaderTestData.SampleData1RecordCount));
     }
 }
예제 #7
0
 public void ReadColumnHeadersTest_NotCalledWithDynamicColumns()
 {
     using (var csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
     {
         csv.DynamicColumnCount = true;
         DelimitedRecordReaderTestData.CheckSampleData1(csv, false, true);
     }
 }
예제 #8
0
        protected override IDataReader CreateDataReaderInstance()
        {
            var reader = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleTypedData1));

            Assert.IsTrue(reader.ReadColumnHeaders() == ReadResult.Success);

            return(reader);
        }
예제 #9
0
		public void ParsingTest_SpecificBufferSize([Random(1, 1024, 1000)] int bufferSize)
		{
			using (DelimitedRecordReader reader = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1), bufferSize))
			{
				Assert.AreEqual(ReadResult.Success, reader.ReadColumnHeaders());
				DelimitedRecordReaderTestData.CheckSampleData1(reader, true, true);
			}
		}
		public static void CheckSampleData1(DelimitedRecordReader csv, bool hasHeaders, long recordIndex)
		{
			Assert.AreEqual(recordIndex, csv.CurrentRecordIndex);
			Assert.AreEqual(SampleData1ColumnCount, csv.Columns.Count);

			long actualRecordIndex = (hasHeaders ? recordIndex + 1 : recordIndex);

			for (int i = 0; i < _data1[actualRecordIndex].Length; i++)
				Assert.AreEqual(_data1[actualRecordIndex][i], csv[i]);
		}
예제 #11
0
        public void ReadColumnHeadersTest_EmptyCsv(string data)
        {
            using (var csv = new DelimitedRecordReader(new StringReader(data)))
            {
                csv.CommentCharacter = '#';
                csv.SkipEmptyLines   = true;

                Assert.AreEqual(ReadResult.EndOfFile, csv.ReadColumnHeaders());
                Assert.AreEqual(0, csv.Columns.Count);
            }
        }
예제 #12
0
        public static void CheckSampleData1(DelimitedRecordReader csv, bool hasHeaders, long recordIndex)
        {
            Assert.AreEqual(recordIndex, csv.CurrentRecordIndex);
            Assert.AreEqual(SampleData1ColumnCount, csv.Columns.Count);

            long actualRecordIndex = (hasHeaders ? recordIndex + 1 : recordIndex);

            for (int i = 0; i < _data1[actualRecordIndex].Length; i++)
            {
                Assert.AreEqual(_data1[actualRecordIndex][i], csv[i]);
            }
        }
예제 #13
0
        public void DataTableLoadTest()
        {
            using (var sr = new StringReader("a,b,c\n1,2,3"))
                using (var csv = new DelimitedRecordReader(sr))
                {
                    var dt = new DataTable();
                    dt.Load(csv);

                    csv.ReadColumnHeaders();
                    Assert.AreEqual(csv.Columns.Count, dt.Columns.Count);
                }
        }
예제 #14
0
        public void ReadColumnHeadersTest_NotCalledWithoutDynamicColumns()
        {
            using (var csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                csv.DynamicColumnCount = false;

                while (csv.Read() == ReadResult.Success)
                {
                    Assert.AreEqual(0, csv.Columns.Count);
                }
            }
        }
예제 #15
0
        public void DataReaderGetOrdinalWithNoCurrentRecordTest()
        {
            using (var csv = new DelimitedRecordReader(new StringReader("0,1,2")))
            {
                csv.ReadColumnHeaders();

                for (int i = 0; i < 3; i++)
                {
                    Assert.AreEqual(i, ((System.Data.IDataReader)csv).GetOrdinal(i.ToString()));
                }
            }
        }
예제 #16
0
        public void CopyCurrentRecordToTest([Range(0, 5)] int startIndex)
        {
            using (var csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                var record = new string[DelimitedRecordReaderTestData.SampleData1ColumnCount + startIndex];

                while (csv.Read() == ReadResult.Success)
                {
                    csv.CopyCurrentRecordTo(record, startIndex);
                    DelimitedRecordReaderTestData.CheckSampleData1(record, false, csv.CurrentRecordIndex, startIndex);
                }
            }
        }
예제 #17
0
        public void MoveToTest_Forward(
            [Values(false, true)] bool readHeaders,
            [Range(0, DelimitedRecordReaderTestData.SampleData1RecordCount - 1)] int toRecordIndex)
        {
            using (var csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                if (readHeaders)
                {
                    Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());
                }

                csv.MoveTo(toRecordIndex);
                DelimitedRecordReaderTestData.CheckSampleData1(csv, readHeaders, toRecordIndex);
            }
        }
예제 #18
0
        public void SkipEmptyLinesTest2()
        {
            using (var csv = new DelimitedRecordReader(new StringReader("00\n\n10")))
            {
                csv.SkipEmptyLines = true;

                Assert.AreEqual(ReadResult.Success, csv.Read());
                Assert.AreEqual(1, csv.Columns.Count);
                Assert.AreEqual("00", csv[0]);

                Assert.AreEqual(ReadResult.Success, csv.Read());
                Assert.AreEqual(1, csv.Columns.Count);
                Assert.AreEqual("10", csv[0]);

                Assert.AreEqual(ReadResult.EndOfFile, csv.Read());
            }
        }
예제 #19
0
        public void MissingFieldUnquotedTest4()
        {
            using (var csv = new DelimitedRecordReader(new StringReader("a,b\n   ")))
            {
                csv.SkipEmptyLines     = true;
                csv.MissingFieldAction = MissingRecordFieldAction.ReturnNullValue;

                csv.DynamicColumnCount = true;
                Assert.AreEqual(ReadResult.Success, csv.Read());
                Assert.AreEqual(2, csv.Columns.Count);
                Assert.AreEqual("a", csv[0]);
                Assert.AreEqual("b", csv[1]);

                csv.DynamicColumnCount = false;
                Assert.AreEqual(ReadResult.Success, csv.Read());
                Assert.AreEqual(string.Empty, csv[0]);
                Assert.AreEqual(null, csv[1]);
            }
        }
		public static void CheckSampleData1(DelimitedRecordReader csv, bool hasHeaders, bool readToEnd)
		{
			if (!readToEnd)
				CheckSampleData1(csv, hasHeaders, csv.CurrentRecordIndex);
			else
			{
				long recordIndex = 0;

				while (csv.Read() == ReadResult.Success)
				{
					CheckSampleData1(csv, hasHeaders, recordIndex);
					recordIndex++;
				}

				if (hasHeaders)
					Assert.AreEqual(SampleData1RecordCount - 1, csv.CurrentRecordIndex);
				else
					Assert.AreEqual(SampleData1RecordCount, csv.CurrentRecordIndex);
			}
		}
예제 #21
0
        public void ColumnsTest_SpecificValueConverterIsUsed()
        {
            using (var csv = new DelimitedRecordReader(new StringReader("a,b\n1.0,2.0")))
            {
                Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());
                Assert.AreEqual(ReadResult.Success, csv.Read());

                CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");

                csv.Columns["a"].ValueConverter = new StringValueConverter {
                    IntegerNumberStyles = NumberStyles.Integer
                };
                csv.Columns["b"].ValueConverter = new StringValueConverter {
                    IntegerNumberStyles = NumberStyles.Float
                };

                Assert.Throws <FormatException>(() => csv.GetValue("a", 0));
                Assert.AreEqual(2.0f, csv.GetValue("b", 0.0f));
            }
        }
예제 #22
0
        public void SkipEmptyLinesTest3()
        {
            using (var csv = new DelimitedRecordReader(new StringReader("00\n\n10")))
            {
                csv.SkipEmptyLines     = false;
                csv.MissingFieldAction = MissingRecordFieldAction.ReturnEmptyValue;

                Assert.AreEqual(ReadResult.Success, csv.Read());
                Assert.AreEqual(1, csv.Columns.Count);
                Assert.AreEqual("00", csv[0]);

                Assert.AreEqual(ReadResult.Success, csv.Read());
                Assert.AreEqual(0, csv.Columns.Count);

                Assert.AreEqual(ReadResult.Success, csv.Read());
                Assert.AreEqual(1, csv.Columns.Count);
                Assert.AreEqual("10", csv[0]);

                Assert.AreEqual(ReadResult.EndOfFile, csv.Read());
            }
        }
예제 #23
0
        public void ReadColumnHeadersTest_WithEmptyColumnNames(string defaultColumnName)
        {
            if (defaultColumnName == null)
            {
                defaultColumnName = "Column";
            }

            using (var csv = new DelimitedRecordReader(new StringReader(",  ,,aaa,\"   \",,,")))
            {
                csv.DefaultColumnNamePrefix = defaultColumnName;

                Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());
                Assert.AreEqual(8, csv.Columns.Count);

                Assert.AreEqual("aaa", csv.Columns[3].Name);
                foreach (var index in new int[] { 0, 1, 2, 4, 5, 6, 7 })
                {
                    Assert.AreEqual(defaultColumnName + index.ToString(), csv.Columns[index].Name);
                }
            }
        }
예제 #24
0
        public void AvancedParsingTest_SpecificBufferSize([Random(10, 1024, 1000)] int bufferSize)
        {
            const string Data = @"\u0031\u0032\u033\u34\x0035\x0036\x037\x38\u0039,\d00049\d0050\d051\d52\o000065\o00066\o0067\o070\o71
									""\n\t\\\r\f\v\a\b\e"""                                    ;

            string[] values = { "123456789", "123456789", "\n\t\\\r\f\v\a\b\u001b" };

            using (DelimitedRecordReader reader = new DelimitedRecordReader(new StringReader(Data), bufferSize))
            {
                reader.AdvancedEscapingEnabled = true;

                Assert.AreEqual(ReadResult.Success, reader.Read());
                Assert.AreEqual(values[0], reader[0]);
                Assert.AreEqual(values[1], reader[1]);

                Assert.AreEqual(ReadResult.Success, reader.Read());
                Assert.AreEqual(values[2], reader[0]);

                Assert.AreEqual(ReadResult.EndOfFile, reader.Read());
            }
        }
예제 #25
0
		public void AvancedParsingTest_SpecificBufferSize([Random(10, 1024, 1000)] int bufferSize)
		{
			const string Data = @"\u0031\u0032\u033\u34\x0035\x0036\x037\x38\u0039,\d00049\d0050\d051\d52\o000065\o00066\o0067\o070\o71
									""\n\t\\\r\f\v\a\b\e""";

			string[] values = { "123456789", "123456789", "\n\t\\\r\f\v\a\b\u001b" };

			using (DelimitedRecordReader reader = new DelimitedRecordReader(new StringReader(Data), bufferSize))
			{
				reader.AdvancedEscapingEnabled = true;

				Assert.AreEqual(ReadResult.Success, reader.Read());
				Assert.AreEqual(values[0], reader[0]);
				Assert.AreEqual(values[1], reader[1]);

				Assert.AreEqual(ReadResult.Success, reader.Read());
				Assert.AreEqual(values[2], reader[0]);

				Assert.AreEqual(ReadResult.EndOfFile, reader.Read());
			}
		}
예제 #26
0
        public void MoreFieldsTest(bool dynamicColumnCount)
        {
            const string Data = "ORIGIN,DESTINATION\nPHL,FLL,kjhkj kjhkjh,eg,fhgf\nNYC,LAX";

            using (var csv = new DelimitedRecordReader(new StringReader(Data)))
            {
                csv.DynamicColumnCount = dynamicColumnCount;

                Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());
                Assert.AreEqual(2, csv.Columns.Count);

                Assert.AreEqual(ReadResult.Success, csv.Read());
                Assert.AreEqual("PHL", csv[0]);
                Assert.AreEqual("FLL", csv[1]);

                Assert.AreEqual(ReadResult.Success, csv.Read());
                Assert.AreEqual("NYC", csv[0]);
                Assert.AreEqual("LAX", csv[1]);

                Assert.AreEqual(ReadResult.EndOfFile, csv.Read());
            }
        }
예제 #27
0
        public void UnicodeParsingTest1()
        {
            // control characters and comma are skipped

            char[] raw = new char[65536 - 13];

            for (int i = 0; i < raw.Length; i++)
            {
                raw[i] = (char)(i + 14);
            }

            raw[44 - 14] = ' ';             // skip comma

            string data = new string(raw);

            using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(data)))
            {
                Assert.AreEqual(ReadResult.Success, csv.Read());
                Assert.AreEqual(data, csv[0]);
                Assert.AreEqual(ReadResult.EndOfFile, csv.Read());
            }
        }
        public void ReadRecord_TwoRecordsInFile_ReturnsTwoRecordObjects()
        {
            // Arrange
              String resourceFileName = "Siftan.IntegrationTests.Resources.TestFile.csv";
              String inputFilePath = this.workingDirectory + resourceFileName;
              Assembly.GetExecutingAssembly().CopyEmbeddedResourceToFile(resourceFileName, inputFilePath);

              var recordDescriptor = new DelimitedRecordDescriptor
              {
            Delimiter = "|",
            Qualifier = '\0',
            LineIDIndex = 0,
            HeaderID = "01",
            Term = new DelimitedRecordDescriptor.TermDefinition("01", 3)
              };

              var reader = new DelimitedRecordReader(recordDescriptor);

              var fileReader = new FileReader(inputFilePath);

              // Act
              var firstRecord = reader.ReadRecord(fileReader);
              Int64 firstRecordEndPosition = fileReader.Position;
              var secondRecord = reader.ReadRecord(fileReader);
              Int64 secondRecordEndPosition = fileReader.Position;

              fileReader.Close();

              // Assert
              firstRecord.Should().NotBeNull();
              secondRecord.Should().NotBeNull();
              firstRecord.Should().NotBeSameAs(secondRecord);

              firstRecord.Start.Should().Be(0);
              firstRecord.End.Should().Be(firstRecordEndPosition);
              secondRecord.Start.Should().Be(firstRecordEndPosition);
              secondRecord.End.Should().Be(secondRecordEndPosition);
        }
예제 #29
0
		public void ArgumentTestCtor1()
		{
			Assert.Throws<ArgumentNullException>(() =>
			{
				using (DelimitedRecordReader csv = new DelimitedRecordReader(null))
				{
				}
			});

			Assert.Throws<ArgumentOutOfRangeException>(() =>
			{
				using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1), 0))
				{
				}
			});

			Assert.Throws<ArgumentOutOfRangeException>(() =>
			{
				using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1), -1))
				{
				}
			});
		}
예제 #30
0
        public void UnicodeParsingTest3()
        {
            const string test = "München";

            byte[] buffer;

            using (MemoryStream stream = new MemoryStream())
            {
                using (TextWriter writer = new StreamWriter(stream, Encoding.Unicode))
                {
                    writer.Write(test);
                }

                buffer = stream.ToArray();
            }

            using (DelimitedRecordReader csv = new DelimitedRecordReader(new StreamReader(new MemoryStream(buffer), Encoding.Unicode, false)))
            {
                Assert.AreEqual(ReadResult.Success, csv.Read());
                Assert.AreEqual(test, csv[0]);
                Assert.AreEqual(ReadResult.EndOfFile, csv.Read());
            }
        }
예제 #31
0
        public void ArgumentTestCtor1()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                using (DelimitedRecordReader csv = new DelimitedRecordReader(null))
                {
                }
            });

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1), 0))
                {
                }
            });

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1), -1))
                {
                }
            });
        }
예제 #32
0
        public void MissingFieldQuotedTest4()
        {
            const string Data = "a,b,c,d\n1,1,1,1\n2,\"2\",\n\"3\",3,3,3";

            using (var csv = new DelimitedRecordReader(new StringReader(Data), 11))
            {
                csv.DynamicColumnCount = false;
                for (int i = 0; i < 4; i++)
                {
                    csv.Columns.Add(new DelimitedRecordColumn(csv.GetDefaultColumnName(i)));
                }

                var ex = Assert.Throws <MissingRecordFieldException>(
                    () =>
                {
                    while (csv.Read() == ReadResult.Success)
                    {
                    }
                });

                Assert.AreEqual(2, ex.CurrentRecordIndex);
                Assert.AreEqual(3, ex.CurrentColumnIndex);
            }
        }
예제 #33
0
		public void ArgumentTestIndexer()
		{
			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.Throws<ArgumentOutOfRangeException>(() =>
				{
					string s = csv[-1];
				});
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.Throws<ArgumentOutOfRangeException>(() =>
				{
					string s = csv[DelimitedRecordReaderTestData.SampleData1RecordCount];
				});
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.AreEqual(ReadResult.Success, csv.Read());

				Assert.Throws<ArgumentOutOfRangeException>(() =>
				{
					string s = csv[-1];
				});
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.AreEqual(ReadResult.Success, csv.Read());

				Assert.Throws<ArgumentOutOfRangeException>(() =>
				{
					string s = csv[DelimitedRecordReaderTestData.SampleData1RecordCount];
				});
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.Throws<InvalidOperationException>(() =>
				{
					string s = csv["asdf"];
				});
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.Throws<InvalidOperationException>(() =>
				{
					string s = csv[DelimitedRecordReaderTestData.SampleData1Header0];
				});
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());

				Assert.Throws<ArgumentNullException>(() =>
				{
					string s = csv[null];
				});
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());

				Assert.Throws<ArgumentNullException>(() =>
				{
					string s = csv[string.Empty];
				});
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());
				Assert.AreEqual(ReadResult.Success, csv.Read());

				Assert.Throws<ArgumentNullException>(() =>
				{
					string s = csv[null];
				});
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());
				Assert.AreEqual(ReadResult.Success, csv.Read());

				Assert.Throws<ArgumentNullException>(() =>
				{
					string s = csv[string.Empty];
				});
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());
				Assert.AreEqual(ReadResult.Success, csv.Read());

				Assert.Throws<ArgumentException>(() =>
				{
					string s = csv["asdf"];
				});
			}
		}
예제 #34
0
        public void ReadColumnHeadersTest_TypedColumns()
        {
            Triple <string, Type, object>[] supported = new Triple <string, Type, object>[] {
                new Triple <string, Type, object>("string", typeof(string), default(string)),
                new Triple <string, Type, object>("char", typeof(char), default(char)),
                new Triple <string, Type, object>("sbyte", typeof(sbyte), default(sbyte)),
                new Triple <string, Type, object>("byte", typeof(byte), default(byte)),
                new Triple <string, Type, object>("short", typeof(short), default(short)),
                new Triple <string, Type, object>("ushort", typeof(ushort), default(ushort)),
                new Triple <string, Type, object>("int", typeof(int), default(int)),
                new Triple <string, Type, object>("uint", typeof(uint), default(uint)),
                new Triple <string, Type, object>("long", typeof(long), default(long)),
                new Triple <string, Type, object>("ulong", typeof(ulong), default(ulong)),
                new Triple <string, Type, object>("float", typeof(float), default(float)),
                new Triple <string, Type, object>("double", typeof(double), default(double)),
                new Triple <string, Type, object>("decimal", typeof(decimal), default(decimal)),
                new Triple <string, Type, object>("DateTime", typeof(DateTime), default(DateTime)),
                new Triple <string, Type, object>("TimeSpan", typeof(TimeSpan), default(TimeSpan)),
                new Triple <string, Type, object>("String", typeof(String), default(String)),
                new Triple <string, Type, object>("Char", typeof(Char), default(Char)),
                new Triple <string, Type, object>("SByte", typeof(SByte), default(SByte)),
                new Triple <string, Type, object>("Byte", typeof(Byte), default(Byte)),
                new Triple <string, Type, object>("Int16", typeof(Int16), default(Int16)),
                new Triple <string, Type, object>("UInt16", typeof(UInt16), default(UInt16)),
                new Triple <string, Type, object>("Int32", typeof(Int32), default(Int32)),
                new Triple <string, Type, object>("UInt32", typeof(UInt32), default(UInt32)),
                new Triple <string, Type, object>("Int64", typeof(Int64), default(Int64)),
                new Triple <string, Type, object>("UInt64", typeof(UInt64), default(UInt64)),
                new Triple <string, Type, object>("Single", typeof(Single), default(Single)),
                new Triple <string, Type, object>("Double", typeof(Double), default(Double)),
                new Triple <string, Type, object>("Decimal", typeof(Decimal), default(Decimal))
            };

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < supported.Length; i++)
            {
                Triple <string, Type, object> entry = supported[i];

                sb.Append(entry.First);
                sb.Append(i);
                sb.Append(':');
                sb.Append(entry.First);

                if (i < supported.Length - 1)
                {
                    sb.Append(',');
                }
            }

            using (var csv = new DelimitedRecordReader(new StringReader(sb.ToString())))
            {
                Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());
                Assert.AreEqual(supported.Length, csv.Columns.Count);

                for (int i = 0; i < supported.Length; i++)
                {
                    Assert.AreEqual(supported[i].Second, csv.Columns[i].DataType);
                    Assert.AreEqual(supported[i].Third, csv.Columns[i].DefaultValue);
                }

                Assert.AreEqual(ReadResult.EndOfFile, csv.Read());
            }
        }
예제 #35
0
        public void ArgumentTestIndexer()
        {
            using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                Assert.Throws <ArgumentOutOfRangeException>(() =>
                {
                    string s = csv[-1];
                });
            }

            using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                Assert.Throws <ArgumentOutOfRangeException>(() =>
                {
                    string s = csv[DelimitedRecordReaderTestData.SampleData1RecordCount];
                });
            }

            using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                Assert.AreEqual(ReadResult.Success, csv.Read());

                Assert.Throws <ArgumentOutOfRangeException>(() =>
                {
                    string s = csv[-1];
                });
            }

            using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                Assert.AreEqual(ReadResult.Success, csv.Read());

                Assert.Throws <ArgumentOutOfRangeException>(() =>
                {
                    string s = csv[DelimitedRecordReaderTestData.SampleData1RecordCount];
                });
            }

            using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                Assert.Throws <InvalidOperationException>(() =>
                {
                    string s = csv["asdf"];
                });
            }

            using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                Assert.Throws <InvalidOperationException>(() =>
                {
                    string s = csv[DelimitedRecordReaderTestData.SampleData1Header0];
                });
            }

            using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());

                Assert.Throws <ArgumentNullException>(() =>
                {
                    string s = csv[null];
                });
            }

            using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());

                Assert.Throws <ArgumentNullException>(() =>
                {
                    string s = csv[string.Empty];
                });
            }

            using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());
                Assert.AreEqual(ReadResult.Success, csv.Read());

                Assert.Throws <ArgumentNullException>(() =>
                {
                    string s = csv[null];
                });
            }

            using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());
                Assert.AreEqual(ReadResult.Success, csv.Read());

                Assert.Throws <ArgumentNullException>(() =>
                {
                    string s = csv[string.Empty];
                });
            }

            using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
            {
                Assert.AreEqual(ReadResult.Success, csv.ReadColumnHeaders());
                Assert.AreEqual(ReadResult.Success, csv.Read());

                Assert.Throws <ArgumentException>(() =>
                {
                    string s = csv["asdf"];
                });
            }
        }
예제 #36
0
		public void ParsingTest(string data, int? bufferSize, ReaderOptions? options, char? delimiter, char? quote, char? comment, string expectedColumnHeaders, IEnumerable<string> expectedRecords)
		{
			using (var reader = new DelimitedRecordReader(new StringReader(data), bufferSize.HasValue ? bufferSize.Value : DelimitedRecordReader.DefaultBufferSize))
			{
				if (options != null)
				{
					reader.AdvancedEscapingEnabled = (options & ReaderOptions.AdvancedEscaping) != 0;
					reader.DoubleQuoteEscapingEnabled = (options & ReaderOptions.NoDoubleQuoteEscaping) == 0;
					reader.SkipEmptyLines = (options & ReaderOptions.NoSkipEmptyLines) == 0;
					reader.TrimWhiteSpaces = (options & ReaderOptions.NoTrim) == 0;
				}

				if (comment != null)
					reader.CommentCharacter = comment.Value;

				if (delimiter != null)
					reader.DelimiterCharacter = delimiter.Value;

				if (quote != null)
					reader.QuoteCharacter = quote.Value;

				string[] headers = null;

				if (!string.IsNullOrEmpty(expectedColumnHeaders))
				{
					reader.DynamicColumnCount = false;
					Assert.AreEqual(ReadResult.Success, reader.ReadColumnHeaders());

					headers = expectedColumnHeaders.Split('|');

					Assert.AreEqual(headers.Length, reader.Columns.Count);

					for (int i = 0; i < headers.Length; i++)
						Assert.AreEqual(headers[i], reader.Columns[i].Name);
				}

				foreach (var record in expectedRecords)
				{
					Assert.AreEqual(ReadResult.Success, reader.Read());

					string[] values = record.Split('|');

					if (headers != null)
						Assert.AreEqual(headers.Length, values.Length);

					Assert.AreEqual(values.Length, reader.Columns.Count);

					for (int columnIndex = 0; columnIndex < values.Length; columnIndex++)
						Assert.AreEqual(values[columnIndex], reader[columnIndex]);

					if (headers != null)
					{
						for (int columnIndex = 0; columnIndex < values.Length; columnIndex++)
							Assert.AreEqual(values[columnIndex], reader[headers[columnIndex]]);
					}
				}

				Assert.AreEqual(ReadResult.EndOfFile, reader.Read());
			}
		}
예제 #37
0
		public void ArgumentTestCopyCurrentRecordTo()
		{
			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.AreEqual(ReadResult.Success, csv.Read());
				Assert.Throws<ArgumentNullException>(() => csv.CopyCurrentRecordTo(null));
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.AreEqual(ReadResult.Success, csv.Read());
				Assert.Throws<ArgumentOutOfRangeException>(() => csv.CopyCurrentRecordTo(new string[1], -1));
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.AreEqual(ReadResult.Success, csv.Read());
				Assert.Throws<ArgumentOutOfRangeException>(() => csv.CopyCurrentRecordTo(new string[1], 1));
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.AreEqual(ReadResult.Success, csv.Read());
				Assert.Throws<ArgumentOutOfRangeException>(() => csv.CopyCurrentRecordTo(new string[DelimitedRecordReaderTestData.SampleData1RecordCount - 1], 0));
			}

			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.AreEqual(ReadResult.Success, csv.Read());
				Assert.Throws<ArgumentOutOfRangeException>(() => csv.CopyCurrentRecordTo(new string[DelimitedRecordReaderTestData.SampleData1RecordCount], 1));
			}
		}
예제 #38
0
        public void ParsingTest(string data, int?bufferSize, ReaderOptions?options, char?delimiter, char?quote, char?comment, string expectedColumnHeaders, IEnumerable <string> expectedRecords)
        {
            using (var reader = new DelimitedRecordReader(new StringReader(data), bufferSize.HasValue ? bufferSize.Value : DelimitedRecordReader.DefaultBufferSize))
            {
                if (options != null)
                {
                    reader.AdvancedEscapingEnabled    = (options & ReaderOptions.AdvancedEscaping) != 0;
                    reader.DoubleQuoteEscapingEnabled = (options & ReaderOptions.NoDoubleQuoteEscaping) == 0;
                    reader.SkipEmptyLines             = (options & ReaderOptions.NoSkipEmptyLines) == 0;
                    reader.TrimWhiteSpaces            = (options & ReaderOptions.NoTrim) == 0;
                }

                if (comment != null)
                {
                    reader.CommentCharacter = comment.Value;
                }

                if (delimiter != null)
                {
                    reader.DelimiterCharacter = delimiter.Value;
                }

                if (quote != null)
                {
                    reader.QuoteCharacter = quote.Value;
                }

                string[] headers = null;

                if (!string.IsNullOrEmpty(expectedColumnHeaders))
                {
                    reader.DynamicColumnCount = false;
                    Assert.AreEqual(ReadResult.Success, reader.ReadColumnHeaders());

                    headers = expectedColumnHeaders.Split('|');

                    Assert.AreEqual(headers.Length, reader.Columns.Count);

                    for (int i = 0; i < headers.Length; i++)
                    {
                        Assert.AreEqual(headers[i], reader.Columns[i].Name);
                    }
                }

                foreach (var record in expectedRecords)
                {
                    Assert.AreEqual(ReadResult.Success, reader.Read());

                    string[] values = record.Split('|');

                    if (headers != null)
                    {
                        Assert.AreEqual(headers.Length, values.Length);
                    }

                    Assert.AreEqual(values.Length, reader.Columns.Count);

                    for (int columnIndex = 0; columnIndex < values.Length; columnIndex++)
                    {
                        Assert.AreEqual(values[columnIndex], reader[columnIndex]);
                    }

                    if (headers != null)
                    {
                        for (int columnIndex = 0; columnIndex < values.Length; columnIndex++)
                        {
                            Assert.AreEqual(values[columnIndex], reader[headers[columnIndex]]);
                        }
                    }
                }

                Assert.AreEqual(ReadResult.EndOfFile, reader.Read());
            }
        }
예제 #39
0
        private void CheckMissingFieldUnquoted(long recordCount, int columnCount, long badRecordIndex, int badColumnIndex, int bufferSize, MissingRecordFieldAction action)
        {
            // construct the csv data with template "00,01,02\n10,11,12\n...." and calculate expected error position

            long capacity = recordCount * (columnCount * 2 + columnCount - 1) + recordCount;

            Assert.IsTrue(capacity <= int.MaxValue);

            var sb = new StringBuilder((int)capacity);

            for (long i = 0; i < recordCount; i++)
            {
                int readColumnCount;

                if (i == badRecordIndex)
                {
                    readColumnCount = badColumnIndex;
                }
                else
                {
                    readColumnCount = columnCount;
                }

                for (int j = 0; j < readColumnCount; j++)
                {
                    sb.Append(i);
                    sb.Append(j);
                    sb.Append(DelimitedRecordReader.DefaultDelimiterCharacter);
                }

                sb.Length--;
                sb.Append('\n');
            }

            // test csv

            const string AssertMessage = "RecordIndex={0}; ColumnIndex={1}; Position={2}; Action={3}";

            using (var csv = new DelimitedRecordReader(new StringReader(sb.ToString()), bufferSize))
            {
                csv.DynamicColumnCount = false;
                for (int i = 0; i < columnCount; i++)
                {
                    csv.Columns.Add(new DelimitedRecordColumn(csv.GetDefaultColumnName(i)));
                }

                csv.MissingFieldAction = action;
                Assert.AreEqual(columnCount, csv.Columns.Count);

                try
                {
                    while (csv.Read() == ReadResult.Success)
                    {
                        Assert.AreEqual(columnCount, csv.Columns.Count);

                        for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
                        {
                            string s = csv[columnIndex];

                            if (csv.CurrentRecordIndex != badRecordIndex || columnIndex < badColumnIndex)
                            {
                                Assert.AreEqual(csv.CurrentRecordIndex.ToString() + columnIndex.ToString(), s);                                //, AssertMessage, csv.CurrentRecordIndex, columnIndex, -1, action);
                            }
                            else
                            {
                                switch (action)
                                {
                                case MissingRecordFieldAction.ReturnEmptyValue:
                                    Assert.AreEqual(string.Empty, s);                                            //, AssertMessage, csv.CurrentRecordIndex, columnIndex, -1, action);
                                    break;

                                case MissingRecordFieldAction.ReturnNullValue:
                                    Assert.IsNull(s);                                            //, AssertMessage, csv.CurrentRecordIndex, columnIndex, -1, action);
                                    break;

                                case MissingRecordFieldAction.HandleAsParseError:
                                    throw new AssertionException(string.Format("Failed to throw HandleAsParseError. - " + AssertMessage, csv.CurrentRecordIndex, columnIndex, -1, action));

                                default:
                                    throw new AssertionException(string.Format("'{0}' is not handled by this test.", action));
                                }
                            }
                        }
                    }
                }
                catch (MissingRecordFieldException ex)
                {
                    Assert.AreEqual(badRecordIndex, ex.CurrentRecordIndex, AssertMessage, ex.CurrentRecordIndex, ex.CurrentColumnIndex, ex.BufferPosition, action);
                    Assert.IsTrue(ex.CurrentColumnIndex >= badColumnIndex, AssertMessage, ex.CurrentRecordIndex, ex.CurrentColumnIndex, ex.BufferPosition, action);
                }
            }
        }
예제 #40
0
		public void ArgumentTestMoveTo()
		{
			using (DelimitedRecordReader csv = new DelimitedRecordReader(new StringReader(DelimitedRecordReaderTestData.SampleData1)))
			{
				Assert.Throws<ArgumentOutOfRangeException>(() => csv.MoveTo(-1));
			}
		}