-
Notifications
You must be signed in to change notification settings - Fork 67
/
TestData.cs
108 lines (90 loc) · 3.35 KB
/
TestData.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MyWarehouse.Domain.Common;
using MyWarehouse.Infrastructure.Identity.Model;
using MyWarehouse.Infrastructure.Persistence.Context;
using Respawn;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace MyWarehouse.Application.IntegrationTests
{
public class TestData
{
private readonly TestHost _host;
private readonly Func<Task> _databaseReset;
private ApplicationDbContext _dbContext;
private IServiceScope _scope;
public TestData(TestHost host)
{
_host = host;
_databaseReset = () => {
return new Checkpoint()
{
TablesToIgnore = new[] { "__EFMigrationsHistory" }
}
.Reset(_host.ConnectionString);
};
{ // Ensure database.
using var scope = _host.ScopeFactory.CreateScope();
using var context = scope.ServiceProvider.GetService<ApplicationDbContext>();
context.Database.Migrate();
}
}
public void CreateScope()
{
if (_dbContext != null) _dbContext.Dispose();
if (_scope != null) _scope.Dispose();
_scope = _host.ScopeFactory.CreateScope();
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
}
public void DisposeScope()
{
_dbContext?.Dispose();
_dbContext = null;
_scope?.Dispose();
_scope = null;
}
public async Task<int> AddAsync<TEntity>(TEntity entity) where TEntity : IEntity
{
_dbContext.Add(entity);
await _dbContext.SaveChangesAsync();
return entity.Id;
}
public async Task AddRangeAsync<TEntity>(params TEntity[] entities) where TEntity : class, IEntity
{
_dbContext.Set<TEntity>().AddRange(entities);
await _dbContext.SaveChangesAsync();
}
public Task<T> FindAsync<T>(int id, params Expression<Func<T, object>>[] includes)
where T : class, IEntity
{
var query = _dbContext.Set<T>().AsQueryable();
foreach (var i in includes)
query = query.Include(i);
return query.SingleOrDefaultAsync(x => x.Id == id);
}
public Task<List<T>> GetAllAsync<T>(params Expression<Func<T, object>>[] includes)
where T : class, IEntity
{
var query = _dbContext.Set<T>().AsQueryable();
foreach (var i in includes)
query = query.Include(i);
return query.ToListAsync();
}
public async Task ResetDatabase()
=> await _databaseReset.Invoke();
public async Task<string> AddUserAsync(string userName, string password)
{
var user = new ApplicationUser { UserName = userName, Email = userName };
using (var scope = _host.ScopeFactory.CreateScope())
{
await scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>()
.CreateAsync(user, password);
}
return user.Id;
}
}
}