Escali License control 1
PriceListServiceTest.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 PriceListServiceTest(ITestOutputHelper testOutputHelper)
20 {
21 _testOutputHelper = testOutputHelper;
22 }
23
29 {
30 return new PriceList()
31 {
32 PriceListName = "2022",
33 DateFrom = DateTime.Parse("2018-01-01"),
34 DateTo = DateTime.Parse("2018-12-31"),
35 ProductSeq = 1,
36 CurrencySeq = 1
37 };
38 }
39
43 [Fact]
44 public async void AddPriceList_Success()
45 {
49
51 var res = await service.AddPriceList(new PriceList()
52 {
53 PriceListName = "2022",
54 DateFrom = DateTime.Parse("2018-01-01"),
55 DateTo = DateTime.Parse("2018-12-31"),
56 ProductSeq = 1,
57 CurrencySeq = 1
58 });
59
60 Assert.NotNull(res);
61 Assert.NotNull(res.Currency);
62 Assert.NotNull(res.Product);
63 Assert.Equal("2022", res.PriceListName);
64
65 }
66
70 [Fact]
72 {
76
78 var res = await service.AddPriceList(new PriceList()
79 {
80 PriceListName = string.Empty,
81 DateFrom = DateTime.Parse("2018-01-01"),
82 DateTo = DateTime.Parse("2018-12-31"),
83 ProductSeq = 1,
84 CurrencySeq = 1
85 });
86
87 // Assert.NotNull(res);
88 // Assert.NotNull(res.Currency);
89 // Assert.NotNull(res.Product);
90 Assert.Equal("", res.PriceListName);
91
92 }
93
97 [Fact]
99 {
103
105 var op = async () => await service.AddPriceList(new PriceList()
106 {
107 PriceListName = "2022",
108 DateFrom = DateTime.Parse("2018-12-31"),
109 DateTo = DateTime.Parse("2018-01-01"),
110 ProductSeq = 1,
111 CurrencySeq = 1
112 });
113
114 await Assert.ThrowsAsync<InvalidOperationException>(op);
115
116 }
117
121 [Fact]
122 public async void AddPriceList_DatesAreNull()
123 {
127
128 var priceList = new PriceList()
129 {
130 PriceListName = "2022",
131 ProductSeq = 1,
132 CurrencySeq = 1
133 };
134
136 var op = async () => await service.AddPriceList(priceList);
137
138 await Assert.ThrowsAsync<DbUpdateException>(op);
139
140 }
141
145 [Fact]
146 public async void AddPriceList_DatesAreEqual()
147 {
151
153 var op = async () => await service.AddPriceList(new PriceList()
154 {
155 PriceListName = "2022",
156 DateFrom = DateTime.Parse("2018-01-01"),
157 DateTo = DateTime.Parse("2018-01-01"),
158 ProductSeq = 1,
159 CurrencySeq = 1
160 });
161
162 await Assert.ThrowsAsync<InvalidOperationException>(op);
163
164 }
165
169 [Fact]
171 {
175
177 var op = async () => await service.AddPriceList(new PriceList()
178 {
179 PriceListName = "2022",
180 DateFrom = DateTime.Parse("2018-01-01"),
181 DateTo = DateTime.Parse("2018-12-31"),
182 ProductSeq = 1
183 });
184
185 await Assert.ThrowsAsync<DbUpdateException>(op);
186
187 }
188
192 [Fact]
193 public async void UpdatePriceList_Success()
194 {
198
199 var priceList = CreatePriceList();
200
201 var priceListDb = context.PriceLists.Add(priceList);
202 await context.SaveChangesAsync();
203
205
206 priceListDb.Entity.DateFrom = DateTime.Parse("2017-01-01");
207 var updatedPriceList = await service.UpdatePriceList(priceListDb.Entity);
208
209 Assert.Equal(priceList.PriceListName, updatedPriceList.PriceListName);
210
211 }
212
216 [Fact]
218 {
222
223 var priceList = CreatePriceList();
224
225 var priceListDb = context.PriceLists.Add(priceList);
226 await context.SaveChangesAsync();
227
229
230 priceListDb.Entity.DateFrom = DateTime.Parse("2019-01-01");
231 var op = async () => await service.UpdatePriceList(priceListDb.Entity);
232
233 await Assert.ThrowsAsync<InvalidOperationException>(op);
234
235 }
236
237 }
238}
var context
Definition: Program.cs:49
var service
Definition: Program.cs:48
PriceListService class inserts and updates PriceList in the Database
async void UpdatePriceList_Success()
passes if pricelist is correctly inserted into the pricelist-list
async void AddPriceList_Success()
passes if pricelist is correctly added to productlist-list
async void AddPriceList_DatesAreEqual()
Passes if pricelist datefrom and dateto is same date, and InvalidOperationException is thrown
async void AddPriceList_DatesAreNull()
passes if dates is not set on pricelist, and DbUpdateException is thrown
async void AddPriceList_DateFromNotBeforeDateTo()
passes if dateto is before datefrom, and InvalidOperationException is thrown
async void AddPriceList_NameIsEmptyString()
passes if inserted pricelist has empty string name
async void UpdatePriceList_DateFromNotBeforeDateTo()
passes if dateFrom not before dateto, throws InvalidOperationException
async void AddPriceList_MissingCurrencyFK()
passes ip pricelist is missing a currency reference, and throws a DbUpdateException
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 InsertProduct(DataContext context)
Inserts Product into the context Used for inserting product into the test environment
Definition: TestData.cs:28
static DataContext InsertCurrency(DataContext context)
Inserts Currency into DataContext
Definition: TestData.cs:87