mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 07:13:12 +00:00
51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
|
|
using Api.SeaHavenIndustries.Filters;
|
||
|
|
using Microsoft.AspNetCore.Http;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.Mvc.Abstractions;
|
||
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||
|
|
using Microsoft.AspNetCore.Routing;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
|
||
|
|
namespace SeaHavenIndustries.Tests;
|
||
|
|
|
||
|
|
public class ConcurrencyExceptionFilterTests
|
||
|
|
{
|
||
|
|
[Fact]
|
||
|
|
public void OnException_DbUpdateConcurrencyException_Returns409Conflict()
|
||
|
|
{
|
||
|
|
var filter = new ConcurrencyExceptionFilter();
|
||
|
|
var context = CreateExceptionContext(new DbUpdateConcurrencyException());
|
||
|
|
|
||
|
|
filter.OnException(context);
|
||
|
|
|
||
|
|
Assert.True(context.ExceptionHandled);
|
||
|
|
var result = Assert.IsType<ConflictObjectResult>(context.Result);
|
||
|
|
Assert.Equal(StatusCodes.Status409Conflict, result.StatusCode);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void OnException_OtherException_DoesNotHandle()
|
||
|
|
{
|
||
|
|
var filter = new ConcurrencyExceptionFilter();
|
||
|
|
var context = CreateExceptionContext(new InvalidOperationException("other"));
|
||
|
|
|
||
|
|
filter.OnException(context);
|
||
|
|
|
||
|
|
Assert.False(context.ExceptionHandled);
|
||
|
|
Assert.Null(context.Result);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static ExceptionContext CreateExceptionContext(Exception exception)
|
||
|
|
{
|
||
|
|
var actionContext = new ActionContext(
|
||
|
|
new DefaultHttpContext(),
|
||
|
|
new RouteData(),
|
||
|
|
new ActionDescriptor());
|
||
|
|
|
||
|
|
return new ExceptionContext(actionContext, [])
|
||
|
|
{
|
||
|
|
Exception = exception
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|