Skip to content

Commit

Permalink
Add the UnitTests.Databases.SqlServer.Dac project.
Browse files Browse the repository at this point in the history
  • Loading branch information
GillesTourreau committed Sep 26, 2024
1 parent e4d9835 commit 75398d0
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 77 deletions.
6 changes: 4 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[*]
[*.csproj]
indent_style = space
indent_size = 4
indent_size = 2

[*.cs]
indent_style = space
indent_size = 4

# StyleCop

Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/github-actions-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,12 @@ jobs:
--property:VersionSuffix=${{ github.event.inputs.VersionSuffix }}
"src/UnitTests.Databases.SqlServer.EntityFramework/UnitTests.Databases.SqlServer.EntityFramework.csproj"

- name: Build UnitTests.Databases.SqlServer.Dac
run: dotnet pack
--property:Configuration=Release
--property:VersionPrefix=${{ github.event.inputs.VersionPrefix }}
--property:VersionSuffix=${{ github.event.inputs.VersionSuffix }}
"src/UnitTests.Databases.SqlServer.Dac/UnitTests.Databases.SqlServer.Dac.csproj"

- name: Publish the package to nuget.org
run: dotnet nuget push "src/**/bin/Release/*.nupkg" --api-key "${{ secrets.NUGET_APIKEY }}" --source https://api.nuget.org/v3/index.json
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ItemGroup>
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.1.5" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
Expand Down
6 changes: 6 additions & 0 deletions PosInformatique.UnitTests.Databases.sln
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{8500A9B6-C
docs\WriteUnitTests.md = docs\WriteUnitTests.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests.Databases.SqlServer.Dac", "src\UnitTests.Databases.SqlServer.Dac\UnitTests.Databases.SqlServer.Dac.csproj", "{8BE60460-EBA5-43DE-B85D-C756E2988DC8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -94,6 +96,10 @@ Global
{04A7AE8F-FE77-435B-9250-600388BB8065}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04A7AE8F-FE77-435B-9250-600388BB8065}.Release|Any CPU.ActiveCfg = Release|Any CPU
{04A7AE8F-FE77-435B-9250-600388BB8065}.Release|Any CPU.Build.0 = Release|Any CPU
{8BE60460-EBA5-43DE-B85D-C756E2988DC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8BE60460-EBA5-43DE-B85D-C756E2988DC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BE60460-EBA5-43DE-B85D-C756E2988DC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BE60460-EBA5-43DE-B85D-C756E2988DC8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
38 changes: 25 additions & 13 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
<Project>

<!-- Imports the parent Directory.Build.props if exist -->
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
<!-- Imports the parent Directory.Build.props if exist -->
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />

<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<Nullable>enable</Nullable>

<PackageIcon>Icon.png</PackageIcon>
<PackageProjectUrl>https://github.com/PosInformatique/PosInformatique.UnitTests.Databases</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReleaseNotes>
1.0.1
- Fix the documentation

1.0.0
- Initial version
</PackageReleaseNotes>
</PropertyGroup>

<!-- Add the InternalsVisibleToAttribute for the unit tests -->
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>$(AssemblyName).Tests</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>DynamicProxyGenAssembly2</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>$(AssemblyName).Tests</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>DynamicProxyGenAssembly2</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>
Binary file added src/UnitTests.Databases.SqlServer.Dac/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//-----------------------------------------------------------------------
// <copyright file="SqlServerDacDatabaseInitializer.cs" company="P.O.S Informatique">
// Copyright (c) P.O.S Informatique. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace PosInformatique.UnitTests.Databases.SqlServer
{
using Microsoft.Data.SqlClient;

/// <summary>
/// Initializer used to initialize the database for the unit tests.
/// Call the <see cref="Initialize(SqlServerDatabaseInitializer, string, string)"/> method to initialize a database from
/// a DACPAC file.
/// </summary>
/// <remarks>The database will be created the call of the <see cref="Initialize(string, string)"/> method. For the next calls
/// the database is preserved but all the data are deleted.</remarks>
public static class SqlServerDacDatabaseInitializer
{
/// <summary>
/// Initialize a SQL Server database from a DACPAC file.
/// </summary>
/// <param name="initializer"><see cref="SqlServerDatabaseInitializer"/> which the initialization will be perform on.</param>
/// <param name="packageName">Full path of the DACPAC file.</param>
/// <param name="connectionString">Connection string to the SQL Server with administrator rights.</param>
/// <returns>An instance of the <see cref="SqlServerDatabase"/> which allows to perform query to initialize the data.</returns>
public static SqlServerDatabase Initialize(this SqlServerDatabaseInitializer initializer, string packageName, string connectionString)
{
var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);

var server = new SqlServer(connectionString);

SqlServerDatabase database;

if (!initializer.IsInitialized)
{
database = server.DeployDacPackage(packageName, connectionStringBuilder.InitialCatalog);

initializer.IsInitialized = true;
}
else
{
database = server.GetDatabase(connectionStringBuilder.InitialCatalog);
}

database.ClearAllData();

return database;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------
// <copyright file="SqlServerExtensions.cs" company="P.O.S Informatique">
// <copyright file="SqlServerDacExtensions.cs" company="P.O.S Informatique">
// Copyright (c) P.O.S Informatique. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
Expand All @@ -11,7 +11,7 @@ namespace PosInformatique.UnitTests.Databases.SqlServer
/// <summary>
/// Extensions methods of the <see cref="SqlServer"/> class.
/// </summary>
public static class SqlServerExtensions
public static class SqlServerDacExtensions
{
/// <summary>
/// Deploy a database using a DACPAC file.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

<Description>UnitTests.Databases.SqlServer.Dac is a library that contains a set of tools for unit testing to deploy DAC (Data-tier Applications) packages (.dacpac files).</Description>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<None Include="Icon.png" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SqlServer.DacFx" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\UnitTests.Databases.SqlServer\UnitTests.Databases.SqlServer.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
<GenerateDocumentationFile>True</GenerateDocumentationFile>

<Description>UnitTests.Databases.SqlServer.EntityFramework is a library that contains a set of tools for unit testing Data Access Layer (repositories) based on Entity Framework and SQL Server.</Description>
<PackageIcon>Icon.png</PackageIcon>
<PackageProjectUrl>https://github.com/PosInformatique/PosInformatique.UnitTests.Databases</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReleaseNotes>
1.0.1
- Fix the documentation

1.0.0
- Initial version
</PackageReleaseNotes>
<PackageTags>unittest entityframework sqlserver repository tdd dataaccesslayer</PackageTags>
</PropertyGroup>

Expand Down
39 changes: 3 additions & 36 deletions src/UnitTests.Databases.SqlServer/SqlServerDatabaseInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,17 @@

namespace PosInformatique.UnitTests.Databases.SqlServer
{
using Microsoft.Data.SqlClient;

/// <summary>
/// Initializer used to initialize the database for the unit tests.
/// Call the <see cref="Initialize(string, string)"/> method to initialize a database from
/// a DACPAC file.
/// Depending of the strategy to use (initialize from Entity Framework DbContext or .dacpac package)
/// add the <c>PosInformatique.UnitTests.Databases.SqlServer.EntityFramework</c> or <c>PosInformatique.UnitTests.Databases.SqlServer.Dac</c>
/// NuGet packages and call the <c>Initialize()</c> method.
/// </summary>
/// <remarks>The database will be created the call of the <see cref="Initialize(string, string)"/> method. For the next calls
/// the database is preserved but all the data are deleted.</remarks>
public class SqlServerDatabaseInitializer
{
/// <summary>
/// Gets or sets a value indicating whether if the database has been initialized.
/// </summary>
public bool IsInitialized { get; set; }

/// <summary>
/// Initialize a SQL Server database from a DACPAC file.
/// </summary>
/// <param name="packageName">Full path of the DACPAC file.</param>
/// <param name="connectionString">Connection string to the SQL Server with administrator rights.</param>
/// <returns>An instance of the <see cref="SqlServerDatabase"/> which allows to perform query to initialize the data.</returns>
public SqlServerDatabase Initialize(string packageName, string connectionString)
{
var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);

var server = new SqlServer(connectionString);

SqlServerDatabase database;

if (!this.IsInitialized)
{
database = server.DeployDacPackage(packageName, connectionStringBuilder.InitialCatalog);

this.IsInitialized = true;
}
else
{
database = server.GetDatabase(connectionStringBuilder.InitialCatalog);
}

database.ClearAllData();

return database;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
<GenerateDocumentationFile>True</GenerateDocumentationFile>

<Description>UnitTests.Databases.SqlServer is a library that contains a set of tools for unit testing Data Access Layer (repositories) based on SQL Server.</Description>
<PackageIcon>Icon.png</PackageIcon>
<PackageProjectUrl>https://github.com/PosInformatique/PosInformatique.UnitTests.Databases</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReleaseNotes>
1.0.1
- Fix the documentation

1.0.0
- Initial version
</PackageReleaseNotes>
<PackageTags>unittest sqlserver repository tdd dataaccesslayer</PackageTags>
</PropertyGroup>

Expand All @@ -28,7 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SqlServer.DacFx" />
<PackageReference Include="Microsoft.Data.SqlClient" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\UnitTests.Databases.SqlServer\UnitTests.Databases.SqlServer.csproj" />
<ProjectReference Include="..\..\src\UnitTests.Databases.SqlServer.Dac\UnitTests.Databases.SqlServer.Dac.csproj" />
<ProjectReference Include="..\UnitTests.Databases.SqlServer.Tests.DacPac\UnitTests.Databases.SqlServer.Tests.DacPac.sqlproj" />
<ProjectReference Include="..\UnitTests.Databases.SqlServer.Tests.Source\UnitTests.Databases.SqlServer.Tests.Source.sqlproj" />
<ProjectReference Include="..\UnitTests.Databases.SqlServer.Tests.Target\UnitTests.Databases.SqlServer.Tests.Target.sqlproj" />
Expand Down

0 comments on commit 75398d0

Please sign in to comment.