-
Notifications
You must be signed in to change notification settings - Fork 232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add array contains matcher #518
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,118 @@ | |
}, | ||
"type": "Synchronous/HTTP" | ||
}, | ||
{ | ||
"description": "a request for multiple orders by id", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: This interaction appears to be in here twice. I was hoping the updated FFI would fix this issue, but perhaps it doesn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is my fault, can see the description here is different to the one above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
"pending": false, | ||
"providerStates": [ | ||
{ | ||
"name": "orders with ids {ids} exist", | ||
"params": { | ||
"ids": "1,2" | ||
} | ||
} | ||
], | ||
"request": { | ||
"headers": { | ||
"Accept": [ | ||
"application/json" | ||
] | ||
}, | ||
"method": "GET", | ||
"path": "/api/orders/many/1,2" | ||
}, | ||
"response": { | ||
"body": { | ||
"content": [ | ||
{ | ||
"date": "2023-06-28T12:13:14.0000000+01:00", | ||
"id": 1, | ||
"status": "Pending" | ||
}, | ||
{ | ||
"date": "2023-06-29T12:13:14.000000+01:00", | ||
"id": 2, | ||
"status": "Pending" | ||
} | ||
], | ||
"contentType": "application/json", | ||
"encoded": false | ||
}, | ||
"headers": { | ||
"Content-Type": [ | ||
"application/json" | ||
] | ||
}, | ||
"matchingRules": { | ||
"body": { | ||
"$": { | ||
"combine": "AND", | ||
"matchers": [ | ||
{ | ||
"match": "arrayContains", | ||
"variants": [ | ||
{ | ||
"index": 0, | ||
"rules": { | ||
"$.date": { | ||
"combine": "AND", | ||
"matchers": [ | ||
{ | ||
"match": "type" | ||
} | ||
] | ||
}, | ||
"$.id": { | ||
"combine": "AND", | ||
"matchers": [ | ||
{ | ||
"match": "integer" | ||
} | ||
] | ||
}, | ||
"$.status": { | ||
"combine": "AND", | ||
"matchers": [ | ||
{ | ||
"match": "regex", | ||
"regex": "Pending|Fulfilling|Shipped" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"index": 1, | ||
"rules": { | ||
"$.date": { | ||
"combine": "AND", | ||
"matchers": [ | ||
{ | ||
"match": "regex", | ||
"regex": "\\d\\d\\d\\d-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\d\\.\\d\\d\\d\\d\\d\\d+\\d\\d:\\d\\d" | ||
} | ||
] | ||
}, | ||
"$.id": { | ||
"combine": "AND", | ||
"matchers": [ | ||
{ | ||
"match": "integer" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"status": 200 | ||
}, | ||
"type": "Synchronous/HTTP" | ||
}, | ||
{ | ||
"description": "a request to update the status of an order", | ||
"pending": false, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Text.Json; | ||
|
@@ -36,7 +37,8 @@ public ProviderStateMiddleware(RequestDelegate next, IOrderRepository orders) | |
|
||
this.providerStates = new Dictionary<string, Func<IDictionary<string, object>, Task>> | ||
{ | ||
["an order with ID {id} exists"] = this.EnsureEventExistsAsync | ||
["an order with ID {id} exists"] = this.EnsureEventExistsAsync, | ||
["orders with ids {ids} exist"] = this.EnsureEventsExistAsync | ||
}; | ||
} | ||
|
||
|
@@ -52,6 +54,20 @@ private async Task EnsureEventExistsAsync(IDictionary<string, object> parameters | |
await this.orders.InsertAsync(new OrderDto(id.GetInt32(), OrderStatus.Fulfilling, DateTimeOffset.Now)); | ||
} | ||
|
||
/// <summary> | ||
/// Ensure a series of events exist | ||
/// </summary> | ||
/// <param name="parameters">Event parameters</param> | ||
/// <returns>Awaitable</returns> | ||
private async Task EnsureEventsExistAsync(IDictionary<string, object> parameters) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docs: Needs doc string like other methods There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
{ | ||
var ids = (JsonElement)parameters["ids"]; | ||
foreach (var id in ids.GetString()!.Split(',').Select(int.Parse)) | ||
{ | ||
await this.orders.InsertAsync(new OrderDto(id, OrderStatus.Fulfilling, DateTimeOffset.Now)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Handle the request | ||
/// </summary> | ||
|
@@ -79,7 +95,7 @@ public async Task InvokeAsync(HttpContext context) | |
try | ||
{ | ||
ProviderState providerState = JsonSerializer.Deserialize<ProviderState>(jsonRequestBody, Options); | ||
|
||
if (!string.IsNullOrEmpty(providerState?.State)) | ||
{ | ||
await this.providerStates[providerState.State].Invoke(providerState.Params); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
@@ -46,6 +47,34 @@ public async Task<IActionResult> GetByIdAsync(int id) | |
} | ||
} | ||
|
||
/// <summary> | ||
/// Get several orders by their comma-separated IDs | ||
/// </summary> | ||
/// <param name="ids"></param> | ||
/// <returns></returns> | ||
[HttpGet("many/{ids}", Name = "getMany")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docs: Needs doc string like other methods There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
[ProducesResponseType(typeof(OrderDto[]), StatusCodes.Status200OK)] | ||
public async Task<IActionResult> GetManyAsync(string ids) | ||
{ | ||
try | ||
{ | ||
var idsAsInts = ids.Split(',').Select(int.Parse); | ||
|
||
List<OrderDto> result = new List<OrderDto>(); | ||
foreach (int id in idsAsInts) | ||
{ | ||
var order = await this.orders.GetAsync(id); | ||
result.Add(order); | ||
} | ||
|
||
return this.Ok(result.ToArray()); | ||
} | ||
catch (KeyNotFoundException) | ||
{ | ||
return this.NotFound(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Create a new pending order | ||
/// </summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace PactNet.Matchers | ||
{ | ||
/// <summary> | ||
/// Matcher for array-contains. Checks whether an array contains the specified variations. | ||
/// </summary> | ||
public class ArrayContainsMatcher : IMatcher | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docs: Needs doc string like other matchers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
{ | ||
/// <summary> | ||
/// Type of the matcher | ||
/// </summary> | ||
[JsonPropertyName("pact:matcher:type")] | ||
public string Type => "array-contains"; | ||
|
||
/// <summary> | ||
/// The items expected to be in the array. | ||
/// </summary> | ||
[JsonPropertyName("variants")] | ||
public dynamic Value { get; } | ||
|
||
/// <summary> | ||
/// Initialises a new instance of the <see cref="ArrayContainsMatcher"/> class. | ||
/// </summary> | ||
/// <param name="variants"></param> | ||
public ArrayContainsMatcher(dynamic[] variants) | ||
{ | ||
Value = variants; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,5 +168,15 @@ public static IMatcher Include(string example) | |
{ | ||
return new IncludeMatcher(example); | ||
} | ||
|
||
/// <summary> | ||
/// Matcher which matches an array containing the specified variations. | ||
/// </summary> | ||
/// <param name="variations">Variations which should be contained in the array.</param> | ||
/// <returns>Matcher</returns> | ||
public static IMatcher ArrayContains(dynamic[] variations) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: The public API should be documented There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noted, will amend There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one still needs documenting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
{ | ||
return new ArrayContainsMatcher(variations); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Text.Json; | ||
using FluentAssertions; | ||
using PactNet.Matchers; | ||
using Xunit; | ||
|
||
namespace PactNet.Abstractions.Tests.Matchers | ||
{ | ||
public class ArrayContainsMatcherTests | ||
{ | ||
[Fact] | ||
public void Ctor_String_SerializesCorrectly() | ||
{ | ||
// Arrange | ||
var example = new[] | ||
{ | ||
"Thing1", | ||
"Thing2", | ||
}; | ||
|
||
var matcher = new ArrayContainsMatcher(example); | ||
|
||
// Act | ||
var actual = JsonSerializer.Serialize(matcher); | ||
|
||
// Assert | ||
actual.Should().Be(@"{""pact:matcher:type"":""array-contains"",""variants"":[""Thing1"",""Thing2""]}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: You can use new string literals to format the JSON nicely here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, quite the oversight from me Nevermind: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, yes my bad. These have to support .Net Framework also |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: I think for this example to 'come to life' then we'd really need it to match multiple different possible variants
Perhaps if there were different order types or something so that they could have different fields on them, then the matching rules could reflect those variants. Like a fulfilled order has an extra date on it for when it was completed, or something like that.