Initial commit

This commit is contained in:
Mark J Price 2022-09-20 09:02:38 +01:00
parent 445d54a047
commit 1da926d605
10 changed files with 159 additions and 6 deletions

View file

@ -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 <project>\bin\<Debug|Release>\net7.0 directory.
path = Path.Combine("..", "..", "..", "..", "Northwind.db");
}
else
{
// Running in the <project> directory.
path = Path.Combine("..", "Northwind.db");
}
optionsBuilder.UseSqlite($"Filename={path}");
}
}

View file

@ -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);
}
}
}
}

View file

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<!-- change Sqlite to SqlServer if you prefer -->
<ProjectReference Include="..\Northwind.Common.DataContext.Sqlite\Northwind.Common.DataContext.Sqlite.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1 @@
global using Xunit;

View file

@ -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

View file

@ -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 <project>\bin\<Debug|Release>\net7.0 directory.
path = Path.Combine("..", "..", "..", "..", "Northwind.db");
}
else
{
// Running in the <project> directory.
path = Path.Combine("..", "Northwind.db");
}
optionsBuilder.UseSqlite($"Filename={path}");
}
}

View file

@ -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);
}
}
}
}

View file

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<!-- change Sqlite to SqlServer if you prefer -->
<ProjectReference Include="..\Northwind.Common.DataContext.Sqlite\Northwind.Common.DataContext.Sqlite.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1 @@
global using Xunit;

View file

@ -12,6 +12,9 @@
{
"path": "Northwind.Common.DataContext.SqlServer"
},
{
"path": "Northwind.Common.UnitTests"
},
{
"path": "Northwind.Web"
},