You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add SqlServerDatabaseComparer class to perform comparison between two databases.
* Add new PosInformatique.UnitTests.Databases.SqlServer.Dac NuGet package which contains DAC package tools.
* Add new SqlServer.CreateDatabaseAsync() extension method to create a database from a DbContext.
* Reduce the Entity Framework / Sql Client NuGet library dependencies with lower version.
Copy file name to clipboardexpand all lines: README.md
+65-11
Original file line number
Diff line number
Diff line change
@@ -4,30 +4,39 @@
4
4
It simplifies writing and executing tests, helping ensure your database and data access code are reliable and bug-free.
5
5
It is ideal for developers who want to validate data access based on SQL Server code during their development.
6
6
7
-
This set of tools supports unit testing of the data access layer based on SQL Server.
7
+
This set of tools supports unit testing of the persistence layer based on SQL Server.
8
8
Any kind of data access framework can be used with these tools:
9
9
- Raw ADO .NET queries.
10
10
- Entity Framework.
11
11
- Dapper.
12
12
- ...
13
13
14
+
You can also use this tools to create and run integration tests with the
15
+
[Integration tests in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-8.0)
16
+
approach.
17
+
18
+
Since the version 2.0.0 this tools provide a comparer to compare the schema of two SQL databases.
19
+
14
20
## The approach of these tools
15
21
16
22
The main approach of these tools is to perform unit tests without using mocking or in-memory alternatives for ADO .NET code or Entity Framework `DbContext`, instead using a real SQL Server database.
17
23
24
+
Also, these tools offer simple way to compare two SQL databases to test migration script (or Entity Framework migration) when upgrading a database.
25
+
18
26
### Why is this approach recommended?
19
27
20
-
- Around 30% to 40% of the code in applications is located in the Data Access layer or repository components. Because it is hard to unit test, developers often skip testing,
28
+
- Around 30% to 40% of the code in applications is located in the persistence layer or repository components. Because it is hard to unit test, developers often skip testing,
21
29
resulting in lower code coverage.
22
30
- When using a mock or in-memory approach for a `DbContext`, you don't truly test the Entity Framework mapping to your database, especially additional SQL constraints like nullability, uniqueness, foreign key cascades, etc.
23
31
You also miss technical behaviors like transactions, connection management, triggers, etc.
24
32
- When inserting data, it is crucial to ensure that the data in the columns are stored correctly (null/not null values, enum values to numerical values, custom or JSON serialized data, etc.).
25
33
- If you use Entity Framework, you can detect warnings/errors raised by the `DbContext` during the development.
26
34
- You perform unit test cases, meaning you write simple tests to validate small features instead of writing complex integration tests.
35
+
- When changing the schema of the database, it is important to test and have a *safeguard* to check that the migration script (or Entity Framework migration actions) will update the database to the expected schema.
27
36
28
-
## How to unit test a Data Access Layer
37
+
## How to unit test a persistence layer
29
38
30
-
To perform unit tests of a Data Access Layer, the approach is straightforward using the Arrange/Act/Assert pattern:
39
+
To perform unit tests of a persistence layer, the approach is straightforward using the Arrange/Act/Assert pattern:
31
40
32
41
Before each unit test (`TestMethod` or `Fact` methods):
33
42
@@ -48,6 +57,34 @@ Before each unit test (`TestMethod` or `Fact` methods):
48
57
49
58
To write a unit test using this approach with the [PosInformatique.UnitTests.Databases](https://github.com/PosInformatique/PosInformatique.UnitTests.Databases) tools, see the [Write unit tests to test the Data Access Layer](./docs/WriteUnitTests.md) page.
50
59
60
+
## How to unit test database migration
61
+
62
+
To perform unit tests of a database migration, the approach is straightforward and required only a unit test which perform the following actions:
63
+
64
+
1. Create an empty database (*initial database*).
65
+
66
+
2. Create a secondary database with the targeted schema (*target database*).
67
+
68
+
There are two ways to do this:
69
+
- Deploy a DACPAC file (built by a SQL Server Database project).
70
+
- Or create a database from a `DbContext` using Entity Framework.
71
+
72
+
3. Execute your database *migration code* on the *initial database*.
73
+
74
+
Your database *migration code* can be:
75
+
- A simple SQL script file.
76
+
- An Entity Framework migration sets executed with the `MigrateAsync()` method.
77
+
78
+
4. Compare the two databases schemas (*initial* and *target*).
79
+
80
+
If the database *migration code* works, the *initial* and *target* must have the same schema.
81
+
82
+
> **NB**: The initial database is not necessarily empty. It can be at a specific schema version X if we want to test the migration from version X to Y.
83
+
84
+
This approach does not test the migration of the data within the database. We can modify this process to inject some data in the first step to test it,
85
+
but writing the unit test can be time-consuming. By focusing on the schema migration of the database, you can verify at least 80-90% of your database
86
+
migration code. It's better than nothing and very useful for detecting issues during development or in a CI process!
87
+
51
88
## What do the PosInformatique.UnitTests.Databases tools provide?
52
89
53
90
Using the previous approach, the [PosInformatique.UnitTests.Databases](https://github.com/PosInformatique/PosInformatique.UnitTests.Databases) libraries allow you to:
@@ -59,33 +96,36 @@ Using the previous approach, the [PosInformatique.UnitTests.Databases](https://g
59
96
60
97
- Offer helpers to easily query and retrieve data from SQL tables (for assertions).
61
98
99
+
- Contain a comparer tool to check schema differences between two databases.
100
+
62
101
## NuGet packages
63
102
64
103
The [PosInformatique.UnitTests.Databases](https://github.com/PosInformatique/PosInformatique.UnitTests.Databases) tools are provided in two NuGet packages:
65
104
66
105
-[PosInformatique.UnitTests.Databases.SqlServer](https://www.nuget.org/packages/PosInformatique.UnitTests.Databases.SqlServer) NuGet package which contains:
67
-
- Tools to deploy a SQL Server database using a DACPAC file before each unit test.
68
106
- Helpers to initialize SQL Server databases with sample data.
69
107
- Helpers to easily query SQL Server databases.
108
+
- Helpers to compare the schema of two SQL Server databases.
109
+
110
+
-[PosInformatique.UnitTests.Databases.SqlServer.Dac](https://www.nuget.org/packages/PosInformatique.UnitTests.Databases.SqlServer.Dac) NuGet package which contains:
111
+
- Tools to deploy a SQL Server database using a DACPAC file before each unit test.
70
112
71
113
-[PosInformatique.UnitTests.Databases.SqlServer.EntityFramework](https://www.nuget.org/packages/PosInformatique.UnitTests.Databases.SqlServer.EntityFramework) NuGet package which contains:
72
114
- Tools to deploy a SQL Server database using a DbContext.
73
-
74
-
This package uses and includes the previous [PosInformatique.UnitTests.Databases.SqlServer](https://www.nuget.org/packages/PosInformatique.UnitTests.Databases.SqlServer) NuGet package.
75
115
76
116
## Samples / Demo
77
117
78
118
A complete sample solution is available in this repository inside the [samples](./samples) folder.
79
119
80
120
The solution contains the following sample projects:
81
121
-[DemoApp.Domain](./samples/DemoApp.Domain/DemoApp.Domain.csproj): Represents the domain of the application with a set of sample business entities.
82
-
-[DemoApp.DataAccessLayer](./samples/DemoApp.DataAccessLayer/DemoApp.DataAccessLayer.csproj): Represents a Data Access Layer with a set of repositories to unit test.
122
+
-[DemoApp.DataAccessLayer](./samples/DemoApp.DataAccessLayer/DemoApp.DataAccessLayer.csproj): Represents a persistence layer with a set of repositories to unit test.
83
123
-[DemoApp.DataAccessLayer.Tests](./samples/DemoApp.DataAccessLayer.Tests/DemoApp.DataAccessLayer.Tests.csproj): Unit test project to test the [DemoApp.DataAccessLayer](./samples/DemoApp.DataAccessLayer/DemoApp.DataAccessLayer.csproj)
84
124
project using the [PosInformatique.UnitTests.Databases.SqlServer.EntityFramework](https://www.nuget.org/packages/PosInformatique.UnitTests.Databases.SqlServer.EntityFramework) package.
85
125
86
-
## Writing unit tests for a Data Access Layer
126
+
## Writing unit tests for a persistence layer
87
127
88
-
To write unit tests for a Data Access Layer, follow the [Write unit tests to test the Data Access Layer](./docs/WriteUnitTests.md) documentation page, which explains the different steps to perform
128
+
To write unit tests for a persistence layer, follow the [Write unit tests to test the persistence layer](./docs/WriteUnitTests.md) documentation page, which explains the different steps to perform
89
129
using the [PosInformatique.UnitTests.Databases.SqlServer.EntityFramework](https://www.nuget.org/packages/PosInformatique.UnitTests.Databases.SqlServer.EntityFramework) library:
90
130
91
131
-[Create the SQL Server instance](./docs/WriteUnitTests.md#create-the-sql-server-instance)
@@ -99,4 +139,18 @@ using the [PosInformatique.UnitTests.Databases.SqlServer.EntityFramework](https:
99
139
-[Write the unit tests for methods that retrieve data](./docs/WriteUnitTests.md#write-the-unit-tests-for-methods-that-retrieve-data)
100
140
-[Write the unit tests for methods that update the data](./docs/WriteUnitTests.md#write-the-unit-tests-for-methods-that-update-the-data)
101
141
-[Execute the unit tests](./docs/WriteUnitTests.md#execute-the-unit-tests)
102
-
-[Check the database state after a unit test has failed](./docs/WriteUnitTests.md#check-the-database-state-after-an-unit-test-has-been-failed)
142
+
-[Check the database state after a unit test has failed](./docs/WriteUnitTests.md#check-the-database-state-after-an-unit-test-has-been-failed)
143
+
144
+
## Writing unit test to check database migration
145
+
146
+
To write an unit test to check the migration of database, follow the [Write unit tests to test database migration](./docs/WriteDatabaseMigrationUnitTest.md)
147
+
documentation page.
148
+
149
+
For Entity Framework migration:
150
+
-[Create the SQL Server instance](./docs/WriteUnitTests.md#create-the-sql-server-instance)
151
+
-[Create the LocalDB instance](./docs/WriteUnitTests.md#create-the-localdb-instance)
-[Create the unit tests project](./docs/WriteDatabaseMigrationUnitTest.md#create-the-unit-tests-project)
154
+
-[Add the NuGet packages](./docs/WriteDatabaseMigrationUnitTest.md#add-the-nuget-packages)
155
+
-[Write unit test to check the migration of the database](./docs/WriteDatabaseMigrationUnitTest.md#write-unit-test-to-check-the-migration-of-the-database)
156
+
-[Check the report details of the `SqlServerDatabaseComparer` tool](./docs/WriteDatabaseMigrationUnitTest.md#check-the-report-details-of-the-sqlserverdatabasecomparer-tool)
0 commit comments