From 9ff762d874bd4250d4bb5df41733f9b06c5da312 Mon Sep 17 00:00:00 2001 From: Gilles TOURREAU Date: Thu, 17 Oct 2024 09:10:20 +0200 Subject: [PATCH] Add the documentation and samples for the ExecuteScript() method. --- docs/WriteTest.md | 35 +++++++++++++++++++ .../CustomerRepositoryTest.cs | 4 +++ .../DemoApp.DataAccessLayer.Tests.csproj | 12 ++++++- .../InsertData.sql | 4 +++ samples/Directory.Packages.props | 10 +++--- src/Directory.Build.props | 2 +- 6 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 samples/DemoApp.DataAccessLayer.Tests/InsertData.sql diff --git a/docs/WriteTest.md b/docs/WriteTest.md index 60cc4f3..1be28db 100644 --- a/docs/WriteTest.md +++ b/docs/WriteTest.md @@ -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(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). diff --git a/samples/DemoApp.DataAccessLayer.Tests/CustomerRepositoryTest.cs b/samples/DemoApp.DataAccessLayer.Tests/CustomerRepositoryTest.cs index 4124519..7838f51 100644 --- a/samples/DemoApp.DataAccessLayer.Tests/CustomerRepositoryTest.cs +++ b/samples/DemoApp.DataAccessLayer.Tests/CustomerRepositoryTest.cs @@ -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] diff --git a/samples/DemoApp.DataAccessLayer.Tests/DemoApp.DataAccessLayer.Tests.csproj b/samples/DemoApp.DataAccessLayer.Tests/DemoApp.DataAccessLayer.Tests.csproj index a4d5b7e..2ebda52 100644 --- a/samples/DemoApp.DataAccessLayer.Tests/DemoApp.DataAccessLayer.Tests.csproj +++ b/samples/DemoApp.DataAccessLayer.Tests/DemoApp.DataAccessLayer.Tests.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -9,6 +9,16 @@ true + + + + + + + PreserveNewest + + + diff --git a/samples/DemoApp.DataAccessLayer.Tests/InsertData.sql b/samples/DemoApp.DataAccessLayer.Tests/InsertData.sql new file mode 100644 index 0000000..831a121 --- /dev/null +++ b/samples/DemoApp.DataAccessLayer.Tests/InsertData.sql @@ -0,0 +1,4 @@ +INSERT INTO [Customer] ([FirstName], [LastName], [Revenue]) + VALUES ('From script', 'Peter', 100) + +GO 10 \ No newline at end of file diff --git a/samples/Directory.Packages.props b/samples/Directory.Packages.props index 6728226..17c9a48 100644 --- a/samples/Directory.Packages.props +++ b/samples/Directory.Packages.props @@ -6,14 +6,14 @@ - - - + + + - + - + \ No newline at end of file diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 0d8078c..2689e88 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -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.