private ICsvRowActivator <T> CreateSut <T>(string[] headerrow)
        {
            ICsvRowActivatorFactoryFactory <T> csvRowActivatorFactoryFactory
                = new DynamicCsvRowActivatorFactoryFactory <T>();
            IColumnNameMapper           columnNameMapper  = new DefaultColumnNameMapper();
            IConverterProvider          converterProvider = new DefaultConverterProvider();
            ICsvRowActivatorFactory <T> csvRowActivatorFactory
                = csvRowActivatorFactoryFactory.Create(converterProvider, columnNameMapper);
            ICsvRowActivator <T> activator = csvRowActivatorFactory.Create(headerrow);

            return(activator);
        }
예제 #2
0
        public void PerformanceTestInitializations()
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            // Arrange
            string[] headerrow = { "ClientId", "BirthDate", "Weight", "ReportName" };
            ICsvRowActivator <J139Report> workload = CreateSut <J139Report>(headerrow);

            TimeSpan endTime = stopwatch.Elapsed;

            Console.Write("Initialization took " + endTime.Milliseconds + "ms");
        }
        public void PullsEmptyStringForEmpty()
        {
            // Arrange
            string[] headerrow = { "ReportName" };
            ICsvRowActivator <J139Report> workload = CreateSut <J139Report>(headerrow);

            string[] valuerow = { "" };

            // Act
            J139Report report = workload.CreateFromRow(valuerow);

            // Assert
            Assert.AreEqual("", report.ReportName);
        }
        public void EmptyGoesToNullForNullableFields()
        {
            // Arrange
            string[] headerrow = { "ClientId", "Weight", "BirthDate" };
            ICsvRowActivator <J139ReportWithNullables> workload = CreateSut <J139ReportWithNullables>(headerrow);

            string[] valuerow = { "", "", "" };

            // Act
            J139ReportWithNullables report = workload.CreateFromRow(valuerow);

            // Assert
            Assert.AreEqual(null, report.ClientId);
            Assert.AreEqual(null, report.Weight);
            Assert.AreEqual(null, report.BirthDate);
        }
        public void NullPropertiesForMissingFields()
        {
            // Arrange
            string[] headerrow = { "A", "B", "C" };
            ICsvRowActivator <J139ReportWithNullables> workload = CreateSut <J139ReportWithNullables>(headerrow);

            string[] valuerow = { "1", "3", "5/3/2013" };

            // Act
            J139ReportWithNullables report = workload.CreateFromRow(valuerow);

            // Assert
            Assert.AreEqual(null, report.ClientId);
            Assert.AreEqual(null, report.Weight);
            Assert.AreEqual(null, report.BirthDate);
        }
        public void CanJustPullSingleField()
        {
            // Arrange
            string[] headerrow = { "Weight" };
            ICsvRowActivator <J139Report> workload = CreateSut <J139Report>(headerrow);

            string[] valuerow = { "3.14" };

            // Act
            J139Report report = workload.CreateFromRow(valuerow);

            // Assert
            Assert.AreEqual(0, report.ClientId);
            Assert.AreEqual(DateTime.MinValue, report.BirthDate);
            Assert.AreEqual(3.14, report.Weight);
            Assert.AreEqual(null, report.ReportName);
        }
        public void CanReorderFields()
        {
            // Arrange
            string[] headerrow = { "ReportName", "Weight", "BirthDate", "ClientId" };
            ICsvRowActivator <J139Report> workload = CreateSut <J139Report>(headerrow);

            string[] valuerow = { "J139", "3.14", "5/3/2013", "156" };

            // Act
            J139Report report = workload.CreateFromRow(valuerow);

            // Assert
            Assert.AreEqual(156, report.ClientId);
            Assert.AreEqual(new DateTime(2013, 5, 3), report.BirthDate);
            Assert.AreEqual(3.14, report.Weight);
            Assert.AreEqual("J139", report.ReportName);
        }
예제 #8
0
        public IEnumerable <T> ReadAll()
        {
            // Create an activator factory
            ICsvRowActivatorFactory <T> activatorFactory = activatorFactoryFactory.Create(
                new DefaultConverterProvider(),
                new DefaultColumnNameMapper()
                );

            // Create a tokenizer that reads from the csvStream
            CsvRowTokenizer tokenizer = new CsvRowTokenizer(csvStream);

            // Start streaming data from the tokenizer
            IEnumerable <string[]> rows = tokenizer.ReadAllRows();

            // Grab the first line (the header)
            IEnumerator <string[]> enumerator = rows.GetEnumerator();

            if (!enumerator.MoveNext())
            {
                throw new InvalidDataException("No header found!");
            }
            string[] headerrow   = enumerator.Current;
            int      columnCount = headerrow.Length;

            // Start streaming tokenized data into the activator
            ICsvRowActivator <T> activator = activatorFactory.Create(headerrow);

            // Stream out all of the CSV rows as objects
            while (enumerator.MoveNext())
            {
                if (enumerator.Current.Length != columnCount)
                {
                    throw new InvalidDataException(
                              string.Format("Field count mismatch: a row had {0} field(s), but header had {1}!", enumerator.Current.Length, columnCount)
                              );
                }
                yield return(activator.CreateFromRow(enumerator.Current));
            }
        }
예제 #9
0
        public void PerformanceTestBulk()
        {
            // Arrange
            string[] headerrow = { "ClientId", "BirthDate", "Weight", "ReportName" };
            ICsvRowActivator <J139Report> workload = CreateSut <J139Report>(headerrow);

            string[] valuerow = { "156", "5/3/2013", "3.14", "J139" };

            // Act

            int max = 1000000;

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int i = 0; i < max; i++)
            {
                J139Report report = workload.CreateFromRow(valuerow);
            }
            TimeSpan endTime = stopwatch.Elapsed;

            Console.Write(max + " runs took " + endTime.Milliseconds + "ms");
        }