-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCustomizationTests.cs
105 lines (87 loc) · 3.22 KB
/
CustomizationTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
namespace FairyBread.Tests;
public class CustomizationTests
{
private const string Query = @"query { read(foo: { someInteger: 1, someString: ""hello"" }) }";
private static async Task<IRequestExecutor> GetRequestExecutorAsync(
Action<IServiceCollection> preBuildProviderAction)
{
var services = new ServiceCollection();
services.AddValidatorsFromAssemblyContaining<CustomValidator>();
preBuildProviderAction?.Invoke(services);
return await services
.AddGraphQL()
.AddQueryType<QueryType>()
.AddMutationType<MutationType>()
.AddFairyBread()
.BuildRequestExecutorAsync();
}
[Fact]
public async Task CustomValidationResultHandler_Works()
{
// Arrange
var executor = await GetRequestExecutorAsync(services =>
{
services.AddSingleton<IValidationErrorsHandler, CustomValidationErrorsHandler>();
});
// Act
var result = await executor.ExecuteAsync(Query) as IOperationResult;
// Assert
Assert.NotNull(result);
Assert.NotNull(result!.Errors);
Assert.NotEmpty(result.Errors);
Assert.True(result.Errors!.All(e => e.Message == "lol"));
}
public class CustomValidationErrorsHandler : DefaultValidationErrorsHandler
{
protected override IErrorBuilder CreateErrorBuilder(IMiddlewareContext context, string argName, IValidator val, ValidationFailure failure) =>
base.CreateErrorBuilder(context, argName, val, failure)
.SetMessage("lol");
}
[Fact]
public async Task CustomValidatorProvider_Works()
{
// Arrange
var executor = await GetRequestExecutorAsync(services =>
{
services.AddSingleton<IValidatorProvider, CustomValidatorProvider>();
});
// Act
var result = await executor.ExecuteAsync(Query);
// Assert
await Verifier.Verify(result);
}
public class CustomValidatorProvider : DefaultValidatorProvider
{
public CustomValidatorProvider(IServiceProvider serviceProvider, IFairyBreadOptions options)
: base(null!) { }
public override IEnumerable<ResolvedValidator> GetValidators(IMiddlewareContext context, IInputField argument)
=> argument.RuntimeType == typeof(FooInputDto)
? (new ResolvedValidator[] { new ResolvedValidator(new CustomValidator()) })
: base.GetValidators(context, argument);
}
#pragma warning disable CA1822 // Mark members as static
public class QueryType
{
public string Read(FooInputDto foo) => $"{foo};";
}
public class MutationType
{
public string Write(FooInputDto foo) => $"{foo};";
}
public class FooInputDto
{
public int SomeInteger { get; set; }
public string SomeString { get; set; } = "";
public override string ToString() =>
$"SomeInteger: {SomeInteger}, " +
$"SomeString: {SomeString}";
}
public class CustomValidator : AbstractValidator<FooInputDto>
{
public CustomValidator()
{
RuleFor(x => x.SomeInteger)
.GreaterThanOrEqualTo(999);
}
}
}