Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,12 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="11.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Data.SqlTypes;
using MassTransit;

namespace GenerateUniqueIDsWithNewId.Tests;

public class NewIdOrderingTests
{
private static List<Guid> Generate(Func<Guid> 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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -100,11 +100,11 @@ await createdOrderResponse.Content.ReadAsStringAsync(),
var updatedOrder = JsonSerializer.Deserialize<OrderDto>(
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]
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="11.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="11.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="11.0.0" />
<PackageReference Include="NewId" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOpenApi();

builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddScoped<IOrderService, OrderService>();
Expand All @@ -19,8 +18,7 @@

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.MapOpenApi();
}

app.UseHttpsRedirection();
Expand All @@ -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();

Expand Down
Loading