From 12b5ecddb47e50c9f4597553a795c68b09acaddb Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 11 Aug 2020 18:16:52 +0800 Subject: Add unit tests. --- Timeline.Tests/Services/TimelineServiceTest.cs | 84 ++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'Timeline.Tests/Services/TimelineServiceTest.cs') diff --git a/Timeline.Tests/Services/TimelineServiceTest.cs b/Timeline.Tests/Services/TimelineServiceTest.cs index e129b49d..36e5ed0c 100644 --- a/Timeline.Tests/Services/TimelineServiceTest.cs +++ b/Timeline.Tests/Services/TimelineServiceTest.cs @@ -32,6 +32,8 @@ namespace Timeline.Tests.Services private TimelineService _timelineService; + private UserDeleteService _userDeleteService; + public TimelineServiceTest() { } @@ -43,6 +45,7 @@ namespace Timeline.Tests.Services _dataManager = new DataManager(_databaseContext, _eTagGenerator); _userService = new UserService(NullLogger.Instance, _databaseContext, _passwordService, _clock); _timelineService = new TimelineService(NullLogger.Instance, _databaseContext, _dataManager, _userService, _imageValidator, _clock); + _userDeleteService = new UserDeleteService(NullLogger.Instance, _databaseContext, _timelineService); } public async Task DisposeAsync() @@ -187,5 +190,86 @@ namespace Timeline.Tests.Services posts.Select(p => p.Deleted).Should().Equal(new bool[] { true, false, true, false }); posts.Where(p => !p.Deleted).Select(p => ((TextTimelinePostContent)p.Content).Text).Should().Equal(new string[] { "b", "d" }); } + + [Theory] + [InlineData("@admin")] + [InlineData("tl")] + public async Task GetPosts_ModifiedSince_UsernameChange(string timelineName) + { + var time1 = _clock.ForwardCurrentTime(); + + var userId = await _userService.GetUserIdByUsername("user"); + + var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal); + if (!isPersonal) + await _timelineService.CreateTimeline(timelineName, userId); + + var postContentList = new string[] { "a", "b", "c", "d" }; + + foreach (var (content, index) in postContentList.Select((v, i) => (v, i))) + { + await _timelineService.CreateTextPost(timelineName, userId, content, null); + } + + var time2 = _clock.ForwardCurrentTime(); + + { + var posts = await _timelineService.GetPosts(timelineName, time2); + posts.Should().HaveCount(0); + } + + { + await _userService.ModifyUser(userId, new User { Nickname = "haha" }); + var posts = await _timelineService.GetPosts(timelineName, time2); + posts.Should().HaveCount(0); + } + + { + await _userService.ModifyUser(userId, new User { Username = "haha" }); + var posts = await _timelineService.GetPosts(timelineName, time2); + posts.Should().HaveCount(4); + } + } + + [Theory] + [InlineData("@admin")] + [InlineData("tl")] + public async Task GetPosts_ModifiedSince_UserDelete(string timelineName) + { + var time1 = _clock.ForwardCurrentTime(); + + var userId = await _userService.GetUserIdByUsername("user"); + var adminId = await _userService.GetUserIdByUsername("admin"); + + var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal); + if (!isPersonal) + await _timelineService.CreateTimeline(timelineName, adminId); + + var postContentList = new string[] { "a", "b", "c", "d" }; + + foreach (var (content, index) in postContentList.Select((v, i) => (v, i))) + { + await _timelineService.CreateTextPost(timelineName, userId, content, null); + } + + var time2 = _clock.ForwardCurrentTime(); + + { + var posts = await _timelineService.GetPosts(timelineName, time2); + posts.Should().HaveCount(0); + } + + await _userDeleteService.DeleteUser("user"); + + { + var posts = await _timelineService.GetPosts(timelineName, time2); + posts.Should().HaveCount(0); + } + + { + var posts = await _timelineService.GetPosts(timelineName, time2, true); + posts.Should().HaveCount(4); + } + } } } -- cgit v1.2.3