-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTestSupport.cs
42 lines (35 loc) · 1.34 KB
/
TestSupport.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
using System.Collections.Generic;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using ContosoAds.Web.DataAccess;
using ContosoAds.Web.Model;
using Microsoft.EntityFrameworkCore;
namespace ContosoAds.Web.UnitTests;
internal static class TestSupport
{
internal static async Task<AdsContext> CreateTestDbContext(string databaseName, bool recreate = false,
IReadOnlyList<Ad>? ads = null)
{
var options = new DbContextOptionsBuilder<AdsContext>()
.UseInMemoryDatabase(databaseName: databaseName)
.Options;
var context = new AdsContext(options);
if (recreate)
{
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
}
if (ads is null or { Count: 0 })
{
return context;
}
await context.Ads.AddRangeAsync(ads);
await context.SaveChangesAsync();
return context;
}
internal static async Task<AdsContext> CreateTestDbContext(string databaseName, bool recreate = false,
params Ad[] ads) =>
await CreateTestDbContext(databaseName, recreate, ads as IReadOnlyList<Ad>);
internal static JsonNode CreateBlobResponse(string fileName) =>
JsonNode.Parse($"{{\"blobURL\":\"https://www.example.com/{fileName}\"}}")!;
}