Dapper.ColumnMapper 的使用

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using FluentAssertions;

namespace Dapper.ColumnMapper.Tests
{
    [TestFixture]
    public class ColumnMapperTests
    {
        private SqlConnection connection;

        public static readonly string connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=True";

        public class ColumnMappingObject
        {
            public string DefaultColumn { get; set; }
            public string NonMatchingColumn { get; set; }
            [ColumnMapping("MappedCol")]
public string MappedColumn { get; set; } public string MiscasedColumn { get; set; } } [TestFixtureSetUp] public void Setup() { connection = new SqlConnection(connectionString); connection.Open(); CreateTestTable(); SqlMapper.SetTypeMap(typeof(ColumnMappingObject), new ColumnTypeMapper(typeof(ColumnMappingObject))); } private void CreateTestTable() { const string createSql = @" create table #Test (Id int, DefaultColumn varchar(20), BadCol varchar(20), MappedCol varchar(20), miscasedColumn varchar(20)) insert #Test values(1, 'DefaultColumn1', 'BadColumn', 'MappedColumn1', 'MiscasedColumn1')"; connection.Execute(createSql); } [TestFixtureTearDown] public void TearDown() { if (connection != null && connection.State == ConnectionState.Open) { const string dropSql = "drop table #Test"; connection.Execute(dropSql); connection.Close(); } connection = null; } [Test] public void Property_Matching_Column_Name_Exactly_Is_Mapped_Correctly() { var selectedObject = SelectObjects(); selectedObject.Should().NotBeNull(); selectedObject.DefaultColumn.Should().NotBeNullOrEmpty(); selectedObject.DefaultColumn.Should().Be("DefaultColumn1"); } [Test] public void Property_Not_Matching_Coumn_Name_Exactly_And_No_Attribute_Is_Not_Mapped() { var selectedObject = SelectObjects(); selectedObject.Should().NotBeNull(); selectedObject.NonMatchingColumn.Should().BeNullOrEmpty(); } [Test] public void Property_Using_Column_Mapping_Attribute_Is_Mapped_Correctly() { var selectedObject = SelectObjects(); selectedObject.Should().NotBeNull(); selectedObject.MappedColumn.Should().NotBeNullOrEmpty(); selectedObject.MappedColumn.Should().Be("MappedColumn1"); } [Test] public void Miscased_Property_Is_Mapped_Correctly() { var selectedObject = SelectObjects(); selectedObject.Should().NotBeNull(); selectedObject.MiscasedColumn.Should().NotBeNullOrEmpty(); selectedObject.MiscasedColumn.Should().Be("MiscasedColumn1"); } private ColumnMappingObject SelectObjects() { const string selectSql = "select * from #Test"; return connection.Query<ColumnMappingObject>(selectSql).FirstOrDefault(); } [Test] public void Can_Register_Single_Type() { ColumnTypeMapper.RegisterForTypes(typeof (DateTime)); var map = SqlMapper.GetTypeMap(typeof(DateTime)); map.Should().NotBeNull(); map.Should().BeOfType<ColumnTypeMapper>(); } [Test] public void Can_Register_Multiple_Types() { ColumnTypeMapper.RegisterForTypes(typeof(DateTime), typeof(TimeSpan)); var dateTimeMap = SqlMapper.GetTypeMap(typeof (DateTime)); dateTimeMap.Should().NotBeNull(); dateTimeMap.Should().BeOfType<ColumnTypeMapper>(); var timeSpanMap = SqlMapper.GetTypeMap(typeof (TimeSpan)); timeSpanMap.Should().NotBeNull(); timeSpanMap.Should().BeOfType<ColumnTypeMapper>(); } } }

 

转载于:https://www.cnblogs.com/starluck/p/4542281.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值