mirror of https://github.com/Squidex/squidex.git
32 changed files with 611 additions and 169 deletions
@ -1,16 +1,19 @@ |
|||||
// ==========================================================================
|
// ==========================================================================
|
||||
// IFieldProperties.cs
|
// UpdateSchemaDto.cs
|
||||
// PinkParrot Headless CMS
|
// PinkParrot Headless CMS
|
||||
// ==========================================================================
|
// ==========================================================================
|
||||
// Copyright (c) PinkParrot Group
|
// Copyright (c) PinkParrot Group
|
||||
// All rights reserved.
|
// All rights reserved.
|
||||
// ==========================================================================
|
// ==========================================================================
|
||||
|
|
||||
using PinkParrot.Infrastructure; |
using System.ComponentModel.DataAnnotations; |
||||
|
using PinkParrot.Core.Schemas; |
||||
|
|
||||
namespace PinkParrot.Core.Schemas |
namespace PinkParrot.Modules.Api.Schemas.Models |
||||
{ |
{ |
||||
public interface IFieldProperties : IValidatable |
public class UpdateSchemaDto |
||||
{ |
{ |
||||
|
[Required] |
||||
|
public SchemaProperties Properties { get; set; } |
||||
} |
} |
||||
} |
} |
||||
@ -0,0 +1,77 @@ |
|||||
|
// ==========================================================================
|
||||
|
// ExceptionFilter.cs
|
||||
|
// PinkParrot Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) PinkParrot Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Microsoft.AspNetCore.Mvc.Filters; |
||||
|
using PinkParrot.Infrastructure; |
||||
|
using PinkParrot.Modules.Api; |
||||
|
|
||||
|
// ReSharper disable InvertIf
|
||||
|
|
||||
|
namespace PinkParrot.Pipeline |
||||
|
{ |
||||
|
public class ApiExceptionFilterAttribute : ActionFilterAttribute, IExceptionFilter |
||||
|
{ |
||||
|
private static readonly List<Func<Exception, IActionResult>> handlers = new List<Func<Exception, IActionResult>>(); |
||||
|
|
||||
|
private static void AddHandler<T>(Func<T, IActionResult> handler) where T : Exception |
||||
|
{ |
||||
|
handlers.Add(ex => |
||||
|
{ |
||||
|
var typed = ex as T; |
||||
|
|
||||
|
return typed != null ? handler(typed) : null; |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
static ApiExceptionFilterAttribute() |
||||
|
{ |
||||
|
AddHandler<DomainObjectNotFoundException>(ex => |
||||
|
new NotFoundResult()); |
||||
|
|
||||
|
AddHandler<DomainException>(ex => |
||||
|
new BadRequestObjectResult(new ErrorDto { Message = ex.Message })); |
||||
|
|
||||
|
AddHandler<ValidationException>(ex => |
||||
|
new BadRequestObjectResult(new ErrorDto { Message = ex.Message, Details = ex.Errors.Select(e => e.Message).ToArray() })); |
||||
|
} |
||||
|
|
||||
|
public override void OnActionExecuting(ActionExecutingContext context) |
||||
|
{ |
||||
|
if (!context.ModelState.IsValid) |
||||
|
{ |
||||
|
var errors = context.ModelState.Values.SelectMany(g => g.Errors).Select(e => new ValidationError(e.ErrorMessage)).ToList(); |
||||
|
|
||||
|
throw new ValidationException("The model is not valid.", errors); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void OnException(ExceptionContext context) |
||||
|
{ |
||||
|
IActionResult result = null; |
||||
|
|
||||
|
foreach (var handler in handlers) |
||||
|
{ |
||||
|
result = handler(context.Exception); |
||||
|
|
||||
|
if (result != null) |
||||
|
{ |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (result != null) |
||||
|
{ |
||||
|
context.Result = result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,37 @@ |
|||||
|
REM Create a 'GeneratedReports' folder if it does not exist |
||||
|
if not exist "%~dp0GeneratedReports" mkdir "%~dp0GeneratedReports" |
||||
|
|
||||
|
REM Remove any previously created test output directories |
||||
|
CD %~dp0 |
||||
|
FOR /D /R %%X IN (%USERNAME%*) DO RD /S /Q "%%X" |
||||
|
|
||||
|
REM Run the tests against the targeted output |
||||
|
call :RunOpenCoverUnitTestMetrics |
||||
|
|
||||
|
REM Generate the report output based on the test results |
||||
|
if %errorlevel% equ 0 ( |
||||
|
call :RunReportGeneratorOutput |
||||
|
) |
||||
|
|
||||
|
REM Launch the report |
||||
|
if %errorlevel% equ 0 ( |
||||
|
call :RunLaunchReport |
||||
|
) |
||||
|
exit /b %errorlevel% |
||||
|
|
||||
|
:RunOpenCoverUnitTestMetrics |
||||
|
"%UserProfile%\.nuget\packages\OpenCover\4.6.519\tools\OpenCover.Console.exe" ^ |
||||
|
-register:user ^ |
||||
|
-target:"C:\Program Files\dotnet\dotnet.exe" ^ |
||||
|
-targetargs:"test %~dp0\pinkparrot_infrastructure\PinkParrot.Infrastructure.Tests" ^ |
||||
|
-filter:"+[PinkParrot*]*" ^ |
||||
|
-skipautoprops ^ |
||||
|
-output:"%~dp0\GeneratedReports\Infrastructure.xml" ^ |
||||
|
-oldStyle |
||||
|
exit /b %errorlevel% |
||||
|
|
||||
|
:RunReportGeneratorOutput |
||||
|
"%UserProfile%\.nuget\packages\ReportGenerator\2.4.5\tools\ReportGenerator.exe" ^ |
||||
|
-reports:"%~dp0\GeneratedReports\Infrastructure.xml" ^ |
||||
|
-targetdir:"%~dp0\GeneratedReports\Output" |
||||
|
exit /b %errorlevel% |
||||
@ -0,0 +1,83 @@ |
|||||
|
// ==========================================================================
|
||||
|
// SimpleMapperTests.cs
|
||||
|
// PinkParrot Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) PinkParrot Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace PinkParrot.Infrastructure.Reflection |
||||
|
{ |
||||
|
public class SimpleMapperTests |
||||
|
{ |
||||
|
public class Class1Base |
||||
|
{ |
||||
|
public string MappedString { get; set; } |
||||
|
|
||||
|
public string MappedNull { get; set; } |
||||
|
|
||||
|
public long MappedNumber { get; set; } |
||||
|
|
||||
|
public Guid MappedGuid { get; set; } |
||||
|
} |
||||
|
|
||||
|
public class Class1 : Class1Base |
||||
|
{ |
||||
|
public string UnmappedString { get; set; } |
||||
|
} |
||||
|
|
||||
|
public class Class2Base |
||||
|
{ |
||||
|
public string MappedString { get; protected set; } |
||||
|
|
||||
|
public int MappedNull { get; set; } |
||||
|
|
||||
|
public int MappedNumber { get; set; } |
||||
|
|
||||
|
public string MappedGuid { get; set; } |
||||
|
} |
||||
|
|
||||
|
public class Class2 : Class2Base |
||||
|
{ |
||||
|
public string UnmappedString |
||||
|
{ |
||||
|
get { return "Value"; } |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_throw_if_mapping_with_null_source() |
||||
|
{ |
||||
|
Assert.Throws<ArgumentNullException>(() => SimpleMapper.Map((Class1)null, new Class2())); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_throw_if_mapping_with_null_target() |
||||
|
{ |
||||
|
Assert.Throws<ArgumentNullException>(() => SimpleMapper.Map(new Class1(), (Class2)null)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_map_between_types() |
||||
|
{ |
||||
|
var class1 = new Class1 |
||||
|
{ |
||||
|
UnmappedString = Guid.NewGuid().ToString(), |
||||
|
MappedString = Guid.NewGuid().ToString(), |
||||
|
MappedNumber = 123, |
||||
|
MappedGuid = Guid.NewGuid() |
||||
|
}; |
||||
|
var class2 = new Class2(); |
||||
|
|
||||
|
SimpleMapper.Map(class1, class2); |
||||
|
|
||||
|
Assert.Equal(class1.MappedString, class2.MappedString); |
||||
|
Assert.Equal(class1.MappedNumber, class2.MappedNumber); |
||||
|
Assert.Equal(class1.MappedGuid.ToString(), class2.MappedGuid); |
||||
|
Assert.NotEqual(class1.UnmappedString, class2.UnmappedString); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue