Ejemplo n.º 1
0
        public void TestGuesser_IntThenLong()
        {
            Guesser t = new Guesser();

            //we see an int
            t.AdjustToCompensateForValue("-100");

            //we guess the column contains ints
            Assert.AreEqual(typeof(int), t.Guess.CSharpType);
            Assert.AreEqual(4, t.Guess.Width);
            Assert.AreEqual(3, t.Guess.Size.NumbersBeforeDecimalPlace);
            Assert.AreEqual(0, t.Guess.Size.NumbersAfterDecimalPlace);

            //we see a long
            t.AdjustToCompensateForValue("9223372036854775807");

            //we change our estimate to the compatible estimate of 'decimal'
            Assert.AreEqual(typeof(decimal), t.Guess.CSharpType);
            Assert.AreEqual(19, t.Guess.Width);
            Assert.AreEqual(19, t.Guess.Size.NumbersBeforeDecimalPlace);
            Assert.AreEqual(0, t.Guess.Size.NumbersAfterDecimalPlace);

            //final estimate is decimal
            Assert.AreEqual(typeof(decimal), t.Guess.CSharpType);
        }
Ejemplo n.º 2
0
        public void ExampleUsage()
        {
            var guesser = new Guesser();

            guesser.AdjustToCompensateForValue("-12.211");
            var guess = guesser.Guess;

            Assert.AreEqual(typeof(decimal), guess.CSharpType);
            Assert.AreEqual(2, guess.Size.NumbersBeforeDecimalPlace);
            Assert.AreEqual(3, guess.Size.NumbersAfterDecimalPlace);
            Assert.AreEqual(7, guess.Width);


            guesser = new Guesser();
            guesser.AdjustToCompensateForValue("1,000");
            guesser.AdjustToCompensateForValue("0.001");
            guess = guesser.Guess;

            Assert.AreEqual(typeof(decimal), guess.CSharpType);
            Assert.AreEqual(4, guess.Size.NumbersBeforeDecimalPlace);
            Assert.AreEqual(3, guess.Size.NumbersAfterDecimalPlace);
            Assert.AreEqual(8, guess.Width);//?


            var someStrings = new [] { "13:11:59", "9AM" };

            guesser = new Guesser();
            guesser.AdjustToCompensateForValues(someStrings);

            var parsed = someStrings.Select(guesser.Parse).ToArray();

            Assert.AreEqual(new TimeSpan(13, 11, 59), parsed[0]);
            Assert.AreEqual(new TimeSpan(9, 0, 0), parsed[1]);
        }
Ejemplo n.º 3
0
        public void TestGuesser_Bool(bool sendStringEquiv)
        {
            Guesser t = new Guesser();

            if (sendStringEquiv)
            {
                t.AdjustToCompensateForValue("True");
            }
            else
            {
                t.AdjustToCompensateForValue(true);
            }

            if (sendStringEquiv)
            {
                t.AdjustToCompensateForValue("False");
            }
            else
            {
                t.AdjustToCompensateForValue(false);
            }

            Assert.AreEqual(typeof(bool), t.Guess.CSharpType);

            t.AdjustToCompensateForValue(null);

            Assert.AreEqual(typeof(bool), t.Guess.CSharpType);

            Assert.AreEqual(0, t.Guess.Size.NumbersAfterDecimalPlace);
            Assert.AreEqual(0, t.Guess.Size.NumbersBeforeDecimalPlace);
        }
Ejemplo n.º 4
0
        public void TestGuesser_IntToDateTime()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue("2013");
            t.AdjustToCompensateForValue("01/01/2001");
            Assert.AreEqual(typeof(string), t.Guess.CSharpType);
        }
Ejemplo n.º 5
0
        public void TestGuesser_DateTime()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue("01/01/2001");
            t.AdjustToCompensateForValue(null);

            Assert.AreEqual(typeof(DateTime), t.Guess.CSharpType);
        }
Ejemplo n.º 6
0
        public void TestGuesser_MixedIntTypes()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue((Int16)5);
            var ex = Assert.Throws <MixedTypingException>(() => t.AdjustToCompensateForValue((Int32)1000));

            StringAssert.Contains("We were adjusting to compensate for object '1000' which is of Type 'System.Int32', we were previously passed a 'System.Int16' type", ex.Message);
        }
Ejemplo n.º 7
0
        public void TestGuesser_DateTime_EnglishWithTimeAndAM()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue(GetCultureSpecificDate() + " 11:10AM");
            t.AdjustToCompensateForValue(null);

            Assert.AreEqual(typeof(DateTime), t.Guess.CSharpType);
        }
Ejemplo n.º 8
0
        public void TestGuesser_FallbackOntoVarcharFromFloat()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue("15.5");
            t.AdjustToCompensateForValue("F");

            Assert.AreEqual(typeof(string), t.Guess.CSharpType);
            Assert.AreEqual("varchar(4)", t.GetSqlDBType(_translater));
        }
Ejemplo n.º 9
0
        public void TestGuesser_Negatives()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue("-1");
            t.AdjustToCompensateForValue("-99.99");

            Assert.AreEqual(t.Guess.CSharpType, typeof(decimal));
            Assert.AreEqual("decimal(4,2)", t.GetSqlDBType(_translater));
        }
Ejemplo n.º 10
0
        public void TestGuesser_DateTime()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue("01/01/2001");
            t.AdjustToCompensateForValue(null);

            Assert.AreEqual(t.Guess.CSharpType, typeof(DateTime));
            Assert.AreEqual("datetime2", t.GetSqlDBType(_translater));
        }
Ejemplo n.º 11
0
        public void TestGuesser_DateTime_EnglishWithTimeAndAM()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue(GetCultureSpecificDate() + " 11:10AM");
            t.AdjustToCompensateForValue(null);

            Assert.AreEqual(t.Guess.CSharpType, typeof(DateTime));
            Assert.AreEqual("datetime2", t.GetSqlDBType(_translater));
        }
Ejemplo n.º 12
0
        public void TestGuesser_FallbackOntoVarcharFromFloat()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue("15.5");
            t.AdjustToCompensateForValue("F");

            Assert.AreEqual(typeof(string), t.Guess.CSharpType);
            Assert.AreEqual(4, t.Guess.Width);
        }
Ejemplo n.º 13
0
        public void TestGuesser_MixedDateAndTime_FallbackToString()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue("09:01");
            Assert.AreEqual(typeof(TimeSpan), t.Guess.CSharpType);

            t.AdjustToCompensateForValue("2001-12-29 23:01");
            Assert.AreEqual(typeof(string), t.Guess.CSharpType);
            Assert.AreEqual("varchar(16)", t.GetSqlDBType(_translater));
        }
Ejemplo n.º 14
0
        public void TestGuesser_FallbackCompatible(string input1, Type expectedTypeAfterFirstInput, string input2, Type expectedTypeAfterSecondInput)
        {
            var t = new Guesser();

            t.AdjustToCompensateForValue(input1);

            Assert.AreEqual(expectedTypeAfterFirstInput, t.Guess.CSharpType);

            t.AdjustToCompensateForValue(input2);
            Assert.AreEqual(expectedTypeAfterSecondInput, t.Guess.CSharpType);
        }
Ejemplo n.º 15
0
        public void TestGuesser_decimal()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue("1.5");
            t.AdjustToCompensateForValue("299.99");
            t.AdjustToCompensateForValue(null);
            t.AdjustToCompensateForValue(DBNull.Value);

            Assert.AreEqual(typeof(decimal), t.Guess.CSharpType);
        }
Ejemplo n.º 16
0
        public void TestGuesser_MixedDateAndTime_FallbackToString()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue("09:01");
            Assert.AreEqual(typeof(TimeSpan), t.Guess.CSharpType);

            t.AdjustToCompensateForValue("2001-12-29 23:01");
            Assert.AreEqual(typeof(string), t.Guess.CSharpType);
            Assert.AreEqual(16, t.Guess.Width);
        }
Ejemplo n.º 17
0
        public void TestGuesser_Negatives()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue("-1");
            t.AdjustToCompensateForValue("-99.99");

            Assert.AreEqual(typeof(decimal), t.Guess.CSharpType);
            Assert.AreEqual(4, t.Guess.Size.Precision);
            Assert.AreEqual(2, t.Guess.Size.Scale);
        }
Ejemplo n.º 18
0
        public void TestGuesser_PreeceedingZeroesAfterFloat()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue("1.5");
            t.AdjustToCompensateForValue("00299.99");
            t.AdjustToCompensateForValue(null);
            t.AdjustToCompensateForValue(DBNull.Value);

            Assert.AreEqual(typeof(string), t.Guess.CSharpType);
        }
Ejemplo n.º 19
0
        public void TestGuesser_HardTypeFloats()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue(1.1f);
            t.AdjustToCompensateForValue(100.01f);
            t.AdjustToCompensateForValue(10000f);

            Assert.AreEqual(typeof(float), t.Guess.CSharpType);
            Assert.AreEqual(2, t.Guess.Size.NumbersAfterDecimalPlace);
            Assert.AreEqual(5, t.Guess.Size.NumbersBeforeDecimalPlace);
        }
Ejemplo n.º 20
0
        public void TestGuesser_IntAnddecimal_MustUsedecimal()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue("15");
            t.AdjustToCompensateForValue("29.9");
            t.AdjustToCompensateForValue("200");
            t.AdjustToCompensateForValue(null);
            t.AdjustToCompensateForValue(DBNull.Value);

            Assert.AreEqual(typeof(decimal), t.Guess.CSharpType);
        }
Ejemplo n.º 21
0
        public void TestGuesser_DateTime_EnglishWithTime()
        {
            Guesser t = new Guesser();

            Console.WriteLine(CultureInfo.CurrentCulture.EnglishName);
            Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.MonthDayPattern);

            t.AdjustToCompensateForValue(GetCultureSpecificDate() + " 11:10");
            t.AdjustToCompensateForValue(null);

            Assert.AreEqual(typeof(DateTime), t.Guess.CSharpType);
        }
Ejemplo n.º 22
0
        public void TestGuesser_MixingTypes_ThrowsException(object o1, object o2)
        {
            //if we pass an hard type...
            //...then we don't accept strings anymore

            Guesser t = new Guesser();

            t.AdjustToCompensateForValue(o1);

            var ex = Assert.Throws <MixedTypingException>(() => t.AdjustToCompensateForValue(o2));

            StringAssert.Contains("mixed with untyped objects", ex.Message);
        }
Ejemplo n.º 23
0
        public void TestGuesser_HardTypeInts()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue(1);
            t.AdjustToCompensateForValue(100);
            t.AdjustToCompensateForValue(null);
            t.AdjustToCompensateForValue(10000);
            t.AdjustToCompensateForValue(DBNull.Value);

            Assert.AreEqual(typeof(int), t.Guess.CSharpType);
            Assert.AreEqual(0, t.Guess.Size.NumbersAfterDecimalPlace);
            Assert.AreEqual(5, t.Guess.Size.NumbersBeforeDecimalPlace);
        }
Ejemplo n.º 24
0
        public void TestGuesser_FallbackOntoStringLength(string legitType, Type expectedLegitType, string str, int expectedLength)
        {
            Guesser t = new Guesser();

            //give it the legit hard typed value e.g. a date
            t.AdjustToCompensateForValue(legitType);
            Assert.AreEqual(expectedLegitType, t.Guess.CSharpType);

            //then give it a string
            t.AdjustToCompensateForValue(str);
            Assert.AreEqual(typeof(string), t.Guess.CSharpType);

            //the length should be the max of the length of the legit string and the string str
            Assert.AreEqual(expectedLength, t.Guess.Width);
        }
Ejemplo n.º 25
0
        public void TestGuesser_Int16s()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue((Int16)5);
            t.AdjustToCompensateForValue((Int16)10);
            t.AdjustToCompensateForValue((Int16)15);
            t.AdjustToCompensateForValue((Int16)30);
            t.AdjustToCompensateForValue((Int16)200);

            Assert.AreEqual(typeof(Int16), t.Guess.CSharpType);

            Assert.AreEqual(3, t.Guess.Size.NumbersBeforeDecimalPlace);
            Assert.AreEqual(0, t.Guess.Size.NumbersAfterDecimalPlace);
        }
Ejemplo n.º 26
0
        public void TestGuesser_RandomCrud(string randomCrud)
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue(randomCrud);
            Assert.AreEqual(typeof(string), t.Guess.CSharpType);
        }
Ejemplo n.º 27
0
        public void TestGuesser_ValidDateStrings(string wierdDateString)
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue(wierdDateString);
            Assert.AreEqual(typeof(DateTime), t.Guess.CSharpType);
        }
Ejemplo n.º 28
0
        public void TestGuesser_IntFloatString()
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue("-1000");
            Assert.AreEqual(typeof(int), t.Guess.CSharpType);

            t.AdjustToCompensateForValue("1.1");
            Assert.AreEqual(typeof(decimal), t.Guess.CSharpType);
            Assert.AreEqual(5, t.Guess.Size.Precision);
            Assert.AreEqual(1, t.Guess.Size.Scale);

            t.AdjustToCompensateForValue("A");
            Assert.AreEqual(typeof(string), t.Guess.CSharpType);
            Assert.AreEqual(6, t.Guess.Width);
        }
Ejemplo n.º 29
0
        [TestCase("5,000", typeof(int), "de-de", 5, 1, 0, 5)]                  //germans swap commas and dots

        public void Test_OneString_IsType(string guessFor, Type expectedGuess, string culture, int expectedStringLength, int expectedBefore, int expectedAfter, object expectedParseValue)
        {
            var cultureInfo = new CultureInfo(culture);
            var guesser     = new Guesser()
            {
                Culture = cultureInfo
            };

            guesser.AdjustToCompensateForValue(guessFor);
            Assert.AreEqual(expectedGuess, guesser.Guess.CSharpType, "Guessed Type did not match");
            Assert.AreEqual(expectedStringLength, guesser.Guess.Width, "String length guessed didn't match");
            Assert.AreEqual(expectedBefore, guesser.Guess.Size.NumbersBeforeDecimalPlace, "BeforeDecimalPlace didn't match");
            Assert.AreEqual(expectedAfter, guesser.Guess.Size.NumbersAfterDecimalPlace, "AfterDecimalPlace didn't match");


            TypeDeciderFactory factory = new TypeDeciderFactory(cultureInfo);

            if (factory.IsSupported(guesser.Guess.CSharpType))
            {
                Assert.AreEqual(expectedParseValue, factory.Create(guesser.Guess.CSharpType).Parse(guessFor));
            }
            else
            {
                Assert.AreEqual(expectedParseValue, guessFor);
            }
        }
Ejemplo n.º 30
0
        public void TestGuesser_DateTime_DodgyFormats(string input, Type expectedOutput)
        {
            Guesser t = new Guesser();

            t.AdjustToCompensateForValue(input);
            Assert.AreEqual(expectedOutput, t.Guess.CSharpType);
        }