-
-
Notifications
You must be signed in to change notification settings - Fork 723
/
AuthorFixture.cs
58 lines (51 loc) · 1.59 KB
/
AuthorFixture.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
using System;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace HotChocolate.Data;
public class AuthorFixture : IDisposable
{
private readonly string _fileName;
private static readonly Author[] _authors =
{
new Author
{
Id = 1, Name = "Foo", Books = { new Book { Id = 1, Title = "Foo1" } }
},
new Author
{
Id = 2, Name = "Bar", Books = { new Book { Id = 2, Title = "Bar1" } }
},
new Author { Id = 3, Name = "Baz", Books = { new Book { Id = 3, Title = "Baz1" } } }
};
private static readonly SingleOrDefaultAuthor[] _singleOrDefaultAuthors =
{
new SingleOrDefaultAuthor { Id = 1, Name = "Foo" }
};
public AuthorFixture()
{
_fileName = Guid.NewGuid().ToString("N") + ".db";
var context = new ServiceCollection()
.AddDbContext<BookContext>(
b => b.UseSqlite("Data Source=" + _fileName))
.AddGraphQL()
.AddFiltering()
.AddSorting()
.AddProjections()
.AddQueryType<Query>()
.Services
.BuildServiceProvider()
.GetRequiredService<BookContext>();
context.Database.EnsureCreated();
context.Authors.AddRange(_authors);
context.SaveChanges();
context.SingleOrDefaultAuthors.AddRange(_singleOrDefaultAuthors);
context.SaveChanges();
Context = context;
}
public BookContext Context;
public void Dispose()
{
File.Delete(_fileName);
}
}