diff options
author | crupest <crupest@outlook.com> | 2020-10-27 19:21:35 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-10-27 19:21:35 +0800 |
commit | ac769e656b122ff569c3f1534701b71e00fed586 (patch) | |
tree | 72966645ff1e25139d3995262e1c4349f2c14733 /Timeline.Tests/Services/TimelineServiceTest.cs | |
parent | 14e5848c23c643cea9b5d709770747d98c3d75e2 (diff) | |
download | timeline-ac769e656b122ff569c3f1534701b71e00fed586.tar.gz timeline-ac769e656b122ff569c3f1534701b71e00fed586.tar.bz2 timeline-ac769e656b122ff569c3f1534701b71e00fed586.zip |
Split front and back end.
Diffstat (limited to 'Timeline.Tests/Services/TimelineServiceTest.cs')
-rw-r--r-- | Timeline.Tests/Services/TimelineServiceTest.cs | 329 |
1 files changed, 0 insertions, 329 deletions
diff --git a/Timeline.Tests/Services/TimelineServiceTest.cs b/Timeline.Tests/Services/TimelineServiceTest.cs deleted file mode 100644 index 5a774b78..00000000 --- a/Timeline.Tests/Services/TimelineServiceTest.cs +++ /dev/null @@ -1,329 +0,0 @@ -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.Models;
-using Timeline.Services;
-using Timeline.Services.Exceptions;
-using Timeline.Tests.Helpers;
-using Xunit;
-
-namespace Timeline.Tests.Services
-{
- public class TimelineServiceTest : IAsyncLifetime, IDisposable
- {
- private readonly 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;
-
- private UserDeleteService _userDeleteService;
-
- 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, _clock);
- _timelineService = new TimelineService(NullLogger<TimelineService>.Instance, _databaseContext, _dataManager, _userService, _imageValidator, _clock);
- _userDeleteService = new UserDeleteService(NullLogger<UserDeleteService>.Instance, _databaseContext, _timelineService);
- }
-
- public async Task DisposeAsync()
- {
- await _testDatabase.DisposeAsync();
- await _databaseContext.DisposeAsync();
- }
-
- public void Dispose()
- {
- _eTagGenerator.Dispose();
- }
-
- [Theory]
- [InlineData("@user")]
- [InlineData("tl")]
- public async Task Timeline_GetLastModified(string timelineName)
- {
- var time = _clock.ForwardCurrentTime();
-
- var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal);
- if (!isPersonal)
- await _timelineService.CreateTimeline(timelineName, await _userService.GetUserIdByUsername("user"));
-
- var t = await _timelineService.GetTimelineLastModifiedTime(timelineName);
-
- t.Should().Be(time);
- }
-
- [Theory]
- [InlineData("@user")]
- [InlineData("tl")]
- public async Task Timeline_GetUnqiueId(string timelineName)
- {
- var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal);
- if (!isPersonal)
- await _timelineService.CreateTimeline(timelineName, await _userService.GetUserIdByUsername("user"));
-
- var uniqueId = await _timelineService.GetTimelineUniqueId(timelineName);
-
- uniqueId.Should().NotBeNullOrEmpty();
- }
-
- [Theory]
- [InlineData("@user")]
- [InlineData("tl")]
- public async Task Timeline_LastModified(string timelineName)
- {
- var initTime = _clock.ForwardCurrentTime();
-
- void Check(Models.Timeline timeline)
- {
- timeline.NameLastModified.Should().Be(initTime);
- timeline.LastModified.Should().Be(_clock.GetCurrentTime());
- }
-
- async Task GetAndCheck()
- {
- Check(await _timelineService.GetTimeline(timelineName));
- }
-
- var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal);
- if (!isPersonal)
- Check(await _timelineService.CreateTimeline(timelineName, await _userService.GetUserIdByUsername("user")));
-
- await GetAndCheck();
-
- _clock.ForwardCurrentTime();
- await _timelineService.ChangeProperty(timelineName, new TimelineChangePropertyRequest { Visibility = TimelineVisibility.Public });
- await GetAndCheck();
-
- _clock.ForwardCurrentTime();
- await _timelineService.ChangeMember(timelineName, new List<string> { "admin" }, null);
- await GetAndCheck();
- }
-
- [Theory]
- [InlineData("@user")]
- [InlineData("tl")]
- public async Task GetPosts_ModifiedSince(string timelineName)
- {
- _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" };
-
- DateTime testPoint = new DateTime();
-
- foreach (var (content, index) in postContentList.Select((v, i) => (v, i)))
- {
- var t = _clock.ForwardCurrentTime();
- if (index == 1)
- testPoint = t;
- await _timelineService.CreateTextPost(timelineName, userId, content, null);
- }
-
- var posts = await _timelineService.GetPosts(timelineName, testPoint);
- posts.Should().HaveCount(3)
- .And.Subject.Select(p => (p.Content as TextTimelinePostContent).Text).Should().Equal(postContentList.Skip(1));
- }
-
- [Theory]
- [InlineData("@user")]
- [InlineData("tl")]
- public async Task GetPosts_IncludeDeleted(string timelineName)
- {
- 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 in postContentList)
- {
- await _timelineService.CreateTextPost(timelineName, userId, content, null);
- }
-
- var posts = await _timelineService.GetPosts(timelineName);
- posts.Should().HaveCount(4);
- posts.Select(p => p.Deleted).Should().Equal(Enumerable.Repeat(false, posts.Count));
- posts.Select(p => ((TextTimelinePostContent)p.Content).Text).Should().Equal(postContentList);
-
- foreach (var id in new long[] { posts[0].Id, posts[2].Id })
- {
- await _timelineService.DeletePost(timelineName, id);
- }
-
- posts = await _timelineService.GetPosts(timelineName);
- posts.Should().HaveCount(2);
- posts.Select(p => p.Deleted).Should().Equal(Enumerable.Repeat(false, posts.Count));
- posts.Select(p => ((TextTimelinePostContent)p.Content).Text).Should().Equal(new string[] { "b", "d" });
-
- posts = await _timelineService.GetPosts(timelineName, includeDeleted: true);
- posts.Should().HaveCount(4);
- 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);
- }
- }
-
- [Theory]
- [InlineData("@admin")]
- [InlineData("tl")]
- public async Task Title(string timelineName)
- {
- var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal);
- if (!isPersonal)
- await _timelineService.CreateTimeline(timelineName, await _userService.GetUserIdByUsername("user"));
-
- {
- var timeline = await _timelineService.GetTimeline(timelineName);
- timeline.Title.Should().Be(timelineName);
- }
-
- {
- await _timelineService.ChangeProperty(timelineName, new TimelineChangePropertyRequest { Title = null });
- var timeline = await _timelineService.GetTimeline(timelineName);
- timeline.Title.Should().Be(timelineName);
- }
-
- {
- await _timelineService.ChangeProperty(timelineName, new TimelineChangePropertyRequest { Title = "atitle" });
- var timeline = await _timelineService.GetTimeline(timelineName);
- timeline.Title.Should().Be("atitle");
- }
- }
-
- [Fact]
- public async Task ChangeName()
- {
- _clock.ForwardCurrentTime();
-
- await _timelineService.Awaiting(s => s.ChangeTimelineName("!!!", "newtl")).Should().ThrowAsync<ArgumentException>();
- await _timelineService.Awaiting(s => s.ChangeTimelineName("tl", "!!!")).Should().ThrowAsync<ArgumentException>();
- await _timelineService.Awaiting(s => s.ChangeTimelineName("tl", "newtl")).Should().ThrowAsync<TimelineNotExistException>();
-
- await _timelineService.CreateTimeline("tl", await _userService.GetUserIdByUsername("user"));
- await _timelineService.CreateTimeline("tl2", await _userService.GetUserIdByUsername("user"));
-
- await _timelineService.Awaiting(s => s.ChangeTimelineName("tl", "tl2")).Should().ThrowAsync<EntityAlreadyExistException>();
-
- var time = _clock.ForwardCurrentTime();
-
- await _timelineService.ChangeTimelineName("tl", "newtl");
-
- {
- var timeline = await _timelineService.GetTimeline("newtl");
- timeline.Name.Should().Be("newtl");
- timeline.LastModified.Should().Be(time);
- timeline.NameLastModified.Should().Be(time);
- }
- }
- }
-}
|