Escali License control 1
CurrencyServiceTest.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 CurrencyServiceTest(ITestOutputHelper testOutputHelper)
20 {
21 _testOutputHelper = testOutputHelper;
22 }
23
29 {
30 return new Currency() {CurrencyName = "USD"};
31 }
32
37 [Fact]
38 public async void AddCurrency_DuplicateName()
39 {
41 context.Currencies.Add(CreateCurrency());
42 await context.SaveChangesAsync();
43
45 var op = async () => await service.AddCurrency(CreateCurrency());
46
47 var allCurrencies = await context.Currencies.ToListAsync();
48
49 await Assert.ThrowsAsync<InvalidOperationException>(op);
50 Assert.Single(allCurrencies);
51
52 }
53
57 [Fact]
59 {
61
63 var res = await service.AddCurrency(new Currency() {CurrencyName = "usd"});
64
65 Assert.Equal("USD", res.CurrencyName);
66
67 }
68
73 [Fact]
75 {
78 var res = context.Currencies.Add(CreateCurrency());
79 await context.SaveChangesAsync();
80
81 context.PriceLists.Add(new PriceList()
82 {
83 PriceListName = "2022 USD",
84 DateFrom = DateTime.Now,
85 DateTo = DateTime.Now,
86 CurrencySeq = res.Entity.CurrencySeq,
87 ProductSeq = 1
88 });
89
90 await context.SaveChangesAsync();
91
93 var op = async () => await service.DeleteCurrency(1);
94
95 await Assert.ThrowsAsync<InvalidOperationException>(op);
96 }
97
101 [Fact]
102 public async void GetCurrencyByName_Success()
103 {
105 var currency = CreateCurrency();
106 var currencyDb = context.Currencies.Add(currency);
107 await context.SaveChangesAsync();
108
110 var res = await service.GetCurrencyByName(currency.CurrencyName);
111
112 Assert.Equal(currencyDb.Entity.CurrencySeq, res.CurrencySeq);
113 }
114
118 [Fact]
119 public async void GetCurrencyByName_Fail()
120 {
122
124 var res = await service.GetCurrencyByName("USD");
125
126 Assert.Null(res);
127 }
128
129
130 }
131}
var context
Definition: Program.cs:49
var service
Definition: Program.cs:48
CurrencyService class inserts and updates Currency in the Database
async void DeleteCurrency_ShouldNotBeDeletedWhenUsedAsFK()
passes if trying to delete currency while it is referenced in pricelist, and throws InvalidOperationE...
async void AddCurrency_NameConvertedToUpperChars()
passes if after adding currencyname is converted to upperchars
async void GetCurrencyByName_Success()
passes if correctly retrieved currency by currencyname
async void AddCurrency_DuplicateName()
passes if currencyname already exist, and it throws InvalidOperationException, and list only has one ...
async void GetCurrencyByName_Fail()
passes if there does not exist a currency with the retrieved currency by currencyname
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