-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathContentstackCollectionUnitTests.cs
More file actions
331 lines (273 loc) · 8.61 KB
/
ContentstackCollectionUnitTests.cs
File metadata and controls
331 lines (273 loc) · 8.61 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AutoFixture;
using Contentstack.Core.Models;
using Xunit;
namespace Contentstack.Core.Unit.Tests
{
/// <summary>
/// Unit tests for ContentstackCollection class - uses mocks and AutoFixture, no real API calls
/// </summary>
public class ContentstackCollectionUnitTests
{
private readonly IFixture _fixture = new Fixture();
#region Initialization Tests
[Fact]
public void ContentstackCollection_Initialization_SetsDefaultValues()
{
// Act
var collection = new ContentstackCollection<string>();
// Assert
Assert.Equal(0, collection.Skip);
Assert.Equal(0, collection.Limit);
Assert.Equal(0, collection.Count);
Assert.Null(collection.Items);
}
[Fact]
public void ContentstackCollection_Initialization_WithItems_SetsItems()
{
// Arrange
var items = new List<string> { "item1", "item2", "item3" };
// Act
var collection = new ContentstackCollection<string>
{
Items = items
};
// Assert
Assert.NotNull(collection.Items);
Assert.Equal(3, collection.Items.Count());
}
#endregion
#region Properties Tests
[Fact]
public void Skip_SetAndGet_ReturnsCorrectValue()
{
// Arrange
var collection = new ContentstackCollection<string>();
var skipValue = _fixture.Create<int>();
// Act
collection.Skip = skipValue;
// Assert
Assert.Equal(skipValue, collection.Skip);
}
[Fact]
public void Limit_SetAndGet_ReturnsCorrectValue()
{
// Arrange
var collection = new ContentstackCollection<string>();
var limitValue = _fixture.Create<int>();
// Act
collection.Limit = limitValue;
// Assert
Assert.Equal(limitValue, collection.Limit);
}
[Fact]
public void Count_SetAndGet_ReturnsCorrectValue()
{
// Arrange
var collection = new ContentstackCollection<string>();
var countValue = _fixture.Create<int>();
// Act
collection.Count = countValue;
// Assert
Assert.Equal(countValue, collection.Count);
}
[Fact]
public void Items_SetAndGet_ReturnsCorrectValue()
{
// Arrange
var collection = new ContentstackCollection<string>();
var items = new List<string> { "item1", "item2" };
// Act
collection.Items = items;
// Assert
Assert.NotNull(collection.Items);
Assert.Equal(items, collection.Items);
}
[Fact]
public void Items_SetToNull_ReturnsNull()
{
// Arrange
var collection = new ContentstackCollection<string>
{
Items = new List<string> { "item1" }
};
// Act
collection.Items = null;
// Assert
Assert.Null(collection.Items);
}
#endregion
#region GetEnumerator Tests
[Fact]
public void GetEnumerator_WithItems_ReturnsEnumerator()
{
// Arrange
var items = new List<string> { "item1", "item2", "item3" };
var collection = new ContentstackCollection<string>
{
Items = items
};
// Act
var enumerator = collection.GetEnumerator();
// Assert
Assert.NotNull(enumerator);
}
[Fact]
public void GetEnumerator_WithItems_IteratesThroughAllItems()
{
// Arrange
var items = new List<string> { "item1", "item2", "item3" };
var collection = new ContentstackCollection<string>
{
Items = items
};
// Act
var result = new List<string>();
foreach (var item in collection)
{
result.Add(item);
}
// Assert
Assert.Equal(3, result.Count);
Assert.Equal("item1", result[0]);
Assert.Equal("item2", result[1]);
Assert.Equal("item3", result[2]);
}
[Fact]
public void GetEnumerator_WithNullItems_ThrowsException()
{
// Arrange
var collection = new ContentstackCollection<string>
{
Items = null
};
// Act & Assert
Assert.Throws<NullReferenceException>(() =>
{
foreach (var item in collection)
{
// This should throw
}
});
}
[Fact]
public void GetEnumerator_WithEmptyItems_ReturnsEmptyEnumerator()
{
// Arrange
var collection = new ContentstackCollection<string>
{
Items = new List<string>()
};
// Act
var result = new List<string>();
foreach (var item in collection)
{
result.Add(item);
}
// Assert
Assert.Empty(result);
}
#endregion
#region IEnumerable.GetEnumerator Tests
[Fact]
public void IEnumerable_GetEnumerator_ReturnsEnumerator()
{
// Arrange
var items = new List<string> { "item1", "item2" };
var collection = new ContentstackCollection<string>
{
Items = items
};
IEnumerable enumerable = collection;
// Act
var enumerator = enumerable.GetEnumerator();
// Assert
Assert.NotNull(enumerator);
}
[Fact]
public void IEnumerable_GetEnumerator_IteratesThroughAllItems()
{
// Arrange
var items = new List<string> { "item1", "item2", "item3" };
var collection = new ContentstackCollection<string>
{
Items = items
};
IEnumerable enumerable = collection;
// Act
var result = new List<object>();
foreach (var item in enumerable)
{
result.Add(item);
}
// Assert
Assert.Equal(3, result.Count);
Assert.Equal("item1", result[0]);
Assert.Equal("item2", result[1]);
Assert.Equal("item3", result[2]);
}
#endregion
#region Complex Type Tests
[Fact]
public void ContentstackCollection_WithComplexType_WorksCorrectly()
{
// Arrange
var items = new List<TestModel>
{
new TestModel { Id = 1, Name = "Test1" },
new TestModel { Id = 2, Name = "Test2" }
};
// Act
var collection = new ContentstackCollection<TestModel>
{
Items = items,
Skip = 0,
Limit = 10,
Count = 2
};
// Assert
Assert.Equal(2, collection.Count);
Assert.Equal(0, collection.Skip);
Assert.Equal(10, collection.Limit);
Assert.Equal(2, collection.Items.Count());
}
[Fact]
public void ContentstackCollection_WithComplexType_IteratesCorrectly()
{
// Arrange
var items = new List<TestModel>
{
new TestModel { Id = 1, Name = "Test1" },
new TestModel { Id = 2, Name = "Test2" }
};
var collection = new ContentstackCollection<TestModel>
{
Items = items
};
// Act
var result = new List<TestModel>();
foreach (var item in collection)
{
result.Add(item);
}
// Assert
Assert.Equal(2, result.Count);
Assert.Equal(1, result[0].Id);
Assert.Equal("Test1", result[0].Name);
Assert.Equal(2, result[1].Id);
Assert.Equal("Test2", result[1].Name);
}
#endregion
}
/// <summary>
/// Test model for complex type testing
/// </summary>
public class TestModel
{
public int Id { get; set; }
public string Name { get; set; }
}
}