Add some service unit tests; add base for Fluxor tests
This commit is contained in:
parent
4e2b4d3806
commit
96553c3e2b
9 changed files with 289 additions and 23 deletions
117
Start_Tests/Server/BookmarkServiceTests.cs
Normal file
117
Start_Tests/Server/BookmarkServiceTests.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
93
Start_Tests/Server/UnitTestWithDb.cs
Normal file
93
Start_Tests/Server/UnitTestWithDb.cs
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
using System;
|
||||
using IdentityServer4.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue