aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-06-18 16:21:39 +0800
committercrupest <crupest@outlook.com>2020-06-18 16:21:39 +0800
commit3e471ac783d91fcc61a90b759fecefe3b80014ba (patch)
tree7821cc512b7262954f4981042dcf599768e068f4 /Timeline.Tests
parent3dc9460798798e5ba155fae7b6afe84522c2c619 (diff)
downloadtimeline-3e471ac783d91fcc61a90b759fecefe3b80014ba.tar.gz
timeline-3e471ac783d91fcc61a90b759fecefe3b80014ba.tar.bz2
timeline-3e471ac783d91fcc61a90b759fecefe3b80014ba.zip
Add last modified info to timeline.
Diffstat (limited to 'Timeline.Tests')
-rw-r--r--Timeline.Tests/Helpers/TestApplication.cs29
-rw-r--r--Timeline.Tests/Helpers/TestClock.cs23
-rw-r--r--Timeline.Tests/Helpers/TestDatabase.cs76
-rw-r--r--Timeline.Tests/Services/TimelineServiceTest.cs87
4 files changed, 193 insertions, 22 deletions
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<DatabaseContext>()
- .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<DatabaseContext>(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<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);
+ }
+ }
+}
diff --git a/Timeline.Tests/Services/TimelineServiceTest.cs b/Timeline.Tests/Services/TimelineServiceTest.cs
new file mode 100644
index 00000000..f7a94dfc
--- /dev/null
+++ b/Timeline.Tests/Services/TimelineServiceTest.cs
@@ -0,0 +1,87 @@
+using Castle.Core.Logging;
+using FluentAssertions;
+using Microsoft.Extensions.Logging.Abstractions;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Timeline.Entities;
+using Timeline.Services;
+using Timeline.Tests.Helpers;
+using Xunit;
+
+namespace Timeline.Tests.Services
+{
+ public class TimelineServiceTest : IAsyncLifetime
+ {
+ private TestDatabase _testDatabase = new TestDatabase();
+
+ private DatabaseContext _databaseContext;
+
+ private readonly PasswordService _passwordService = new PasswordService();
+
+ private readonly ETagGenerator _eTagGenerator = new ETagGenerator();
+
+ private readonly ImageValidator _imageValidator = new ImageValidator();
+
+ private readonly TestClock _clock = new TestClock();
+
+ private DataManager _dataManager;
+
+ private UserService _userService;
+
+ private TimelineService _timelineService;
+
+ public TimelineServiceTest()
+ {
+ }
+
+ public async Task InitializeAsync()
+ {
+ await _testDatabase.InitializeAsync();
+ _databaseContext = _testDatabase.CreateContext();
+ _dataManager = new DataManager(_databaseContext, _eTagGenerator);
+ _userService = new UserService(NullLogger<UserService>.Instance, _databaseContext, _passwordService);
+ _timelineService = new TimelineService(NullLogger<TimelineService>.Instance, _databaseContext, _dataManager, _userService, _imageValidator, _clock);
+ }
+
+ public async Task DisposeAsync()
+ {
+ await _testDatabase.DisposeAsync();
+ }
+
+ [Fact]
+ public async Task PersonalTimeline_LastModified()
+ {
+ var mockTime = new DateTime(2000, 1, 1, 1, 1, 1);
+
+ _clock.SetCurrentTime(mockTime);
+
+ var timeline = await _timelineService.GetTimeline("@user");
+
+ timeline.NameLastModified.Should().Be(mockTime);
+ timeline.LastModified.Should().Be(mockTime);
+ }
+
+ [Fact]
+ public async Task OrdinaryTimeline_LastModified()
+ {
+ var mockTime = new DateTime(2000, 1, 1, 1, 1, 1);
+
+ _clock.SetCurrentTime(mockTime);
+
+ {
+ var timeline = await _timelineService.CreateTimeline("tl", await _userService.GetUserIdByUsername("user"));
+
+ timeline.NameLastModified.Should().Be(mockTime);
+ timeline.LastModified.Should().Be(mockTime);
+ }
+
+ {
+ var timeline = await _timelineService.GetTimeline("tl");
+ timeline.NameLastModified.Should().Be(mockTime);
+ timeline.LastModified.Should().Be(mockTime);
+ }
+ }
+ }
+}