-
Notifications
You must be signed in to change notification settings - Fork 1
/
UnitTest.cs
236 lines (213 loc) · 8.74 KB
/
UnitTest.cs
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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Mvc;
using FormCollectionExtend.MVC;
using System.Collections.Generic;
namespace UnitTest
{
[TestClass]
public class UnitTest
{
/// <summary>
/// List Count
/// </summary>
int ListCount = 2;
#region TestMethod
/// <summary>
/// Convert FormCollection To Single Object.
/// </summary>
[TestMethod]
public void ToSingleObjectTest()
{
// Arrange
FormCollection Stub = CreateTestData();
// Act
TestModel Test = Stub.ToSingleObject<TestModel>();
// Assert
Assert.AreEqual(Test.Name.ToString(), TestData.Name.ToString());
Assert.AreEqual(Test.age, TestData.age);
Assert.AreEqual(Test.PhoneNumber, TestData.PhoneNumber);
Assert.AreEqual(Test.Married, TestData.Married);
Assert.AreEqual(Test.CreatedDateTime, TestData.CreatedDateTime);
}
/// <summary>
/// Convert FormCollection To Single Object With Empty Field.
/// </summary>
[TestMethod]
public void ToSingleObjectTestWithEmptyField()
{
// Arrange
FormCollection Stub = CreateTestDataWithEmptyField();
// Act
TestModel Test = Stub.ToSingleObject<TestModel>();
// Assert
Assert.AreEqual(Test.Name, null); // It should be null
Assert.AreEqual(Test.age, TestData.age);
Assert.AreEqual(Test.PhoneNumber, TestData.PhoneNumber);
Assert.AreEqual(Test.Married, TestData.Married);
Assert.AreEqual(Test.CreatedDateTime, TestData.CreatedDateTime);
}
/// <summary>
/// ToSingleObjectTestWithDictionary
/// </summary>
[TestMethod]
public void ToSingleObjectTestWithDictionary()
{
// Arrange
FormCollection Stub = CreateTestDataWithDifferentNaming();
Dictionary<string, string> MappingDictionary = CreateMappingDictionary();
// Act
TestModel Test = Stub.ToSingleObject<TestModel>(MappingDictionary);
// Assert
Assert.AreEqual(Test.Name.ToString(), TestData.Name.ToString());
Assert.AreEqual(Test.age, TestData.age);
Assert.AreEqual(Test.PhoneNumber, TestData.PhoneNumber);
Assert.AreEqual(Test.Married, TestData.Married);
Assert.AreEqual(Test.CreatedDateTime, TestData.CreatedDateTime);
}
/// <summary>
/// 將FormCollection轉為List形別物件
/// </summary>
[TestMethod]
public void ToListObjectTest()
{
// Arrange
FormCollection Stub = CreateTestDataSet();
string PrimaryKeyName = "Name";
// Act
List<TestModel> Test = Stub.ToListObject<TestModel>(PrimaryKeyName);
//Assert
Assert.AreEqual(Test.Count, ListCount);
foreach (var i in Test)
{
Assert.AreEqual(i.Name.ToString(), TestData.Name.ToString());
Assert.AreEqual(i.age, TestData.age);
Assert.AreEqual(i.PhoneNumber, TestData.PhoneNumber);
Assert.AreEqual(i.Married, TestData.Married);
Assert.AreEqual(i.CreatedDateTime, TestData.CreatedDateTime);
}
}
/// <summary>
/// 將FormCollection轉為List形別物件 ( 例外對映 )
/// </summary>
[TestMethod]
public void ToListObjectWithDictionary()
{
// Arrange
FormCollection Stub = CreateTestDataSetWithDifferentNaming();
Dictionary<string, string> MappingDictionary = CreateMappingDictionary();
string PrimaryKeyName = "Name111";
// Act
List<TestModel> Test = Stub.ToListObject<TestModel>(MappingDictionary, PrimaryKeyName);
// Assert
foreach (var i in Test)
{
Assert.AreEqual(i.Name.ToString(), TestData.Name.ToString());
Assert.AreEqual(i.age, TestData.age);
Assert.AreEqual(i.PhoneNumber, TestData.PhoneNumber);
Assert.AreEqual(i.Married, TestData.Married);
Assert.AreEqual(i.CreatedDateTime, TestData.CreatedDateTime);
}
}
#endregion
#region DataSource
/// <summary>
/// Create FormCollection and add TestData.
/// </summary>
/// <returns>TestData</returns>
FormCollection CreateTestData()
{
FormCollection Stub = new FormCollection();
Stub.Add("Name", $"{TestData.Name}");
Stub.Add("age", $"{TestData.age}");
Stub.Add("PhoneNumber", $"{TestData.PhoneNumber}");
Stub.Add("Married", $"{TestData.Married}");
Stub.Add("CreatedDateTime", $"{TestData.CreatedDateTime}");
Stub.Add("ModifyDateTime", $"{TestData.ModifyDateTime}");
return Stub;
}
/// <summary>
/// Create FormCollection and add TestData with empty field.
/// </summary>
/// <returns>TestData</returns>
FormCollection CreateTestDataWithEmptyField()
{
FormCollection Stub = new FormCollection();
//Stub.Add("Name", $"{TestData.Name}"); // comment out this one,when data after mapping,this field should be null.
Stub.Add("age", $"{TestData.age}");
Stub.Add("PhoneNumber", $"{TestData.PhoneNumber}");
Stub.Add("Married", $"{TestData.Married}");
Stub.Add("CreatedDateTime", $"{TestData.CreatedDateTime}");
Stub.Add("ModifyDateTime", $"{TestData.ModifyDateTime}");
return Stub;
}
/// <summary>
/// Create FormCollection and add TestDataWithDifferentNaming.
/// </summary>
/// <returns>TestData</returns>
FormCollection CreateTestDataWithDifferentNaming()
{
FormCollection Stub = new FormCollection();
Stub.Add("Name111", $"{TestData.Name}");
Stub.Add("age222", $"{TestData.age}");
Stub.Add("PhoneNumber333", $"{TestData.PhoneNumber}");
Stub.Add("Married444", $"{TestData.Married}");
Stub.Add("CreatedDateTime555", $"{TestData.CreatedDateTime}");
Stub.Add("ModifyDateTime666", $"{TestData.ModifyDateTime}");
return Stub;
}
/// <summary>
/// Create FormCollection and add Test Data Set.
/// </summary>
/// <returns>TestData</returns>
FormCollection CreateTestDataSet()
{
FormCollection Stub = new FormCollection();
for (int i = 1; i <= ListCount; i++)
{
Stub.Add("Name", $"{TestData.Name}");
Stub.Add("age", $"{TestData.age}");
Stub.Add("PhoneNumber", $"{TestData.PhoneNumber}");
Stub.Add("Married", $"{TestData.Married}");
Stub.Add("CreatedDateTime", $"{TestData.CreatedDateTime}");
Stub.Add("ModifyDateTime", $"{TestData.ModifyDateTime}");
}
return Stub;
}
/// <summary>
/// Create FormCollection and add Test Data Set With Different Naming.
/// </summary>
/// <returns>TestData</returns>
FormCollection CreateTestDataSetWithDifferentNaming()
{
FormCollection Stub = new FormCollection();
for (int i = 1; i <= ListCount; i++)
{
Stub.Add("Name111", $"{TestData.Name}");
Stub.Add("age222", $"{TestData.age}");
Stub.Add("PhoneNumber333", $"{TestData.PhoneNumber}");
Stub.Add("Married444", $"{TestData.Married}");
Stub.Add("CreatedDateTime555", $"{TestData.CreatedDateTime}");
Stub.Add("ModifyDateTime666", $"{TestData.ModifyDateTime}");
}
return Stub;
}
/// <summary>
/// Create Mapping Dictionary.
/// </summary>
/// <returns>Mapping Dictionary</returns>
Dictionary<string, string> CreateMappingDictionary()
{
// Setting up mapping dictionary.
// Ex. Key.Add( "Model field", "Form name" );
Dictionary<string, string> Dictionary = new Dictionary<string, string>();
Dictionary.Add("Name", "Name111");
Dictionary.Add("age", "age222");
Dictionary.Add("PhoneNumber", "PhoneNumber333");
Dictionary.Add("Married", "Married444");
Dictionary.Add("CreatedDateTime", "CreatedDateTime555");
Dictionary.Add("ModifyDateTime", "ModifyDateTime666");
return Dictionary;
}
#endregion
}
}