示例#1
0
        public void CustomSerializerWorksWithTVPs()
        {
            DbSerializationRule.Serialize <EncodedInt>(new EncodedTypeSerializer());

            var e = new EncodedInt()
            {
                Encoded = "Two"
            };
            var o = new TestWithSerializedObject()
            {
                Encoded = e
            };
            var data = new List <TestWithSerializedObject>()
            {
                o
            };

            try
            {
                Connection().ExecuteSql("CREATE TYPE TestTableWithEncodedInt AS TABLE (Encoded int)");

                using (var c = Connection().OpenWithTransaction())
                {
                    c.ExecuteSql("CREATE PROC TestEncoded(@Encoded [TestTableWithEncodedInt] READONLY) AS SELECT * FROM @Encoded");
                    var e2 = c.Query <TestWithSerializedObject>("TestEncoded", new { Encoded = data }).First();
                    Assert.AreEqual(e.Encoded, e2.Encoded.Encoded);
                }
            }
            finally
            {
                Connection().ExecuteSql("DROP TYPE TestTableWithEncodedInt");
            }
        }
示例#2
0
 public void TearDown()
 {
     ColumnMapping.All.ResetTransforms();
     ColumnMapping.All.ResetMappers();
     ColumnMapping.All.ResetChildBinding();
     DbSerializationRule.ResetRules();
 }
示例#3
0
        public void Given_a_type_which_requires_serialisation_When_using_the_type_in_an_sql_statement_Then_it_does_not_explode()
        {
            DbSerializationRule.Serialize <MyCustomStruct>(new MyCustomSerialiser <MyCustomStruct>());

            var roundtrippedValue = Connection().ExecuteScalarSql <string>("select @value", new { value = new MyCustomStruct {
                                                                                                      Value = "structs are best"
                                                                                                  } });

            Assert.That(roundtrippedValue, Is.EqualTo("structs are best"));
        }
示例#4
0
        public void SerializationHandlerDoesNotSwitchObjectToXmlWhenNameDoesNotMatch()
        {
            DbSerializationRule.Serialize<JsonClass>("foo", SerializationMode.Xml);

            var input = new JsonClass();
            input.SubClass = new JsonSubClass() { Foo = "foo", Bar = 5 };

            var s = Connection().Single<string>("MappingAsJson4", input);
            Assert.IsFalse(Connection().Single<string>("MappingAsJson4", input).StartsWith("<MappingTests."));
        }
示例#5
0
        public void SerializationHandlerSwitchesObjectToXmlWhenTypeMatches()
        {
            DbSerializationRule.Serialize<JsonSubClass>(SerializationMode.Xml);

            var input = new JsonClass();
            input.SubClass = new JsonSubClass() { Foo = "foo", Bar = 5 };

            var s = Connection().Single<string>("MappingAsJson3", input);
            Assert.IsTrue(Connection().Single<string>("MappingAsJson3", input).StartsWith("<MappingTests."));
        }
示例#6
0
        public void Given_a_table_type_with_a_property_that_is_a_struct_which_requires_serialisation_When_using_the_type_in_an_sql_statement_Then_it_does_not_explode()
        {
            DbSerializationRule.Serialize <MyCustomStruct>(new MyCustomSerialiser <MyCustomStruct>());

            var roundtrippedValue = Connection().ExecuteScalarSql <string>("select top 1 String from @values", new { values = new[] { new InsightTestDataString {
                                                                                                                                          String = new MyCustomStruct {
                                                                                                                                              Value = "structs are better"
                                                                                                                                          }
                                                                                                                                      } } });

            Assert.That(roundtrippedValue, Is.EqualTo("structs are better"));
        }
示例#7
0
        public void BooleanSerializerWorksWithStrings()
        {
            DbSerializationRule.Serialize <bool>(new BooleanYNSerializer());
            DbSerializationRule.Serialize <bool?>(new BooleanYNSerializer());

            using (var c = Connection().OpenWithTransaction())
            {
                var b = c.QuerySql <HasBool>("SELECT IsBool='Y', IsNullableBool=NULL").First();

                c.ExecuteSql("CREATE PROC TestBool(@IsBool varchar(10), @IsNullableBool varchar(10)) AS SELECT IsBool=@IsBool, IsNullableBool=@IsNullableBool;");
                var b2 = c.Query <HasBool>("TestBool", b).First();
            }
        }
示例#8
0
        public void CustomSerializerSupportsTableParameters()
        {
            DbSerializationRule.Serialize <Instant>(new InstantDeserializer());

            var array = new List <InstantTable>
            {
                new InstantTable
                {
                    Id    = Guid.NewGuid(),
                    Value = SystemClock.Instance.Now
                },
                new InstantTable
                {
                    Id    = Guid.NewGuid(),
                    Value = SystemClock.Instance.Now - Duration.FromHours(-1)
                }
            };

            using (var connection = Connection().OpenWithTransaction())
            {
                // Return values from recordset
                var result = connection.QuerySql <InstantTable>("SELECT Value=SYSDATETIMEOFFSET(), Id=NEWID()").First();
                Console.WriteLine(result.Id);
                Console.WriteLine(result.Value);

                // Accept and return parameters
                connection.ExecuteSql("CREATE PROC TestInstant(@Value DATETIMEOFFSET, @Id UNIQUEIDENTIFIER) AS SELECT Value=@Value, Id=@Id;");
                var nextResult = connection.Query <InstantTable>("TestInstant", result).First();
                Console.WriteLine(result.Id);
                Console.WriteLine(result.Value);

                Assert.AreEqual(result.Id, nextResult.Id);
                Assert.AreEqual(result.Value, nextResult.Value);

                // Use table parameter
                var items = connection.Query <InstantTable>("DateTimeTypeProc", array);

                Assert.AreEqual(items.Count, 2);

                var firstItem = items.Single(x => x.Id == array[0].Id);

                Assert.AreEqual(firstItem.Value, array[0].Value);

                var secondItem = items.Single(x => x.Id == array[1].Id);

                Assert.AreEqual(secondItem.Value, array[1].Value);
            }
        }
示例#9
0
        public void CustomSerializerWorksWithOtherTypes()
        {
            DbSerializationRule.Serialize <EncodedInt>("Encoded", new EncodedIntSerializer());

            using (var c = Connection().OpenWithTransaction())
            {
                var e = new EncodedInt()
                {
                    Encoded = "Two"
                };

                c.ExecuteSql("CREATE PROC TestEncoded(@Encoded int) AS SELECT Encoded=@Encoded");
                var e2 = c.Query <EncodedInt>("TestEncoded", e).First();
                Assert.AreEqual(e.Encoded, e2.Encoded);
            }
        }
示例#10
0
        public void CustomSerializerWorksWithXmlFields()
        {
            var customSerializer = new CustomXmlSerializer();

            DbSerializationRule.Serialize <EncodedInt>(customSerializer);

            var e = new EncodedInt()
            {
                Encoded = "Two"
            };

            using (var c = Connection().OpenWithTransaction())
            {
                c.ExecuteSql("CREATE PROC TestEncodedXml(@Encoded [Xml]) AS SELECT Encoded=@Encoded");
                var e2 = c.Query <TestWithSerializedObject>("TestEncodedXml", new { Encoded = e }).First();
                Assert.AreEqual(e.Encoded, e2.Encoded.Encoded);
            }

            Assert.IsTrue(customSerializer.DidSerialize);
            Assert.IsTrue(customSerializer.DidDeserialize);
        }
示例#11
0
 public void TearDown()
 {
     DbSerializationRule.ResetRules();
 }