From 3e471ac783d91fcc61a90b759fecefe3b80014ba Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 18 Jun 2020 16:21:39 +0800 Subject: Add last modified info to timeline. --- Timeline.Tests/Helpers/TestApplication.cs | 29 +++--------- Timeline.Tests/Helpers/TestClock.cs | 23 ++++++++++ Timeline.Tests/Helpers/TestDatabase.cs | 76 +++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 22 deletions(-) create mode 100644 Timeline.Tests/Helpers/TestClock.cs create mode 100644 Timeline.Tests/Helpers/TestDatabase.cs (limited to 'Timeline.Tests/Helpers') diff --git a/Timeline.Tests/Helpers/TestApplication.cs b/Timeline.Tests/Helpers/TestApplication.cs index 45807516..684ffe2c 100644 --- a/Timeline.Tests/Helpers/TestApplication.cs +++ b/Timeline.Tests/Helpers/TestApplication.cs @@ -10,14 +10,13 @@ using System.IO; using System.Threading.Tasks; using Timeline.Configs; using Timeline.Entities; -using Timeline.Migrations; using Xunit; namespace Timeline.Tests.Helpers { public class TestApplication : IAsyncLifetime { - public SqliteConnection DatabaseConnection { get; private set; } + public TestDatabase Database { get; } public IHost Host { get; private set; } @@ -25,30 +24,16 @@ namespace Timeline.Tests.Helpers public TestApplication() { - + Database = new TestDatabase(false); } public async Task InitializeAsync() { + await Database.InitializeAsync(); + WorkDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(WorkDir); - DatabaseConnection = new SqliteConnection("Data Source=:memory:;"); - await DatabaseConnection.OpenAsync(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(DatabaseConnection).Options; - - using (var context = new DatabaseContext(options)) - { - await context.Database.EnsureCreatedAsync(); - context.JwtToken.Add(new JwtTokenEntity - { - Key = JwtTokenGenerateHelper.GenerateKey() - }); - await context.SaveChangesAsync(); - } - Host = await Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder() .ConfigureAppConfiguration((context, config) => { @@ -62,7 +47,7 @@ namespace Timeline.Tests.Helpers { services.AddDbContext(options => { - options.UseSqlite(DatabaseConnection); + options.UseSqlite(Database.Connection); }); }) .ConfigureWebHost(webBuilder => @@ -79,9 +64,9 @@ namespace Timeline.Tests.Helpers await Host.StopAsync(); Host.Dispose(); - await DatabaseConnection.CloseAsync(); - await DatabaseConnection.DisposeAsync(); Directory.Delete(WorkDir, true); + + await Database.DisposeAsync(); } } } diff --git a/Timeline.Tests/Helpers/TestClock.cs b/Timeline.Tests/Helpers/TestClock.cs new file mode 100644 index 00000000..7febc0fe --- /dev/null +++ b/Timeline.Tests/Helpers/TestClock.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Timeline.Services; + +namespace Timeline.Tests.Helpers +{ + public class TestClock : IClock + { + private DateTime? _currentTime = null; + + public DateTime GetCurrentTime() + { + return _currentTime ?? DateTime.Now; + } + + public void SetCurrentTime(DateTime? mockTime) + { + _currentTime = mockTime; + } + } +} diff --git a/Timeline.Tests/Helpers/TestDatabase.cs b/Timeline.Tests/Helpers/TestDatabase.cs new file mode 100644 index 00000000..40f8f2d4 --- /dev/null +++ b/Timeline.Tests/Helpers/TestDatabase.cs @@ -0,0 +1,76 @@ +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging.Abstractions; +using System.Threading.Tasks; +using Timeline.Entities; +using Timeline.Migrations; +using Timeline.Models; +using Timeline.Services; +using Xunit; + +namespace Timeline.Tests.Helpers +{ + public class TestDatabase : IAsyncLifetime + { + private readonly bool _createUser; + + public TestDatabase(bool createUser = true) + { + _createUser = createUser; + Connection = new SqliteConnection("Data Source=:memory:;"); + } + + public async Task InitializeAsync() + { + await Connection.OpenAsync(); + + using (var context = CreateContext()) + { + await context.Database.EnsureCreatedAsync(); + context.JwtToken.Add(new JwtTokenEntity + { + Key = JwtTokenGenerateHelper.GenerateKey() + }); + await context.SaveChangesAsync(); + + if (_createUser) + { + var passwordService = new PasswordService(); + var userService = new UserService(NullLogger.Instance, context, passwordService); + + await userService.CreateUser(new User + { + Username = "admin", + Password = "adminpw", + Administrator = true, + Nickname = "administrator" + }); + + await userService.CreateUser(new User + { + Username = "user", + Password = "userpw", + Administrator = false, + Nickname = "imuser" + }); + } + } + } + + public async Task DisposeAsync() + { + await Connection.CloseAsync(); + await Connection.DisposeAsync(); + } + + public SqliteConnection Connection { get; } + + public DatabaseContext CreateContext() + { + var options = new DbContextOptionsBuilder() + .UseSqlite(Connection).Options; + + return new DatabaseContext(options); + } + } +} -- cgit v1.2.3