This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
OrderAggregateTest.cs
175 lines (152 loc) · 5.6 KB
/
OrderAggregateTest.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
namespace Ordering.UnitTests.Domain;
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
public class OrderAggregateTest
{
public OrderAggregateTest()
{ }
[Fact]
public void Create_order_item_success()
{
//Arrange
var productId = 1;
var productName = "FakeProductName";
var unitPrice = 12;
var discount = 15;
var pictureUrl = "FakeUrl";
var units = 5;
//Act
var fakeOrderItem = new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units);
//Assert
Assert.NotNull(fakeOrderItem);
}
[Fact]
public void Invalid_number_of_units()
{
//Arrange
var productId = 1;
var productName = "FakeProductName";
var unitPrice = 12;
var discount = 15;
var pictureUrl = "FakeUrl";
var units = -1;
//Act - Assert
Assert.Throws<OrderingDomainException>(() => new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units));
}
[Fact]
public void Invalid_total_of_order_item_lower_than_discount_applied()
{
//Arrange
var productId = 1;
var productName = "FakeProductName";
var unitPrice = 12;
var discount = 15;
var pictureUrl = "FakeUrl";
var units = 1;
//Act - Assert
Assert.Throws<OrderingDomainException>(() => new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units));
}
[Fact]
public void Invalid_discount_setting()
{
//Arrange
var productId = 1;
var productName = "FakeProductName";
var unitPrice = 12;
var discount = 15;
var pictureUrl = "FakeUrl";
var units = 5;
//Act
var fakeOrderItem = new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units);
//Assert
Assert.Throws<OrderingDomainException>(() => fakeOrderItem.SetNewDiscount(-1));
}
[Fact]
public void Invalid_units_setting()
{
//Arrange
var productId = 1;
var productName = "FakeProductName";
var unitPrice = 12;
var discount = 15;
var pictureUrl = "FakeUrl";
var units = 5;
//Act
var fakeOrderItem = new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units);
//Assert
Assert.Throws<OrderingDomainException>(() => fakeOrderItem.AddUnits(-1));
}
[Fact]
public void when_add_two_times_on_the_same_item_then_the_total_of_order_should_be_the_sum_of_the_two_items()
{
var address = new AddressBuilder().Build();
var order = new OrderBuilder(address)
.AddOne(1, "cup", 10.0m, 0, string.Empty)
.AddOne(1, "cup", 10.0m, 0, string.Empty)
.Build();
Assert.Equal(20.0m, order.GetTotal());
}
[Fact]
public void Add_new_Order_raises_new_event()
{
//Arrange
var street = "fakeStreet";
var city = "FakeCity";
var state = "fakeState";
var country = "fakeCountry";
var zipcode = "FakeZipCode";
var cardTypeId = 5;
var cardNumber = "12";
var cardSecurityNumber = "123";
var cardHolderName = "FakeName";
var cardExpiration = DateTime.Now.AddYears(1);
var expectedResult = 1;
//Act
var fakeOrder = new Order("1", "fakeName", new Address(street, city, state, country, zipcode), cardTypeId, cardNumber, cardSecurityNumber, cardHolderName, cardExpiration);
//Assert
Assert.Equal(fakeOrder.DomainEvents.Count, expectedResult);
}
[Fact]
public void Add_event_Order_explicitly_raises_new_event()
{
//Arrange
var street = "fakeStreet";
var city = "FakeCity";
var state = "fakeState";
var country = "fakeCountry";
var zipcode = "FakeZipCode";
var cardTypeId = 5;
var cardNumber = "12";
var cardSecurityNumber = "123";
var cardHolderName = "FakeName";
var cardExpiration = DateTime.Now.AddYears(1);
var expectedResult = 2;
//Act
var fakeOrder = new Order("1", "fakeName", new Address(street, city, state, country, zipcode), cardTypeId, cardNumber, cardSecurityNumber, cardHolderName, cardExpiration);
fakeOrder.AddDomainEvent(new OrderStartedDomainEvent(fakeOrder, "fakeName", "1", cardTypeId, cardNumber, cardSecurityNumber, cardHolderName, cardExpiration));
//Assert
Assert.Equal(fakeOrder.DomainEvents.Count, expectedResult);
}
[Fact]
public void Remove_event_Order_explicitly()
{
//Arrange
var street = "fakeStreet";
var city = "FakeCity";
var state = "fakeState";
var country = "fakeCountry";
var zipcode = "FakeZipCode";
var cardTypeId = 5;
var cardNumber = "12";
var cardSecurityNumber = "123";
var cardHolderName = "FakeName";
var cardExpiration = DateTime.Now.AddYears(1);
var fakeOrder = new Order("1", "fakeName", new Address(street, city, state, country, zipcode), cardTypeId, cardNumber, cardSecurityNumber, cardHolderName, cardExpiration);
var @fakeEvent = new OrderStartedDomainEvent(fakeOrder, "1", "fakeName", cardTypeId, cardNumber, cardSecurityNumber, cardHolderName, cardExpiration);
var expectedResult = 1;
//Act
fakeOrder.AddDomainEvent(@fakeEvent);
fakeOrder.RemoveDomainEvent(@fakeEvent);
//Assert
Assert.Equal(fakeOrder.DomainEvents.Count, expectedResult);
}
}