Escali License control 1
ModuleLevelServiceTest.cs
Gå til dokumentasjonen til denne filen.
1using System;
5using Microsoft.EntityFrameworkCore;
6using Xunit;
7using Xunit.Abstractions;
8
10{
11
16{
17 private readonly ITestOutputHelper _testOutputHelper;
18
19 public ModuleLevelServiceTest(ITestOutputHelper testOutputHelper)
20 {
21 _testOutputHelper = testOutputHelper;
22 }
23
29 {
30 return new ModuleLevel()
31 {
32 ModuleLevelName = "Stocks 1",
33 ModuleLevelRestriction = "250M/100tr",
34 ModuleSeq = 1
35 };
36 }
37
41 [Fact]
42 public async void GetAllModuleLevels_IsEmpty()
43 {
44
45 // Arrange
47
48 // Act
50 var res = await service.GetAllModuleLevels();
51
52 // Assert
53 Assert.Empty(res);
54
55 }
56
61 [Fact]
63 {
64
65 // Arrange
68 var moduleLevel = CreateModuleLevelObject();
69 var addedModuleLevel = context.ModuleLevels.Add(moduleLevel);
70 await context.SaveChangesAsync();
71
72 // Act
74 var res = await service.GetAllModuleLevels();
75
76 // Assert
77 Assert.NotEmpty(res);
78 Assert.Equal(moduleLevel.ModuleLevelName, res[0].ModuleLevelName);
79
80 }
81
85 [Fact]
87 {
88
89 // Arrange
91
92 // Act
94 var res = await service.GetModuleLevelById(100);
95
96 // Assert
97 Assert.Null(res);
98
99 }
100
104 [Fact]
106 {
107
108 // Arrange
111 context.ModuleLevels.Add(CreateModuleLevelObject());
112 await context.SaveChangesAsync();
113
114 // Act
115 var moduleService = new ModuleService(context);
116 var res = await moduleService.GetModuleById(1);
117
118 // Assert
119 Assert.NotNull(res);
120
121 }
122
126 [Fact]
127 public async void AddModuleLevel_Success()
128 {
129
132
134 var moduleLevel = CreateModuleLevelObject();
135 _testOutputHelper.WriteLine(moduleLevel.ModuleLevelRestriction);
136
137 var res = await service.AddModuleLevel(moduleLevel);
138
139 Assert.Equal(moduleLevel.ModuleLevelName, res.ModuleLevelName);
140 Assert.NotNull(moduleLevel.Module);
141 _testOutputHelper.WriteLine(moduleLevel.ModuleLevelRestriction);
142
143 }
144
148 [Fact]
150 {
151
154
156 var moduleLevel = CreateModuleLevelObject();
157
158 await service.AddModuleLevel(moduleLevel);
159 var op = async () => await service.AddModuleLevel(CreateModuleLevelObject());
160
161 var ex = await Assert.ThrowsAsync<InvalidOperationException>(op);
162
163 }
164
168 [Fact]
170 {
171
173
175 var moduleLevel = new ModuleLevel()
176 {
177 ModuleLevelName = "Stocks 1",
178 ModuleLevelRestriction = "250M/100tr"
179 }; ;
180
181 var op = async () => await service.AddModuleLevel(moduleLevel);
182
183 await Assert.ThrowsAsync<DbUpdateException>(op);
184
185 }
186
187 }
188}
var context
Definition: Program.cs:49
var service
Definition: Program.cs:48
ModuleLevelService class inserts and updates ModuleLevel in the Database
ModuleService class inserts and updates Module in the Database
async void AddModuleLevel_DuplicateName()
passes if modulelevelname already exist, and it throws an InvalidOperationException
async void GetModuleLevelById_ModuleLevelNotExisting()
passes if modulelevel with id 100 does not exist
async void AddModuleLevel_MissingModuleRef()
passes if modulelevel is missing a reference to a module, and it throws a DbUpdateException
async void AddModuleLevel_Success()
passes if modulelevel is correctly inserted
async void GetModuleById_ModuleExisting()
passes if modulelevel is inserted, and module is found by id
async void GetAllModuleLevels_IsEmpty()
passes if modulelevels-list is empty
async void GetAllModuleLevels_NotEmptyAndCorrectObject()
passes if modulelevel-list is not empty after insertion, and the modulelevelname equals the added mod...
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