Skip to content

Commit f0446a6

Browse files
Fix documentation.
1 parent f25295a commit f0446a6

File tree

2 files changed

+55
-53
lines changed

2 files changed

+55
-53
lines changed

README.md

+53-51
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,102 @@
11
# PosInformatique.UnitTests.Databases
2-
PosInformatique.UnitTests.Databases is a set of tools for unit testing databases.
3-
It simplifies writing and executing tests, helping ensure your database and data access code is reliable and bug-free.
4-
Ideal for developers who want to validate data access based on SQL Server code during their development.
52

6-
This set of tools support to perform unit tests of the data access layer based on SQL Server. Any kind of the
7-
data access framework can be used with theses tools:
3+
**PosInformatique.UnitTests.Databases** is a set of tools for unit testing databases.
4+
It simplifies writing and executing tests, helping ensure your database and data access code are reliable and bug-free.
5+
It is ideal for developers who want to validate data access based on SQL Server code during their development.
6+
7+
This set of tools supports unit testing of the data access layer based on SQL Server.
8+
Any kind of data access framework can be used with these tools:
89
- Raw ADO .NET queries.
9-
- Entity Framework
10-
- Dapper
10+
- Entity Framework.
11+
- Dapper.
1112
- ...
1213

13-
## The approach of this tools
14+
## The approach of these tools
15+
16+
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.
1417

15-
The main approach of this tools is to perform unit tests without using mocking or in-memory the ADO .NET code or Entity Framework `DbContext` and using a real SQL Server database.
18+
### Why is this approach recommended?
1619

17-
Why this approach is recommanded?
18-
- Around 30% / 40% of the code is the applications is located in the Data Access layer / repositories components. Because it is hard to unit tests, developers do not perform unit tests
19-
and 30% / 40% of code coverage.
20-
- When using a mock or in-memory approach of a `DbContext` you don't really test the Entity Framework mapping to your database and specially additional SQL constraints like nullability, unicity, foreign key cascade,... And
21-
also all technical behavior like transactions, opening/closing connections,...
22-
- When inserting data, we want to be sure that the data in the columns are stored correctly (null / not null values, enum values to numerical values, customer or JSON serialized data,...)
23-
- If you use Entity Framework, you can detect warnings / errors raised by the `DbContext` (and not in the logs when deploying the application).
24-
- You perform unit tests cases, it is mean you write simple tests to test small feature instead to write a complex integration test.
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,
21+
- resulting in lower code coverage (30%-40%).
22+
- 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+
- You also miss technical behaviors like transactions, connection management, etc.
24+
- 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+
- If you use Entity Framework, you can detect warnings/errors raised by the `DbContext` that might not appear in logs when deploying the application.
26+
- You perform unit test cases, meaning you write simple tests to validate small features instead of writing complex integration tests.
2527

26-
## How to unit tests a Data Access Layer.
28+
## How to unit test a Data Access Layer
2729

28-
To perform unit tests of a Data Access Layer the approach is easily using the Arrange / Act / Assert pattern:
30+
To perform unit tests of a Data Access Layer, the approach is straightforward using the Arrange/Act/Assert pattern:
31+
32+
Before each unit test (`TestMethod` or `Fact` methods):
2933

30-
Before each units (`TestMethod` or `Fact` methods):
3134
1. Create an empty database with the SQL schema of the application.
3235

33-
There is two ways to do it:
34-
- Deploying a DACPAC file (built by an SQL Server Database project).
35-
- Or executing creating a database from a `DbContext` using Entity Framework.
36+
There are two ways to do this:
37+
- Deploy a DACPAC file (built by a SQL Server Database project).
38+
- Or create a database from a `DbContext` using Entity Framework.
3639

37-
1. Fill the tables with sample of data that we need.
40+
2. Fill the tables with the sample data needed.
3841

39-
1. Executing the code (the method of the repository to test).
42+
3. Execute the code (the method of the repository to be tested).
4043

41-
1. Assert the results of the code executed.
44+
4. Assert the results of the executed code.
4245

43-
If the method tested returns data (perform a SELECT query), we assert the objects returned with your favorite assert framework (FluentAssertions for example).
44-
If the method insert, update or delete data, we assert the content of the tables to check that all the data is stored correctly.
46+
- If the tested method returns data (performs a SELECT query), assert the returned objects using your favorite assertion framework (e.g., FluentAssertions).
47+
- If the method inserts, updates, or deletes data, assert the content of the tables to check that all data is stored correctly.
4548

46-
To write an unit test using this approach with the [PosInformatique.UnitTests.Databases](https://github.com/PosInformatique/PosInformatique.UnitTests.Databases) tools
47-
see the [Write unit tests to test the Data Access Layer](./docs/WriteUnitTests.md) page.
49+
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.
4850

49-
## What provides the PosInformatique.UnitTests.Databases tools?
51+
## What do the PosInformatique.UnitTests.Databases tools provide?
5052

51-
Using the previous approach, the [PosInformatique.UnitTests.Databases](https://github.com/PosInformatique/PosInformatique.UnitTests.Databases) libraries allows to:
53+
Using the previous approach, the [PosInformatique.UnitTests.Databases](https://github.com/PosInformatique/PosInformatique.UnitTests.Databases) libraries allow you to:
5254

53-
- Deploy easily a database before each unit tests execution.
54-
Database and the schema creation is an operation that can take lot of time (around 5 to 10 seconds), the [PosInformatique.UnitTests.Databases](https://github.com/PosInformatique/PosInformatique.UnitTests.Databases) libraries
55-
create physically the database at the first unit test execution. For the other unit tests execution, all the data are deleted in the database which allows to increase the speed of the unit tests execution.
55+
- Easily deploy a database before each unit test execution.
56+
Database and schema creation can take a lot of time (around 5 to 10 seconds). The [PosInformatique.UnitTests.Databases](https://github.com/PosInformatique/PosInformatique.UnitTests.Databases) libraries physically create the database during the first unit test execution. For subsequent tests, all data is deleted in the database, which speeds up the unit test execution.
5657

57-
- Provides a simple syntax to fill the tables with sample of data.
58+
- Provide a simple syntax to fill the tables with sample data.
5859

59-
- Provides helpers to query and retrieve easily data in SQL tables (to perform assertions).
60+
- Offer helpers to easily query and retrieve data from SQL tables (for assertions).
6061

6162
## NuGet packages
6263

63-
The [PosInformatique.UnitTests.Databases](https://github.com/PosInformatique/PosInformatique.UnitTests.Databases) are provided into two NuGet packages:
64+
The [PosInformatique.UnitTests.Databases](https://github.com/PosInformatique/PosInformatique.UnitTests.Databases) tools are provided in two NuGet packages:
65+
6466
- [![Nuget](https://img.shields.io/nuget/v/PosInformatique.UnitTests.Databases.SqlServer)](https://www.nuget.org/packages/PosInformatique.UnitTests.Databases.SqlServer) which contains:
65-
- Tools to deploy a SQL Server database using a DACPAC file before each unit tests.
66-
- Helper to initialize the SQL Server databases with sample data.
67-
- Helper to query easily SQL Server databases.
67+
- Tools to deploy a SQL Server database using a DACPAC file before each unit test.
68+
- Helpers to initialize SQL Server databases with sample data.
69+
- Helpers to easily query SQL Server databases.
6870

6971
- [![Nuget](https://img.shields.io/nuget/v/PosInformatique.UnitTests.Databases.SqlServer.EntityFramework)](https://www.nuget.org/packages/PosInformatique.UnitTests.Databases.SqlServer.EntityFramework) which contains:
7072
- Tools to deploy a SQL Server database using a DbContext.
7173

72-
This package use and contains the previous [PosInformatique.UnitTests.Databases.SqlServer](https://www.nuget.org/packages/PosInformatique.UnitTests.Databases.SqlServer) NuGet package.
74+
This package uses and includes the previous [PosInformatique.UnitTests.Databases.SqlServer](https://www.nuget.org/packages/PosInformatique.UnitTests.Databases.SqlServer) NuGet package.
7375

7476
## Samples / Demo
7577

7678
A complete sample solution is available in this repository in the [./samples](./samples) folder.
7779

7880
The solution contains the following projects:
7981
- [DemoApp.Domain](./samples/DemoApp.Domain/DemoApp.Domain.csproj): Represents the domain of the application with a set of sample business entities.
80-
- [DemoApp.DataAccessLayer](./samples/DemoApp.DataAccessLayer/DemoApp.DataAccessLayer.csproj): Represents a Data Access Layer with a set of repository to unit tests.
81-
- [DemoApp.DataAccessLayer.Tests](./samples/DemoApp.DataAccessLayer.Tests/DemoApp.DataAccessLayer.Tests.csproj): Unit tests projects to test the [DemoApp.DataAccessLayer](./samples/DemoApp.DataAccessLayer/DemoApp.DataAccessLayer.csproj)
82-
project using the [PosInformatique.UnitTests.Databases.SqlServer](https://www.nuget.org/packages/PosInformatique.UnitTests.Databases.SqlServer) package.
82+
- [DemoApp.DataAccessLayer](./samples/DemoApp.DataAccessLayer/DemoApp.DataAccessLayer.csproj): Represents a Data Access Layer with a set of repositories to unit test.
83+
- [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+
- project using the [PosInformatique.UnitTests.Databases.SqlServer](https://www.nuget.org/packages/PosInformatique.UnitTests.Databases.SqlServer) package.
8385

84-
## Write unit tests to test a Data Access Layer.
86+
## Writing unit tests for a Data Access Layer
8587

86-
To write unit tests for a Data Access Layer follows [Write unit tests to test the Data Access Layer](./docs/WriteUnitTests.md) documentation page, which explain the different steps to perform
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
8789
using the [PosInformatique.UnitTests.Databases.SqlServer.EntityFramework](https://www.nuget.org/packages/PosInformatique.UnitTests.Databases.SqlServer.EntityFramework) library:
8890

8991
- [Create the SQL Server instance](./docs/WriteUnitTests.md#create-the-sql-server-instance)
9092
- [Create the LocalDB instance](./docs/WriteUnitTests.md#create-the-localdb-instance)
91-
- [Create an unit tests project](./docs/WriteUnitTests.md#create-the-unit-tests-project)
93+
- [Create the unit tests project](./docs/WriteUnitTests.md#create-the-unit-tests-project)
9294
- [Add the NuGet packages](./docs/WriteUnitTests.md#add-the-nuget-packages)
9395
- [Unit test class](./docs/WriteUnitTests.md#unit-test-class)
9496
- [Deploy a new instance of the database from a DbContext](./docs/WriteUnitTests.md#deploy-a-new-instance-of-the-database-from-a-dbcontext)
95-
- [Parallelism execution of the unit tests](./docs/WriteUnitTests.md#parallelism-execution-of-the-unit-tests)
96-
- [Initializes the data of the database](./docs/WriteUnitTests.md#initializes-the-data-of-the-database)
97+
- [Parallel execution of the unit tests](./docs/WriteUnitTests.md#parallel-execution-of-the-unit-tests)
98+
- [Initialize the database data](./docs/WriteUnitTests.md#initializes-the-data-of-the-database)
9799
- [Write the unit tests for methods that retrieve data](./docs/WriteUnitTests.md#write-the-unit-tests-for-methods-that-retrieve-data)
98100
- [Write the unit tests for methods that update the data](./docs/WriteUnitTests.md#write-the-unit-tests-for-methods-that-update-the-data)
99101
- [Execute the unit tests](./docs/WriteUnitTests.md#execute-the-unit-tests)
100-
- [Check the database state after an unit test has been failed](./docs/WriteUnitTests.md#check-the-database-state-after-an-unit-test-has-been-failed)
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)

docs/WriteUnitTests.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ sqllocaldb c <instance name>
106106
With:
107107
- `<instance name>`: The name of the instance to create. For example: `demoapp`.
108108

109-
## Create an unit tests project
109+
## Create the unit tests project
110110

111111
To test the `CustomerRepository`, we create an `xUnit Test Project` in Visual Studio which reference our project that contains the
112112
Data Access Layer to test.
@@ -198,7 +198,7 @@ public static class UnitTestsConnectionStrings
198198
depending of the SQL Server instance your target. It is also very useful if you plan to execute the unit tests
199199
in a CI process (Git Actions, Azure Pipelines,...).
200200

201-
### Parallelism execution of the unit tests
201+
### Parallel execution of the unit tests
202202

203203
By default, with XUnit all the unit tests will be executed in parallel. Also, our unit tests in the `CustomerRepositoryTest`
204204
will works on the same database. It will not work of course, so it is mean that our unit tests must be executed in series

0 commit comments

Comments
 (0)