-
Notifications
You must be signed in to change notification settings - Fork 69
/
CreateTransactionTests.cs
121 lines (105 loc) · 5.26 KB
/
CreateTransactionTests.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
using FluentAssertions;
using MyWarehouse.Application.Common.Exceptions;
using MyWarehouse.Application.Transactions.CreateTransaction;
using MyWarehouse.Domain.Partners;
using MyWarehouse.Domain.Products;
using MyWarehouse.Domain.Transactions;
using MyWarehouse.Domain.Exceptions;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyWarehouse.Application.IntegrationTests.Transactions
{
public class CreateTransactionTests : TestBase
{
[Test]
public void WhenDtoIsBlank_ShouldThrow_InputValidationException()
{
var command = new CreateTransactionCommand();
FluentActions.Invoking(() => TestFramework.SendAsync(command))
.Should().ThrowExactly<InputValidationException>();
}
[Test]
public async Task ProcurementRequest_ShouldCreateTransaction()
{
var partner = (await TestFramework.DataFactory.AddPartners(1)).First();
var products = await TestFramework.DataFactory.AddProducts(3);
var command = new CreateTransactionCommand()
{
PartnerId = partner.Id,
TransactionType = Domain.TransactionType.Procurement,
TransactionLines = products.Select(p => new CreateTransactionCommand.TransactionLine() {
ProductId = p.Id,
ProductQuantity = 5}).ToArray()
};
// Act.
var result = await TestFramework.SendAsync(command);
var transaction = await TestFramework.Data.FindAsync<Transaction>(result, x => x.TransactionLines, x => x.Partner);
transaction.Should().NotBeNull();
transaction.Partner.Id.Should().Be(partner.Id);
transaction.Total.Amount.Should().Be(transaction.TransactionLines.Sum(x => x.UnitPrice.Amount * x.Quantity));
transaction.TransactionLines.Should().HaveSameCount(products)
.And.Subject.Select(x => new { x.ProductId, ProductQuantity = x.Quantity })
.Should().BeEquivalentTo(command.TransactionLines,
o => o.ComparingByMembers<CreateTransactionCommand.TransactionLine>());
var newProducts = await TestFramework.Data.GetAllAsync<Product>();
newProducts.Select(x => new { id = x.Id, qty = x.NumberInStock })
.Should().BeEquivalentTo(command.TransactionLines
.Select(x => new { id = x.ProductId, qty = x.ProductQuantity }));
}
[Test]
public async Task SalesRequest_ShouldCreateTransaction()
{
CreateTransactionCommand.TransactionLine[] lines;
Partner partner;
{ // Create procurement transaction to prepare product stock for sales.
var t = await TestFramework.DataFactory.CreateProcurementTransaction(3);
lines = t.TransactionLines.Select(x => new CreateTransactionCommand.TransactionLine()
{
ProductId = x.Product.Id,
ProductQuantity = x.Quantity
}).ToArray();
partner = t.Partner;
}
var command = new CreateTransactionCommand()
{
TransactionType = Domain.TransactionType.Sales,
PartnerId = partner.Id,
TransactionLines = lines
};
// Act.
var result = await TestFramework.SendAsync(command);
var transaction = await TestFramework.Data.FindAsync<Transaction>(result, x => x.TransactionLines, x => x.Partner);
transaction.Should().NotBeNull();
transaction.Partner.Id.Should().Be(partner.Id);
transaction.Total.Amount.Should().Be(transaction.TransactionLines.Sum(x => x.UnitPrice.Amount * x.Quantity));
transaction.TransactionLines.Should().HaveSameCount(lines)
.And.Subject.Select(x => new { x.ProductId, ProductQuantity = x.Quantity })
.Should().BeEquivalentTo(command.TransactionLines,
o => o.ComparingByMembers<CreateTransactionCommand.TransactionLine>());
var products = await TestFramework.Data.GetAllAsync<Product>();
products.Should().Match(x => x.All(p => p.NumberInStock == 0)); // Because we sold the exact amount we procured.
}
[Test]
public async Task SalesRequest_WithoutStock_ShouldThrowException()
{
var partner = (await TestFramework.DataFactory.AddPartners(1)).First();
var products = await TestFramework.DataFactory.AddProducts(2);
var command = new CreateTransactionCommand()
{
TransactionType = Domain.TransactionType.Sales,
PartnerId = partner.Id,
TransactionLines = products.Select(p => new CreateTransactionCommand.TransactionLine()
{
ProductId = p.Id,
ProductQuantity = 5
}).ToArray()
};
FluentActions.Invoking(() => TestFramework.SendAsync(command))
.Should().ThrowExactly<InputValidationException>()
.And.Errors.SelectMany(e => e.Value).Should().ContainMatch("*stock*");
}
}
}