/
ComplexTypeDemos.cs
96 lines (81 loc) · 3.49 KB
/
ComplexTypeDemos.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Threading.Tasks;
using Demos.ComplexTypes.Model;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Xunit;
using Xunit.Abstractions;
namespace Demos.ComplexTypes;
public class ComplexTypeDemos
{
private readonly ITestOutputHelper output;
public ComplexTypeDemos(ITestOutputHelper output)
{
this.output = output;
}
[Fact]
public async Task TrackingMutablePrices_LeadsToIssues()
{
// Add product
await using var writeDbContext = DemoDbContext.Create(output: output);
var productPrice = new MutablePrice(100, Currency.USD);
writeDbContext.Set<MutablePriceProduct>().Add(new MutablePriceProduct("ProductA")
{
RegularPrice = productPrice,
RetailPrice = productPrice
});
await writeDbContext.SaveChangesAsync();
// Change one of the prices on product
var productToModify = await writeDbContext.Set<MutablePriceProduct>().FirstAsync();
productToModify.RegularPrice.Amount = 250;
await writeDbContext.SaveChangesAsync();
// Assert product
await using var readContext = DemoDbContext.Create();
var resultingProduct = await readContext.Set<MutablePriceProduct>().FirstAsync();
resultingProduct.RegularPrice.Amount.Should().Be(250m);
resultingProduct.RetailPrice.Amount.Should().Be(250m);
}
[Fact]
public async Task TrackingImmutablePrices_LeadsToExpectedResults()
{
// Add product
await using var writeDbContext = DemoDbContext.Create(output: output);
var productPrice = new ImmutablePrice(100, Currency.USD);
writeDbContext.Set<ImmutablePriceProduct>().Add(new ImmutablePriceProduct("ProductA")
{
RegularPrice = productPrice,
RetailPrice = productPrice
});
await writeDbContext.SaveChangesAsync();
// Change one of the prices on product
var productToModify = await writeDbContext.Set<ImmutablePriceProduct>().FirstAsync();
productToModify.RegularPrice = new ImmutablePrice(250, Currency.USD);
await writeDbContext.SaveChangesAsync();
// Assert product
await using var readContext = DemoDbContext.Create();
var resultingProduct = await readContext.Set<ImmutablePriceProduct>().FirstAsync();
resultingProduct.RegularPrice.Amount.Should().Be(250m);
resultingProduct.RetailPrice.Amount.Should().Be(100m);
}
[Fact]
public async Task TrackingRecordPrices_LeadsToExpectedResults()
{
// Add product
await using var writeDbContext = DemoDbContext.Create(output: output);
var productPrice = new RecordPrice(100, Currency.USD);
writeDbContext.Set<RecordPriceProduct>().Add(new RecordPriceProduct("ProductA")
{
RegularPrice = productPrice,
RetailPrice = productPrice
});
await writeDbContext.SaveChangesAsync();
// Change one of the prices on product
var productToModify = await writeDbContext.Set<RecordPriceProduct>().FirstAsync();
productToModify.RegularPrice = new RecordPrice(250, Currency.USD);
await writeDbContext.SaveChangesAsync();
// Assert product
await using var readContext = DemoDbContext.Create();
var resultingProduct = await readContext.Set<RecordPriceProduct>().FirstAsync();
resultingProduct.RegularPrice.Amount.Should().Be(250m);
resultingProduct.RetailPrice.Amount.Should().Be(100m);
}
}