aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline.Tests
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-01-19 15:27:01 +0800
committerGitHub <noreply@github.com>2021-01-19 15:27:01 +0800
commit23433673f91c1b07c43c4b2280e29db45e8896b5 (patch)
tree455fbd07678b1b21a1bfa3ad7e3778b1957fd12f /BackEnd/Timeline.Tests
parenta23b8af0b06be2ab58d1831a0a25a30d934ec1e2 (diff)
parentfb6442f1716406c7a2da79e4a1cc42e23908d218 (diff)
downloadtimeline-23433673f91c1b07c43c4b2280e29db45e8896b5.tar.gz
timeline-23433673f91c1b07c43c4b2280e29db45e8896b5.tar.bz2
timeline-23433673f91c1b07c43c4b2280e29db45e8896b5.zip
Merge pull request #211 from crupest/search
Back search feature.
Diffstat (limited to 'BackEnd/Timeline.Tests')
-rw-r--r--BackEnd/Timeline.Tests/IntegratedTests/BaseTimelineTest.cs2
-rw-r--r--BackEnd/Timeline.Tests/IntegratedTests/BookmarkTimelineTest.cs4
-rw-r--r--BackEnd/Timeline.Tests/IntegratedTests/HighlightTimelineTest.cs4
-rw-r--r--BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs5
-rw-r--r--BackEnd/Timeline.Tests/IntegratedTests/SearchTest.cs63
-rw-r--r--BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs8
-rw-r--r--BackEnd/Timeline.Tests/Services/DatabaseBasedTest.cs55
-rw-r--r--BackEnd/Timeline.Tests/Services/SearchServiceTest.cs49
-rw-r--r--BackEnd/Timeline.Tests/Services/ServiceTestBase.cs71
-rw-r--r--BackEnd/Timeline.Tests/Services/UserDeleteServiceTest.cs4
-rw-r--r--BackEnd/Timeline.Tests/Services/UserPermissionServiceTest.cs4
11 files changed, 201 insertions, 68 deletions
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/BaseTimelineTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/BaseTimelineTest.cs
index 0bf3b2b2..006b5128 100644
--- a/BackEnd/Timeline.Tests/IntegratedTests/BaseTimelineTest.cs
+++ b/BackEnd/Timeline.Tests/IntegratedTests/BaseTimelineTest.cs
@@ -15,7 +15,7 @@ namespace Timeline.Tests.IntegratedTests
for (int i = 0; i <= 3; i++)
{
using var client = await CreateClientAs(i);
- await client.TestPostAsync("timelines", new TimelineCreateRequest { Name = $"t{i}" });
+ await client.TestPostAsync("timelines", new HttpTimelineCreateRequest { Name = $"t{i}" });
}
}
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/BookmarkTimelineTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/BookmarkTimelineTest.cs
index 99cf6d3a..23d67d44 100644
--- a/BackEnd/Timeline.Tests/IntegratedTests/BookmarkTimelineTest.cs
+++ b/BackEnd/Timeline.Tests/IntegratedTests/BookmarkTimelineTest.cs
@@ -34,7 +34,7 @@ namespace Timeline.Tests.IntegratedTests
public async Task ShouldWork()
{
using var client = await CreateClientAsUser();
- await client.TestPostAsync("timelines", new TimelineCreateRequest { Name = "t1" });
+ await client.TestPostAsync("timelines", new HttpTimelineCreateRequest { Name = "t1" });
{
@@ -88,7 +88,7 @@ namespace Timeline.Tests.IntegratedTests
public async Task TimelineGet_IsBookmarkField_ShouldWork()
{
using var client = await CreateClientAsUser();
- await client.TestPostAsync("timelines", new TimelineCreateRequest { Name = "t" });
+ await client.TestPostAsync("timelines", new HttpTimelineCreateRequest { Name = "t" });
{
var t = await client.TestGetAsync<HttpTimeline>("timelines/t");
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/HighlightTimelineTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/HighlightTimelineTest.cs
index 440759f4..cfc41468 100644
--- a/BackEnd/Timeline.Tests/IntegratedTests/HighlightTimelineTest.cs
+++ b/BackEnd/Timeline.Tests/IntegratedTests/HighlightTimelineTest.cs
@@ -35,7 +35,7 @@ namespace Timeline.Tests.IntegratedTests
{
{
using var client1 = await CreateClientAsUser();
- await client1.TestPostAsync("timelines", new TimelineCreateRequest { Name = "t1" });
+ await client1.TestPostAsync("timelines", new HttpTimelineCreateRequest { Name = "t1" });
}
using var client = await CreateClientAsAdministrator();
@@ -91,7 +91,7 @@ namespace Timeline.Tests.IntegratedTests
public async Task TimelineGet_IsHighlighField_Should_Work()
{
using var client = await CreateClientAsAdministrator();
- await client.TestPostAsync("timelines", new TimelineCreateRequest { Name = "t" });
+ await client.TestPostAsync("timelines", new HttpTimelineCreateRequest { Name = "t" });
{
var t = await client.TestGetAsync<HttpTimeline>("timelines/t");
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs
index b219f092..9848564f 100644
--- a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs
+++ b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs
@@ -78,6 +78,11 @@ namespace Timeline.Tests.IntegratedTests
return await client.TestJsonSendAsync<T>(HttpMethod.Put, url, jsonBody, expectedStatusCode: expectedStatusCode);
}
+ public static async Task TestPatchAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
+ {
+ await client.TestJsonSendAsync(HttpMethod.Patch, url, jsonBody, expectedStatusCode: expectedStatusCode);
+ }
+
public static async Task<T> TestPatchAsync<T>(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
{
return await client.TestJsonSendAsync<T>(HttpMethod.Patch, url, jsonBody, expectedStatusCode: expectedStatusCode);
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/SearchTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/SearchTest.cs
new file mode 100644
index 00000000..f96acfea
--- /dev/null
+++ b/BackEnd/Timeline.Tests/IntegratedTests/SearchTest.cs
@@ -0,0 +1,63 @@
+using FluentAssertions;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Timeline.Models.Http;
+using Xunit;
+
+namespace Timeline.Tests.IntegratedTests
+{
+ public class SearchTest : IntegratedTestBase
+ {
+ [Fact]
+ public async Task TimelineSearch_Should_Work()
+ {
+ var client = await CreateClientAsUser();
+
+ {
+ await client.TestPostAsync("timelines", new HttpTimelineCreateRequest { Name = "hahaha" });
+ await client.TestPostAsync("timelines", new HttpTimelineCreateRequest { Name = "bababa" });
+ await client.TestPatchAsync("timelines/bababa", new HttpTimelinePatchRequest { Title = "hahaha" });
+ await client.TestPostAsync("timelines", new HttpTimelineCreateRequest { Name = "gagaga" });
+ }
+
+ {
+ var res = await client.TestGetAsync<List<HttpTimeline>>("search/timelines?q=hah");
+ res.Should().HaveCount(2);
+ res[0].Name.Should().Be("hahaha");
+ res[1].Name.Should().Be("bababa");
+ }
+
+ {
+ var res = await client.TestGetAsync<List<HttpTimeline>>("search/timelines?q=wuhu");
+ res.Should().BeEmpty();
+ }
+ }
+
+ [Fact]
+ public async Task UserSearch_Should_Work()
+ {
+ var client = await CreateClientAsAdministrator();
+
+ {
+ await client.TestPostAsync("userop/createuser", new HttpCreateUserRequest { Username = "hahaha", Password = "p" });
+ await client.TestPostAsync("userop/createuser", new HttpCreateUserRequest { Username = "bababa", Password = "p" });
+ await client.TestPatchAsync("users/bababa", new HttpUserPatchRequest { Nickname = "hahaha" });
+ await client.TestPostAsync("userop/createuser", new HttpCreateUserRequest { Username = "gagaga", Password = "p" });
+ }
+
+ {
+ var res = await client.TestGetAsync<List<HttpUser>>("search/users?q=hah");
+ res.Should().HaveCount(2);
+ res[0].Username.Should().Be("hahaha");
+ res[1].Username.Should().Be("bababa");
+ }
+
+ {
+ var res = await client.TestGetAsync<List<HttpUser>>("search/users?q=wuhu");
+ res.Should().BeEmpty();
+ }
+ }
+ }
+}
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs
index 66261b36..4247e572 100644
--- a/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs
+++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs
@@ -179,20 +179,20 @@ namespace Timeline.Tests.IntegratedTests
{
{
using var client = await CreateDefaultClient();
- await client.TestPostAssertUnauthorizedAsync("timelines", new TimelineCreateRequest { Name = "aaa" });
+ await client.TestPostAssertUnauthorizedAsync("timelines", new HttpTimelineCreateRequest { Name = "aaa" });
}
{
using var client = await CreateClientAsUser();
- await client.TestPostAssertInvalidModelAsync("timelines", new TimelineCreateRequest { Name = "!!!" });
+ await client.TestPostAssertInvalidModelAsync("timelines", new HttpTimelineCreateRequest { Name = "!!!" });
{
- var body = await client.TestPostAsync<HttpTimeline>("timelines", new TimelineCreateRequest { Name = "aaa" });
+ var body = await client.TestPostAsync<HttpTimeline>("timelines", new HttpTimelineCreateRequest { Name = "aaa" });
body.Should().BeEquivalentTo(await client.GetTimelineAsync("aaa"));
}
- await client.TestPostAssertErrorAsync("timelines", new TimelineCreateRequest { Name = "aaa" }, errorCode: ErrorCodes.TimelineController.NameConflict);
+ await client.TestPostAssertErrorAsync("timelines", new HttpTimelineCreateRequest { Name = "aaa" }, errorCode: ErrorCodes.TimelineController.NameConflict);
}
}
diff --git a/BackEnd/Timeline.Tests/Services/DatabaseBasedTest.cs b/BackEnd/Timeline.Tests/Services/DatabaseBasedTest.cs
deleted file mode 100644
index 90fb6463..00000000
--- a/BackEnd/Timeline.Tests/Services/DatabaseBasedTest.cs
+++ /dev/null
@@ -1,55 +0,0 @@
-using System.Threading.Tasks;
-using Timeline.Entities;
-using Timeline.Tests.Helpers;
-using Xunit;
-using Xunit.Abstractions;
-
-namespace Timeline.Tests.Services
-{
- public abstract class DatabaseBasedTest : IAsyncLifetime
- {
- protected TestDatabase TestDatabase { get; }
- protected DatabaseContext Database { get; private set; } = default!;
-
- private readonly ITestOutputHelper? _testOutputHelper;
-
- protected DatabaseBasedTest(bool databaseCreateUsers = true, ITestOutputHelper? testOutputHelper = null)
- {
- _testOutputHelper = testOutputHelper;
- TestDatabase = new TestDatabase(databaseCreateUsers);
- }
-
- protected DatabaseBasedTest(ITestOutputHelper? testOutputHelper) : this(true, testOutputHelper) { }
-
- public async Task InitializeAsync()
- {
- await TestDatabase.InitializeAsync();
- Database = TestDatabase.CreateContext(_testOutputHelper);
- await OnDatabaseCreatedAsync();
- OnDatabaseCreated();
- }
-
- public async Task DisposeAsync()
- {
- BeforeDatabaseDestroy();
- await BeforeDatabaseDestroyAsync();
- await Database.DisposeAsync();
- await TestDatabase.DisposeAsync();
- }
-
-
- protected virtual void OnDatabaseCreated() { }
- protected virtual void BeforeDatabaseDestroy() { }
-
-
- protected virtual Task OnDatabaseCreatedAsync()
- {
- return Task.CompletedTask;
- }
-
- protected virtual Task BeforeDatabaseDestroyAsync()
- {
- return Task.CompletedTask;
- }
- }
-}
diff --git a/BackEnd/Timeline.Tests/Services/SearchServiceTest.cs b/BackEnd/Timeline.Tests/Services/SearchServiceTest.cs
new file mode 100644
index 00000000..968352c0
--- /dev/null
+++ b/BackEnd/Timeline.Tests/Services/SearchServiceTest.cs
@@ -0,0 +1,49 @@
+using FluentAssertions;
+using System.Threading.Tasks;
+using Timeline.Services;
+using Xunit;
+
+namespace Timeline.Tests.Services
+{
+ public class SearchServiceTest : ServiceTestBase
+ {
+ private SearchService _service = default!;
+
+ protected override void OnInitialize()
+ {
+ _service = new SearchService(Database);
+ }
+
+ [Fact]
+ public async Task TimelineSearch_Should_Work()
+ {
+ await TimelineService.CreateTimeline("hahaha", UserId);
+ var t2 = await TimelineService.CreateTimeline("bababa", UserId);
+ await TimelineService.ChangeProperty(t2.Id, new TimelineChangePropertyParams { Title = "hahaha" });
+ await TimelineService.CreateTimeline("bbbbbb", UserId);
+
+ var searchResult = await _service.SearchTimeline("hah");
+ searchResult.Items.Should().HaveCount(2);
+ searchResult.Items[0].Item.Name.Should().Be("hahaha");
+ searchResult.Items[0].Score.Should().Be(2);
+ searchResult.Items[1].Item.Name.Should().Be("bababa");
+ searchResult.Items[1].Score.Should().Be(1);
+ }
+
+ [Fact]
+ public async Task UserSearch_Should_Work()
+ {
+ await UserService.CreateUser("hahaha", "p");
+ var u2 = await UserService.CreateUser("bababa", "p");
+ await UserService.ModifyUser(u2.Id, new ModifyUserParams { Nickname = "hahaha" });
+ await UserService.CreateUser("bbbbbb", "p");
+
+ var searchResult = await _service.SearchUser("hah");
+ searchResult.Items.Should().HaveCount(2);
+ searchResult.Items[0].Item.Username.Should().Be("hahaha");
+ searchResult.Items[0].Score.Should().Be(2);
+ searchResult.Items[1].Item.Username.Should().Be("bababa");
+ searchResult.Items[1].Score.Should().Be(1);
+ }
+ }
+}
diff --git a/BackEnd/Timeline.Tests/Services/ServiceTestBase.cs b/BackEnd/Timeline.Tests/Services/ServiceTestBase.cs
new file mode 100644
index 00000000..5a3e1e19
--- /dev/null
+++ b/BackEnd/Timeline.Tests/Services/ServiceTestBase.cs
@@ -0,0 +1,71 @@
+using Microsoft.Extensions.Logging.Abstractions;
+using System.Threading.Tasks;
+using Timeline.Entities;
+using Timeline.Services;
+using Timeline.Tests.Helpers;
+using Xunit;
+using Xunit.Abstractions;
+
+namespace Timeline.Tests.Services
+{
+ public abstract class ServiceTestBase : IAsyncLifetime
+ {
+ protected TestDatabase TestDatabase { get; }
+ protected DatabaseContext Database { get; private set; } = default!;
+
+ private readonly ITestOutputHelper? _testOutputHelper;
+
+ protected TestClock Clock { get; } = new TestClock();
+ protected UserService UserService { get; private set; } = default!;
+ protected TimelineService TimelineService { get; private set; } = default!;
+
+ protected long UserId { get; private set; }
+ protected long AdminId { get; private set; }
+
+ protected ServiceTestBase(bool databaseCreateUsers = true, ITestOutputHelper? testOutputHelper = null)
+ {
+ _testOutputHelper = testOutputHelper;
+ TestDatabase = new TestDatabase(databaseCreateUsers);
+ }
+
+ protected ServiceTestBase(ITestOutputHelper? testOutputHelper) : this(true, testOutputHelper) { }
+
+ public async Task InitializeAsync()
+ {
+ await TestDatabase.InitializeAsync();
+ Database = TestDatabase.CreateContext(_testOutputHelper);
+
+ UserService = new UserService(NullLogger<UserService>.Instance, Database, new PasswordService(), Clock);
+ TimelineService = new TimelineService(Database, UserService, Clock);
+
+ UserId = await UserService.GetUserIdByUsername("user");
+ AdminId = await UserService.GetUserIdByUsername("admin");
+
+ await OnInitializeAsync();
+ OnInitialize();
+ }
+
+ public async Task DisposeAsync()
+ {
+ OnDispose();
+ await OnDisposeAsync();
+ await Database.DisposeAsync();
+ await TestDatabase.DisposeAsync();
+ }
+
+
+ protected virtual void OnInitialize() { }
+ protected virtual void OnDispose() { }
+
+
+ protected virtual Task OnInitializeAsync()
+ {
+ return Task.CompletedTask;
+ }
+
+ protected virtual Task OnDisposeAsync()
+ {
+ return Task.CompletedTask;
+ }
+ }
+}
diff --git a/BackEnd/Timeline.Tests/Services/UserDeleteServiceTest.cs b/BackEnd/Timeline.Tests/Services/UserDeleteServiceTest.cs
index 59c0a9af..10014d2b 100644
--- a/BackEnd/Timeline.Tests/Services/UserDeleteServiceTest.cs
+++ b/BackEnd/Timeline.Tests/Services/UserDeleteServiceTest.cs
@@ -8,12 +8,12 @@ using Xunit;
namespace Timeline.Tests.Services
{
- public class UserDeleteServiceTest : DatabaseBasedTest
+ public class UserDeleteServiceTest : ServiceTestBase
{
private readonly Mock<ITimelinePostService> _mockTimelinePostService = new Mock<ITimelinePostService>();
private UserDeleteService _service = default!;
- protected override void OnDatabaseCreated()
+ protected override void OnInitialize()
{
_service = new UserDeleteService(NullLogger<UserDeleteService>.Instance, Database, _mockTimelinePostService.Object);
}
diff --git a/BackEnd/Timeline.Tests/Services/UserPermissionServiceTest.cs b/BackEnd/Timeline.Tests/Services/UserPermissionServiceTest.cs
index f20a7d62..0c43c025 100644
--- a/BackEnd/Timeline.Tests/Services/UserPermissionServiceTest.cs
+++ b/BackEnd/Timeline.Tests/Services/UserPermissionServiceTest.cs
@@ -7,7 +7,7 @@ using Xunit;
namespace Timeline.Tests.Services
{
- public class UserPermissionServiceTest : DatabaseBasedTest
+ public class UserPermissionServiceTest : ServiceTestBase
{
private UserPermissionService _service = default!;
@@ -16,7 +16,7 @@ namespace Timeline.Tests.Services
}
- protected override void OnDatabaseCreated()
+ protected override void OnInitialize()
{
_service = new UserPermissionService(Database);
}