From 1da926d605335c259a81eed276be477d5e91a8fd Mon Sep 17 00:00:00 2001 From: Mark J Price Date: Tue, 20 Sep 2022 09:02:38 +0100 Subject: [PATCH] Initial commit --- .../NorthwindContext.cs | 16 +++++++++- .../EntityModelTests.cs | 28 ++++++++++++++++++ .../Northwind.Common.UnitTests.csproj | 29 +++++++++++++++++++ .../Northwind.Common.UnitTests/Usings.cs | 1 + vs4win/PracticalApps/PracticalApps.sln | 14 ++++++--- .../NorthwindContext.cs | 16 +++++++++- .../EntityModelTests.cs | 28 ++++++++++++++++++ .../Northwind.Common.UnitTests.csproj | 29 +++++++++++++++++++ .../Northwind.Common.UnitTests/Usings.cs | 1 + .../PracticalApps.code-workspace | 3 ++ 10 files changed, 159 insertions(+), 6 deletions(-) create mode 100644 vs4win/PracticalApps/Northwind.Common.UnitTests/EntityModelTests.cs create mode 100644 vs4win/PracticalApps/Northwind.Common.UnitTests/Northwind.Common.UnitTests.csproj create mode 100644 vs4win/PracticalApps/Northwind.Common.UnitTests/Usings.cs create mode 100644 vscode/PracticalApps/Northwind.Common.UnitTests/EntityModelTests.cs create mode 100644 vscode/PracticalApps/Northwind.Common.UnitTests/Northwind.Common.UnitTests.csproj create mode 100644 vscode/PracticalApps/Northwind.Common.UnitTests/Usings.cs diff --git a/vs4win/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContext.cs b/vs4win/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContext.cs index ace1b59..408ec3d 100644 --- a/vs4win/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContext.cs +++ b/vs4win/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContext.cs @@ -31,7 +31,21 @@ namespace Packt.Shared { if (!optionsBuilder.IsConfigured) { - optionsBuilder.UseSqlite("Filename=../Northwind.db"); + string dir = Environment.CurrentDirectory; + string path = string.Empty; + + if (dir.EndsWith("net7.0")) + { + // Running in the \bin\\net7.0 directory. + path = Path.Combine("..", "..", "..", "..", "Northwind.db"); + } + else + { + // Running in the directory. + path = Path.Combine("..", "Northwind.db"); + } + + optionsBuilder.UseSqlite($"Filename={path}"); } } diff --git a/vs4win/PracticalApps/Northwind.Common.UnitTests/EntityModelTests.cs b/vs4win/PracticalApps/Northwind.Common.UnitTests/EntityModelTests.cs new file mode 100644 index 0000000..db22ab7 --- /dev/null +++ b/vs4win/PracticalApps/Northwind.Common.UnitTests/EntityModelTests.cs @@ -0,0 +1,28 @@ +using Packt.Shared; // NorthwindContext + +namespace Northwind.Common.UnitTests +{ + public class EntityModelTests + { + [Fact] + public void DatabaseConnectTest() + { + using (NorthwindContext db = new()) + { + Assert.True(db.Database.CanConnect()); + } + } + + [Fact] + public void CategoryCountTest() + { + using (NorthwindContext db = new()) + { + int expected = 8; + int actual = db.Categories.Count(); + + Assert.Equal(expected, actual); + } + } + } +} \ No newline at end of file diff --git a/vs4win/PracticalApps/Northwind.Common.UnitTests/Northwind.Common.UnitTests.csproj b/vs4win/PracticalApps/Northwind.Common.UnitTests/Northwind.Common.UnitTests.csproj new file mode 100644 index 0000000..ba2eb5a --- /dev/null +++ b/vs4win/PracticalApps/Northwind.Common.UnitTests/Northwind.Common.UnitTests.csproj @@ -0,0 +1,29 @@ + + + + net7.0 + enable + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + diff --git a/vs4win/PracticalApps/Northwind.Common.UnitTests/Usings.cs b/vs4win/PracticalApps/Northwind.Common.UnitTests/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/vs4win/PracticalApps/Northwind.Common.UnitTests/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/vs4win/PracticalApps/PracticalApps.sln b/vs4win/PracticalApps/PracticalApps.sln index 4459a58..a2d47e9 100644 --- a/vs4win/PracticalApps/PracticalApps.sln +++ b/vs4win/PracticalApps/PracticalApps.sln @@ -23,13 +23,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Northwind.WebApi", "Northwi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Minimal.WebApi", "Minimal.WebApi\Minimal.WebApi.csproj", "{36CEABAE-DE5F-4784-B4FB-6C281B322ECD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Northwind.BlazorServer", "Northwind.BlazorServer\Northwind.BlazorServer.csproj", "{142A4264-F385-4982-906C-70C6C8C3EDBA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Northwind.BlazorServer", "Northwind.BlazorServer\Northwind.BlazorServer.csproj", "{142A4264-F385-4982-906C-70C6C8C3EDBA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Northwind.BlazorWasm.Server", "Northwind.BlazorWasm\Server\Northwind.BlazorWasm.Server.csproj", "{8CDA76AC-F6CE-4F76-81C7-5F92EE5BCCAB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Northwind.BlazorWasm.Server", "Northwind.BlazorWasm\Server\Northwind.BlazorWasm.Server.csproj", "{8CDA76AC-F6CE-4F76-81C7-5F92EE5BCCAB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Northwind.BlazorWasm.Client", "Northwind.BlazorWasm\Client\Northwind.BlazorWasm.Client.csproj", "{00A2DE18-4C31-4338-A387-0AB5BC5E378D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Northwind.BlazorWasm.Client", "Northwind.BlazorWasm\Client\Northwind.BlazorWasm.Client.csproj", "{00A2DE18-4C31-4338-A387-0AB5BC5E378D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Northwind.BlazorWasm.Shared", "Northwind.BlazorWasm\Shared\Northwind.BlazorWasm.Shared.csproj", "{5803C108-3748-46EB-9940-85AE15F699E2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Northwind.BlazorWasm.Shared", "Northwind.BlazorWasm\Shared\Northwind.BlazorWasm.Shared.csproj", "{5803C108-3748-46EB-9940-85AE15F699E2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Northwind.Common.UnitTests", "Northwind.Common.UnitTests\Northwind.Common.UnitTests.csproj", "{691FCF8F-191C-47E9-9BCD-58BF412FE09D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -89,6 +91,10 @@ Global {5803C108-3748-46EB-9940-85AE15F699E2}.Debug|Any CPU.Build.0 = Debug|Any CPU {5803C108-3748-46EB-9940-85AE15F699E2}.Release|Any CPU.ActiveCfg = Release|Any CPU {5803C108-3748-46EB-9940-85AE15F699E2}.Release|Any CPU.Build.0 = Release|Any CPU + {691FCF8F-191C-47E9-9BCD-58BF412FE09D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {691FCF8F-191C-47E9-9BCD-58BF412FE09D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {691FCF8F-191C-47E9-9BCD-58BF412FE09D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {691FCF8F-191C-47E9-9BCD-58BF412FE09D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/vscode/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContext.cs b/vscode/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContext.cs index ace1b59..408ec3d 100644 --- a/vscode/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContext.cs +++ b/vscode/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContext.cs @@ -31,7 +31,21 @@ namespace Packt.Shared { if (!optionsBuilder.IsConfigured) { - optionsBuilder.UseSqlite("Filename=../Northwind.db"); + string dir = Environment.CurrentDirectory; + string path = string.Empty; + + if (dir.EndsWith("net7.0")) + { + // Running in the \bin\\net7.0 directory. + path = Path.Combine("..", "..", "..", "..", "Northwind.db"); + } + else + { + // Running in the directory. + path = Path.Combine("..", "Northwind.db"); + } + + optionsBuilder.UseSqlite($"Filename={path}"); } } diff --git a/vscode/PracticalApps/Northwind.Common.UnitTests/EntityModelTests.cs b/vscode/PracticalApps/Northwind.Common.UnitTests/EntityModelTests.cs new file mode 100644 index 0000000..db22ab7 --- /dev/null +++ b/vscode/PracticalApps/Northwind.Common.UnitTests/EntityModelTests.cs @@ -0,0 +1,28 @@ +using Packt.Shared; // NorthwindContext + +namespace Northwind.Common.UnitTests +{ + public class EntityModelTests + { + [Fact] + public void DatabaseConnectTest() + { + using (NorthwindContext db = new()) + { + Assert.True(db.Database.CanConnect()); + } + } + + [Fact] + public void CategoryCountTest() + { + using (NorthwindContext db = new()) + { + int expected = 8; + int actual = db.Categories.Count(); + + Assert.Equal(expected, actual); + } + } + } +} \ No newline at end of file diff --git a/vscode/PracticalApps/Northwind.Common.UnitTests/Northwind.Common.UnitTests.csproj b/vscode/PracticalApps/Northwind.Common.UnitTests/Northwind.Common.UnitTests.csproj new file mode 100644 index 0000000..ba2eb5a --- /dev/null +++ b/vscode/PracticalApps/Northwind.Common.UnitTests/Northwind.Common.UnitTests.csproj @@ -0,0 +1,29 @@ + + + + net7.0 + enable + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + diff --git a/vscode/PracticalApps/Northwind.Common.UnitTests/Usings.cs b/vscode/PracticalApps/Northwind.Common.UnitTests/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/vscode/PracticalApps/Northwind.Common.UnitTests/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/vscode/PracticalApps/PracticalApps.code-workspace b/vscode/PracticalApps/PracticalApps.code-workspace index d084673..b7a3e17 100644 --- a/vscode/PracticalApps/PracticalApps.code-workspace +++ b/vscode/PracticalApps/PracticalApps.code-workspace @@ -12,6 +12,9 @@ { "path": "Northwind.Common.DataContext.SqlServer" }, + { + "path": "Northwind.Common.UnitTests" + }, { "path": "Northwind.Web" },