/
StandardPopulatorTests.cs
362 lines (261 loc) · 11.4 KB
/
StandardPopulatorTests.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
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
/*
Copyright 2016, 2017, 2018, 2019 Alexander Kuperman
This file is part of TestDataFramework.
TestDataFramework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TestDataFramework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TestDataFramework. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using log4net.Config;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using TestDataFramework.AttributeDecorator.Concrete;
using TestDataFramework.AttributeDecorator.Concrete.TableTypeCacheService.Wrappers;
using TestDataFramework.AttributeDecorator.Interfaces;
using TestDataFramework.DeepSetting;
using TestDataFramework.HandledTypeGenerator;
using TestDataFramework.Helpers;
using TestDataFramework.ListOperations.Concrete;
using TestDataFramework.Persistence.Interfaces;
using TestDataFramework.Populator;
using TestDataFramework.Populator.Concrete;
using TestDataFramework.Populator.Concrete.OperableList;
using TestDataFramework.TypeGenerator.Concrete;
using TestDataFramework.TypeGenerator.Interfaces;
using TestDataFramework.ValueGenerator.Interfaces;
using Tests.Mocks;
using Tests.TestModels;
namespace Tests.Tests
{
[TestClass]
public class StandardPopulatorTests
{
private IAttributeDecorator attributeDecorator;
private Mock<IHandledTypeGenerator> handledTypeGeneratorMock;
private Mock<IPersistence> persistenceMock;
private StandardPopulator populator;
private Mock<ITypeGenerator> typeGeneratorMock;
private Mock<IValueGenerator> valueGeneratorMock;
[TestInitialize]
public void Initialize()
{
Helpers.ConfigureLogger();
this.attributeDecorator = new StandardAttributeDecorator(null, new AssemblyWrapper(), new Schema());
this.persistenceMock = new Mock<IPersistence>();
this.typeGeneratorMock = new Mock<ITypeGenerator>();
this.handledTypeGeneratorMock = new Mock<IHandledTypeGenerator>();
this.valueGeneratorMock = new Mock<IValueGenerator>();
this.populator = new StandardPopulator(this.typeGeneratorMock.Object, this.persistenceMock.Object,
this.attributeDecorator, this.handledTypeGeneratorMock.Object, this.valueGeneratorMock.Object, null,
null, null);
}
[TestMethod]
public void Add_Test()
{
// Arrange
var expected = new SubjectClass();
var populator = new StandardPopulator(Helpers.GetTypeGeneratorMock(expected).Object, new MockPersistence(),
this.attributeDecorator, null, null, null, null, null);
// Act
RecordReference<SubjectClass> reference = populator.Add<SubjectClass>();
populator.Bind();
// Assert
Assert.IsNotNull(reference);
Assert.AreEqual(expected.GetType(), reference.RecordType);
Assert.AreEqual(expected, reference.RecordObject);
}
[TestMethod]
public void Records_Cleared_After_Bind_Test()
{
// Arrange
var expected = new SubjectClass {AnEmailAddress = "email"};
var mockPersistence = new MockPersistence();
var populator = new StandardPopulator(Helpers.GetTypeGeneratorMock(expected).Object, mockPersistence,
this.attributeDecorator, null, null, null, null, null);
// Act
populator.Add<SubjectClass>();
populator.Bind();
populator.Add<SubjectClass>();
populator.Bind();
RecordReference<SubjectClass> reference = populator.Add<SubjectClass>();
populator.Bind();
// Assert
Assert.AreEqual(3, mockPersistence.Storage.Count);
Assert.AreEqual(reference.RecordObject.AnEmailAddress, mockPersistence.Storage[0]["AnEmailAddress"]);
}
[TestMethod]
public void Bind_Test()
{
// Arrange
const int integer = 5;
const string text = "abcde";
var inputRecord = new SubjectClass {Integer = integer, Text = text};
Helpers.SetupTypeGeneratorMock(this.typeGeneratorMock, inputRecord);
var mockPersistence = new MockPersistence();
this.populator = new StandardPopulator(this.typeGeneratorMock.Object, mockPersistence,
this.attributeDecorator, null, null, null, null, null);
// Act
this.populator.Add<SubjectClass>();
this.populator.Bind();
// Assert
IDictionary<string, object> record = mockPersistence.Storage.FirstOrDefault();
Assert.IsNotNull(record);
Assert.AreEqual(record["Integer"], integer);
Assert.AreEqual(record["Text"], text);
}
[TestMethod]
public void Bind_SingleResult_Test()
{
// Arrange
var referenceMock = new Mock<RecordReference<SubjectClass>>(null, null, null, null, null, null);
// Act
this.populator.Bind(referenceMock.Object);
// Assert
referenceMock.VerifyGet(m => m.IsPopulated);
referenceMock.Verify(m => m.Populate());
this.persistenceMock.Verify(m => m.Persist(
It.Is<IEnumerable<RecordReference>>(referenceSet => referenceSet.Single() == referenceMock.Object)));
}
[TestMethod]
public void Bind_RecordReference_Test()
{
// Arrange
var referenceMock = new Mock<RecordReference<SubjectClass>>(null, null, null, null, null, null);
// Act
this.populator.Bind(referenceMock.Object);
// Assert
referenceMock.Verify(m => m.Populate(), Times.Once);
}
[TestMethod]
public void MutliRecord_Test()
{
// Arrange
var mockPersistence = new MockPersistence();
var populator = new StandardPopulator(this.typeGeneratorMock.Object, mockPersistence,
this.attributeDecorator, null, null, null, null, null);
Helpers.SetupTypeGeneratorMock(this.typeGeneratorMock, new SubjectClass());
Helpers.SetupTypeGeneratorMock(this.typeGeneratorMock, new SecondClass());
// Act
populator.Add<SubjectClass>();
populator.Add<SecondClass>();
populator.Bind();
// Assert
Assert.AreEqual(2, mockPersistence.Storage.Count);
}
[TestMethod]
public void Extend_Test()
{
// Arrange
var valueGetterDictionary = new Dictionary<Type, HandledTypeValueGetterWithContext>();
this.handledTypeGeneratorMock.SetupGet(m => m.HandledTypeValueGetterDictionary)
.Returns(valueGetterDictionary);
var subject = new SubjectClass();
// Act
this.populator.Extend(typeof(SubjectClass), type => subject);
// Assert
Assert.AreEqual(1, valueGetterDictionary.Count);
Assert.AreEqual(subject, valueGetterDictionary[typeof(SubjectClass)](typeof(SubjectClass), null));
}
[TestMethod]
public void Extend_ExistingType_Test()
{
// Arrange
var valueGetterDictionary =
new Dictionary<Type, HandledTypeValueGetterWithContext>
{
{typeof(SubjectClass), (type, context) => null}
};
this.handledTypeGeneratorMock.SetupGet(m => m.HandledTypeValueGetterDictionary)
.Returns(valueGetterDictionary);
var subject = new SubjectClass();
// Act
this.populator.Extend(typeof(SubjectClass), type => subject);
// Assert
Assert.AreEqual(1, valueGetterDictionary.Count);
Assert.AreEqual(subject, valueGetterDictionary[typeof(SubjectClass)](typeof(SubjectClass), null));
}
[TestMethod]
public void Bind_RecordReference_Is_Populated_Test()
{
// Arrange
var recordReference =
new RecordReference<SubjectClass>(null, null, null, null, null, null) {IsPopulated = true};
// Act
this.populator.Bind(recordReference);
// Assert
this.persistenceMock.Verify(m => m.Persist(It.IsAny<RecordReference[]>()), Times.Never);
this.typeGeneratorMock.Verify(m => m.GetObject<SubjectClass>(It.IsAny<TypeGeneratorContext>()),
Times.Never);
}
[TestMethod]
public void Add_Gets_Collection_Test()
{
// Arrange
var primaryKeys = new[]
{
new RecordReference<SecondClass>(null, null, null, null, null, null),
new RecordReference<SecondClass>(null, null, null, null, null, null)
};
// Act
OperableList<SubjectClass> result = this.populator.Add<SubjectClass>(5, primaryKeys);
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(5, result.Count);
}
[TestMethod]
public void ValueGenerator_Get()
{
IValueGenerator result = this.populator.ValueGenerator;
Assert.AreEqual(this.valueGeneratorMock.Object, result);
}
[TestMethod]
public void Clear_Test()
{
RecordReference<SubjectClass> reference = this.populator.Add<SubjectClass>();
// Act
this.populator.Clear();
this.populator.Bind();
// Assert
Assert.IsNull(reference.RecordObject);
}
[TestMethod]
public void MakeT_Test()
{
var subject = new SubjectClass();
this.typeGeneratorMock.Setup(m => m.GetObject<SubjectClass>(It.IsAny<TypeGeneratorContext>()))
.Returns(subject);
var result = this.populator.Make<SubjectClass>();
Assert.AreEqual(subject, result);
Assert.AreEqual(0, this.populator.Populatables.Count);
}
[TestMethod]
public void Make_Collection_Test()
{
var subject = new SubjectClass();
this.typeGeneratorMock.Setup(m => m.GetObject<SubjectClass>(It.IsAny<TypeGeneratorContext>()))
.Returns(subject);
List<SubjectClass> result = this.populator.Make<SubjectClass>(5).ToList();
Assert.IsNotNull(result);
Assert.AreEqual(5, result.Count);
result.ForEach(s => Assert.AreEqual(subject, s));
Assert.AreEqual(0, this.populator.Populatables.Count);
}
[TestMethod]
public void Remove_Test()
{
Populatable recordReference = this.populator.Add<SubjectClass>();
Assert.AreEqual(recordReference, this.populator.Populatables.Single());
this.populator.Remove(recordReference);
Assert.AreEqual(0, this.populator.Populatables.Count);
}
}
}