Add a unit test project and set up a test in memory DB
This commit is contained in:
		
							parent
							
								
									90adbcfb7c
								
							
						
					
					
						commit
						4e2b4d3806
					
				| 
						 | 
					@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
 | 
				
			||||||
		.gitignore = .gitignore
 | 
							.gitignore = .gitignore
 | 
				
			||||||
	EndProjectSection
 | 
						EndProjectSection
 | 
				
			||||||
EndProject
 | 
					EndProject
 | 
				
			||||||
 | 
					Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Start_Tests", "Start_Tests\Start_Tests.csproj", "{74F33155-6FE2-409B-A5AC-52BC7433CE65}"
 | 
				
			||||||
 | 
					EndProject
 | 
				
			||||||
Global
 | 
					Global
 | 
				
			||||||
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 | 
						GlobalSection(SolutionConfigurationPlatforms) = preSolution
 | 
				
			||||||
		Debug|Any CPU = Debug|Any CPU
 | 
							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}.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.ActiveCfg = Release|Any CPU
 | 
				
			||||||
		{5F06E094-DDD9-4093-9979-9D9FFED2310F}.Release|Any CPU.Build.0 = 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
 | 
						EndGlobalSection
 | 
				
			||||||
	GlobalSection(SolutionProperties) = preSolution
 | 
						GlobalSection(SolutionProperties) = preSolution
 | 
				
			||||||
		HideSolutionNode = FALSE
 | 
							HideSolutionNode = FALSE
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										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…
	
		Reference in a new issue