Skip to content

Commit 9ff762d

Browse files
Add the documentation and samples for the ExecuteScript() method.
1 parent 01f9962 commit 9ff762d

6 files changed

+60
-7
lines changed

docs/WriteTest.md

+35
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,41 @@ public CustomerRepositoryTest(SqlServerDatabaseInitializer initializer)
298298
Now, every time we will execute an test in the `CustomerRepositoryTest` class,
299299
a database will be deployed with these 3 `Customer` before the execution of the test.
300300

301+
### Initializes the data from a T-SQL script
302+
303+
Since the version 2.1.0, it is possible to execute a T-SQL script by using the `SqlServerDatabase.ExecuteScript()` method.
304+
305+
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:
306+
307+
```sql
308+
INSERT INTO [Customer] ([FirstName], [LastName], [Revenue])
309+
VALUES
310+
('John', 'DOE', 110.50),
311+
('Marcel', 'DUPONT', 4852.45),
312+
('Andres', 'GARCIA', 0)
313+
```
314+
315+
In Visual Studio, set the following project properties for the `InsertData.sql` file:
316+
- `Build Action`: `Content`. This option will recompile the test project every time the T-SQL script has been changed.
317+
- `Copy to Output Directory`: `Copy if newer`. This option allows the compiler to copy the file in the output directory and
318+
make it available for the tests in the current directory when execution the tests.
319+
320+
In the constructor of the `CustomerRepositoryTest` call the script using the `SqlServerDatabase.ExecuteScript()` method.
321+
322+
```csharp
323+
public CustomerRepositoryTest(SqlServerDatabaseInitializer initializer)
324+
{
325+
using var dbContext = new DemoAppDbContext(DatabaseTestsConnectionStrings.CreateDbContextOptions<DemoAppDbContext>(DatabaseName));
326+
327+
this.database = initializer.Initialize(dbContext);
328+
329+
this.database.ExecuteScript(File.ReadAllText("InsertData.sql"));
330+
}
331+
```
332+
333+
Now, every time we will execute an test in the `CustomerRepositoryTest` class,
334+
a database will be deployed and the `InsertData.sql` will be executed.
335+
301336
## Write the tests for methods that retrieve data
302337

303338
This section describes how to write an test for methods that retrieve data (SELECT queries).

samples/DemoApp.DataAccessLayer.Tests/CustomerRepositoryTest.cs

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public CustomerRepositoryTest(SqlServerDatabaseInitializer initializer)
3535
// Also, we recommand to force to set the IDENTITY column values explicit to avoid
3636
// to update lot of code if you delete some rows later...
3737
this.database.InsertCustomer(id: 20, firstName: "Andres", lastName: "GARCIA");
38+
39+
// - Here we use a T-SQL script to insert data in the Customer table.
40+
// The script can contains GO instructions.
41+
this.database.ExecuteScript(File.ReadAllText("InsertData.sql"));
3842
}
3943

4044
[Fact]

samples/DemoApp.DataAccessLayer.Tests/DemoApp.DataAccessLayer.Tests.csproj

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
@@ -9,6 +9,16 @@
99
<IsTestProject>true</IsTestProject>
1010
</PropertyGroup>
1111

12+
<ItemGroup>
13+
<None Remove="InsertData.sql" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<Content Include="InsertData.sql">
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</Content>
20+
</ItemGroup>
21+
1222
<ItemGroup>
1323
<PackageReference Include="coverlet.collector" />
1424
<PackageReference Include="FluentAssertions" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
INSERT INTO [Customer] ([FirstName], [LastName], [Revenue])
2+
VALUES ('From script', 'Peter', 100)
3+
4+
GO 10

samples/Directory.Packages.props

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
77
<PackageVersion Include="FluentAssertions" Version="6.12.1" />
88
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.0.1" />
9-
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
10-
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
11-
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8" />
9+
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
10+
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" />
11+
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10" />
1212
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
1313
<PackageVersion Include="Microsoft.SqlServer.DacFx" Version="161.6374.0" />
14-
<PackageVersion Include="PosInformatique.Testing.Databases.SqlServer.EntityFramework" Version="2.0.0-rc.1" />
14+
<PackageVersion Include="PosInformatique.Testing.Databases.SqlServer.EntityFramework" Version="2.1.0-rc.3" />
1515
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556" />
16-
<PackageVersion Include="xunit" Version="2.9.1" />
16+
<PackageVersion Include="xunit" Version="2.9.2" />
1717
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
1818
</ItemGroup>
1919
</Project>

src/Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
- PosInformatique.Testing.Databases.SqlServer.EntityFramework target the .NET Core 6.0
1818
- Reduce the dependencies to Entity Framework 6.0
1919
- Reduce the dependencies of DACfx to a more earlier version.
20-
- Add new method SqlServerDatabase.ExecuteScript() to execute T-SQL script.
20+
- Add new method SqlServerDatabase.ExecuteScript() to execute T-SQL scripts.
2121

2222
2.0.0
2323
- Add SqlServerDatabaseComparer class to perform comparison between two databases.

0 commit comments

Comments
 (0)