Escali License control 1
CustomerServiceTest.cs
Gå til dokumentasjonen til denne filen.
1using System;
2using System.Threading.Tasks;
6using Xunit;
7
9{
10
15{
16
20 [Fact]
21 public async void GetAllCustomers_ListIsEmpty()
22 {
24 var customerService = new CustomerService(context);
25
26 var res = await customerService.GetAllCustomers();
27
28 Assert.Empty(res);
29
30 }
31
35 [Fact]
37 {
40 var customersService = new CustomerService(context);
41
42 var res = await Task.Run(() => customersService.GetAllCustomers());
43
44 Assert.NotEmpty(res);
45 }
46
50 [Fact]
52 {
55
56 var customerService = new CustomerService(context);
57
58 var res = await Assert.ThrowsAsync<InvalidOperationException>(() => customerService.AddCustomer(new Customer()
59 {
60 OrganizationNumber = "111 111 111",
61 CustomerName = "Customer1",
62 CustomerSince = DateTime.Now
63 }));
64 Assert.Equal($"OrgNr or CustomerName already Exist", res.Message);
65 }
66
70 [Fact]
72 {
74 var customerService = new CustomerService(context);
75
76 var res = await Task.Run(() => customerService.AddCustomer(new Customer()
77 {
78 OrganizationNumber = "111 111 111",
79 CustomerName = "Customer1",
80 CustomerSince = DateTime.Now,
81 Region = new Region()
82 {
83 Country = "Norge"
84 }
85 }));
86 Assert.NotNull(res);
87 }
88
93 [Fact]
95 {
98
99 var customerService = new CustomerService(context);
100
101 var name = await Assert.ThrowsAsync<InvalidOperationException>(() => customerService.UpdateCustomer(new Customer()
102 {
103 OrganizationNumber = "111 111 111",
104 CustomerName = "Customer1",
105 CustomerSince = DateTime.Now
106 }));
107
108 var number = await Assert.ThrowsAsync<InvalidOperationException>(() => customerService.UpdateCustomer(new Customer()
109 {
110 OrganizationNumber = "111 111 111",
111 CustomerName = "Customer123",
112 CustomerSince = DateTime.Now
113 }));
114
115 Assert.Equal($"Kunde med navn 'Customer1' finnes allerede.", name.Message);
116 Assert.Equal($"Kunde med org.nr. '111 111 111' finnes allerede.", number.Message);
117 }
118
122 [Fact]
124 {
127 var customerService = new CustomerService(context);
128
129 var res = customerService.UpdateCustomer(new Customer()
130 {
131 OrganizationNumber = "999 999 999",
132 CustomerName = "999999",
133 CustomerSince = DateTime.Now
134 });
135
136 //True hvis Task har kastet et unhandled exception
137 //Så bra at den er true i så måte at det ikke vil skje noe dersom
138 //noen prøver å oppdatere noe som ikke finnes
139 Assert.True(res.IsFaulted);
140 }
141
145 [Fact]
147 {
150
151 var customerService = new CustomerService(context);
152 var customer = customerService.GetCustomerById(1);
153 customer.Result.CustomerName = "CustomerUpdated";
154 var res = await Task.Run(() => customerService.UpdateCustomer(customer.Result));
155
156 Assert.NotNull(res);
157 Assert.Equal(customer.Result.CustomerName, res.CustomerName);
158
159 }
160
164 [Fact]
166 {
167
170
171 var customerService = new CustomerService(context);
172 var res = async () => await customerService.DeactivateCustomer(2);
173
174 await Assert.ThrowsAsync<NullReferenceException>(res);
175 }
176
180 [Fact]
182 {
183
186
187 var customerService = new CustomerService(context);
188 var res = await Task.Run(() => customerService.DeactivateCustomer(1));
189
190 Assert.True(res.IsActive == false);
191
192 }
193
198 [Fact]
200 {
201
204
205 var customerService = new CustomerService(context);
206 var res = async () => await customerService.ActivateCustomer(2);
207
208 await Assert.ThrowsAsync<NullReferenceException>(res);
209 }
210
214 [Fact]
216 {
217
220
221 var customerService = new CustomerService(context);
222 var res = await Task.Run(() => customerService.ActivateCustomer(1));
223
224 Assert.True(res.IsActive == true);
225
226 }
227
231 [Fact]
233 {
236 var customerService = new CustomerService(context);
237
238 var firstEntry = await Task.Run(() => customerService.GetRegionSeq(region: context.Customers.Find(1).Region));
239 var secondEntry = await Task.Run(() => customerService.GetRegionSeq(region: context.Customers.Find(2).Region));
240 Assert.Equal(1, firstEntry);
241 Assert.Equal(2, secondEntry);
242 }
243
247 [Fact]
249 {
252 var customerService = new CustomerService(context);
253
254 var res = await Task.Run(() => customerService.AddCustomer(new Customer()
255 {
256 CustomerName = "Ikke brukt",
257 OrganizationNumber = "000 000 000",
258 CustomerSince = DateTime.Now,
259 Region = new Region()
260 {
261 Country = "Sverige",
262 Area = "Stockholm"
263 }
264 }));
265
266 Assert.Equal(3, res.RegionSeq);
267 }
268 }
269}
var context
Definition: Program.cs:49
CustomerService class inserts and updates Customer in the Database
async Task CustomerService_GetRegionSeq()
passes if region reference is found on customers in customerlist
async void DeactivateCustomer_CustomerDoesNotExist()
passes if trying to deactivate a customer that does not exist, and it throws NullReferenceException
async Task AddCustomer_CustomerDoesNotExist()
passes if customer does not exist and is correctly added
async Task AddCustomer_CustomerNameAlreadyExist()
passes if customername already exist in customer-list and it throws InvalidOperationException
async Task ActivateCustomer_CustomerExist()
passes if trying to activate a customer that exist
async Task UpdateCustomer_CustomerNameAndOrganizationNumberExist()
passes if customername and organizationname already exist in customer-list, and it throws InvalidOper...
async void UpdateCustomer_CustomerDoesNotExistReturnsNull()
passes if trying to update a customer that does not exist in customerlist
async Task UpdateCustomer_CustomerDoesExistReturnsEntity()
passes if customer exist in customerlist, and is correctly updated
async Task DeactivateCustomer_CustomerExist()
passes if trying to deactivate customer that does exist
async void ActivateCustomer_CustomerDoesNotExist()
passes if trying to activate a customer that does not exist, and it throws NullReferenceException
async void GetAllCustomers_ListIsEmpty()
passes if customerlist is empty
async Task GetAllCustomers_AddedElementExist()
passes if customer-list is not empty after adding a customer
async Task CustomerServiceCreatesNewRegion_GetRegionSeq()
passes if trying to add customer with new region gets a new regionseq
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 InsertCustomers(DataContext context)
Insert two Customers into DataContext
Definition: TestData.cs:149
static DataContext InsertCustomerWithRegionData(DataContext context)
Inserts two Customer combined with region data into the DataContext
Definition: TestData.cs:172
static DataContext InsertCustomer(DataContext context)
Inserting customer into DB (only non nullable field defined)
Definition: TestData.cs:132