usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingnunit.framework;usingfluentassertions;namespacedapper.columnmapper.tests{[Testfixture] Public classcolumnmappertests {PrivateSqlConnection Connection; Public Static ReadOnly stringConnectionString ="Data source=.\\sqlexpress;initial catalog=tempdb;integrated security=true"; Public classColumnmappingobject { Public stringDefaultcolumn {Get;Set; } Public stringNonmatchingcolumn {Get;Set; } [ColumnMapping ("Mappedcol")]
Public stringMappedcolumn {Get;Set; } Public stringMiscasedcolumn {Get;Set; } } [Testfixturesetup] Public voidSetup () {connection=NewSqlConnection (connectionString); Connection. Open (); Createtesttable (); Sqlmapper.settypemap ( typeof(columnmappingobject), new columntypemapper (typeof (Columnmappingobject))); } Private voidcreatetesttable () {Const stringCreatesql =@"CREATE TABLE #Test (Id int, defaultcolumn varchar), badcol varchar, mappedcol varchar, misc Asedcolumn varchar) Insert #Test VALUES (1, ' DefaultColumn1 ', ' badcolumn ', ' MappedColumn1 ', ' Miscasedcol Umn1 ')"; Connection. Execute (Createsql); } [Testfixtureteardown] Public voidTearDown () {if(Connection! =NULL&& connection. state = =ConnectionState.Open) {Const stringDropsql ="drop table #Test"; Connection. Execute (Dropsql); Connection. Close (); } Connection=NULL; } [Test] Public voidproperty_matching_column_name_exactly_is_mapped_correctly () {varSelectedobject =selectobjects (); Selectedobject.should (). Notbenull (); SelectedObject.DefaultColumn.Should (). Notbenullorempty (); SelectedObject.DefaultColumn.Should (). Be ("DefaultColumn1"); } [Test] Public voidproperty_not_matching_coumn_name_exactly_and_no_attribute_is_not_mapped () {varSelectedobject =selectobjects (); Selectedobject.should (). Notbenull (); SelectedObject.NonMatchingColumn.Should (). Benullorempty (); } [Test] Public voidproperty_using_column_mapping_attribute_is_mapped_correctly () {varSelectedobject =selectobjects (); Selectedobject.should (). Notbenull (); SelectedObject.MappedColumn.Should (). Notbenullorempty (); SelectedObject.MappedColumn.Should (). Be ("MappedColumn1"); } [Test] Public voidmiscased_property_is_mapped_correctly () {varSelectedobject =selectobjects (); Selectedobject.should (). Notbenull (); SelectedObject.MiscasedColumn.Should (). Notbenullorempty (); SelectedObject.MiscasedColumn.Should (). Be ("MiscasedColumn1"); } Privatecolumnmappingobject selectobjects () {Const stringSelectsql ="SELECT * from #Test"; returnConnection. Query<columnmappingobject>(Selectsql). FirstOrDefault (); } [Test] Public voidCan_register_single_type () {columntypemapper.registerfortypes (typeof(DateTime)); varMap = Sqlmapper.gettypemap (typeof(DateTime)); Map. Should (). Notbenull (); Map. Should (). Beoftype<ColumnTypeMapper>(); } [Test] Public voidcan_register_multiple_types () {columntypemapper.registerfortypes (typeof(DateTime),typeof(TimeSpan)); varDatetimemap = Sqlmapper.gettypemap (typeof(DateTime)); Datetimemap.should (). Notbenull (); Datetimemap.should (). Beoftype<ColumnTypeMapper>(); varTimespanmap = Sqlmapper.gettypemap (typeof(TimeSpan)); Timespanmap.should (). Notbenull (); Timespanmap.should (). Beoftype<ColumnTypeMapper>(); } }}
Use of Dapper.columnmapper