diff options
author | 杨宇千 <crupest@outlook.com> | 2019-11-20 18:21:17 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-20 18:21:17 +0800 |
commit | ae5bb5cbef2aec94673e26712d7937fca3996f5b (patch) | |
tree | 788b8acdf1141c757cb3226d3cd5f64594386b8f /Timeline.Tests/Helpers/TestDatabase.cs | |
parent | 37a2e6340ab20de1f9e847d795c0cbec9846de97 (diff) | |
parent | ca87f6781a5b0e80989a66be338a699846c40f8d (diff) | |
download | timeline-ae5bb5cbef2aec94673e26712d7937fca3996f5b.tar.gz timeline-ae5bb5cbef2aec94673e26712d7937fca3996f5b.tar.bz2 timeline-ae5bb5cbef2aec94673e26712d7937fca3996f5b.zip |
Merge pull request #54 from crupest/timeline
Add core feature Timeline (currently only personal timeline)
Diffstat (limited to 'Timeline.Tests/Helpers/TestDatabase.cs')
-rw-r--r-- | Timeline.Tests/Helpers/TestDatabase.cs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/Timeline.Tests/Helpers/TestDatabase.cs b/Timeline.Tests/Helpers/TestDatabase.cs new file mode 100644 index 00000000..10224c27 --- /dev/null +++ b/Timeline.Tests/Helpers/TestDatabase.cs @@ -0,0 +1,89 @@ +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using Timeline.Entities; +using Timeline.Models; +using Timeline.Services; + +namespace Timeline.Tests.Helpers +{ + public class TestDatabase : IDisposable + { + // currently password service is thread safe, so we share a static one. + private static PasswordService PasswordService { get; } = new PasswordService(); + + private static User CreateEntityFromMock(MockUser user) + { + return new User + { + Name = user.Username, + EncryptedPassword = PasswordService.HashPassword(user.Password), + RoleString = UserRoleConvert.ToString(user.Administrator), + Avatar = null + }; + } + + private static IEnumerable<User> CreateDefaultMockEntities() + { + // emmmmmmm. Never reuse the user instances because EF Core uses them, which will cause strange things. + yield return CreateEntityFromMock(MockUser.User); + yield return CreateEntityFromMock(MockUser.Admin); + } + + private static void InitDatabase(DatabaseContext context) + { + context.Database.EnsureCreated(); + context.Users.AddRange(CreateDefaultMockEntities()); + context.SaveChanges(); + } + + public SqliteConnection Connection { get; } + public DatabaseContext Context { get; } + + public TestDatabase() + { + Connection = new SqliteConnection("Data Source=:memory:;"); + Connection.Open(); + + var options = new DbContextOptionsBuilder<DatabaseContext>() + .UseSqlite(Connection) + .Options; + + Context = new DatabaseContext(options); + + InitDatabase(Context); + } + + private List<MockUser> _extraMockUsers; + + public IReadOnlyList<MockUser> ExtraMockUsers => _extraMockUsers; + + public void CreateExtraMockUsers(int count) + { + if (count <= 0) + throw new ArgumentOutOfRangeException(nameof(count), count, "Additional user count must be bigger than 0."); + if (_extraMockUsers != null) + throw new InvalidOperationException("Already create mock users."); + + _extraMockUsers = new List<MockUser>(); + for (int i = 0; i < count; i++) + { + _extraMockUsers.Add(new MockUser($"user{i}", $"password", false)); + } + + Context.AddRange(_extraMockUsers.Select(u => CreateEntityFromMock(u))); + Context.SaveChanges(); + } + + public void Dispose() + { + Context.Dispose(); + + Connection.Close(); + Connection.Dispose(); + } + + } +} |