diff --git a/Start.sln b/Start.sln index 01bb304..cc25f89 100644 --- a/Start.sln +++ b/Start.sln @@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .gitignore = .gitignore EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Start_Tests", "Start_Tests\Start_Tests.csproj", "{74F33155-6FE2-409B-A5AC-52BC7433CE65}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -34,6 +36,10 @@ Global {5F06E094-DDD9-4093-9979-9D9FFED2310F}.Debug|Any CPU.Build.0 = Debug|Any CPU {5F06E094-DDD9-4093-9979-9D9FFED2310F}.Release|Any CPU.ActiveCfg = Release|Any CPU {5F06E094-DDD9-4093-9979-9D9FFED2310F}.Release|Any CPU.Build.0 = Release|Any CPU + {74F33155-6FE2-409B-A5AC-52BC7433CE65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {74F33155-6FE2-409B-A5AC-52BC7433CE65}.Debug|Any CPU.Build.0 = Debug|Any CPU + {74F33155-6FE2-409B-A5AC-52BC7433CE65}.Release|Any CPU.ActiveCfg = Release|Any CPU + {74F33155-6FE2-409B-A5AC-52BC7433CE65}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Start_Tests/Start_Tests.csproj b/Start_Tests/Start_Tests.csproj new file mode 100644 index 0000000..1223883 --- /dev/null +++ b/Start_Tests/Start_Tests.csproj @@ -0,0 +1,20 @@ + + + + net5.0 + + false + + + + + + + + + + + + + + diff --git a/Start_Tests/UnitTest1.cs b/Start_Tests/UnitTest1.cs new file mode 100644 index 0000000..73d56f9 --- /dev/null +++ b/Start_Tests/UnitTest1.cs @@ -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"); + } + } +} diff --git a/Start_Tests/UnitTestWithDb.cs b/Start_Tests/UnitTestWithDb.cs new file mode 100644 index 0000000..1c8e318 --- /dev/null +++ b/Start_Tests/UnitTestWithDb.cs @@ -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() + .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(); + } + + /// + /// Checks the DB connection works. Note that MSTest won't run this - you need to do so in + /// inheriting classes like this: + /// + /// + /// [TestMethod] + /// public override void TestDatabaseOK() { + /// base.TestDatabaseOK(); + /// } + /// + /// + [TestMethod] + public virtual void TestDatabaseOK() { + Assert.IsTrue(this._db.Database.CanConnect()); + } + + public void Dispose() { + _connection.Close(); + } + } +}