예제 #1
0
            public void GlobalConvertAll_WillTransformSingleIntegerWhenTransformerUsed()
            {
                var rdr = new Mock <IDataReader>();

                rdr.Setup(r => r.GetFieldType(0)).Returns(typeof(double));
                rdr.Setup(r => r.GetValue(0)).Returns(42.0);
                rdr.SetupSequence(r => r.Read())
                .Returns(true)
                .Returns(false);

                var xf = new Mock <IDataTransformer>();

                xf.Setup(x => x.CanTransform(42.0, typeof(IntEnum), false, It.IsAny <IEnumerable <Attribute> >()))
                .Returns(true);
                xf.Setup(x => x.Transform(42.0, typeof(IntEnum), false, It.IsAny <IEnumerable <Attribute> >()))
                .Returns(2f);

                using (GlobalSettings.UseTestInstance())
                {
                    StoredProcedure.EnableConvertOnAllNumericValues();
                    var toTest = new EnumRowFactory <IntEnum>();

                    toTest.ParseRows(rdr.Object, new[] { xf.Object }, CancellationToken.None)
                    .Single().Should().Be(IntEnum.Two);
                }
            }
예제 #2
0
            public void ReturnsEnumViaValue()
            {
                var rdr = new Mock <IDataReader>();

                rdr.Setup(r => r.GetFieldType(0)).Returns(typeof(int));
                rdr.Setup(r => r.GetInt32(0)).Returns(1);
                rdr.SetupSequence(r => r.Read())
                .Returns(true)
                .Returns(false);

                var toTest = new EnumRowFactory <IntEnum>();

                toTest.ParseRows(rdr.Object, Enumerable.Empty <IDataTransformer>(), CancellationToken.None)
                .Single().Should().Be(IntEnum.One);
            }
예제 #3
0
            public void ParsesMultipleFlagsViaName()
            {
                var rdr = new Mock <IDataReader>();

                rdr.Setup(r => r.GetFieldType(0)).Returns(typeof(string));
                rdr.Setup(r => r.GetString(0)).Returns("Red, Blue");
                rdr.SetupSequence(r => r.Read())
                .Returns(true)
                .Returns(false);

                var toTest = new EnumRowFactory <FlagsEnum>();

                toTest.ParseRows(rdr.Object, Enumerable.Empty <IDataTransformer>(), CancellationToken.None)
                .Single().Should().Be(FlagsEnum.Red | FlagsEnum.Blue);
            }
예제 #4
0
            public async Task ReturnsEnumViaName()
            {
                var rdr = new Mock <DbDataReader>();

                rdr.Setup(r => r.GetFieldType(0)).Returns(typeof(string));
                rdr.Setup(r => r.GetString(0)).Returns("One");
                rdr.SetupSequence(r => r.ReadAsync(It.IsAny <CancellationToken>()))
                .ReturnsAsync(true)
                .ReturnsAsync(false);

                var toTest = new EnumRowFactory <IntEnum>();

                var res = await toTest.ParseRowsAsync(rdr.Object, Enumerable.Empty <IDataTransformer>(), CancellationToken.None);

                res.Single().Should().Be(IntEnum.One);
            }
예제 #5
0
            public void GlobalConvertAll_WillTransformSingleInteger()
            {
                var rdr = new Mock <IDataReader>();

                rdr.Setup(r => r.GetFieldType(0)).Returns(typeof(float));
                rdr.Setup(r => r.GetFloat(0)).Returns(1f);
                rdr.SetupSequence(r => r.Read())
                .Returns(true)
                .Returns(false);

                using (GlobalSettings.UseTestInstance())
                {
                    StoredProcedure.EnableConvertOnAllNumericValues();
                    var toTest = new EnumRowFactory <IntEnum>();

                    toTest.ParseRows(rdr.Object, Enumerable.Empty <IDataTransformer>(), CancellationToken.None)
                    .Single().Should().Be(IntEnum.One);
                }
            }
예제 #6
0
            public void ReturnsEnumViaBoxedValue()
            {
                var rdr = new Mock <IDataReader>();

                rdr.Setup(r => r.GetFieldType(0)).Returns(typeof(int));
                rdr.Setup(r => r.GetValue(0)).Returns(-1);
                rdr.SetupSequence(r => r.Read())
                .Returns(true)
                .Returns(false);

                var toTest = new EnumRowFactory <IntEnum>();
                var xf     = new Mock <IDataTransformer>();

                xf.Setup(x => x.CanTransform(-1, typeof(IntEnum), false, It.Is <IEnumerable <Attribute> >(attrs => attrs != null)))
                .Returns(true);
                xf.Setup(x => x.Transform(-1, typeof(IntEnum), false, It.Is <IEnumerable <Attribute> >(attrs => attrs != null)))
                .Returns(1);

                toTest.ParseRows(rdr.Object, new[] { xf.Object }, CancellationToken.None)
                .Single().Should().Be(IntEnum.One);
            }
예제 #7
0
            public void ReturnsEnumViaNameTransformsStringBeforeParsing()
            {
                var rdr = new Mock <IDataReader>();

                rdr.Setup(r => r.GetFieldType(0)).Returns(typeof(string));
                rdr.Setup(r => r.GetString(0)).Returns("One");
                rdr.SetupSequence(r => r.Read())
                .Returns(true)
                .Returns(false);

                var toTest = new EnumRowFactory <IntEnum>();
                var xf     = new Mock <IDataTransformer>();

                xf.Setup(x => x.CanTransform("One", typeof(IntEnum), false, It.Is <IEnumerable <Attribute> >(attrs => attrs != null)))
                .Returns(true);
                xf.Setup(x => x.Transform("One", typeof(IntEnum), false, It.Is <IEnumerable <Attribute> >(attrs => attrs != null)))
                .Returns("Two");

                toTest.ParseRows(rdr.Object, new[] { xf.Object }, CancellationToken.None)
                .Single().Should().Be(IntEnum.Two);
            }