PageRenderTime 15ms CodeModel.GetById 10ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/PetaPoco.Tests/ColumnMapper.cs

http://github.com/toptensoftware/PetaPoco
C# | 90 lines | 71 code | 14 blank | 5 comment | 5 complexity | dae0cefdb209451326347759f547468d MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using PetaTest;
 6
 7namespace PetaPoco.Tests
 8{
 9	public class Poco2
10	{
11		public string prop1 { get; set; }
12		public string prop2 { get; set; }
13		public string prop3 { get; set; }
14		public string prop4 { get; set; }
15	}
16
17	public class MyColumnMapper : PetaPoco.IMapper
18	{
19		public void GetTableInfo(Type t, TableInfo ti)
20		{
21			if (t == typeof(Poco2))
22			{
23				ti.TableName = "petapoco";
24				ti.PrimaryKey = "id";
25			}
26		}
27		public bool MapPropertyToColumn(System.Reflection.PropertyInfo pi, ref string columnName, ref bool resultColumn)
28		{
29			if (pi.DeclaringType == typeof(Poco2))
30			{
31				switch (pi.Name)
32				{
33					case "prop1":
34						// Leave this property as is
35						return true;
36
37					case "prop2":
38						// Rename this column
39						columnName = "remapped2";
40						return true;
41
42					case "prop3":
43						// Mark this as a result column
44						resultColumn = true;
45						return true;
46
47					case "prop4":
48						// Ignore this property
49						return false;
50				}
51			}
52
53			// Do default property mapping
54			return true;
55		}
56
57
58		public Func<object, object> GetFromDbConverter(System.Reflection.PropertyInfo pi, Type SourceType)
59		{
60			return null;
61		}
62
63		public Func<object, object> GetToDbConverter(Type SourceType)
64		{
65			return null;
66		}
67	}
68
69	[TestFixture]
70	public class ColumnMapper 
71	{
72
73
74		[Test]
75		public void NoColumnMapper()
76		{
77
78			PetaPoco.Database.Mapper = new MyColumnMapper();
79			var pd=PetaPoco.Database.PocoData.ForType(typeof(Poco2));
80
81			Assert.AreEqual(pd.Columns.Count, 3);
82			Assert.AreEqual(pd.Columns["prop1"].PropertyInfo.Name, "prop1");
83			Assert.AreEqual(pd.Columns["remapped2"].ColumnName, "remapped2");
84			Assert.AreEqual(pd.Columns["prop3"].ColumnName, "prop3");
85			Assert.AreEqual(string.Join(", ", pd.QueryColumns), "prop1, remapped2");
86			Assert.AreEqual(pd.TableInfo.PrimaryKey, "id");
87			Assert.AreEqual(pd.TableInfo.TableName, "petapoco");
88		}
89	}
90}