-
-
Notifications
You must be signed in to change notification settings - Fork 723
/
EntityFrameworkResolverCompilerIntegrationTests.cs
98 lines (82 loc) · 2.84 KB
/
EntityFrameworkResolverCompilerIntegrationTests.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
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Execution;
using HotChocolate.Tests;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Snapshooter.Xunit;
using Xunit;
namespace HotChocolate.Data;
public class EntityFrameworkResolverCompilerIntegrationTests
{
[Fact]
public async Task Resolver_Pipeline_With_DbContext_Is_Created()
{
Snapshot.FullName();
using AuthorFixture authorFixture = new();
var contextFactory = new Mock<IDbContextFactory<BookContext>>();
#if NET6_0_OR_GREATER
contextFactory
.Setup(t => t.CreateDbContextAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(authorFixture.Context));
#else
contextFactory
.Setup(t => t.CreateDbContext())
.Returns(authorFixture.Context);
#endif
await new ServiceCollection()
.AddSingleton(contextFactory.Object)
.AddGraphQL()
.AddQueryType<Query>()
.RegisterDbContext<BookContext>(DbContextKind.Pooled)
.ExecuteRequestAsync("{ books { title } }")
.MatchSnapshotAsync();
}
[Fact]
public async Task Resolver_Pipeline_With_Request_DbContext_Is_Created()
{
Snapshot.FullName();
using AuthorFixture authorFixture = new();
using var scope = new ServiceCollection()
.AddScoped(_ => authorFixture.Context)
.AddGraphQL()
.AddQueryType<Query>()
.RegisterDbContext<BookContext>(DbContextKind.Synchronized)
.ModifyRequestOptions(o => o.IncludeExceptionDetails = true)
.Services
.BuildServiceProvider()
.CreateScope();
var result = await scope.ServiceProvider.ExecuteRequestAsync(
QueryRequestBuilder.New()
.SetQuery("{ books { title } }")
.SetServices(scope.ServiceProvider)
.Create());
result.MatchSnapshot();
}
[Fact]
public async Task Resolver_Pipeline_With_Field_DbContext_Is_Created()
{
Snapshot.FullName();
using AuthorFixture authorFixture = new();
await using var service = new ServiceCollection()
.AddScoped(_ => authorFixture.Context)
.AddGraphQL()
.AddQueryType<Query>()
.RegisterDbContext<BookContext>(DbContextKind.Resolver)
.Services
.BuildServiceProvider();
await service.ExecuteRequestAsync(
QueryRequestBuilder.New()
.SetQuery("{ books { title } }")
.SetServices(service)
.Create())
.MatchSnapshotAsync();
}
public class Query
{
public IQueryable<Book> GetBooks(BookContext context)
=> context.Books;
}
}