From 15eb51e4b5014e1969677819ced1892c9e41944a Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 23 Apr 2021 17:44:02 +0800 Subject: test: Test database now use migrator. --- BackEnd/Timeline.Tests/Helpers/TestDatabase.cs | 34 ++++++---------------- BackEnd/Timeline.Tests/Services/ServiceTestBase.cs | 6 ++-- 2 files changed, 11 insertions(+), 29 deletions(-) (limited to 'BackEnd') diff --git a/BackEnd/Timeline.Tests/Helpers/TestDatabase.cs b/BackEnd/Timeline.Tests/Helpers/TestDatabase.cs index d37aace4..c3b61919 100644 --- a/BackEnd/Timeline.Tests/Helpers/TestDatabase.cs +++ b/BackEnd/Timeline.Tests/Helpers/TestDatabase.cs @@ -3,7 +3,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging.Abstractions; using System.Threading.Tasks; using Timeline.Entities; -using Timeline.Migrations; using Timeline.Services; using Xunit; using Xunit.Abstractions; @@ -12,11 +11,8 @@ namespace Timeline.Tests.Helpers { public class TestDatabase : IAsyncLifetime { - private readonly bool _init; - - public TestDatabase(bool init = true) + public TestDatabase() { - _init = init; Connection = new SqliteConnection("Data Source=:memory:;"); } @@ -24,29 +20,17 @@ namespace Timeline.Tests.Helpers { await Connection.OpenAsync(); - if (_init) - { - using var context = CreateContext(); - await context.Database.EnsureCreatedAsync(); - - context.JwtToken.Add(new JwtTokenEntity - { - Key = JwtTokenGenerateHelper.GenerateKey() - }); - - await context.SaveChangesAsync(); - - var passwordService = new PasswordService(); - var userService = new UserService(NullLogger.Instance, context, passwordService, new Clock()); + using var context = CreateContext(); + await context.Database.MigrateAsync(); - var admin = await userService.CreateUser("admin", "adminpw"); - await userService.ModifyUser(admin.Id, new ModifyUserParams() { Nickname = "administrator" }); + var userService = new UserService(NullLogger.Instance, context, new PasswordService(), new Clock()); - await context.SaveChangesAsync(); + await userService.ModifyUser( + await userService.GetUserIdByUsername("administrator"), + new ModifyUserParams() { Username = "admin", Password = "adminpw", Nickname = "administrator" }); - var user = await userService.CreateUser("user", "userpw"); - await userService.ModifyUser(user.Id, new ModifyUserParams() { Nickname = "imuser" }); - } + var user = await userService.CreateUser("user", "userpw"); + await userService.ModifyUser(user.Id, new ModifyUserParams() { Nickname = "imuser" }); } public async Task DisposeAsync() diff --git a/BackEnd/Timeline.Tests/Services/ServiceTestBase.cs b/BackEnd/Timeline.Tests/Services/ServiceTestBase.cs index 5a3e1e19..654116d0 100644 --- a/BackEnd/Timeline.Tests/Services/ServiceTestBase.cs +++ b/BackEnd/Timeline.Tests/Services/ServiceTestBase.cs @@ -22,14 +22,12 @@ namespace Timeline.Tests.Services protected long UserId { get; private set; } protected long AdminId { get; private set; } - protected ServiceTestBase(bool databaseCreateUsers = true, ITestOutputHelper? testOutputHelper = null) + protected ServiceTestBase(ITestOutputHelper? testOutputHelper = null) { _testOutputHelper = testOutputHelper; - TestDatabase = new TestDatabase(databaseCreateUsers); + TestDatabase = new TestDatabase(); } - protected ServiceTestBase(ITestOutputHelper? testOutputHelper) : this(true, testOutputHelper) { } - public async Task InitializeAsync() { await TestDatabase.InitializeAsync(); -- cgit v1.2.3