forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoMappingObjectDictionaryTests.cs
More file actions
598 lines (529 loc) · 22.9 KB
/
Copy pathAutoMappingObjectDictionaryTests.cs
File metadata and controls
598 lines (529 loc) · 22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Northwind.Common.DataModel;
using NUnit.Framework;
using ServiceStack.Common.Tests.Models;
namespace ServiceStack.Text.Tests
{
[TestFixture]
public class AutoMappingObjectDictionaryTests
{
[Test]
public void Can_convert_Car_to_ObjectDictionary_WithMapper()
{
var dto = new DtoWithEnum { Name = "Dan", Color = Color.Blue};
var map = dto.ToObjectDictionary((k,v) => k == nameof(Color) ? v.ToString().ToLower() : v);
Assert.That(map["Color"], Is.EqualTo(Color.Blue.ToString().ToLower()));
Assert.That(map["Name"], Is.EqualTo(dto.Name));
}
[Test]
public void Can_convert_Car_to_ObjectDictionary()
{
var dto = new Car { Age = 10, Name = "ZCar" };
var map = dto.ToObjectDictionary();
Assert.That(map["Age"], Is.EqualTo(dto.Age));
Assert.That(map["Name"], Is.EqualTo(dto.Name));
var fromDict = (Car)map.FromObjectDictionary(typeof(Car));
Assert.That(fromDict.Age, Is.EqualTo(dto.Age));
Assert.That(fromDict.Name, Is.EqualTo(dto.Name));
}
[Test]
public void Can_convert_Cart_to_ObjectDictionary()
{
var dto = new User
{
FirstName = "First",
LastName = "Last",
Car = new Car { Age = 10, Name = "ZCar" },
};
var map = dto.ToObjectDictionary();
Assert.That(map["FirstName"], Is.EqualTo(dto.FirstName));
Assert.That(map["LastName"], Is.EqualTo(dto.LastName));
Assert.That(((Car)map["Car"]).Age, Is.EqualTo(dto.Car.Age));
Assert.That(((Car)map["Car"]).Name, Is.EqualTo(dto.Car.Name));
var fromDict = map.FromObjectDictionary<User>();
Assert.That(fromDict.FirstName, Is.EqualTo(dto.FirstName));
Assert.That(fromDict.LastName, Is.EqualTo(dto.LastName));
Assert.That(fromDict.Car.Age, Is.EqualTo(dto.Car.Age));
Assert.That(fromDict.Car.Name, Is.EqualTo(dto.Car.Name));
}
[Test]
public void Can_Convert_from_ObjectDictionary_with_Different_Types()
{
var map = new Dictionary<string, object>
{
{ "FirstName", 1 },
{ "LastName", true },
{ "Car", new SubCar { Age = 10, Name = "SubCar", Custom = "Custom"} },
};
var fromDict = (User)map.FromObjectDictionary(typeof(User));
Assert.That(fromDict.FirstName, Is.EqualTo("1"));
Assert.That(fromDict.LastName, Is.EqualTo(bool.TrueString));
Assert.That(fromDict.Car.Age, Is.EqualTo(10));
Assert.That(fromDict.Car.Name, Is.EqualTo("SubCar"));
}
[Test]
public void Can_Convert_from_ObjectDictionary_with_Different_Types_with_camelCase_names()
{
var map = new Dictionary<string, object>
{
{ "firstName", 1 },
{ "lastName", true },
{ "car", new SubCar { Age = 10, Name = "SubCar", Custom = "Custom"} },
};
var fromDict = (User)map.FromObjectDictionary(typeof(User));
Assert.That(fromDict.FirstName, Is.EqualTo("1"));
Assert.That(fromDict.LastName, Is.EqualTo(bool.TrueString));
Assert.That(fromDict.Car.Age, Is.EqualTo(10));
Assert.That(fromDict.Car.Name, Is.EqualTo("SubCar"));
}
[Test]
public void Can_Convert_from_ObjectDictionary_with_Read_Only_Dictionary()
{
var map = new Dictionary<string, object>
{
{ "FirstName", 1 },
{ "LastName", true },
{ "Car", new SubCar { Age = 10, Name = "SubCar", Custom = "Custom"} },
};
var readOnlyMap = new ReadOnlyDictionary<string, object>(map);
var fromDict = (User)readOnlyMap.FromObjectDictionary(typeof(User));
Assert.That(fromDict.FirstName, Is.EqualTo("1"));
Assert.That(fromDict.LastName, Is.EqualTo(bool.TrueString));
Assert.That(fromDict.Car.Age, Is.EqualTo(10));
Assert.That(fromDict.Car.Name, Is.EqualTo("SubCar"));
}
public class QueryCustomers : QueryDb<Customer>
{
public string CustomerId { get; set; }
public string[] CountryIn { get; set; }
public string[] CityIn { get; set; }
}
[Test]
public void Can_convert_from_ObjectDictionary_into_AutoQuery_DTO()
{
var map = new Dictionary<string, object>
{
{ "CustomerId", "CustomerId"},
{ "CountryIn", new[]{"UK", "Germany"}},
{ "CityIn", "London,Berlin"},
{ "take", 5 },
{ "Meta", "{foo:bar}" },
};
var request = map.FromObjectDictionary<QueryCustomers>();
Assert.That(request.CustomerId, Is.EqualTo("CustomerId"));
Assert.That(request.CountryIn, Is.EquivalentTo(new[]{"UK", "Germany" }));
Assert.That(request.CityIn, Is.EquivalentTo(new[]{ "London", "Berlin" }));
Assert.That(request.Take, Is.EqualTo(5));
Assert.That(request.Meta, Is.EquivalentTo(new Dictionary<string, object> {{"foo", "bar"}}));
}
public class PersonWithIdentities
{
public string Name { get; set; }
public List<OtherName> OtherNames { get;set; }
}
public class OtherName
{
public string Name { get; set; }
}
[Test]
public void Can_Convert_from_ObjectDictionary_Containing_Another_Object_Dictionary()
{
var map = new Dictionary<string, object>
{
{ "name", "Foo" },
{ "otherNames", new List<object>
{
new Dictionary<string, object> { { "name", "Fu" } },
new Dictionary<string, object> { { "name", "Fuey" } }
}
}
};
var fromDict = map.FromObjectDictionary<PersonWithIdentities>();
Assert.That(fromDict.Name, Is.EqualTo("Foo"));
Assert.That(fromDict.OtherNames.Count, Is.EqualTo(2));
Assert.That(fromDict.OtherNames.First().Name, Is.EqualTo("Fu"));
Assert.That(fromDict.OtherNames.Last().Name, Is.EqualTo("Fuey"));
var toDict = fromDict.ToObjectDictionary();
Assert.That(toDict["Name"], Is.EqualTo("Foo"));
Assert.That(toDict["OtherNames"], Is.EqualTo(fromDict.OtherNames));
}
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string DisplayName { get; set; }
}
[Test]
public void Can_create_new_object_using_MergeIntoObjectDictionary()
{
var customer = new User { FirstName = "John", LastName = "Doe" };
var map = customer.MergeIntoObjectDictionary(new { Initial = "Z" });
map["DisplayName"] = map["FirstName"] + " " + map["Initial"] + " " + map["LastName"];
var employee = map.FromObjectDictionary<Employee>();
Assert.That(employee.DisplayName, Is.EqualTo("John Z Doe"));
}
[Test]
public void Can_create_new_object_from_merged_objects()
{
var customer = new User { FirstName = "John", LastName = "Doe" };
var map = MergeObjects(customer, new { Initial = "Z" });
map["DisplayName"] = map["FirstName"] + " " + map["Initial"] + " " + map["LastName"];
var employee = map.FromObjectDictionary<Employee>();
Dictionary<string,object> MergeObjects(params object[] sources) {
var to = new Dictionary<string, object>();
sources.Each(x => x.ToObjectDictionary().Each(entry => to[entry.Key] = entry.Value));
return to;
}
Assert.That(employee.DisplayName, Is.EqualTo("John Z Doe"));
}
[Test, TestCaseSource(nameof(TestDataFromObjectDictionaryWithNullableTypes))]
public void Can_Convert_from_ObjectDictionary_with_Nullable_Properties(
Dictionary<string, object> map,
ModelWithFieldsOfNullableTypes expected)
{
var actual = map.FromObjectDictionary<ModelWithFieldsOfNullableTypes>();
ModelWithFieldsOfNullableTypes.AssertIsEqual(actual, expected);
}
private static IEnumerable<TestCaseData> TestDataFromObjectDictionaryWithNullableTypes
{
get
{
var defaults = ModelWithFieldsOfNullableTypes.CreateConstant(1);
yield return new TestCaseData(
new Dictionary<string, object>
{
{ "Id", defaults.Id },
{ "NId", defaults.NId },
{ "NLongId", defaults.NLongId },
{ "NGuid", defaults.NGuid },
{ "NBool", defaults.NBool },
{ "NDateTime", defaults.NDateTime },
{ "NFloat", defaults.NFloat },
{ "NDouble", defaults.NDouble },
{ "NDecimal", defaults.NDecimal },
{ "NTimeSpan", defaults.NTimeSpan }
},
defaults).SetName("All values populated");
yield return new TestCaseData(
new Dictionary<string, object>
{
{ "Id", defaults.Id.ToString() },
{ "NId", defaults.NId.ToString() },
{ "NLongId", defaults.NLongId.ToString() },
{ "NGuid", defaults.NGuid.ToString() },
{ "NBool", defaults.NBool.ToString() },
{ "NDateTime", defaults.NDateTime?.ToString("o") },
{ "NFloat", defaults.NFloat.ToString() },
{ "NDouble", defaults.NDouble.ToString() },
{ "NDecimal", defaults.NDecimal.ToString() },
{ "NTimeSpan", defaults.NTimeSpan.ToString() }
},
defaults).SetName("All values populated as strings");
yield return new TestCaseData(
new Dictionary<string, object>
{
{ "Id", defaults.Id },
{ "NId", null },
{ "NLongId", null },
{ "NGuid", null },
{ "NBool", null },
{ "NDateTime", null },
{ "NFloat", null },
{ "NDouble", null },
{ "NDecimal", null },
{ "NTimeSpan", null }
},
new ModelWithFieldsOfNullableTypes
{
Id = defaults.Id
}).SetName("Nullables set to null");
yield return new TestCaseData(
new Dictionary<string, object>
{
{ "Id", defaults.Id }
},
new ModelWithFieldsOfNullableTypes
{
Id = defaults.Id
}).SetName("Nullables unassigned");
yield return new TestCaseData(
new Dictionary<string, object>
{
{ "Id", defaults.Id },
{ "NLongId", 2 },
{ "NFloat", "3.1" },
{ "NDecimal", 4.2d },
{ "NTimeSpan", null }
},
new ModelWithFieldsOfNullableTypes
{
Id = defaults.Id,
NLongId = 2,
NFloat = 3.1f,
NDecimal = 4.2m
}).SetName("Mixed properties");
yield return new TestCaseData(
new Dictionary<string, object>
{
{ "Id", defaults.Id },
{ "NMadeUp", 99.9 },
{ "NLongId", 2 },
{ "NFloat", "3.1" },
{ "NRandom", "RANDOM" },
{ "NDecimal", 4.2d },
{ "NTimeSpan", null },
{ "NNull", null }
},
new ModelWithFieldsOfNullableTypes
{
Id = defaults.Id,
NLongId = 2,
NFloat = 3.1f,
NDecimal = 4.2m
}).SetName("Mixed properties with some foreign key/values");
}
}
[Test]
public void Can_Convert_from_ObjectDictionary_with_Nullable_Collection_Properties()
{
var map = new Dictionary<string, object>
{
{ "Id", 1 },
{ "Users", new[] { new User { FirstName = "Foo", LastName = "Bar", Car = new Car { Name = "Jag", Age = 25 }}}},
{ "Cars", new List<Car> { new Car { Name = "Toyota", Age = 2 }, new Car { Name = "Lexus", Age = 1 }}},
{ "Colors", null }
};
var actual = map.FromObjectDictionary<ModelWithCollectionsOfNullableTypes>();
Assert.That(actual.Id, Is.EqualTo(1));
Assert.That(actual.Users, Is.Not.Null);
Assert.That(actual.Users.Count(), Is.EqualTo(1));
var user = actual.Users.Single();
Assert.That(user.FirstName, Is.EqualTo("Foo"));
Assert.That(user.LastName, Is.EqualTo("Bar"));
Assert.That(user.Car, Is.Not.Null);
Assert.That(user.Car.Name, Is.EqualTo("Jag"));
Assert.That(user.Car.Age, Is.EqualTo(25));
Assert.That(actual.Cars, Is.Not.Null);
Assert.That(actual.Cars.Count, Is.EqualTo(2));
var firstCar = actual.Cars.First();
Assert.That(firstCar.Name, Is.EqualTo("Toyota"));
Assert.That(firstCar.Age, Is.EqualTo(2));
var secondCar = actual.Cars.Last();
Assert.That(secondCar.Name, Is.EqualTo("Lexus"));
Assert.That(secondCar.Age, Is.EqualTo(1));
Assert.That(actual.Colors, Is.Null);
}
public class ModelWithCollectionsOfNullableTypes
{
public int Id { get; set; }
public IEnumerable<User> Users { get; set; }
public Car[] Cars { get; set; }
public IList<Color> Colors { get; set; }
}
public class ModelWithTimeSpan
{
public TimeSpan Time { get; set; }
}
public class ModelWithLong
{
public long Time { get; set; }
}
[Test]
public void FromObjectDictionary_does_Convert_long_to_TimeSpan()
{
var time = new TimeSpan(1,1,1,1);
var map = new Dictionary<string, object> {
[nameof(ModelWithTimeSpan.Time)] = time.Ticks
};
var dto = map.FromObjectDictionary<ModelWithTimeSpan>();
Assert.That(dto.Time, Is.EqualTo(time));
map = new Dictionary<string, object> {
[nameof(ModelWithTimeSpan.Time)] = time
};
var dtoLong = map.FromObjectDictionary<ModelWithLong>();
Assert.That(dtoLong.Time, Is.EqualTo(time.Ticks));
}
public enum CefLogSeverity
{
Default,
Verbose,
Debug = Verbose,
Info,
Warning,
Error,
ErrorReport,
Disable = 99,
}
public class CefSettings
{
public CefLogSeverity LogSeverity { get; set; }
}
public class CefConfig
{
public string WindowTitle { get; set; }
public string Icon { get; set; }
public string CefPath { get; set; }
public string[] Args { get; set; }
public CefSettings CefSettings { get; set; }
public string StartUrl { get; set; }
public int? X { get; set; }
public int? Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public bool CenterToScreen { get; set; }
public bool HideConsoleWindow { get; set; }
}
[Test]
public void Can_use_PopulateInstance_to_populate_enum()
{
var map = new Dictionary<string, object> {
["LogSeverity"] = "Verbose"
};
var config = new CefConfig { CefSettings = new CefSettings { LogSeverity = CefLogSeverity.Info } };
map.PopulateInstance(config.CefSettings);
Assert.That(config.CefSettings.LogSeverity, Is.EqualTo(CefLogSeverity.Verbose));
}
[Test]
public void Can_convert_Dictionary_FromObjectDictionary()
{
var dict = new Dictionary<string,object>();
var to = dict.FromObjectDictionary<Dictionary<string, object>>();
Assert.That(to == dict);
}
[Test]
public void Can_convert_inner_dictionary()
{
var map = new Dictionary<string, object>
{
{ "FirstName", "Foo" },
{ "LastName", "Bar" },
{ "Car", new Dictionary<string, object>
{
{ "Name", "Tesla" },
{ "Age", 2 }
}}
};
var user = map.FromObjectDictionary<User>();
Assert.That(user.FirstName, Is.EqualTo("Foo"));
Assert.That(user.LastName, Is.EqualTo("Bar"));
Assert.That(user.Car, Is.Not.Null);
Assert.That(user.Car.Name, Is.EqualTo("Tesla"));
Assert.That(user.Car.Age, Is.EqualTo(2));
}
[Test]
public void Can_convert_inner_collection_of_dictionaries()
{
var map = new Dictionary<string, object>
{
{ "Name", "Tesla" },
{ "Age", "2" },
{ "Specs", new List<Dictionary<string, object>>
{
new Dictionary<string, object>
{
{"Item", "Model"},
{"Value", "S"}
},
new Dictionary<string, object>
{
{"Item", "Engine"},
{"Value", "Electric"}
},
new Dictionary<string, object>
{
{"Item", "Color"},
{"Value", "Red"}
},
new Dictionary<string, object>
{
{"Item", "PowerKW"},
{"Value", 285}
},
new Dictionary<string, object>
{
{"Item", "TorqueNm"},
{"Value", 430}
},
}}
};
var carWithSpecs = map.FromObjectDictionary<CarWithSpecs>();
Assert.That(carWithSpecs.Name, Is.EqualTo("Tesla"));
Assert.That(carWithSpecs.Age, Is.EqualTo(2));
Assert.That(carWithSpecs.Specs.Count, Is.EqualTo(5));
var model = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "Model");
Assert.That(model, Is.Not.Null);
Assert.That(model.Value, Is.EqualTo("S"));
var engine = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "Engine");
Assert.That(engine, Is.Not.Null);
Assert.That(engine.Value, Is.EqualTo("Electric"));
var color = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "Color");
Assert.That(color, Is.Not.Null);
Assert.That(color.Value, Is.EqualTo("Red"));
var power = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "PowerKW");
Assert.That(power, Is.Not.Null);
Assert.That(power.Value, Is.EqualTo("285"));
var torque = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "TorqueNm");
Assert.That(torque, Is.Not.Null);
Assert.That(torque.Value, Is.EqualTo("430"));
}
[Test]
public void Can_convert_inner_array_of_dictionaries()
{
var map = new Dictionary<string, object>
{
{ "Name", "Tesla" },
{ "Age", "2" },
{ "Specs", new[]
{
new Dictionary<string, object>
{
{"Item", "Model"},
{"Value", "S"}
},
new Dictionary<string, object>
{
{"Item", "Engine"},
{"Value", "Electric"}
},
new Dictionary<string, object>
{
{"Item", "Color"},
{"Value", "Red"}
},
new Dictionary<string, object>
{
{"Item", "PowerKW"},
{"Value", 285}
},
new Dictionary<string, object>
{
{"Item", "TorqueNm"},
{"Value", 430}
},
}}
};
var carWithSpecs = map.FromObjectDictionary<CarWithSpecs>();
Assert.That(carWithSpecs.Name, Is.EqualTo("Tesla"));
Assert.That(carWithSpecs.Age, Is.EqualTo(2));
Assert.That(carWithSpecs.Specs.Count, Is.EqualTo(5));
var model = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "Model");
Assert.That(model, Is.Not.Null);
Assert.That(model.Value, Is.EqualTo("S"));
var engine = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "Engine");
Assert.That(engine, Is.Not.Null);
Assert.That(engine.Value, Is.EqualTo("Electric"));
var color = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "Color");
Assert.That(color, Is.Not.Null);
Assert.That(color.Value, Is.EqualTo("Red"));
var power = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "PowerKW");
Assert.That(power, Is.Not.Null);
Assert.That(power.Value, Is.EqualTo("285"));
var torque = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "TorqueNm");
Assert.That(torque, Is.Not.Null);
Assert.That(torque.Value, Is.EqualTo("430"));
}
}
}