-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestHelper.cs
100 lines (86 loc) · 3.47 KB
/
TestHelper.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
using Blazored.Toast;
using BlazorStrap;
using Bunit;
using CardOverflow.Api;
using CardOverflow.Debug;
using CardOverflow.Entity;
using CardOverflow.Server;
using CardOverflow.Server.Pages.Deck;
using FluentValidation;
using FsToolkit.ErrorHandling;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using ThoughtDesign.WebLibrary;
using Xunit;
namespace CardOverflow.FrontEndTest {
public static class TestHelper {
public static T SideEffect<T>(T input, Action<T> action) {
action(input);
return input;
}
public static bool Not(bool input) => !input;
public static void Setup(TestServiceProvider services, Action<CardOverflowDb> setupDb) {
if (!services.IsProviderInitialized) {
services.AddBlazoredToast();
services.AddScoped<NavigationManager, MockNavigationManager>();
services.AddBootstrapCss();
services.AddTransient<IValidator<FollowCommandViewModel>, FollowCommandViewModelValidator>();
services.AddSingleton<DbExecutor>();
services.AddSingleton<TimeProvider>();
services.AddSingleton<Scheduler>();
services.AddSingleton<RandomProvider>();
services.AddDbContextPool<CardOverflowDb>(x => x.UseInMemoryDatabase(Guid.NewGuid().ToString()));
services.AddSingleton<Func<Task<NpgsqlConnection>>>(_ => async () => {
var conn = new NpgsqlConnection("Host=localhost;Database=CardOverflow;Username=postgres;");
await conn.OpenAsync();
return conn;
});
}
using var databaseContext = services.GetService<CardOverflowDb>();
databaseContext.Database.EnsureDeleted();
databaseContext.Database.EnsureCreated();
setupDb(databaseContext);
databaseContext.SaveChanges();
foreach (var entity in databaseContext.ChangeTracker.Entries()) {
entity.State = EntityState.Detached;
}
}
}
// temp example code; this entire class may be deleted. If you do, also delete Microsoft.AspNetCore.TestHost from the csproj
//public class PrimeWebDefaultRequestShould {
// private readonly TestServer _server;
// private readonly HttpClient _client;
// public PrimeWebDefaultRequestShould() {
// // Arrange
// //Host.CreateDefaultBuilder(args)
// // .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>())
// _server = new TestServer(new WebHostBuilder()
// .UseStartup<Startup>());
// _client = _server.CreateClient();
// }
// [Fact]
// public async System.Threading.Tasks.Task ReturnHelloWorld() {
// //Act
// var f = _server.Host.Services.GetService<Func<Task<NpgsqlConnection>>>();
// var ex = _server.Host.Services.GetService<DbExecutor>();
// //var db = _server.Host.Services.GetService<CardOverflowDb>();
// var ffasdf = await ex.QueryAsync(x => x.User.ToListAsync());
// var conn = await f();
// var asdf = await HistoryRepository.getHeatmap(conn, Guid.NewGuid());
// asdf.D();
// //var response = await _client.GetAsync("/terp");
// // response.EnsureSuccessStatusCode();
// // var responseString = await response.Content.ReadAsStringAsync();
// // // Assert
// // Assert.Equal("Hello World!", responseString);
// }
//}
}