Add a unit test project and set up a test in memory DB
This commit is contained in:
parent
90adbcfb7c
commit
4e2b4d3806
4 changed files with 125 additions and 0 deletions
20
Start_Tests/Start_Tests.csproj
Normal file
20
Start_Tests/Start_Tests.csproj
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Start\Server\Start.Server.csproj" />
|
||||
<ProjectReference Include="..\Start\Shared\Start.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
20
Start_Tests/UnitTest1.cs
Normal file
20
Start_Tests/UnitTest1.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
79
Start_Tests/UnitTestWithDb.cs
Normal file
79
Start_Tests/UnitTestWithDb.cs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
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 {
|
||||
public class UnitTestWithDb : IDisposable {
|
||||
private const string InMemoryConnectionString = "DataSource=:memory:";
|
||||
private SqliteConnection _connection;
|
||||
|
||||
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 = "test_user",
|
||||
UserName = "test_user"
|
||||
};
|
||||
_db.Users.Add(testUser);
|
||||
_db.SaveChanges();
|
||||
|
||||
BookmarkContainer testContainer = new BookmarkContainer(testUser.Id, "Test Container",
|
||||
0);
|
||||
_db.BookmarkContainers.Add(testContainer);
|
||||
_db.SaveChanges();
|
||||
|
||||
BookmarkGroup testGroup = new BookmarkGroup("Test Group", "#000000", 0,
|
||||
testContainer.BookmarkContainerId);
|
||||
_db.BookmarkGroups.Add(testGroup);
|
||||
_db.SaveChanges();
|
||||
|
||||
Bookmark testBookmark = new Bookmark("Test Bookmark", "http://example.com",
|
||||
"Test Notes", 0, testGroup.BookmarkGroupId);
|
||||
_db.Bookmarks.Add(testBookmark);
|
||||
_db.SaveChanges();
|
||||
}
|
||||
|
||||
/// <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