-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTest.cs
More file actions
388 lines (326 loc) · 11.8 KB
/
UnitTest.cs
File metadata and controls
388 lines (326 loc) · 11.8 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
using Blog;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
namespace BlogTests
{
[TestClass]
public class BlogDataTests : IDisposable
{
private MyDbContext _context;
[TestInitialize]
public void TestInitialize()
{
// Arrange - Create in-memory database
var loggerFactory = LoggerFactory.Create(builder =>
builder.SetMinimumLevel(LogLevel.Warning));
_context = new MyDbContext(loggerFactory);
_context.Database.EnsureDeleted();
_context.Database.EnsureCreated();
// Seed test data
SeedTestData();
}
public void Dispose()
{
_context?.Dispose();
}
private void SeedTestData()
{
// Test data from Program.InitializeData
_context.BlogPosts.AddRange(
new BlogPost("Post1") {
Comments = new List<BlogComment>
{
new BlogComment("Comment1", new DateTime(2020, 3, 2), "Petr"),
new BlogComment("Comment2", new DateTime(2020, 3, 4), "Elena"),
new BlogComment("Comment3", new DateTime(2020, 3, 5), "Ivan")
}
},
new BlogPost("Post2") {
Comments = new List<BlogComment>
{
new BlogComment("Comment4", new DateTime(2020, 3, 5), "Elena"),
new BlogComment("Comment5", new DateTime(2020, 3, 6), "Ivan")
}
},
new BlogPost("Post3") {
Comments = new List<BlogComment>
{
new BlogComment("Comment6", new DateTime(2020, 2, 7), "Ivan"),
new BlogComment("Comment7", new DateTime(2020, 2, 9), "Elena"),
new BlogComment("Comment8", new DateTime(2020, 2, 10), "Ivan"),
new BlogComment("Comment9", new DateTime(2020, 2, 14), "Petr")
}
}
);
_context.SaveChanges();
}
/*
* 1. Data Validation Tests
*
* Each test verifies one specific aspect of the seeded data
*/
[TestMethod]
public void SeedData_ContainsExactlyThreeBlogPosts()
{
// Act
var postCount = _context.BlogPosts.Count();
// Assert
Assert.AreEqual(3, postCount);
}
[TestMethod]
public void SeedData_Post1_HasExactlyThreeComments()
{
// Act
var commentCount = _context.BlogPosts
.Include(p => p.Comments)
.First(p => p.Title == "Post1")
.Comments.Count;
// Assert
Assert.AreEqual(3, commentCount);
}
[TestMethod]
public void SeedData_Post2_HasExactlyTwoComments()
{
// Act
var commentCount = _context.BlogPosts
.Include(p => p.Comments)
.First(p => p.Title == "Post2")
.Comments.Count;
// Assert
Assert.AreEqual(2, commentCount);
}
[TestMethod]
public void SeedData_Post3_HasExactlyFourComments()
{
// Act
var commentCount = _context.BlogPosts
.Include(p => p.Comments)
.First(p => p.Title == "Post3")
.Comments.Count;
// Assert
Assert.AreEqual(4, commentCount);
}
[TestMethod]
public void SeedData_AllCommentsHaveValidDates()
{
// Act
var invalidDates = _context.BlogComments
.Where(c => c.CreatedDate > DateTime.Now)
.ToList();
// Assert
Assert.AreEqual(0, invalidDates.Count);
}
/*
* 2. Query Tests
* Each test verifies one specific query from Program.cs
*/
[TestMethod]
public void Query1_CountCommentsPerUser_PetrHasTwoComments()
{
// Act
var petrComments = _context.BlogPosts
.SelectMany(p => p.Comments)
.Count(c => c.UserName == "Petr");
// Assert
Assert.AreEqual(2, petrComments);
}
[TestMethod]
public void Query1_CountCommentsPerUser_ElenaHasThreeComments()
{
// Act
var elenaComments = _context.BlogPosts
.SelectMany(p => p.Comments)
.Count(c => c.UserName == "Elena");
// Assert
Assert.AreEqual(3, elenaComments);
}
[TestMethod]
public void Query1_CountCommentsPerUser_IvanHasFourComments()
{
// Act
var ivanComments = _context.BlogPosts
.SelectMany(p => p.Comments)
.Count(c => c.UserName == "Ivan");
// Assert
Assert.AreEqual(4, ivanComments);
}
[TestMethod]
public void Query2_PostsOrderedByLastCommentDate_CorrectOrder()
{
// Act
var orderedPosts = _context.BlogPosts
.Select(p => new {
p.Title,
LastCommentDate = p.Comments.Max(c => c.CreatedDate)
})
.OrderByDescending(p => p.LastCommentDate)
.Select(p => p.Title)
.ToList();
// Assert
CollectionAssert.AreEqual(
new List<string> { "Post2", "Post1", "Post3" },
orderedPosts
);
}
[TestMethod]
public void Query3_CountLastCommentsPerUser_IvanHasTwo()
{
// Act
var ivanLastComments = _context.BlogPosts
.Select(p => p.Comments
.OrderByDescending(c => c.CreatedDate)
.First())
.Count(c => c.UserName == "Ivan");
// Assert
Assert.AreEqual(2, ivanLastComments);
}
//[TestMethod]
//public void Query3_CountLastCommentsPerUser_ElenaHasOne()
//{
// // Arrange - Setup is handled in TestInitialize
// // Act
// var elenaLastComments = _context.BlogPosts
// .Select(p => p.Comments
// .OrderByDescending(c => c.CreatedDate)
// .FirstOrDefault()) // Use FirstOrDefault instead of First
// .Where(c => c != null && c.UserName == "Elena") // Explicit null check
// .Count();
// // Assert
// Assert.AreEqual(1, elenaLastComments,
// "Elena should have exactly 1 last comment across all posts");
//}
[TestMethod]
public void Query_Performance_GetAllPostsUnder50ms()
{
// Arrange
var stopwatch = new Stopwatch();
// Act
stopwatch.Start();
var posts = _context.BlogPosts.ToList();
stopwatch.Stop();
// Assert
Assert.IsTrue(stopwatch.ElapsedMilliseconds < 50);
}
/*
* 3. CRUD Operation Tests
* Each test verifies one create/read/update/delete operation
*/
[TestMethod]
public void Create_NewBlogPost_SavesToDatabase()
{
// Arrange
var newPost = new BlogPost("New Post");
// Act
_context.BlogPosts.Add(newPost);
_context.SaveChanges();
// Assert
Assert.IsTrue(_context.BlogPosts.Any(p => p.Title == "New Post"));
}
[TestMethod]
public void Create_NewComment_SavesToDatabase()
{
// Arrange
var post = _context.BlogPosts.First();
var newComment = new BlogComment("New Comment", DateTime.Now, "TestUser");
// Act
post.Comments.Add(newComment);
_context.SaveChanges();
// Assert
Assert.IsTrue(_context.BlogComments.Any(c => c.Text == "New Comment"));
}
[TestMethod]
public void Update_BlogPostTitle_UpdatesInDatabase()
{
// Arrange
var post = _context.BlogPosts.First();
var newTitle = "Updated Title";
// Act
post.Title = newTitle;
_context.SaveChanges();
// Assert
Assert.AreEqual(newTitle, _context.BlogPosts.Find(post.Id).Title);
}
[TestMethod]
public void Delete_BlogPost_RemovesFromDatabase()
{
// Arrange
var post = _context.BlogPosts.First();
// Act
_context.BlogPosts.Remove(post);
_context.SaveChanges();
// Assert
Assert.IsFalse(_context.BlogPosts.Any(p => p.Id == post.Id));
}
//[TestMethod]
//public void ConcurrentAccess_MultipleReads_Succeeds()
//{
// // Arrange - Setup is handled in TestInitialize
// // Act
// var task1 = Task.Run(() => _context.BlogPosts.ToList());
// var task2 = Task.Run(() => _context.BlogComments.ToList());
// Task.WaitAll(task1, task2);
// // Assert
// Assert.AreEqual(3, task1.Result.Count,
// "Should retrieve all 3 seeded blog posts concurrently");
// Assert.AreEqual(9, task2.Result.Count,
// "Should retrieve all 9 seeded comments concurrently");
//}
/*
* 4. Relationship Tests
* Each test verifies navigation properties and foreign keys
*/
[TestMethod]
public void BlogComment_BlogPostNavigation_IsCorrect()
{
// Act
var comment = _context.BlogComments
.Include(c => c.BlogPost)
.First();
// Assert
Assert.IsNotNull(comment.BlogPost);
Assert.AreEqual(comment.BlogPostId, comment.BlogPost.Id);
}
[TestMethod]
public void BlogPost_CommentsNavigation_IsCorrect()
{
// Act
var post = _context.BlogPosts
.Include(p => p.Comments)
.First();
// Assert
Assert.IsNotNull(post.Comments);
Assert.IsTrue(post.Comments.All(c => c.BlogPostId == post.Id));
}
[TestMethod]
public void BlogPost_WithNoComments_ReturnsEmptyCollection()
{
// Arrange
var newPost = new BlogPost("Empty Post");
_context.BlogPosts.Add(newPost);
_context.SaveChanges();
// Act
var comments = _context.Entry(newPost)
.Collection(p => p.Comments)
.Query()
.ToList();
// Assert
Assert.AreEqual(0, comments.Count);
}
/*
* 5. JSON Output Test
* Verifies the JSON serialization shown in Program.cs
*/
[TestMethod]
public void JsonOutput_ContainsAllPostTitles()
{
// Act
var json = System.Text.Json.JsonSerializer.Serialize(
_context.BlogPosts.Select(p => p.Title).ToList()
);
// Assert
Assert.IsTrue(json.Contains("Post1"));
Assert.IsTrue(json.Contains("Post2"));
Assert.IsTrue(json.Contains("Post3"));
}
}
}