Rename and move the tests project to be consistent with the other projects

This commit is contained in:
Neil Brommer 2022-05-03 13:55:07 -07:00
parent 83cc477170
commit 10283a2053
9 changed files with 3 additions and 3 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.Dispatcher.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,39 @@
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 IDispatcher Dispatcher { 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.Dispatcher = this.BunitTc.Services.GetRequiredService<IDispatcher>();
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

@ -0,0 +1,93 @@
using System;
using Duende.IdentityServer.EntityFramework.Options;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Start.Server.Data;
using Start.Server.Models;
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() {
_connection = new SqliteConnection(InMemoryConnectionString);
_connection.Open();
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseSqlite(_connection)
.Options;
this._db = new ApplicationDbContext(options,
Options.Create(new OperationalStoreOptions()));
this._db.Database.EnsureCreated();
}
protected void ResetDb() {
_db.Database.EnsureDeleted();
_db.Database.EnsureCreated();
}
protected void FillDbTestData() {
ApplicationUser testUser = new ApplicationUser {
Id = this.TestUserId,
UserName = "test_user_name"
};
_db.Users.Add(testUser);
_db.SaveChanges();
BookmarkContainer testContainer = new BookmarkContainer(testUser.Id, "Test Container",
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>
/// Checks the DB connection works. Note that MSTest won't run this - you need to do so in
/// inheriting classes like this:
///
/// <code>
/// [TestMethod]
/// public override void TestDatabaseOK() {
/// base.TestDatabaseOK();
/// }
/// </code>
/// </summary>
[TestMethod]
public virtual void TestDatabaseOK() {
Assert.IsTrue(this._db.Database.CanConnect());
}
public void Dispose() {
_connection.Close();
}
}
}

View file

@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Fluxor" Version="5.3.0" />
<PackageReference Include="Blazored.LocalStorage" Version="4.2.0" />
<PackageReference Include="bunit" Version="1.7.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Server\Start.Server.csproj" />
<ProjectReference Include="..\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>