public void UnknownTypeShouldThrowAnException()
        {
            var    testSubject = new DelimitedColumnFactory();
            Action test        = () => testSubject.Create("field:_unknown_type_");

            test.ShouldThrow <ArgumentException>();
        }
        public void ParseKnownPrimitiveTypeNames()
        {
            var testSubject = new DelimitedColumnFactory();

            testSubject.Create("field1:bool").Type.Should().Be(typeof(bool));
            testSubject.Create("field1:bool?").Type.Should().Be(typeof(bool?));
            testSubject.Create("field1:byte").Type.Should().Be(typeof(byte));
            testSubject.Create("field1:byte?").Type.Should().Be(typeof(byte?));
            testSubject.Create("field1:char").Type.Should().Be(typeof(char));
            testSubject.Create("field1:char?").Type.Should().Be(typeof(char?));
            testSubject.Create("field1:DateTime").Type.Should().Be(typeof(DateTime));
            testSubject.Create("field1:DateTime?").Type.Should().Be(typeof(DateTime?));
            testSubject.Create("field1:DateTimeOffset").Type.Should().Be(typeof(DateTimeOffset));
            testSubject.Create("field1:DateTimeOffset?").Type.Should().Be(typeof(DateTimeOffset?));
            testSubject.Create("field1:decimal").Type.Should().Be(typeof(decimal));
            testSubject.Create("field1:decimal?").Type.Should().Be(typeof(decimal?));
            testSubject.Create("field1:double").Type.Should().Be(typeof(double));
            testSubject.Create("field1:double?").Type.Should().Be(typeof(double?));
            testSubject.Create("field1:float").Type.Should().Be(typeof(float));
            testSubject.Create("field1:float?").Type.Should().Be(typeof(float?));
            testSubject.Create("field1:Guid").Type.Should().Be(typeof(Guid));
            testSubject.Create("field1:Guid?").Type.Should().Be(typeof(Guid?));
            testSubject.Create("field1:short").Type.Should().Be(typeof(short));
            testSubject.Create("field1:short?").Type.Should().Be(typeof(short?));
            testSubject.Create("field1:int").Type.Should().Be(typeof(int));
            testSubject.Create("field1:int?").Type.Should().Be(typeof(int?));
            testSubject.Create("field1:long").Type.Should().Be(typeof(long));
            testSubject.Create("field1:long?").Type.Should().Be(typeof(long?));
            testSubject.Create("field1:string").Type.Should().Be(typeof(string));
            testSubject.Create("field1:timespan").Type.Should().Be(typeof(TimeSpan));
            testSubject.Create("field1:timespan?").Type.Should().Be(typeof(TimeSpan?));
        }
        public void FactoryEnumerableShouldDefaultToString()
        {
            var testSubject = new DelimitedColumnFactory();

            testSubject.Create(Enumerable.Repeat("field", 10))
            .All(x => x.Type == typeof(string))
            .Should()
            .BeTrue();
        }
Example #4
0
        private DelimitedParserSettings GetDelimitedParserSettings(
            IEnumerable <string> delimitedColumnStrings,
            char delimiter)
        {
            var factory = new DelimitedColumnFactory();
            var columns = factory.Create(delimitedColumnStrings);

            var settings = new DelimitedParserSettings()
            {
                DelimitedHeader = new DelimitedHeader(columns),
                Delimiter       = delimiter,
            };

            return(settings);
        }
Example #5
0
        public void DelimitedHeaderWithMultipleFields()
        {
            var factory     = new DelimitedColumnFactory();
            var testSubject = new DelimitedHeader(
                new[] { "field1:int", "field2:bool", "field3" }.Select(factory.Create));

            Assert.Equal(typeof(int), testSubject.DelimitedColumns[0].Type);
            Assert.Equal(typeof(bool), testSubject.DelimitedColumns[1].Type);
            Assert.Equal(typeof(string), testSubject.DelimitedColumns[2].Type);

            Assert.Equal("field1", testSubject[0]);
            Assert.Equal("field2", testSubject[1]);
            Assert.Equal("field3", testSubject[2]);

            Assert.Equal(0, testSubject["field1"]);
            Assert.Equal(1, testSubject["field2"]);
            Assert.Equal(2, testSubject["field3"]);

            Assert.Equal(3, testSubject.DelimitedColumns.Length);
        }
        public void FactoryShouldDefaultToString()
        {
            var testSubject = new DelimitedColumnFactory();

            testSubject.Create("field").Type.Should().Be(typeof(string));
        }
Example #7
0
        public void SqlBulkCopy_DelimitedParser()
        {
            string path = Path.Combine(
                Directory.GetCurrentDirectory(),
                Path.GetRandomFileName());

            path = Path.ChangeExtension(path, "mdf");

            // Use LocalDB as the temporary database for the test.  Substitute with a
            // connection string more appropriate to your environment.
            string connectionString = $@"Server=(localdb)\MSSQLLocalDB;AttachDbFilename={path};Trusted_Connection=True";

            // Build up some fake Cosmos output.
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("1,http://support.microsoft.com/kb/100,100,0.01,0.25,0.5,0.75,2.1,5.01");
            sb.AppendLine("1,http://support.microsoft.com/kb/101,500,0.01,0.25,0.5,0.75,2.1,5.01");
            sb.AppendLine("1,http://support.microsoft.com/kb/200,700,0.01,0.25,0.5,0.75,2.1,5.01");
            sb.AppendLine("1,http://support.microsoft.com/kb/321,902,0.01,0.25,0.5,0.75,2.1,5.01");
            sb.AppendLine("1,http://support.microsoft.com/kb/732,199,0.01,0.25,0.5,0.75,2.1,5.01");
            sb.AppendLine("1,http://support.microsoft.com/kb/376,112,0.01,0.25,0.5,0.75,2.1,5.01");
            sb.AppendLine("1,http://support.microsoft.com/kb/546,414,0.01,0.25,0.5,0.75,2.1,5.01");

            // Read in the fake Cosmos output.  Instead of using this stream, why not use
            //  -> new CosmosStream("http://cosmos05.osdinfra.net:88/cosmos/0365exp.adhoc/my/stream.csv");

            var factory         = new DelimitedColumnFactory();
            var delimitedHeader = new DelimitedHeader(
                factory.Create(new[] { "ID:long", "Uri", "Count:long", "Min:double", "P25:double", "P50:double", "P75:double", "P99:double", "Max:double" }));

            var settings = new DelimitedParserSettings();

            settings.DelimitedHeader = delimitedHeader;
            settings.Delimiter       = ',';

            var parser = DelimitedParser.Create(settings, sb.ToStream());

            // Ensure the database is created before bulk loading the data.
            using (var context = new Context(connectionString))
            {
                Assert.False(context.WebMetrics.Any());
            }

            // Bulk load the data.
            using (var transactionScope = new TransactionScope(
                       TransactionScopeOption.Required,
                       new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted
            }))
                using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(connectionString))
                {
                    sqlBulkCopy.DestinationTableName = "WebMetrics";
                    sqlBulkCopy.BatchSize            = 10000;

                    sqlBulkCopy.WriteToServer(parser);
                    transactionScope.Complete();
                }

            // Read data back out of the DB...party on it.
            using (var context = new Context(connectionString))
            {
                Assert.Equal(7, context.WebMetrics.Count());

                // There are seven unique IDs.
                var set = new HashSet <long>(context.WebMetrics.Select(x => x.ID));
                Assert.Equal(7, set.Count);

                Assert.True(context.WebMetrics.All(x => x.Min == 0.01));
                Assert.True(context.WebMetrics.All(x => x.Max == 5.01));
            }
        }