-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathOrderSqlRepositoryTests.cs
executable file
·215 lines (195 loc) · 9.51 KB
/
OrderSqlRepositoryTests.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using FluentAssertions;
using Marten.Exceptions;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MyCompany.ECommerce.Sales.Commons;
using MyCompany.ECommerce.Sales.Orders.PriceChanges;
using MyCompany.ECommerce.Sales.Pricing;
using MyCompany.ECommerce.Sales.Products;
using MyCompany.ECommerce.Sales.Time;
using MyCompany.ECommerce.TechnicalStuff.Persistence;
using MyCompany.ECommerce.TechnicalStuff.Postgres;
using Xunit;
namespace MyCompany.ECommerce.Sales.Orders;
public class OrderSqlRepositoryTests : IClassFixture<WebApplicationFactory<Program>>
{
private readonly PriceChangesPolicy _priceChangesPolicies = new FakePriceChangesPolicy();
private readonly SystemClock _clock = new();
private readonly IServiceProvider _serviceProvider;
private string _repositoryImplementation;
public OrderSqlRepositoryTests(WebApplicationFactory<Program> appFactory) =>
_serviceProvider = appFactory.Services;
[Theory]
[InlineData(nameof(OrderSqlRepository.Raw))]
[InlineData(nameof(OrderSqlRepository.EF))]
[InlineData(nameof(OrderSqlRepository.Document))]
[InlineData(nameof(OrderSqlRepository.EventsSourcing))]
public async Task RestoredOrderIsEqualToOriginal(string repositoryImplementation)
{
_repositoryImplementation = repositoryImplementation;
var (productAmount1, productAmount2, productAmount3) = CreateProductAmounts();
var offer1 = CreateOfferFor(productAmount1, productAmount2);
var offer2 = CreateOfferFor(productAmount1, productAmount2, productAmount3);
var now = _clock.Now;
var orderId = await TestRestoreForNewOrder();
await TestRestoreAfter(orderId, order => order.Add(productAmount1));
await TestRestoreAfter(orderId, order => order.Add(productAmount2));
await TestRestoreAfter(orderId, order => order.ConfirmPrices(offer1, _priceChangesPolicies, now.AddDays(2)));
await TestRestoreAfter(orderId, order => order.Add(productAmount3));
await TestRestoreAfter(orderId, order => order.ConfirmPrices(offer2, _priceChangesPolicies, now.AddDays(3)));
await TestRestoreAfter(orderId, order => order.Place(now.AddDays(2)));
await TestRestoreForOrderFromOffer(offer2);
}
[Theory]
[InlineData(nameof(OrderSqlRepository.Raw))]
[InlineData(nameof(OrderSqlRepository.EF))]
[InlineData(nameof(OrderSqlRepository.Document))]
[InlineData(nameof(OrderSqlRepository.EventsSourcing))]
public async Task CanNotSaveOrderIfVersionHasChangedInDb(string repositoryImplementation)
{
_repositoryImplementation = repositoryImplementation;
using var scope0 = CreateScope();
var order = scope0.OrderFactory.NewWith(Money.Of(decimal.MaxValue, Currency.PLN));
await scope0.OrderRepository.Save(order);
await scope0.TransactionProvider.CommitCurrentTransaction();
using var scope1 = CreateScope();
using var scope2 = CreateScope();
var order1 = await scope1.OrderRepository.GetBy(order.Id);
var order2 = await scope2.OrderRepository.GetBy(order.Id);
order1.Add(ProductAmount.Of(ProductId.New(), Amount.Of(3, AmountUnit.Box)));
order2.Add(ProductAmount.Of(ProductId.New(), Amount.Of(7, AmountUnit.Unit)));
var action1 = async () =>
{
await scope1.OrderRepository.Save(order1);
await scope1.TransactionProvider.CommitCurrentTransaction();
};
var action2 = async () =>
{
await scope2.OrderRepository.Save(order2);
await scope2.TransactionProvider.CommitCurrentTransaction();
};
await action1.Should().NotThrowAsync();
switch (repositoryImplementation)
{
case nameof(OrderSqlRepository.Raw):
await action2.Should().ThrowExactlyAsync<OptimisticLockException>();
break;
case nameof(OrderSqlRepository.EF):
await action2.Should().ThrowExactlyAsync<DbUpdateConcurrencyException>();
break;
case nameof(OrderSqlRepository.Document):
await action2.Should().ThrowExactlyAsync<ConcurrencyException>();
break;
case nameof(OrderSqlRepository.EventsSourcing):
await action2.Should().ThrowExactlyAsync<EventStreamUnexpectedMaxEventIdException>();
break;
default:
throw new ArgumentOutOfRangeException(nameof(_repositoryImplementation),
repositoryImplementation,
null);
}
}
private static (ProductAmount, ProductAmount, ProductAmount) CreateProductAmounts()
{
var product1 = ProductId.New();
var product2 = ProductId.New();
var productAmount1 = ProductAmount.Of(
product1,
Amount.Of(3, AmountUnit.Palette));
var productAmount2 = ProductAmount.Of(
product2,
Amount.Of(5, AmountUnit.Box));
var productAmount3 = ProductAmount.Of(
product2,
Amount.Of(7, AmountUnit.Unit));
return (productAmount1, productAmount2, productAmount3);
}
private static Offer CreateOfferFor(params ProductAmount[] productAmounts) => Offer.FromQuotes(
Currency.PLN,
productAmounts.Select((productAmount, index) => Quote.For(
productAmount,
Money.Of((index + 1) * 1.23m, Currency.PLN))));
private async Task<OrderId> TestRestoreForNewOrder()
{
using var scope = CreateScope();
var order = scope.OrderFactory.NewWith(Money.Of(decimal.MaxValue, Currency.PLN));
await scope.OrderRepository.Save(order);
await scope.TransactionProvider.CommitCurrentTransaction();
await TestRestore(order);
return order.Id;
}
private async Task TestRestoreForOrderFromOffer(Offer offer)
{
using var scope = CreateScope();
var order = scope.OrderFactory.ImmediatelyPlacedBasedOn(offer);
await scope.OrderRepository.Save(order);
await scope.TransactionProvider.CommitCurrentTransaction();
await TestRestore(order);
}
private async Task TestRestoreAfter(OrderId orderId, Action<Order> action)
{
using var scope = CreateScope();
var order = await scope.OrderRepository.GetBy(orderId);
action(order);
await scope.OrderRepository.Save(order);
await scope.TransactionProvider.CommitCurrentTransaction();
await TestRestore(order);
}
private async Task TestRestore(Order savedOrder)
{
using var scope = CreateScope();
var restoredOrder = await scope.OrderRepository.GetBy(savedOrder.Id);
restoredOrder.Should().BeEquivalentTo(savedOrder);
}
private Scope CreateScope() => new(_serviceProvider, _repositoryImplementation);
private class Scope : IDisposable
{
private readonly IServiceScope _scope;
public PostgresTransactionProvider TransactionProvider { get; }
public Order.Repository OrderRepository { get; }
public Order.Factory OrderFactory { get; }
public Scope(IServiceProvider serviceProvider, string repositoryImplementation)
{
_scope = serviceProvider.CreateScope();
TransactionProvider = _scope.ServiceProvider.GetRequiredService<PostgresTransactionProvider>();
OrderRepository = CreateRepository(_scope, repositoryImplementation);
OrderFactory = CreateFactory(_scope, repositoryImplementation);
}
public void Dispose()
{
TransactionProvider.Dispose();
_scope.Dispose();
}
private static Order.Repository CreateRepository(IServiceScope scope, string repositoryImplementation) =>
repositoryImplementation switch
{
nameof(OrderSqlRepository.Raw) => scope.ServiceProvider
.GetRequiredService<OrderSqlRepository.Raw>(),
nameof(OrderSqlRepository.EF) => scope.ServiceProvider
.GetRequiredService<OrderSqlRepository.EF>(),
nameof(OrderSqlRepository.Document) => scope.ServiceProvider
.GetRequiredService<OrderSqlRepository.Document>(),
nameof(OrderSqlRepository.EventsSourcing) => scope.ServiceProvider
.GetRequiredService<OrderSqlRepository.EventsSourcing>(),
_ => throw new ArgumentOutOfRangeException(nameof(_repositoryImplementation),
repositoryImplementation,
null)
};
private static Order.Factory CreateFactory(IServiceScope scope, string repositoryImplementation) =>
repositoryImplementation switch
{
nameof(OrderSqlRepository.Raw) => scope.ServiceProvider
.GetRequiredService<OrderSqlRepository.Raw>(),
nameof(OrderSqlRepository.EF) => scope.ServiceProvider
.GetRequiredService<OrderSqlRepository.EF>(),
nameof(OrderSqlRepository.Document) => scope.ServiceProvider
.GetRequiredService<OrderSqlRepository.Document>(),
nameof(OrderSqlRepository.EventsSourcing) => scope.ServiceProvider
.GetRequiredService<OrderSqlRepository.EventsSourcing>(),
_ => throw new ArgumentOutOfRangeException(nameof(_repositoryImplementation),
repositoryImplementation,
null)
};
}
}