diff --git a/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/GenerateUniqueIDsWithNewId.Tests.csproj b/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/GenerateUniqueIDsWithNewId.Tests.csproj index e640ae7ff1..079c23c30e 100644 --- a/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/GenerateUniqueIDsWithNewId.Tests.csproj +++ b/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/GenerateUniqueIDsWithNewId.Tests.csproj @@ -1,7 +1,7 @@ - net8.0 + net11.0 enable enable @@ -10,12 +10,11 @@ - - - - - - + + + + + diff --git a/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/GlobalUsings.cs b/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/GlobalUsings.cs index 0ae6437182..14270f0009 100644 --- a/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/GlobalUsings.cs +++ b/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/GlobalUsings.cs @@ -1,5 +1,4 @@ -global using FluentAssertions; -global using GenerateUniqueIDsWithNewId.Contracts; +global using GenerateUniqueIDsWithNewId.Contracts; global using Microsoft.AspNetCore.Mvc.Testing; global using System.Net; global using System.Net.Http.Json; diff --git a/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/NewIdOrderingTests.cs b/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/NewIdOrderingTests.cs new file mode 100644 index 0000000000..72f81f463b --- /dev/null +++ b/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/NewIdOrderingTests.cs @@ -0,0 +1,40 @@ +using System.Data.SqlTypes; +using MassTransit; + +namespace GenerateUniqueIDsWithNewId.Tests; + +public class NewIdOrderingTests +{ + private static List Generate(Func next, int count = 1000) => + Enumerable.Range(0, count).Select(_ => next()).ToList(); + + [Fact] + public void WhenNextGuidIsUsed_ThenSqlServerOrderMatchesCreationOrder() + { + var ids = Generate(NewId.NextGuid); + + Assert.Equal(ids, ids.OrderBy(id => new SqlGuid(id))); + } + + [Fact] + public void WhenNextSequentialGuidIsUsed_ThenGuidAndTextOrderMatchCreationOrder() + { + var ids = Generate(NewId.NextSequentialGuid); + + Assert.Equal(ids, ids.OrderBy(id => id)); + Assert.Equal(ids, ids.OrderBy(id => id.ToString(), StringComparer.Ordinal)); + } + + [Fact] + public void WhenCreateVersion7IsUsedOncePerMillisecond_ThenGuidOrderMatchesButSqlServerOrderDoesNot() + { + var ids = Generate(() => + { + Thread.Sleep(2); + return Guid.CreateVersion7(); + }, 50); + + Assert.Equal(ids, ids.OrderBy(id => id)); + Assert.NotEqual(ids, ids.OrderBy(id => new SqlGuid(id))); + } +} diff --git a/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/OrdersEndpointTests.cs b/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/OrdersEndpointTests.cs index f1f05faa91..b2195e8fae 100644 --- a/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/OrdersEndpointTests.cs +++ b/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.Tests/OrdersEndpointTests.cs @@ -18,7 +18,7 @@ public async Task WhenGetOrdersEndpointIsInvoked_ThenAllOrdersAreReturned() var result = await client.GetAsync("/orders"); // Assert - result.StatusCode.Should().Be(HttpStatusCode.OK); + Assert.Equal(HttpStatusCode.OK, result.StatusCode); } [Fact] @@ -44,11 +44,11 @@ await response.Content.ReadAsStringAsync(), await result.Content.ReadAsStringAsync(), _options); // Assert - result.StatusCode.Should().Be(HttpStatusCode.OK); - returnedOrder.Should().NotBeNull(); - returnedOrder!.CustomerName.Should().Be(order.CustomerName); - returnedOrder.Products.Should().BeEquivalentTo(order.Products); - returnedOrder.TotalAmount.Should().Be(order.TotalAmount); + Assert.Equal(HttpStatusCode.OK, result.StatusCode); + Assert.NotNull(returnedOrder); + Assert.Equal(order.CustomerName, returnedOrder!.CustomerName); + Assert.Equal(order.Products, returnedOrder.Products); + Assert.Equal(order.TotalAmount, returnedOrder.TotalAmount); } [Fact] @@ -67,7 +67,7 @@ public async Task WhenAddOrderEndpointIsInvoked_ThenOrderIsAdded() var response = await client.PostAsJsonAsync("/orders", order); // Assert - response.StatusCode.Should().Be(HttpStatusCode.Created); + Assert.Equal(HttpStatusCode.Created, response.StatusCode); } [Fact] @@ -100,11 +100,11 @@ await createdOrderResponse.Content.ReadAsStringAsync(), var updatedOrder = JsonSerializer.Deserialize( await updatedOrderResponse.Content.ReadAsStringAsync(), _options); - updatedOrderResponse.StatusCode.Should().Be(HttpStatusCode.OK); - updatedOrder!.Should().NotBeNull(); - updatedOrder!.CustomerName.Should().Be(orderForUpdate.CustomerName); - updatedOrder.Products.Should().BeEquivalentTo(orderForUpdate.Products); - updatedOrder.TotalAmount.Should().Be(orderForUpdate.TotalAmount); + Assert.Equal(HttpStatusCode.OK, updatedOrderResponse.StatusCode); + Assert.NotNull(updatedOrder); + Assert.Equal(orderForUpdate.CustomerName, updatedOrder!.CustomerName); + Assert.Equal(orderForUpdate.Products, updatedOrder.Products); + Assert.Equal(orderForUpdate.TotalAmount, updatedOrder.TotalAmount); } [Fact] @@ -127,6 +127,6 @@ await response.Content.ReadAsStringAsync(), var result = await client.DeleteAsync($"/orders/{orderDto!.Id}"); // Assert - result.StatusCode.Should().Be(HttpStatusCode.NoContent); + Assert.Equal(HttpStatusCode.NoContent, result.StatusCode); } } diff --git a/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.csproj b/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.csproj index e388957338..cf6306d5bc 100644 --- a/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.csproj +++ b/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId.csproj @@ -1,17 +1,16 @@ - net8.0 + net11.0 enable enable - - - + + + - diff --git a/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId/Program.cs b/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId/Program.cs index a882f03cf2..d6baf09c50 100644 --- a/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId/Program.cs +++ b/dotnet-client-libraries/GenerateUniqueIDsWithNewId/GenerateUniqueIDsWithNewId/Program.cs @@ -6,8 +6,7 @@ var builder = WebApplication.CreateBuilder(args); -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddOpenApi(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -19,8 +18,7 @@ if (app.Environment.IsDevelopment()) { - app.UseSwagger(); - app.UseSwaggerUI(); + app.MapOpenApi(); } app.UseHttpsRedirection(); @@ -30,40 +28,35 @@ var orders = await service.GetAllAsync(); return Results.Ok(orders); -}) -.WithOpenApi(); +}); app.MapGet("/orders/{id:guid}", async (Guid id, IOrderService service) => { var order = await service.GetByIdAsync(id); return Results.Ok(order); -}) -.WithOpenApi(); +}); app.MapPost("/orders", async (OrderForCreationDto dto, IOrderService service) => { var order = await service.CreateAsync(dto); return Results.Created($"/orders/{order.Id}", order); -}) -.WithOpenApi(); +}); app.MapPut("/orders/{id:guid}", async (Guid id, OrderForUpdateDto dto, IOrderService service) => { await service.UpdateAsync(id, dto); return Results.NoContent(); -}) -.WithOpenApi(); +}); app.MapDelete("/orders/{id:guid}", async (Guid id, IOrderService service) => { await service.DeleteAsync(id); return Results.NoContent(); -}) -.WithOpenApi(); +}); app.Run();