CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
DataMapperTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="DataMapperTests.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>no summary</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Text;
11using Csla.TestHelpers;
12
13#if !NUNIT
14using Microsoft.VisualStudio.TestTools.UnitTesting;
15
16#else
17using NUnit.Framework;
18using TestClass = NUnit.Framework.TestFixtureAttribute;
19using TestInitialize = NUnit.Framework.SetUpAttribute;
20using TestCleanup = NUnit.Framework.TearDownAttribute;
21using TestMethod = NUnit.Framework.TestAttribute;
22#endif
23
25{
26 [TestClass]
27 public class DataMapperTests
28 {
29 private static TestDIContext _testDIContext;
30
32 public static void ClassInitialize(TestContext context)
33 {
34 _testDIContext = TestDIContextFactory.CreateDefaultContext();
35 }
36
37 [TestMethod]
38 public void DictionaryMap()
39 {
40 IDataPortal<ManagedTarget> dataPortal = _testDIContext.CreateDataPortal<ManagedTarget>();
41
42 var target = dataPortal.Create();
43 var source = new Dictionary<string, object>();
44 source.Add("MyInt", 42);
45
46 Csla.Data.DataMapper.Load(source, target, (n) => n);
47 Assert.AreEqual(42, target.MyInt, "Int should match");
48
49 }
50
51 [TestMethod]
52 public void NumericTypes()
53 {
54 DataMapTarget target = new DataMapTarget();
55
56 Csla.Data.DataMapper.SetPropertyValue(target, "MyInt", 42);
57 Assert.AreEqual(42, target.MyInt, "Int should match");
58
59 Csla.Data.DataMapper.SetPropertyValue(target, "MyInt", "24");
60 Assert.AreEqual(24, target.MyInt, "Int from string should be 24");
61
62 Csla.Data.DataMapper.SetPropertyValue(target, "MyInt", "");
63 Assert.AreEqual(0, target.MyInt, "Int from empty string should be 0");
64
65 Csla.Data.DataMapper.SetPropertyValue(target, "MyInt", null);
66 Assert.AreEqual(0, target.MyInt, "Int from null should be 0");
67
68 Csla.Data.DataMapper.SetPropertyValue(target, "MyDouble", 4.2);
69 Assert.AreEqual(4.2, target.MyDouble, "Double should match");
70 }
71
72 [TestMethod]
73 public void BooleanTypes()
74 {
75 DataMapTarget target = new DataMapTarget();
76
77 Csla.Data.DataMapper.SetPropertyValue(target, "MyBool", true);
78 Assert.AreEqual(true, target.MyBool, "Bool should be true");
79
80 Csla.Data.DataMapper.SetPropertyValue(target, "MyBool", false);
81 Assert.AreEqual(false, target.MyBool, "Bool should be false");
82
83 Csla.Data.DataMapper.SetPropertyValue(target, "MyBool", "");
84 Assert.AreEqual(false, target.MyBool, "Bool from empty string should be false");
85
86 Csla.Data.DataMapper.SetPropertyValue(target, "MyBool", null);
87 Assert.AreEqual(false, target.MyBool, "Bool from null should be false");
88 }
89
90 [TestMethod]
91 public void GuidTypes()
92 {
93 DataMapTarget target = new DataMapTarget();
94
95 Guid testValue = Guid.NewGuid();
96
97 Csla.Data.DataMapper.SetPropertyValue(target, "MyGuid", testValue);
98 Assert.AreEqual(testValue, target.MyGuid, "Guid values should match");
99
100 Csla.Data.DataMapper.SetPropertyValue(target, "MyGuid", Guid.Empty);
101 Assert.AreEqual(Guid.Empty, target.MyGuid, "Empty guid values should match");
102
103 Csla.Data.DataMapper.SetPropertyValue(target, "MyGuid", testValue.ToString());
104 Assert.AreEqual(testValue, target.MyGuid, "Guid values from string should match");
105 }
106
107 [TestMethod]
108 public void NullableTypes()
109 {
110 DataMapTarget target = new DataMapTarget();
111
112 Csla.Data.DataMapper.SetPropertyValue(target, "MyNInt", 42);
113 Assert.AreEqual(42, target.MyNInt, "Int should match");
114
115 Csla.Data.DataMapper.SetPropertyValue(target, "MyNInt", 0);
116 Assert.AreEqual(0, target.MyNInt, "Int should be 0");
117
118 Csla.Data.DataMapper.SetPropertyValue(target, "MyNInt", string.Empty);
119 Assert.AreEqual(null, target.MyNInt, "Int from string.Empty should be null");
120
121 Csla.Data.DataMapper.SetPropertyValue(target, "MyNInt", null);
122 Assert.AreEqual(null, target.MyNInt, "Int should be null");
123 }
124
125 [TestMethod]
126 public void EnumTypes()
127 {
128 DataMapTarget target = new DataMapTarget();
129
130 Csla.Data.DataMapper.SetPropertyValue(target, "MyEnum", DataMapEnum.Second);
131 Assert.AreEqual(DataMapEnum.Second, target.MyEnum, "Enum should be Second");
132
133 Csla.Data.DataMapper.SetPropertyValue(target, "MyEnum", "First");
134 Assert.AreEqual(DataMapEnum.First, target.MyEnum, "Enum should be First");
135
136 Csla.Data.DataMapper.SetPropertyValue(target, "MyEnum", 2);
137 Assert.AreEqual(DataMapEnum.Third, target.MyEnum, "Enum should be Third");
138 }
139
140 [TestMethod]
141 public void DateTimeTypes()
142 {
143 DataMapTarget target = new DataMapTarget();
144
145 Csla.Data.DataMapper.SetPropertyValue(target, "MyDate", DateTime.Today);
146 Assert.AreEqual(DateTime.Today, target.MyDate, "Date should be Today");
147
148 Csla.Data.DataMapper.SetPropertyValue(target, "MyDate", "1/1/2007");
149 Assert.AreEqual(new DateTime(2007, 1, 1), target.MyDate, "Date should be 1/1/2007");
150
151 Csla.Data.DataMapper.SetPropertyValue(target, "MyDate", new Csla.SmartDate("1/1/2007"));
152 Assert.AreEqual(new DateTime(2007, 1, 1), target.MyDate, "Date should be 1/1/2007");
153 }
154
155 [TestMethod]
156 public void SmartDateTypes()
157 {
158 DataMapTarget target = new DataMapTarget();
159
160 Csla.Data.DataMapper.SetPropertyValue(target, "MySmartDate", DateTime.Today);
161 Assert.AreEqual(new Csla.SmartDate(DateTime.Today), target.MySmartDate, "SmartDate should be Today");
162
163 Csla.Data.DataMapper.SetPropertyValue(target, "MySmartDate", "1/1/2007");
164 Assert.AreEqual(new Csla.SmartDate(new DateTime(2007, 1, 1)), target.MySmartDate, "SmartDate should be 1/1/2007");
165
166 Csla.Data.DataMapper.SetPropertyValue(target, "MySmartDate", new Csla.SmartDate("1/1/2007"));
167 Assert.AreEqual(new Csla.SmartDate(new DateTime(2007, 1, 1)), target.MySmartDate, "SmartDate should be 1/1/2007");
168
169 Csla.Data.DataMapper.SetPropertyValue(target, "MySmartDate", new DateTimeOffset(new DateTime(2004, 3, 2)));
170 Assert.AreEqual(new Csla.SmartDate(new DateTime(2004, 3, 2)), target.MySmartDate, "SmartDate should be 3/2/2004");
171
172 target.MySmartDate = new Csla.SmartDate(DateTime.Today, Csla.SmartDate.EmptyValue.MaxDate);
173 Assert.IsFalse(target.MySmartDate.EmptyIsMin, "EmptyIsMin should be false before set");
174 Csla.Data.DataMapper.SetPropertyValue(target, "MySmartDate", DateTime.Parse("1/1/2007"));
175 Assert.IsFalse(target.MySmartDate.EmptyIsMin, "EmptyIsMin should be false after set");
176 }
177
178 [TestMethod]
179 public void SetFields()
180 {
181 DataMapTarget target = new DataMapTarget();
182
183 Csla.Data.DataMapper.SetFieldValue(target, "_int", 42);
184 Assert.AreEqual(42, target.MyInt, "Int should match");
185
186 Csla.Data.DataMapper.SetFieldValue(target, "_double", 4.2);
187 Assert.AreEqual(4.2, target.MyDouble, "Double should match");
188
189 Csla.Data.DataMapper.SetFieldValue(target, "_bool", true);
190 Assert.AreEqual(true, target.MyBool, "Bool should be true");
191
192 Csla.Data.DataMapper.SetFieldValue(target, "_bool", false);
193 Assert.AreEqual(false, target.MyBool, "Bool should be false");
194
195 Csla.Data.DataMapper.SetFieldValue(target, "_smartDate", "2/1/2007");
196 Assert.AreEqual(new Csla.SmartDate("2/1/2007"), target.MySmartDate, "SmartDate should be 2/1/2007");
197
198 Csla.Data.DataMapper.SetFieldValue(target, "_smartDate", new Csla.SmartDate("1/1/2007"));
199 Assert.AreEqual(new Csla.SmartDate("1/1/2007"), target.MySmartDate, "SmartDate should be 1/1/2007");
200
201 Csla.Data.DataMapper.SetFieldValue(target, "_smartDate", new DateTimeOffset(new DateTime(2004, 3, 2)));
202 Assert.AreEqual(new Csla.SmartDate(new DateTime(2004, 3, 2)), target.MySmartDate, "SmartDate should be 3/2/2004");
203 }
204
205 [TestMethod]
206 public void BasicDataMap()
207 {
208 Csla.Data.DataMap map = new Csla.Data.DataMap(typeof(DataMapTarget), typeof(DataMapTarget));
209
210 DataMapTarget source = new DataMapTarget();
211 DataMapTarget target = new DataMapTarget();
212 source.MyInt = 123;
213 source.MyDouble = 456;
214 source.MyBool = true;
215 source.MyEnum = DataMapEnum.Second;
216 var g = Guid.NewGuid();
217 source.MyGuid = g;
218 source.MyNInt = 321;
219 source.MySmartDate = new Csla.SmartDate(new DateTime(2002, 12, 4));
220 source.MyDate = new DateTime(2002, 11, 2);
221 source.MyString = "Third";
222
223 map.AddFieldMapping("_int", "_int");
224 map.AddFieldToPropertyMapping("_double", "MyDouble");
225 map.AddPropertyMapping("MyBool", "MyBool");
226 map.AddPropertyToFieldMapping("MyGuid", "_guid");
227 map.AddPropertyMapping("MyEnum", "MyString");
228 map.AddPropertyMapping("MyString", "MyEnum");
229
230 Csla.Data.DataMapper.Map(source, target, map);
231
232 Assert.AreEqual(123, target.MyInt, "Int should match");
233 Assert.AreEqual(456, target.MyDouble, "Double should match");
234 Assert.AreEqual(true, target.MyBool, "bool should match");
235 Assert.AreEqual(g, target.MyGuid, "guid should match");
236 Assert.AreEqual("Second", target.MyString, "string should match (converted enum)");
237 Assert.AreEqual(DataMapEnum.Third, target.MyEnum, "enum should match (parsed enum)");
238 Assert.AreNotEqual(source.MyDate, target.MyDate, "Dates should not match");
239 }
240 }
241
242 public enum DataMapEnum
243 {
244 First,
245 Second,
246 Third
247 }
248
250 public class ManagedTarget : BusinessBase<ManagedTarget>
251 {
252 public static PropertyInfo<int> MyIntProperty = RegisterProperty<int>(c => c.MyInt);
253 public int MyInt
254 {
255 get { return GetProperty(MyIntProperty); }
256 set { SetProperty(MyIntProperty, value); }
257 }
258
259 [Create]
260 private void Create()
261 {
262 }
263 }
264
265 public class DataMapTarget
266 {
267 private int _int;
268
269 public int MyInt
270 {
271 get { return _int; }
272 set { _int = value; }
273 }
274
275 private double _double;
276
277 public double MyDouble
278 {
279 get { return _double; }
280 set { _double = value; }
281 }
282
283 private bool _bool;
284
285 public bool MyBool
286 {
287 get { return _bool; }
288 set { _bool = value; }
289 }
290
291 private Nullable<int> _nint;
292
293 public Nullable<int> MyNInt
294 {
295 get { return _nint; }
296 set { _nint = value; }
297 }
298
299 private DataMapEnum _enum;
300
302 {
303 get { return _enum; }
304 set { _enum = value; }
305 }
306
307 private DateTime _date;
308
309 public DateTime MyDate
310 {
311 get { return _date; }
312 set { _date = value; }
313 }
314
315 private Csla.SmartDate _smartDate;
316
318 {
319 get { return _smartDate; }
320 set { _smartDate = value; }
321 }
322
323 private Guid _guid;
324
325 public Guid MyGuid
326 {
327 get { return _guid; }
328 set { _guid = value; }
329 }
330
331 private string _string;
332 public string MyString
333 {
334 get { return _string; }
335 set { _string = value; }
336 }
337 }
338}
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
Defines a mapping between two sets of properties/fields for use by DataMapper.
Definition: DataMap.cs:20
Maintains metadata about a property.
static void ClassInitialize(TestContext context)
static PropertyInfo< int > MyIntProperty
Type to carry context information for DI in unit tests
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
object Create(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
@ Serializable
Prevents updating or inserting until the transaction is complete.
Provides a date data type that understands the concept of an empty date value.
Definition: SmartDate.cs:32
EmptyValue
Indicates the empty value of a SmartDate.
Definition: SmartDate.cs:52
bool EmptyIsMin
Gets a value indicating whether an empty date is the min or max possible date value.
Definition: SmartDate.cs:568