comparison UnitTests/CS/Mapping/MemberMapperTest.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Data;
3 using System.Data.SqlTypes;
4 using System.Globalization;
5
6 using NUnit.Framework;
7
8 using BLToolkit.Data;
9 using BLToolkit.Mapping;
10
11 namespace Mapping
12 {
13 [TestFixture]
14 public class MemberMapperTest
15 {
16 public class Object1
17 {
18 public int Int32;
19 public float Float;
20 public DayOfWeek Dow1;
21 public DayOfWeek Dow2;
22 }
23
24 [Test]
25 public void PrimitiveMemberTest()
26 {
27 ObjectMapper om = Map.GetObjectMapper(typeof(Object1));
28
29 Object1 o = new Object1();
30
31 DayOfWeek de = DayOfWeek.Thursday;
32 short di = (short)de;
33
34 om.SetValue(o, "Int32", 123.56);
35 om.SetValue(o, "Float", 123.57.ToString());
36 om.SetValue(o, "Dow1", de);
37 om.SetValue(o, "Dow2", di);
38
39 Assert.AreEqual(123, om.GetValue(o, "Int32"));
40 Assert.AreEqual(de, om.GetValue(o, "Dow1"));
41 Assert.AreEqual(de, om.GetValue(o, "Dow2"));
42 Assert.IsTrue (Math.Abs(123.57 - (float)om.GetValue(o, "Float")) < 0.0001);
43
44 Assert.IsNull(om.GetValue(o, "blah-blah-blah"));
45 }
46
47 public class AnsiStringObject
48 {
49 [MemberMapper(typeof(AnsiStringNumberMapper))]
50 public string ansi;
51 public string unicode;
52 }
53
54 internal class AnsiStringNumberMapper : MemberMapper
55 {
56 public override void Init(MapMemberInfo mapMemberInfo)
57 {
58 mapMemberInfo.DbType = DbType.AnsiString;
59 base.Init(mapMemberInfo);
60 }
61 }
62
63 #if !SQLCE
64 [Test]
65 #endif
66 public void ProvideCustomDBTypeTest()
67 {
68 AnsiStringObject obj = new AnsiStringObject();
69 obj.ansi = "ansi";
70 obj.unicode = "unicode";
71
72 IDbDataParameter[] parametrs = new DbManager().CreateParameters( obj );
73
74 Assert.AreEqual(2, parametrs.Length);
75 Assert.AreEqual(DbType.String, parametrs[1].DbType);
76
77 #if !FIREBIRD
78 // AnsiString is not supported by FB.
79 //
80 Assert.AreEqual(DbType.AnsiString, parametrs[0].DbType);
81 #endif
82 }
83
84 public class Object2
85 {
86 public short? Int16;
87 public int? Int32;
88 public long? Int64;
89 public float? Float;
90 public Guid? Guid;
91 public DayOfWeek? Dow1;
92 public DayOfWeek? Dow2;
93 }
94
95 [Test]
96 public void NullableMemberTest()
97 {
98 Object2 o = new Object2();
99
100 short? s = 125;
101 Guid g = Guid.NewGuid();
102 DayOfWeek de = DayOfWeek.Thursday;
103 int di = (int)de;
104
105 ObjectMapper<Object2>.SetValue(o, "Int16", s);
106 ObjectMapper<Object2>.SetValue(o, "Int32", 123.56);
107 ObjectMapper<Object2>.SetValue(o, "Int64", null);
108 ObjectMapper<Object2>.SetValue(o, "Float", 123.57.ToString());
109 ObjectMapper<Object2>.SetValue(o, "Guid", (Guid?)g);
110 ObjectMapper<Object2>.SetValue(o, "Guid", g);
111 ObjectMapper<Object2>.SetValue(o, "Dow1", de);
112 ObjectMapper<Object2>.SetValue(o, "Dow1", (DayOfWeek?)de);
113 ObjectMapper<Object2>.SetValue(o, "Dow2", di);
114
115 Assert.AreEqual(125, o.Int16);
116 Assert.AreEqual(123, o.Int32);
117 Assert.IsNull ( o.Int64);
118 Assert.AreEqual(g, o.Guid);
119 Assert.AreEqual(de, o.Dow1);
120 Assert.AreEqual(de, o.Dow2);
121 Assert.IsTrue (Math.Abs(123.57 - o.Float.Value) < 0.0001);
122
123 Assert.AreEqual(125, ObjectMapper<Object2>.GetValue(o, "Int16"));
124 Assert.AreEqual(123, ObjectMapper<Object2>.GetValue(o, "Int32"));
125 Assert.IsNull ( ObjectMapper<Object2>.GetValue(o, "Int64"));
126 Assert.AreEqual(g, ObjectMapper<Object2>.GetValue(o, "Guid"));
127 Assert.AreEqual(de, ObjectMapper<Object2>.GetValue(o, "Dow1"));
128 Assert.AreEqual(de, ObjectMapper<Object2>.GetValue(o, "Dow2"));
129 Assert.IsTrue (Math.Abs(123.57 - (float)ObjectMapper<Object2>.GetValue(o, "Float")) < 0.0001);
130 }
131
132 public class Object3
133 {
134 public SqlInt32 Int32;
135 public SqlSingle Single;
136 }
137
138
139 [Test]
140 // fixes test fail due to use of "," vs "." in numbers parsing for some cultures
141 [SetCulture("")]
142 public void SqlTypeMemberTest()
143 {
144 ObjectMapper om = Map.GetObjectMapper(typeof(Object3));
145
146 Object3 o = new Object3();
147
148 om.SetValue(o, "Int32", 123.56);
149 om.SetValue(o, "Single", 123.57.ToString(CultureInfo.InvariantCulture));
150
151 Assert.AreEqual(123, o.Int32. Value);
152 Assert.AreEqual(123.57f, o.Single.Value);
153
154 Assert.AreEqual(123, om.GetValue(o, "Int32"));
155 Assert.AreEqual(123.57f, om.GetValue(o, "Single"));
156 }
157
158 public interface IClassInterface
159 {
160 IClassInterface classInterface { get; set;}
161 }
162
163 public class ClassInterface : IClassInterface
164 {
165 private IClassInterface _ici;
166
167 [MapIgnore(false)]
168 public IClassInterface classInterface
169 {
170 get { return _ici; }
171 set { _ici = value; }
172 }
173 }
174
175 [Test]
176 public void DerivedTypeTest()
177 {
178 IClassInterface ici = new ClassInterface();
179 ObjectMapper om = Map.GetObjectMapper(ici.GetType());
180 MemberMapper mm = om["classInterface"];
181 mm.SetValue(ici, new ClassInterface());
182 }
183
184 public class Class1
185 {
186 int _int32 = 0;
187 [MapField(Storage = "_int32")]
188 public int Int32
189 {
190 get { return _int32; }
191 }
192 }
193
194 [Test]
195 public void MapToStorageTest()
196 {
197 Class1 o = new Class1();
198 ObjectMapper om = Map.GetObjectMapper(o.GetType());
199 MemberMapper mm = om["Int32"];
200 mm.SetValue(o, 5);
201
202 Assert.AreEqual(5, o.Int32);
203 }
204 }
205 }