-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathInMemoryTests.cs
More file actions
58 lines (47 loc) · 1.76 KB
/
InMemoryTests.cs
File metadata and controls
58 lines (47 loc) · 1.76 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
51
52
53
54
55
56
57
58
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace EfCoreInMemory;
public class EfCoreInMemory
{
[Fact]
public async Task TestWithInMemory()
{
var builder = new DbContextOptionsBuilder<TestDbContext>();
builder.UseInMemoryDatabase("test_db");
DbContextOptions<TestDbContext> options = builder.Options;
await DatabaseTest(options);
}
[Fact]
public async Task TestWithSqliteInMemory()
{
var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" };
var connectionString = connectionStringBuilder.ToString();
var connection = new SqliteConnection(connectionString);
var builder = new DbContextOptionsBuilder<TestDbContext>();
builder.UseSqlite(connection);
DbContextOptions<TestDbContext> options = builder.Options;
await using (var context = new TestDbContext(options))
{
await context.Database.OpenConnectionAsync();
await context.Database.EnsureCreatedAsync();
}
await DatabaseTest(options);
}
private async Task DatabaseTest(DbContextOptions<TestDbContext> options)
{
var testEntity = new TestEntity { Id = 1, Name = "Test Entity" };
await using (var context = new TestDbContext(options))
{
context.TestEntities.Add(testEntity);
await context.SaveChangesAsync();
}
TestEntity foundEntity;
await using (var context = new TestDbContext(options))
{
foundEntity = await context.TestEntities.FirstOrDefaultAsync(x => x.Id == testEntity.Id);
}
Assert.NotNull(foundEntity);
Assert.Equal(testEntity.Name, foundEntity.Name);
}
}