diff options
author | crupest <crupest@outlook.com> | 2020-06-18 18:14:18 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-18 18:14:18 +0800 |
commit | 135f47e7477bb2a72c423145dcd286ae494fd3ed (patch) | |
tree | c4e27cac4b2057c645f031be12eda896b96aa435 /Timeline.Tests/Helpers/TestDatabase.cs | |
parent | 3dc9460798798e5ba155fae7b6afe84522c2c619 (diff) | |
parent | 2f91cce1c29ac4b9b49ca389b565c66199985f2d (diff) | |
download | timeline-135f47e7477bb2a72c423145dcd286ae494fd3ed.tar.gz timeline-135f47e7477bb2a72c423145dcd286ae494fd3ed.tar.bz2 timeline-135f47e7477bb2a72c423145dcd286ae494fd3ed.zip |
Merge pull request #111 from crupest/timeline-last-modified
Timeline last modified property.
Diffstat (limited to 'Timeline.Tests/Helpers/TestDatabase.cs')
-rw-r--r-- | Timeline.Tests/Helpers/TestDatabase.cs | 76 |
1 files changed, 76 insertions, 0 deletions
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<UserService>.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<DatabaseContext>()
+ .UseSqlite(Connection).Options;
+
+ return new DatabaseContext(options);
+ }
+ }
+}
|