Add some service unit tests; add base for Fluxor tests

This commit is contained in:
Neil Brommer 2022-04-20 15:34:13 -07:00
parent 4e2b4d3806
commit 96553c3e2b
9 changed files with 289 additions and 23 deletions

View file

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Refit;
using Start.Shared;
using Start.Shared.Api;
namespace Start_Tests.Client.MockApis {
public class MockBookmarkContainersApi : IBookmarkContainersApi {
public Task<ApiResponse<IEnumerable<BookmarkContainerDto>>> GetAllBookmarkContainers() {
throw new NotImplementedException();
}
public Task<ApiResponse<BookmarkContainerDto>> GetBookmarkContainer(
int bookmarkContainerId) {
throw new NotImplementedException();
}
public Task<ApiResponse<BookmarkContainerDto>> CreateBookmarkContainer(string title,
int sortOrder) {
throw new NotImplementedException();
}
public Task<HttpResponseMessage> DeleteBookmarkContainer(int bookmarkContainerId) {
throw new NotImplementedException();
}
}
}

View file

@ -0,0 +1,23 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Refit;
using Start.Shared;
using Start.Shared.Api;
namespace Start_Tests.Client.MockApis {
public class MockBookmarkGroupsApi : IBookmarkGroupsApi {
public Task<ApiResponse<BookmarkGroupDto>> GetBookmarkGroup(int bookmarkGroupId) {
throw new NotImplementedException();
}
public Task<ApiResponse<BookmarkGroupDto>> CreateBookmarkGroup(string title, string color,
int sortOrder, int bookmarkContainerId) {
throw new NotImplementedException();
}
public Task<HttpResponseMessage> DeleteBookmarkGroup(int bookmarkGroupId) {
throw new NotImplementedException();
}
}
}

View file

@ -0,0 +1,23 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Refit;
using Start.Shared;
using Start.Shared.Api;
namespace Start_Tests.Client.MockApis {
public class MockBookmarksApi : IBookmarksApi {
public Task<ApiResponse<BookmarkDto>> GetBookmark(int bookmarkId) {
throw new NotImplementedException();
}
public Task<ApiResponse<BookmarkDto>> CreateBookmark(string title, string url,
string notes, int sortOrder, int bookmarkGroupId) {
throw new NotImplementedException();
}
public Task<HttpResponseMessage> DeleteBookmark(int bookmarkId) {
throw new NotImplementedException();
}
}
}

View file

@ -0,0 +1,25 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Start.Client.Store.Features.ContainersList;
namespace Start_Tests.Client.Store {
[TestClass]
public class ContainerListTests : UnitTestWithFluxor {
public TestContext TestContext { get; set; }
// Only RootState is needed, so no need to get child objects
[TestMethod]
public void OnFetchContainersList() {
base.Store.Dispatch(new FetchContainerListAction());
Assert.IsTrue(base.State.Value.ContainerListState.IsLoadingContainersList);
Assert.AreEqual(0, this.State.Value.ContainerListState.Containers.Count);
}
[TestInitialize]
public void InitializeState() {
TestContext.WriteLine("Resetting Fluxor state");
base.ResetState();
}
}
}

View file

@ -0,0 +1,37 @@
using System;
using Blazored.LocalStorage;
using Fluxor;
using Microsoft.Extensions.DependencyInjection;
using Start.Client.Store.State;
using Start.Shared.Api;
using Start_Tests.Client.MockApis;
namespace Start_Tests.Client.Store {
public abstract class UnitTestWithFluxor {
protected IServiceProvider ServiceProvider { get; set; }
protected IStore Store { get; set; }
protected IState<RootState> State { get; set; }
// Add child states in the individual tests
protected Bunit.TestContext BunitTc { get; set; }
public UnitTestWithFluxor() {
this.ResetState();
}
public void ResetState() {
this.BunitTc = new Bunit.TestContext();
BunitTc.Services.AddBlazoredLocalStorage();
BunitTc.Services.AddFluxor(config => config.ScanAssemblies(typeof(RootState).Assembly));
BunitTc.Services.AddScoped<IBookmarksApi>(sp => new MockBookmarksApi());
BunitTc.Services.AddScoped<IBookmarkGroupsApi>(sp => new MockBookmarkGroupsApi());
BunitTc.Services
.AddScoped<IBookmarkContainersApi>(sp => new MockBookmarkContainersApi());
this.Store = this.BunitTc.Services.GetRequiredService<IStore>();
this.State = this.BunitTc.Services.GetRequiredService<IState<RootState>>();
this.Store.InitializeAsync().Wait();
}
}
}

View file

@ -0,0 +1,117 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Start.Server.Data.Services;
using Start.Server.Models;
namespace Start_Tests.Server {
[TestClass]
public class BookmarkServiceTests : UnitTestWithDb {
public TestContext TestContext { get; set; }
public BookmarkService BookmarkService { get; set; }
public BookmarkServiceTests() {
this.BookmarkService = new BookmarkService(_db);
}
[TestMethod]
public override void TestDatabaseOK() {
base.TestDatabaseOK();
}
#region CreateBookmark
[TestMethod]
public async Task CreateBookmark_Valid() {
int initialCount = _db.Bookmarks.Count();
await this.BookmarkService.CreateBookmark(base.TestUserId,
"Bookmark Service Test Title", "http://example.com", null, 1,
this.TestBookmarkGroup.BookmarkGroupId);
int updatedCount = _db.Bookmarks.Count();
Assert.AreEqual(initialCount + 1, updatedCount);
}
[TestMethod]
[ExpectedException(typeof(DbUpdateException))]
public async Task CreateBookmark_InvalidTitle() {
await this.BookmarkService.CreateBookmark(base.TestUserId,
null, "http://example.com", null, 1,
this.TestBookmarkGroup.BookmarkGroupId);
}
[TestMethod]
[ExpectedException(typeof(DbUpdateException))]
public async Task CreateBookmark_InvalidUrl() {
await this.BookmarkService.CreateBookmark(base.TestUserId,
"Bookmark Service Test Title", null, null, 1,
this.TestBookmarkGroup.BookmarkGroupId);
}
#endregion
#region GetBookmark
[TestMethod]
public async Task GetBookmark_CorrectUser() {
Bookmark bookmark = await this.BookmarkService
.GetBookmark(base.TestUserId,base.TestBookmark.BookmarkId);
Assert.IsNotNull(bookmark);
Assert.AreEqual(bookmark.BookmarkId, base.TestBookmark.BookmarkId);
Assert.AreEqual(bookmark.Url, base.TestBookmark.Url);
}
[TestMethod]
public async Task GetBookmark_WrongUser() {
Bookmark bookmark = await this.BookmarkService
.GetBookmark(base.InvalidUserId, base.TestBookmark.BookmarkId);
// Should return null if the user doesn't own the bookmark
Assert.IsNull(bookmark);
}
[TestMethod]
public async Task GetBookmark_WrongId() {
// Ensure that we use an invalid ID by going past the highest ID value
int maxBookmarkId = _db.Bookmarks.Max(b => b.BookmarkId);
Bookmark bookmark = await this.BookmarkService
.GetBookmark(base.TestUserId, maxBookmarkId + 1);
Assert.IsNull(bookmark);
}
#endregion
#region UpdateBookmark
[TestMethod]
public async Task UpdateBookmark_ValidTitle() {
string testTitleUpdate = "Update bookkmark test title";
base.TestBookmark.Title = testTitleUpdate;
Bookmark updatedBookmark = await this.BookmarkService
.UpdateBookmark(base.TestUserId, base.TestBookmark);
Assert.IsNotNull(updatedBookmark);
Assert.AreEqual(updatedBookmark.Title, testTitleUpdate);
Bookmark fromDb = _db.Bookmarks.Single(b => b.BookmarkId == TestBookmark.BookmarkId);
Assert.AreEqual(fromDb.Title, testTitleUpdate);
}
#endregion
[TestInitialize]
public void ResetDatabase() {
TestContext.WriteLine("Reseting test DB for the next test");
base.ResetAndFillDb();
}
}
}

View file

@ -7,11 +7,17 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
using Start.Server.Data;
using Start.Server.Models;
namespace Start_Tests {
namespace Start_Tests.Server {
public class UnitTestWithDb : IDisposable {
private const string InMemoryConnectionString = "DataSource=:memory:";
private SqliteConnection _connection;
protected string TestUserId { get; } = "test_user";
protected string InvalidUserId { get; } = "invalid_user";
protected BookmarkContainer TestBookmarkContainer { get; set; }
protected BookmarkGroup TestBookmarkGroup { get; set; }
protected Bookmark TestBookmark { get; set; }
protected readonly ApplicationDbContext _db;
public UnitTestWithDb() {
@ -34,8 +40,8 @@ namespace Start_Tests {
protected void FillDbTestData() {
ApplicationUser testUser = new ApplicationUser {
Id = "test_user",
UserName = "test_user"
Id = this.TestUserId,
UserName = "test_user_name"
};
_db.Users.Add(testUser);
_db.SaveChanges();
@ -44,16 +50,24 @@ namespace Start_Tests {
0);
_db.BookmarkContainers.Add(testContainer);
_db.SaveChanges();
this.TestBookmarkContainer = testContainer;
BookmarkGroup testGroup = new BookmarkGroup("Test Group", "#000000", 0,
testContainer.BookmarkContainerId);
_db.BookmarkGroups.Add(testGroup);
_db.SaveChanges();
this.TestBookmarkGroup = testGroup;
Bookmark testBookmark = new Bookmark("Test Bookmark", "http://example.com",
"Test Notes", 0, testGroup.BookmarkGroupId);
_db.Bookmarks.Add(testBookmark);
_db.SaveChanges();
this.TestBookmark = testBookmark;
}
protected void ResetAndFillDb() {
this.ResetDb();
this.FillDbTestData();
}
/// <summary>

View file

@ -11,10 +11,28 @@
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="coverlet.collector" Version="3.0.2" />
<PackageReference Include="Fluxor" Version="4.2.1" />
<PackageReference Include="Blazored.LocalStorage" Version="4.1.5" />
<PackageReference Include="bunit" Version="1.6.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Start\Server\Start.Server.csproj" />
<ProjectReference Include="..\Start\Shared\Start.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="Server\" />
<None Remove="Client\" />
<None Remove="Client\Store\" />
<None Remove="Fluxor" />
<None Remove="Blazored.LocalStorage" />
<None Remove="bunit" />
<None Remove="Client\MockServices\" />
</ItemGroup>
<ItemGroup>
<Folder Include="Server\" />
<Folder Include="Client\" />
<Folder Include="Client\Store\" />
<Folder Include="Client\MockApis\" />
</ItemGroup>
</Project>

View file

@ -1,20 +0,0 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Start.Server.Models;
namespace Start_Tests {
[TestClass]
public class UnitTest1 : UnitTestWithDb {
public TestContext TestContext { get; set; }
[TestMethod]
public override void TestDatabaseOK() {
base.TestDatabaseOK();
}
[TestMethod]
public void TestMethod1() {
TestContext.WriteLine("Running TestMethod1 from TestContext");
}
}
}