forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringSerializerTranslationTests.cs
More file actions
142 lines (122 loc) · 6.12 KB
/
StringSerializerTranslationTests.cs
File metadata and controls
142 lines (122 loc) · 6.12 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
#if !MONO
using System;
using System.Collections.Generic;
using System.Globalization;
using Northwind.Common.ComplexModel;
using Northwind.Common.DataModel;
using Northwind.Common.ServiceModel;
using NUnit.Framework;
using ServiceStack.Common.Tests.Models;
using ServiceStack.Text.Common;
namespace ServiceStack.Text.Tests
{
[TestFixture]
public class StringSerializerTranslationTests
: TestBase
{
public StringSerializerTranslationTests()
{
NorthwindData.LoadData(false);
}
[Test]
public void Can_convert_from_Customer_to_Dictionary()
{
var model = DtoFactory.CustomerDto;
var modelString = TypeSerializer.SerializeToString(model);
var translateToModel = TypeSerializer.DeserializeFromString<Dictionary<string, string>>(modelString);
AssertDictonaryIsEqualToCustomer(model, translateToModel);
}
private static void AssertDictonaryIsEqualToCustomer(CustomerDto model, IDictionary<string, string> translateToModel)
{
Assert.That(translateToModel["Id"], Is.EqualTo(model.Id));
Assert.That(translateToModel["Address"], Is.EqualTo(model.Address));
Assert.That(translateToModel["City"], Is.EqualTo(model.City));
Assert.That(translateToModel["CompanyName"], Is.EqualTo(model.CompanyName));
Assert.That(translateToModel["ContactName"], Is.EqualTo(model.ContactName));
Assert.That(translateToModel["ContactTitle"], Is.EqualTo(model.ContactTitle));
Assert.That(translateToModel["Country"], Is.EqualTo(model.Country));
Assert.That(translateToModel["Fax"], Is.EqualTo(model.Fax));
Assert.That(translateToModel["Phone"], Is.EqualTo(model.Phone));
Assert.That(translateToModel["PostalCode"], Is.EqualTo(model.PostalCode));
Assert.That(model.Picture, Is.Null);
Assert.That(translateToModel.ContainsKey("Picture"), Is.False);
}
[Test]
public void Can_convert_ModelWithFieldsOfDifferentTypes_to_string_Dictionary()
{
var model = ModelWithFieldsOfDifferentTypes.Create(1);
var modelString = TypeSerializer.SerializeToString(model);
var translateToModel = TypeSerializer.DeserializeFromString<Dictionary<string, string>>(modelString);
Assert.That(translateToModel["Id"], Is.EqualTo(model.Id.ToString()));
Assert.That(translateToModel["Name"], Is.EqualTo(model.Name));
Assert.That(translateToModel["Bool"], Is.EqualTo(model.Bool.ToString()));
Assert.That(translateToModel["DateTime"], Is.EqualTo(DateTimeSerializer.ToShortestXsdDateTimeString(model.DateTime)));
Assert.That(translateToModel["Double"], Is.EqualTo(model.Double.ToString(CultureInfo.InvariantCulture)));
Assert.That(translateToModel["Guid"], Is.EqualTo(model.Guid.ToString("N")));
Assert.That(translateToModel["LongId"], Is.EqualTo(model.LongId.ToString()));
}
[Test]
public void Can_convert_string_Dictionary_to_ModelWithFieldsOfDifferentTypes()
{
var model = new Dictionary<string, string>
{
{ "Id", "1" },
{ "Name", "Name1" },
{ "Bool", "False" },
{ "DateTime", "2008-01-10" },
{ "Double", "1.11" },
{ "Guid", "99161EEC-2857-4031-8CED-EAE21F954496" },
{ "LongId", "999" },
};
var modelString = TypeSerializer.SerializeToString(model);
var translateToModel = TypeSerializer.DeserializeFromString<ModelWithFieldsOfDifferentTypes>(modelString);
Assert.That(translateToModel.Id, Is.EqualTo(int.Parse(model["Id"])));
Assert.That(translateToModel.Name, Is.EqualTo(model["Name"]));
Assert.That(translateToModel.Bool.ToString(), Is.EqualTo(model["Bool"]));
Assert.That(translateToModel.DateTime, Is.EqualTo(new DateTime(2008, 1, 10)));
Assert.That(translateToModel.Double, Is.EqualTo(double.Parse(model["Double"], CultureInfo.InvariantCulture)));
Assert.That(translateToModel.Guid, Is.EqualTo(new Guid("99161EEC-2857-4031-8CED-EAE21F954496")));
Assert.That(translateToModel.LongId, Is.EqualTo(long.Parse(model["LongId"])));
}
[Test]
public void Can_convert_ModelWithFieldsOfDifferentTypes_to_object_Dictionary()
{
var model = ModelWithFieldsOfDifferentTypes.Create(1);
var modelString = TypeSerializer.SerializeToString(model);
var translateToModel = TypeSerializer.DeserializeFromString<Dictionary<string, object>>(modelString);
Assert.That(translateToModel["Id"], Is.EqualTo(model.Id.ToString()));
Assert.That(translateToModel["Bool"], Is.EqualTo(model.Bool.ToString()));
Assert.That(translateToModel["DateTime"], Is.EqualTo(DateTimeSerializer.ToShortestXsdDateTimeString(model.DateTime)));
Assert.That(translateToModel["Double"], Is.EqualTo(model.Double.ToString(CultureInfo.InvariantCulture)));
Assert.That(translateToModel["Guid"], Is.EqualTo(model.Guid.ToString("N")));
Assert.That(translateToModel["LongId"], Is.EqualTo(model.LongId.ToString()));
Assert.That(translateToModel["Name"], Is.EqualTo(model.Name));
}
[Test]
public void Can_convert_Dictionary_to_ModelWithIdAndName()
{
var model = new Dictionary<string, string> { { "Id", "1" }, { "Name", "Name" } };
var modelString = TypeSerializer.SerializeToString(model);
var translateToModel = TypeSerializer.DeserializeFromString<ModelWithIdAndName>(modelString);
Assert.That(translateToModel.Id, Is.EqualTo(int.Parse(model["Id"])));
Assert.That(translateToModel.Name, Is.EqualTo(model["Name"]));
}
public class MultiCustomerDictionaries
{
public Dictionary<string, string> Customer1 { get; set; }
public Dictionary<string, string> Customer2 { get; set; }
public Dictionary<string, string> Customer3 { get; set; }
}
[Test]
public void Can_convert_MultiCustomerProperties_to_MultiCustomerDictionaries()
{
var model = DtoFactory.MultiCustomerProperties;
var modelString = TypeSerializer.SerializeToString(model);
var translateToModel = TypeSerializer.DeserializeFromString<MultiCustomerDictionaries>(modelString);
AssertDictonaryIsEqualToCustomer(model.Customer1, translateToModel.Customer1);
AssertDictonaryIsEqualToCustomer(model.Customer2, translateToModel.Customer2);
AssertDictonaryIsEqualToCustomer(model.Customer3, translateToModel.Customer3);
}
}
}
#endif