Escali License control 1
ModuleServiceTest.cs
Gå til dokumentasjonen til denne filen.
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
7using Microsoft.Data.Sqlite;
8using Microsoft.EntityFrameworkCore;
9using Xunit;
10using Xunit.Abstractions;
11
13{
14
19{
20 private readonly ITestOutputHelper _testOutputHelper;
21
22 public ModuleServiceTest(ITestOutputHelper testOutputHelper)
23 {
24 _testOutputHelper = testOutputHelper;
25 }
26
30 [Fact]
31 public async void GetAllModules_IsEmpty()
32 {
33
34 // Arrange
36
37 // Act
38 var moduleService = new ModuleService(context);
39 var res = await moduleService.GetAllModules();
40
41 // Assert
42 Assert.Empty(res);
43
44 }
45
49 [Fact]
51 {
52
53 // Arrange
56 var moduleName = context.Modules.FindAsync(1).Result.ModuleName;
57
58 // Act
59 var moduleService = new ModuleService(context);
60 var res = await moduleService.GetAllModules();
61
62 // Assert
63 Assert.NotEmpty(res);
64 Assert.Equal(moduleName, res[0].ModuleName);
65
66 }
67
71 [Fact]
73 {
74
75 // Arrange
77
78 // Act
79 var moduleService = new ModuleService(context);
80 var res = await moduleService.GetModuleById(100);
81
82 // Assert
83 Assert.Null(res);
84
85 }
86
90 [Fact]
91 public async void GetModuleById_ModuleExisting()
92 {
93
94 // Arrange
97
98 // Act
99 var moduleService = new ModuleService(context);
100 var res = await moduleService.GetModuleById(1);
101
102 // Assert
103 Assert.NotNull(res);
104
105 }
106
107
108
112 [Fact]
114 {
115 // Arrange
117
118 // Act
119 var moduleService = new ModuleService(context);
120 var res = Task.Run(() => moduleService.AddModule(new Module()
121 {
122 ModuleName = "Module1"
123 }));
124
125 //var ex = await Assert.ThrowsAsync<DbUpdateException>(op);
126 //Assert.IsType<SqliteException>(ex.InnerException);
127 //_testOutputHelper.WriteLine(ex.InnerException.Message);
128 Assert.NotNull(res);
129
130 }
131
136 [Fact]
137 public async void AddModule_ProductRefMissing()
138 {
139 // Arrange
141
142 // Act
143 var moduleService = new ModuleService(context);
144 var op = async () => await moduleService.AddModule(new Module()
145 {
146 ModuleName = "Module1"
147 });
148
149 // Assert
150 // Because product key is required in Module
151 await Assert.ThrowsAsync<ArgumentNullException>(op);
152 }
153
157 [Fact]
158 public async void AddModule_DuplicateName()
159 {
160
161 // Arrange
164
165 // Act
166 var moduleService = new ModuleService(context);
167
168 var op = async () => await moduleService.AddModule(new Module()
169 {
170 ModuleName = "FirstModule",
171 ProductSeq = context.Products.ToListAsync().Result.Find(p => p.ProductName == "BestProductEver").ProductSeq
172 });
173
174 await Assert.ThrowsAsync<InvalidOperationException>(op);
175
176 }
177
181 [Fact]
183 {
184 // Arrange
187
188 // Act
189 ModuleService moduleService = new ModuleService(context);
190 //var moduleFromDb = await moduleService.AddModule(new Module()
191 //{
192 // ModuleName = "NewModule",
193 // ProductSeq = context.Products.ToListAsync().Result.Find(p => p.ProductName == "BestProductEver").ProductSeq
194 //});
195 var moduleFromDb = await moduleService.GetModuleById(1);
196 var bestProductEver = context.Products.ToListAsync().Result.Find(p => p.ProductName == "BestProductEver");
197
198
199 // Assert
200 Assert.Equal(moduleFromDb.ModuleSeq, bestProductEver.Modules.Find(m => m.ProductSeq == bestProductEver.ProductSeq).ModuleSeq);
201 Assert.Equal(moduleFromDb.ModuleName, bestProductEver.Modules.Find(m => m.ProductSeq == bestProductEver.ProductSeq).ModuleName);
202
203 }
204
209 [Fact]
211 {
212 // Arrange
214
215 var product = context.Products.Add(TestData.CreateProduct());
216 await context.SaveChangesAsync();
217
218 context.Modules.Add(new Module()
219 {
220 ModuleName = "NewModule",
221 ProductSeq = 1
222 });
223 context.Modules.Add(new Module()
224 {
225 ModuleName = "AnotherModule",
226 ProductSeq = 1
227 });
228 await context.SaveChangesAsync();
229
230 ModuleService moduleService = new ModuleService(context);
231
232 // Act
233 var anotherModule = await context.Modules.FindAsync(2);
234 anotherModule.ModuleName = "NewModule";
235 var op = async () => await moduleService.UpdateModule(anotherModule);
236
237 // Assert
238 await Assert.ThrowsAsync<InvalidOperationException>(op);
239
240 }
241
245 [Fact]
247 {
248 // Arrange
251
252 ModuleService moduleService = new ModuleService(context);
254 var products = ps.GetAllProducts().Result;
255
256 // Act
257 var moduleFromDb = moduleService.GetAllModules().Result.Find(m => m.ModuleName == "FirstModule");
258 // Product before updating
259 Assert.Equal("BestProductEver", products.Find(m => m.ProductSeq == moduleFromDb.ProductSeq).ProductName);
260
261 moduleFromDb!.ProductSeq = products.Find(p => p.ProductName == "AnotherProduct").ProductSeq;
262 await moduleService.UpdateModule(moduleFromDb);
263
264 moduleFromDb = moduleService.GetAllModules().Result.Find(m => m.ModuleName == "FirstModule");
265 // Assert
266 // Product after update
267 Assert.Equal("AnotherProduct", products.Find(m => m.ProductSeq == moduleFromDb.ProductSeq).ProductName);
268
269 }
270
275 [Fact]
277 {
278 // Arrange
281
282 ModuleService moduleService = new ModuleService(context);
283
284 // Act (set to non existing object)
285 var moduleFromDb = moduleService.GetAllModules().Result.Find(m => m.ModuleName == "FirstModule");
286 moduleFromDb.ProductSeq = 534;
287 var op = async () => await moduleService.UpdateModule(moduleFromDb);
288
289 // Assert
290 await Assert.ThrowsAsync<DbUpdateException>(op);
291
292 }
293
297 [Fact]
299 {
300 // Arrange
303
304 ModuleService moduleService = new ModuleService(context);
305
306 // Act (set to null)
307 var moduleFromDb = moduleService.GetAllModules().Result.Find(m => m.ModuleName == "FirstModule");
308 moduleFromDb.Product = null;
309 var op = async () => await moduleService.UpdateModule(moduleFromDb);
310
311 // Assert
312 await Assert.ThrowsAsync<InvalidOperationException>(op);
313
314 }
315
319 [Fact]
320 public async void DeactiveModule_Success()
321 {
322 // Arrange
325
326 ModuleService moduleService = new ModuleService(context);
327
328 // Act
329 var moduleFromDbModuleSeq = moduleService.GetAllModules().Result.Find(m => m.ModuleName == "FirstModule").ModuleSeq;
330 await moduleService.DeactivateModule(moduleFromDbModuleSeq);
331
332 var deactivatedModule = moduleService.GetAllModules().Result.Find(m => m.ModuleName == "FirstModule");
333
334 // Assert
335 Assert.Equal(false, deactivatedModule.IsActive);
336
337 }
338
342 [Fact]
344 {
345
346 // Arrange
348 var product = context.Products.Add(new Product() { ProductName = "BestProductEver" });
349 await context.SaveChangesAsync();
350
351 var service = new ModuleService(context);
352
353 // Act
354 var moduleLevel = new ModuleLevel()
355 {
356 ModuleLevelName = "Duplicate",
357 ModuleLevelRestriction = "Res"
358 };
359
360 var moduleLevels = new List<ModuleLevel>
361 {
362 moduleLevel,
363 moduleLevel
364 };
365
366 var newModule = new Module()
367 {
368 ProductSeq = product.Entity.ProductSeq,
369 ModuleName = "ModuleName",
370 ModuleLevels = moduleLevels
371 };
372
373 // Assert
374 var op = async () => await service.AddModule(newModule);
375
376 var ex = await Assert.ThrowsAsync<InvalidOperationException>(op);
377 Assert.Equal("Modulnivåer kan ikke ha samme navn.", ex.Message);
378
379 }
380
384 [Fact]
386 {
387
388 // Arrange
390 var product = context.Products.Add(new Product() { ProductName = "BestProductEver" });
391 await context.SaveChangesAsync();
392
393 var service = new ModuleService(context);
394
395 // Act
396 var moduleLevel1 = new ModuleLevel()
397 {
398 ModuleLevelName = "Duplicate",
399 ModuleLevelRestriction = "Res"
400 };
401
402 var moduleLevel2 = new ModuleLevel()
403 {
404 ModuleLevelName = "Not duplicate",
405 ModuleLevelRestriction = "Res"
406 };
407
408 var moduleLevels = new List<ModuleLevel>
409 {
410 moduleLevel1,
411 moduleLevel2
412 };
413
414 var newModule = new Module()
415 {
416 ProductSeq = product.Entity.ProductSeq,
417 ModuleName = "ModuleName",
418 ModuleLevels = moduleLevels
419 };
420
421 var res = await service.AddModule(newModule);
422
423 Assert.Equal(2, res.ModuleLevels.Count);
424
425 }
426
430 [Fact]
431 public async void ActiveModule_Success()
432 {
433 // Arrange
436
437 ModuleService moduleService = new ModuleService(context);
438
439 // Act
440 var moduleFromDb = moduleService.GetAllModules().Result.Find(m => m.ModuleName == "FirstModule");
441 await moduleService.DeactivateModule(moduleFromDb.ModuleSeq);
442 // Checking if module is deactivated IsActive=false
443 Assert.False(moduleFromDb.IsActive);
444
445 var updatedModule = moduleService.GetAllModules().Result.Find(m => m.ModuleName == "FirstModule");
446 await moduleService.ActivateModule(updatedModule.ModuleSeq);
447
448 var activatedModule = moduleService.GetAllModules().Result.Find(m => m.ModuleName == "FirstModule");
449
450 // Assert, IsActive=true
451 Assert.True(activatedModule.IsActive);
452
453 }
454
455 }
456}
var context
Definition: Program.cs:49
var service
Definition: Program.cs:48
ModuleService class inserts and updates Module in the Database
async Task< Module > GetModuleById(int id)
Read one Module with id from database
async Task< Module > UpdateModule(Module module)
Updates changes on a Module in the database
async Task< Module > ActivateModule(int moduleSeq)
Activates a Module by setting IsActive attribute to true
async Task< Module > DeactivateModule(int moduleSeq)
Deactivated a Module by setting IsActive attribute to false
async Task< List< Module > > GetAllModules()
Read all Modules from database
ProductService class inserts and updates Product in the Database
async Task< List< Product > > GetAllProducts()
Read all Products from database
async void GetAllModules_NotEmptyAndCorrectObject()
passes if after insertion module-list is not empty and added module is same as in list
async void UpdateModule_ChangeProductForModuleSuccess()
passes if product reference is correctly changed when updating product reference
async void AddModule_CheckDupliacteModuleLevelNames()
passes if trying to add new modulelevel with already existing modulelevelname
async void UpdateModule_DuplicateNameCheck()
passes if trying to update modulename to name that already exist in module-list, and throws InvalidOp...
async void DeactiveModule_Success()
passes if moduleinactiv is set to false
async void AddModule_ModuleLevelsNotDuplicateNames()
passes if correctly added modulelevelnames in module for insertion
async void GetModuleById_ModuleExisting()
passes if module is found by id
async void AddModule_ProductHasCorrectModule()
passes if product-list contain correct modules from
async void AddModule_ProductRefMissing()
passes if trying to add module without product reference, and it throws DbUpdateException
async void UpdateModule_ChangeProductSetNullShouldFail()
passes if trying to set module's product reference to null, and it throws InvalidOperationException
async void ActiveModule_Success()
passes if inactive module(module.isActive = false) is set to true
async void AddModule_DuplicateName()
passes if modulename already exist in module-list, and throws InvalidOperationException
async void UpdateModule_ChangeProductToProductSeqNotExisting()
passes if trying to change module's product ref to a product that doesn't exist, and it throws a DbUp...
async void GetAllModules_IsEmpty()
passes if module-list is empty
async void GetModuleById_ModuleNotExisting()
passes if module with moduleid does not exist, returned null
static DataContext GetContext()
Creates a testcontext for testing
Definition: TestBase.cs:14
Reusable methods for inserting information into tests
Definition: TestData.cs:11
static DataContext InsertProductAndModule(DataContext context)
Inserts Product and Module into the context Used for inserting product and module into the test envir...
Definition: TestData.cs:41
static Product CreateProduct()
Creates a Product
Definition: TestData.cs:17
static DataContext InsertDoubleProductAndModule(DataContext context)
Inserts two Product and Module into the context Used for inserting product and module into the test e...
Definition: TestData.cs:55