Skip to content

Commit 7c36b7d

Browse files
tests: this commit introduces unit tests for payment pipeline filters
1 parent 00a9b01 commit 7c36b7d

1 file changed

Lines changed: 281 additions & 0 deletions

File tree

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
namespace Comanda.Payments.TestSuite.Unit;
2+
3+
public sealed class PaymentPipelineDefinitionTests
4+
{
5+
[Fact(DisplayName = "[filters] - when payment identifier is provided then it should be included in the filter definition")]
6+
public void When_PaymentIdentifierIsProvided_ThenItShouldBeIncludedInTheFilterDefinition()
7+
{
8+
var identifier = Identifier.Generate<Payment>();
9+
var filters = PaymentFilters.WithSpecifications()
10+
.WithIdentifier(identifier)
11+
.Build();
12+
13+
var pipeline = PipelineDefinitionBuilder
14+
.For<Payment>()
15+
.As<Payment, Payment, BsonDocument>()
16+
.FilterPayments(filters);
17+
18+
var rendered = pipeline.Render(new(
19+
BsonSerializer.SerializerRegistry.GetSerializer<Payment>(),
20+
BsonSerializer.SerializerRegistry));
21+
22+
Assert.Single(rendered.Documents);
23+
24+
var match = rendered.Documents[0]["$match"].AsBsonDocument;
25+
26+
Assert.True(match.Contains(Documents.Payment.Identifier));
27+
Assert.Equal(identifier, match[Documents.Payment.Identifier].AsString);
28+
}
29+
30+
[Fact(DisplayName = "[filters] - when payer identifier is provided then it should be included in the filter definition")]
31+
public void When_PayerIdentifierIsProvided_ThenItShouldBeIncludedInTheFilterDefinition()
32+
{
33+
var identifier = Identifier.Generate<User>();
34+
var filters = PaymentFilters.WithSpecifications()
35+
.WithPayerId(identifier)
36+
.Build();
37+
38+
var pipeline = PipelineDefinitionBuilder
39+
.For<Payment>()
40+
.As<Payment, Payment, BsonDocument>()
41+
.FilterPayments(filters);
42+
43+
var rendered = pipeline.Render(new(
44+
BsonSerializer.SerializerRegistry.GetSerializer<Payment>(),
45+
BsonSerializer.SerializerRegistry));
46+
47+
Assert.Single(rendered.Documents);
48+
49+
var match = rendered.Documents[0]["$match"].AsBsonDocument;
50+
51+
Assert.True(match.Contains(Documents.Payment.PayerId));
52+
Assert.Equal(identifier, match[Documents.Payment.PayerId].AsString);
53+
}
54+
55+
[Fact(DisplayName = "[filters] - when external identifier is provided then it should be included in the filter definition")]
56+
public void When_ExternalIdentifierIsProvided_ThenItShouldBeIncludedInTheFilterDefinition()
57+
{
58+
var identifier = Identifier.Generate<Payment>();
59+
var filters = PaymentFilters.WithSpecifications()
60+
.WithExternalId(identifier)
61+
.Build();
62+
63+
var pipeline = PipelineDefinitionBuilder
64+
.For<Payment>()
65+
.As<Payment, Payment, BsonDocument>()
66+
.FilterPayments(filters);
67+
68+
var rendered = pipeline.Render(new(
69+
BsonSerializer.SerializerRegistry.GetSerializer<Payment>(),
70+
BsonSerializer.SerializerRegistry));
71+
72+
Assert.Single(rendered.Documents);
73+
74+
var match = rendered.Documents[0]["$match"].AsBsonDocument;
75+
76+
Assert.True(match.Contains(Documents.Payment.ExternalId));
77+
Assert.Equal(identifier, match[Documents.Payment.ExternalId].AsString);
78+
}
79+
80+
[Fact(DisplayName = "[filters] - when reference identifier is provided then it should be included in the filter definition")]
81+
public void When_ReferenceIdentifierIsProvided_ThenItShouldBeIncludedInTheFilterDefinition()
82+
{
83+
var reference = Identifier.Generate<Payment>();
84+
var filters = PaymentFilters.WithSpecifications()
85+
.WithReferenceId(reference)
86+
.Build();
87+
88+
var pipeline = PipelineDefinitionBuilder
89+
.For<Payment>()
90+
.As<Payment, Payment, BsonDocument>()
91+
.FilterPayments(filters);
92+
93+
var rendered = pipeline.Render(new(
94+
BsonSerializer.SerializerRegistry.GetSerializer<Payment>(),
95+
BsonSerializer.SerializerRegistry));
96+
97+
Assert.Single(rendered.Documents);
98+
99+
var match = rendered.Documents[0]["$match"].AsBsonDocument;
100+
101+
Assert.True(match.Contains(Documents.Payment.ReferenceId));
102+
Assert.Equal(reference, match[Documents.Payment.ReferenceId].AsString);
103+
}
104+
105+
[Fact(DisplayName = "[filters] - when status is provided then it should be included in the filter definition")]
106+
public void When_StatusIsProvided_ThenItShouldBeIncludedInTheFilterDefinition()
107+
{
108+
var status = Status.Paid;
109+
var filters = PaymentFilters.WithSpecifications()
110+
.WithStatus(status)
111+
.Build();
112+
113+
var pipeline = PipelineDefinitionBuilder
114+
.For<Payment>()
115+
.As<Payment, Payment, BsonDocument>()
116+
.FilterPayments(filters);
117+
118+
var rendered = pipeline.Render(new(
119+
BsonSerializer.SerializerRegistry.GetSerializer<Payment>(),
120+
BsonSerializer.SerializerRegistry));
121+
122+
Assert.Single(rendered.Documents);
123+
124+
var match = rendered.Documents[0]["$match"].AsBsonDocument;
125+
126+
Assert.True(match.Contains(Documents.Payment.Status));
127+
Assert.Equal((int)status, match[Documents.Payment.Status].ToInt32());
128+
}
129+
130+
[Fact(DisplayName = "[filters] - when method is provided then it should be included in the filter definition")]
131+
public void When_MethodIsProvided_ThenItShouldBeIncludedInTheFilterDefinition()
132+
{
133+
var method = Method.Card;
134+
var filters = PaymentFilters.WithSpecifications()
135+
.WithMethod(method)
136+
.Build();
137+
138+
var pipeline = PipelineDefinitionBuilder
139+
.For<Payment>()
140+
.As<Payment, Payment, BsonDocument>()
141+
.FilterPayments(filters);
142+
143+
var rendered = pipeline.Render(new(
144+
BsonSerializer.SerializerRegistry.GetSerializer<Payment>(),
145+
BsonSerializer.SerializerRegistry));
146+
147+
Assert.Single(rendered.Documents);
148+
149+
var match = rendered.Documents[0]["$match"].AsBsonDocument;
150+
151+
Assert.True(match.Contains(Documents.Payment.Method));
152+
Assert.Equal((int)method, match[Documents.Payment.Method].ToInt32());
153+
}
154+
155+
[Fact(DisplayName = "[filters] - when creation period is provided then it should be included in the filter definition")]
156+
public void When_CreationPeriodIsProvided_ThenItShouldBeIncludedInTheFilterDefinition()
157+
{
158+
var createdAfter = DateOnly.FromDateTime(DateTime.UtcNow.AddDays(-2));
159+
var createdBefore = DateOnly.FromDateTime(DateTime.UtcNow);
160+
161+
var filters = PaymentFilters.WithSpecifications()
162+
.WithCreatedAfter(createdAfter)
163+
.WithCreatedBefore(createdBefore)
164+
.Build();
165+
166+
var pipeline = PipelineDefinitionBuilder
167+
.For<Payment>()
168+
.As<Payment, Payment, BsonDocument>()
169+
.FilterPayments(filters);
170+
171+
var rendered = pipeline.Render(new(
172+
BsonSerializer.SerializerRegistry.GetSerializer<Payment>(),
173+
BsonSerializer.SerializerRegistry));
174+
175+
Assert.Single(rendered.Documents);
176+
177+
var match = rendered.Documents[0]["$match"].AsBsonDocument;
178+
var createdAt = match[Documents.Payment.CreatedAt].AsBsonDocument;
179+
180+
Assert.True(createdAt.Contains("$gte"));
181+
Assert.True(createdAt.Contains("$lte"));
182+
183+
Assert.Equal(createdAfter, DateOnly.FromDateTime(createdAt["$gte"].ToUniversalTime()));
184+
Assert.Equal(createdBefore, DateOnly.FromDateTime(createdAt["$lte"].ToUniversalTime()));
185+
}
186+
187+
[Fact(DisplayName = "[filters] - when amount range is provided then it should be included in the filter definition")]
188+
public void When_AmountRangeIsProvided_ThenItShouldBeIncludedInTheFilterDefinition()
189+
{
190+
const decimal minAmount = 10.5m;
191+
const decimal maxAmount = 72.9m;
192+
193+
var filters = PaymentFilters.WithSpecifications()
194+
.WithMinAmount(minAmount)
195+
.WithMaxAmount(maxAmount)
196+
.Build();
197+
198+
var pipeline = PipelineDefinitionBuilder
199+
.For<Payment>()
200+
.As<Payment, Payment, BsonDocument>()
201+
.FilterPayments(filters);
202+
203+
var rendered = pipeline.Render(new(
204+
BsonSerializer.SerializerRegistry.GetSerializer<Payment>(),
205+
BsonSerializer.SerializerRegistry));
206+
207+
Assert.Single(rendered.Documents);
208+
209+
var match = rendered.Documents[0]["$match"].AsBsonDocument;
210+
var amount = match[Documents.Payment.Amount].AsBsonDocument;
211+
212+
Assert.True(amount.Contains("$gte"));
213+
Assert.True(amount.Contains("$lte"));
214+
215+
Assert.Equal(minAmount, amount["$gte"].ToDecimal());
216+
Assert.Equal(maxAmount, amount["$lte"].ToDecimal());
217+
}
218+
219+
[Fact(DisplayName = "[filters] - when all specifications are provided then all filters should be included in the filter definition")]
220+
public void When_AllSpecificationsAreProvided_ThenAllFiltersShouldBeIncludedInTheFilterDefinition()
221+
{
222+
var identifier = Identifier.Generate<Payment>();
223+
var payerId = Identifier.Generate<User>();
224+
225+
var externalId = Identifier.Generate<Payment>();
226+
var referenceId = Identifier.Generate<Payment>();
227+
228+
var status = Status.Refunded;
229+
var method = Method.Pix;
230+
231+
var createdAfter = DateOnly.FromDateTime(DateTime.UtcNow.AddDays(-1));
232+
var createdBefore = DateOnly.FromDateTime(DateTime.UtcNow);
233+
234+
const decimal minAmount = 19.9m;
235+
const decimal maxAmount = 149.9m;
236+
237+
var filters = PaymentFilters.WithSpecifications()
238+
.WithIdentifier(identifier)
239+
.WithPayerId(payerId)
240+
.WithExternalId(externalId)
241+
.WithReferenceId(referenceId)
242+
.WithStatus(status)
243+
.WithMethod(method)
244+
.WithCreatedAfter(createdAfter)
245+
.WithCreatedBefore(createdBefore)
246+
.WithMinAmount(minAmount)
247+
.WithMaxAmount(maxAmount)
248+
.Build();
249+
250+
var pipeline = PipelineDefinitionBuilder
251+
.For<Payment>()
252+
.As<Payment, Payment, BsonDocument>()
253+
.FilterPayments(filters);
254+
255+
var rendered = pipeline.Render(new(
256+
BsonSerializer.SerializerRegistry.GetSerializer<Payment>(),
257+
BsonSerializer.SerializerRegistry));
258+
259+
Assert.Single(rendered.Documents);
260+
261+
var match = rendered.Documents[0]["$match"].AsBsonDocument;
262+
263+
Assert.Equal(identifier, match[Documents.Payment.Identifier].AsString);
264+
Assert.Equal(payerId, match[Documents.Payment.PayerId].AsString);
265+
266+
Assert.Equal(externalId, match[Documents.Payment.ExternalId].AsString);
267+
Assert.Equal(referenceId, match[Documents.Payment.ReferenceId].AsString);
268+
269+
Assert.Equal((int)status, match[Documents.Payment.Status].ToInt32());
270+
Assert.Equal((int)method, match[Documents.Payment.Method].ToInt32());
271+
272+
var createdAt = match[Documents.Payment.CreatedAt].AsBsonDocument;
273+
var amount = match[Documents.Payment.Amount].AsBsonDocument;
274+
275+
Assert.Equal(createdAfter, DateOnly.FromDateTime(createdAt["$gte"].ToUniversalTime()));
276+
Assert.Equal(createdBefore, DateOnly.FromDateTime(createdAt["$lte"].ToUniversalTime()));
277+
278+
Assert.Equal(minAmount, amount["$gte"].ToDecimal());
279+
Assert.Equal(maxAmount, amount["$lte"].ToDecimal());
280+
}
281+
}

0 commit comments

Comments
 (0)