Skip to content

Commit

Permalink
Add the documentation and samples for the ExecuteScript() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
GillesTourreau committed Oct 17, 2024
1 parent 01f9962 commit 9ff762d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
35 changes: 35 additions & 0 deletions docs/WriteTest.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,41 @@ public CustomerRepositoryTest(SqlServerDatabaseInitializer initializer)
Now, every time we will execute an test in the `CustomerRepositoryTest` class,
a database will be deployed with these 3 `Customer` before the execution of the test.

### Initializes the data from a T-SQL script

Since the version 2.1.0, it is possible to execute a T-SQL script by using the `SqlServerDatabase.ExecuteScript()` method.

For example, if we want to initialize the previous customers using a T-SQL, add a `.sql` file in the project with the following code:

```sql
INSERT INTO [Customer] ([FirstName], [LastName], [Revenue])
VALUES
('John', 'DOE', 110.50),
('Marcel', 'DUPONT', 4852.45),
('Andres', 'GARCIA', 0)
```

In Visual Studio, set the following project properties for the `InsertData.sql` file:
- `Build Action`: `Content`. This option will recompile the test project every time the T-SQL script has been changed.
- `Copy to Output Directory`: `Copy if newer`. This option allows the compiler to copy the file in the output directory and
make it available for the tests in the current directory when execution the tests.

In the constructor of the `CustomerRepositoryTest` call the script using the `SqlServerDatabase.ExecuteScript()` method.

```csharp
public CustomerRepositoryTest(SqlServerDatabaseInitializer initializer)
{
using var dbContext = new DemoAppDbContext(DatabaseTestsConnectionStrings.CreateDbContextOptions<DemoAppDbContext>(DatabaseName));

this.database = initializer.Initialize(dbContext);

this.database.ExecuteScript(File.ReadAllText("InsertData.sql"));
}
```

Now, every time we will execute an test in the `CustomerRepositoryTest` class,
a database will be deployed and the `InsertData.sql` will be executed.

## Write the tests for methods that retrieve data

This section describes how to write an test for methods that retrieve data (SELECT queries).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public CustomerRepositoryTest(SqlServerDatabaseInitializer initializer)
// Also, we recommand to force to set the IDENTITY column values explicit to avoid
// to update lot of code if you delete some rows later...
this.database.InsertCustomer(id: 20, firstName: "Andres", lastName: "GARCIA");

// - Here we use a T-SQL script to insert data in the Customer table.
// The script can contains GO instructions.
this.database.ExecuteScript(File.ReadAllText("InsertData.sql"));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -9,6 +9,16 @@
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<None Remove="InsertData.sql" />
</ItemGroup>

<ItemGroup>
<Content Include="InsertData.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" />
<PackageReference Include="FluentAssertions" />
Expand Down
4 changes: 4 additions & 0 deletions samples/DemoApp.DataAccessLayer.Tests/InsertData.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
INSERT INTO [Customer] ([FirstName], [LastName], [Revenue])
VALUES ('From script', 'Peter', 100)

GO 10
10 changes: 5 additions & 5 deletions samples/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="FluentAssertions" Version="6.12.1" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.0.1" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="Microsoft.SqlServer.DacFx" Version="161.6374.0" />
<PackageVersion Include="PosInformatique.Testing.Databases.SqlServer.EntityFramework" Version="2.0.0-rc.1" />
<PackageVersion Include="PosInformatique.Testing.Databases.SqlServer.EntityFramework" Version="2.1.0-rc.3" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556" />
<PackageVersion Include="xunit" Version="2.9.1" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- PosInformatique.Testing.Databases.SqlServer.EntityFramework target the .NET Core 6.0
- Reduce the dependencies to Entity Framework 6.0
- Reduce the dependencies of DACfx to a more earlier version.
- Add new method SqlServerDatabase.ExecuteScript() to execute T-SQL script.
- Add new method SqlServerDatabase.ExecuteScript() to execute T-SQL scripts.

2.0.0
- Add SqlServerDatabaseComparer class to perform comparison between two databases.
Expand Down

0 comments on commit 9ff762d

Please sign in to comment.