-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathFakeDbContextFactory.cs
More file actions
50 lines (44 loc) · 1.33 KB
/
FakeDbContextFactory.cs
File metadata and controls
50 lines (44 loc) · 1.33 KB
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
using MainCore.Entities;
using MainCore.Infrasturecture.Persistence;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
namespace MainCore.Test
{
public class FakeDbContextFactory
{
private const string _connectionString = "DataSource=:memory:";
public AppDbContext CreateDbContext(bool setup = false)
{
var connection = new SqliteConnection(_connectionString);
connection.Open();
var options = new DbContextOptionsBuilder<AppDbContext>()
.EnableSensitiveDataLogging()
.UseSqlite(connection)
.Options;
var context = new AppDbContext(options);
context.Database.EnsureCreated();
if (setup)
{
SetupDatabase(context);
}
return context;
}
public void SetupDatabase(AppDbContext context)
{
context.Add(new Account
{
Id = 1,
Username = "TestAccount",
Server = "https://test.server",
Villages = [
new Village
{
Id = 1,
Name = "TestVillage",
}
]
});
context.SaveChanges();
}
}
}