-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRecipeCatalogDbContextUnitTests.cs
88 lines (71 loc) · 2.2 KB
/
RecipeCatalogDbContextUnitTests.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
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using RecipeCatalog.Tests.Shared;
namespace RecipeCatalog.Domain.Tests;
public sealed class RecipeCatalogDbContextUnitTests : IDisposable
{
private readonly SqliteConnection _connection;
private readonly DbContextOptions<RecipeCatalogDbContext> _contextOptions;
private RecipeCatalogDbContext CreateContext() => new(_contextOptions);
public RecipeCatalogDbContextUnitTests()
{
_connection = new("Filename=:memory:");
_connection.Open();
_contextOptions = new DbContextOptionsBuilder<RecipeCatalogDbContext>()
.UseSqlite(_connection)
.Options;
var context = CreateContext();
context.Database.EnsureCreated();
context.Users.AddRange(TestData.Users);
context.Roles.AddRange(TestData.Roles);
context.UserRoles.AddRange(TestData.UserRoles);
context.Cuisines.AddRange(TestData.Cuisines);
context.Recipes.AddRange(TestData.Recipes);
context.SaveChanges();
}
public void Dispose()
{
_connection.Dispose();
}
[Fact]
public void RecipeHasRelatedData()
{
// Arrange
var context = CreateContext();
// Act
var recipe = context.Recipes
.Include(x => x.Owner)
.Include(x => x.Cuisine)
.First();
// Assert
Assert.NotNull(recipe);
Assert.NotNull(recipe.Owner);
Assert.NotNull(recipe.Cuisine);
}
[Fact]
public void CuisineHasRelatedData()
{
// Arrange
var context = CreateContext();
// Act
var cuisine = context.Cuisines
.Include(x => x.Recipes)
.First(x => x.Id == TestData.Recipes[0].CuisineId);
// Assert
Assert.NotNull(cuisine);
Assert.NotNull(cuisine.Recipes);
}
[Fact]
public void UserHasRelatedData()
{
// Arrange
var context = CreateContext();
// Act
var user = context.Users
.Include(x => x.Recipes)
.First(x => x.Id == TestData.Recipes[0].OwnerId);
// Assert
Assert.NotNull(user);
Assert.NotNull(user.Recipes);
}
}