diff options
author | crupest <crupest@outlook.com> | 2020-12-18 22:40:22 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-12-18 22:40:22 +0800 |
commit | ab3aedad37fe4634efb0d6939d7a40642bfff032 (patch) | |
tree | 4201ce9060ea097da9b4a3f15d40e4c9073f3a79 | |
parent | 53291d2c16047d3eb4c5eeb9a216c106cf47ead3 (diff) | |
download | timeline-ab3aedad37fe4634efb0d6939d7a40642bfff032.tar.gz timeline-ab3aedad37fe4634efb0d6939d7a40642bfff032.tar.bz2 timeline-ab3aedad37fe4634efb0d6939d7a40642bfff032.zip |
feat: Bookmark timeline service unit tests.
3 files changed, 95 insertions, 2 deletions
diff --git a/BackEnd/Timeline.Tests/Services/BookmarkTimelineServiceTest.cs b/BackEnd/Timeline.Tests/Services/BookmarkTimelineServiceTest.cs new file mode 100644 index 00000000..1b8bff63 --- /dev/null +++ b/BackEnd/Timeline.Tests/Services/BookmarkTimelineServiceTest.cs @@ -0,0 +1,89 @@ +using FluentAssertions;
+using Microsoft.Extensions.Logging.Abstractions;
+using System.Threading.Tasks;
+using Timeline.Services;
+using Timeline.Tests.Helpers;
+using Xunit;
+
+namespace Timeline.Tests.Services
+{
+ public class BookmarkTimelineServiceTest : DatabaseBasedTest
+ {
+ private BookmarkTimelineService _service = default!;
+ private UserService _userService = default!;
+ private TimelineService _timelineService = default!;
+
+ protected override void OnDatabaseCreated()
+ {
+ var clock = new TestClock();
+ _userService = new UserService(NullLogger<UserService>.Instance, Database, new PasswordService(), new UserPermissionService(Database), clock);
+ _timelineService = new TimelineService(Database, _userService, clock);
+ _service = new BookmarkTimelineService(Database, _userService, _timelineService);
+ }
+
+ [Fact]
+ public async Task Should_Work()
+ {
+ var userId = await _userService.GetUserIdByUsername("user");
+
+ {
+ var b = await _service.GetBookmarks(userId);
+ b.Should().BeEmpty();
+ }
+
+ await _timelineService.CreateTimeline("tl", userId);
+ await _service.AddBookmark(userId, "tl");
+
+ {
+ var b = await _service.GetBookmarks(userId);
+ b.Should().HaveCount(1).And.BeEquivalentTo(await _timelineService.GetTimeline("tl"));
+ }
+ }
+
+ [Fact]
+ public async Task NewOne_Should_BeAtLast()
+ {
+ var userId = await _userService.GetUserIdByUsername("user");
+ await _timelineService.CreateTimeline("t1", userId);
+ await _service.AddBookmark(userId, "t1");
+
+ await _timelineService.CreateTimeline("t2", userId);
+ await _service.AddBookmark(userId, "t2");
+
+ var b = await _service.GetBookmarks(userId);
+
+ b.Should().HaveCount(2);
+ b[0].Name.Should().Be("t1");
+ b[1].Name.Should().Be("t2");
+ }
+
+ [Fact]
+ public async Task Multiple_Should_Work()
+ {
+ var userId = await _userService.GetUserIdByUsername("user");
+
+ // make timeline id not same as entity id.
+ await _timelineService.CreateTimeline("t0", userId);
+
+ await _timelineService.CreateTimeline("t1", userId);
+ await _service.AddBookmark(userId, "t1");
+
+ await _timelineService.CreateTimeline("t2", userId);
+ await _service.AddBookmark(userId, "t2");
+
+ await _timelineService.CreateTimeline("t3", userId);
+ await _service.AddBookmark(userId, "t3");
+
+ await _service.MoveBookmark(userId, "t3", 2);
+ (await _service.GetBookmarks(userId))[1].Name.Should().Be("t3");
+
+ await _service.MoveBookmark(userId, "t1", 3);
+ (await _service.GetBookmarks(userId))[2].Name.Should().Be("t1");
+
+ await _service.RemoveBookmark(userId, "t2");
+ await _service.RemoveBookmark(userId, "t1");
+ await _service.RemoveBookmark(userId, "t3");
+ (await _service.GetBookmarks(userId)).Should().BeEmpty();
+ }
+ }
+}
diff --git a/BackEnd/Timeline.Tests/Services/HighlightTimelineServiceTest.cs b/BackEnd/Timeline.Tests/Services/HighlightTimelineServiceTest.cs index dca070c6..f48404a9 100644 --- a/BackEnd/Timeline.Tests/Services/HighlightTimelineServiceTest.cs +++ b/BackEnd/Timeline.Tests/Services/HighlightTimelineServiceTest.cs @@ -68,6 +68,10 @@ namespace Timeline.Tests.Services public async Task Multiple_Should_Work()
{
var userId = await _userService.GetUserIdByUsername("user");
+
+ // make timeline id not same as entity id.
+ await _timelineService.CreateTimeline("t0", userId);
+
await _timelineService.CreateTimeline("t1", userId);
await _service.AddHighlightTimeline("t1", userId);
diff --git a/BackEnd/Timeline/Services/BookmarkTimelineService.cs b/BackEnd/Timeline/Services/BookmarkTimelineService.cs index f65a1ff0..09438193 100644 --- a/BackEnd/Timeline/Services/BookmarkTimelineService.cs +++ b/BackEnd/Timeline/Services/BookmarkTimelineService.cs @@ -109,7 +109,7 @@ namespace Timeline.Services if (!await _userService.CheckUserExistence(userId))
throw new UserNotExistException(userId);
- var entities = await _database.BookmarkTimelines.Where(t => t.UserId == userId).Select(t => new { t.TimelineId }).ToListAsync();
+ var entities = await _database.BookmarkTimelines.Where(t => t.UserId == userId).OrderBy(t => t.Rank).Select(t => new { t.TimelineId }).ToListAsync();
List<TimelineInfo> result = new();
@@ -140,7 +140,7 @@ namespace Timeline.Services }
else
{
- var totalCount = await _database.HighlightTimelines.CountAsync();
+ var totalCount = await _database.BookmarkTimelines.CountAsync(t => t.UserId == userId);
if (newPosition > totalCount) newPosition = totalCount;
}
|