Skip to content

Commit

Permalink
Readme and interfaces from Core library
Browse files Browse the repository at this point in the history
* moved interfaces to core library

* readme file updated, small test fix
  • Loading branch information
mihaj authored Nov 24, 2020
1 parent a19e029 commit ff25884
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 45 deletions.
69 changes: 68 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,74 @@
[![Sonarcloud Quality gate](https://github.com/qatoolkit/qatoolkit-engine-database-net/workflows/Sonarqube%20Analyze/badge.svg)](https://sonarcloud.io/dashboard?id=qatoolkit_qatoolkit-engine-database-net)
[![NuGet package](https://img.shields.io/nuget/v/QAToolKit.Engine.DataBase?label=QAToolKit.Engine.Database)](https://www.nuget.org/packages/QAToolKit.Engine.Database/)

**Not ready for release.**
## Description
`QAToolKit.Engine.Database` is a .NET standard library, which can be used to do database fitness tests. For example, if you want to test that table is present in database, or certain number of records exist in specific table or if a record exists.

`DatabaseTestType` enumeration currently described those three test types:
- `ObjectExits`: Check if table, view or stored procedure exists.
- `RecordCount`: Check if record count in specific table equals an expression.
- `RecordExist`: Check if a record exists in specific table.

Currently supports only relational databases: `SQLServer`, `MySQL` and `PostgreSQL`.

## Sample

```csharp
var generator = new SqlServerTestGenerator(options =>
{
options.AddDatabaseObjectExitsRule(new string[] { "mytable" }, DatabaseObjectType.Table);

options.AddDatabaseRecordExitsRule(
new List<DatabaseRule>()
{
new DatabaseRule()
{
TableName = "mytable",
PredicateValue = "name = 'myname'"
}
});

options.AddDatabaseRecordsCountRule(
new List<DatabaseRule>()
{
new DatabaseRule()
{
TableName = "mytable",
PredicateValue = "=100"
}
});
});

List<DatabaseScript> scripts = await generator.Generate();
```
The code above will generate a SQLServer `DatabaseScript` list, which will be used by runner to run the tests against database.

Above example adds all three test types to the generator:
- `AddDatabaseObjectExitsRule`: will check if a table `mytable` exists in the database.
- `AddDatabaseRecordExitsRule`: will check if a record in table `mytable` with `name` equals `myname` exists.
- `AddDatabaseRecordsCountRule`: will check if there is exactly 100 records in the `mytable` table.

Alternatively if you want to use `MySQL` or `PostgreSQL` generators, you can use MySqlTestGenerator` or `PostgresqlTestGenerator` respectively.

To run the tests, we create a `SqlServerTestRunner` runner:

```csharp
var runner = new SqlServerTestRunner(scripts, options =>
{
options.AddSQLServerConnection("server=localhost;user=sa;password=Mihaj666.;Initial Catalog=");
});

List<DatabaseScriptResult> results = await runner.Run();
```

Alternatively if you want to use `MySQL` or `PostgreSQL` runners, you can use MySqlTestRunner` or `PostgresqlTestRunner` respectively.

Please note that **your user must have correct database permissions**. I suggest a read-only permissions that can also access `sys` or `information_schema` schemas.

## To-Do

- Implement asserters for processing the `DatabaseScriptResult` list.
- Add more test types if necessary.

## License

Expand Down
4 changes: 2 additions & 2 deletions src/QAToolKit.Engine.Database.Test/MySqlTestGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public async Task MySqlRecordExistScriptTest_Success()
new DatabaseRule()
{
TableName = "mytable",
PredicateValue = "= 'myname'"
PredicateValue = "name = 'myname'"
}
});
});
Expand All @@ -181,7 +181,7 @@ public async Task MySqlRecordExistScriptTest_Success()
{
new DatabaseScript(
"mytable",
$@"SELECT EXISTS (SELECT 1 FROM mytable WHERE = 'myname');",
$@"SELECT EXISTS (SELECT 1 FROM mytable WHERE name = 'myname');",
DatabaseTestType.RecordExist,
DatabaseKind.MySQL)
}.ToExpectedObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public async Task PostgresqlRecordExistScriptTest_Success()
new DatabaseRule()
{
TableName = "mytable",
PredicateValue = "= 'myname'"
PredicateValue = "name = 'myname'"
}
});
});
Expand All @@ -181,7 +181,7 @@ public async Task PostgresqlRecordExistScriptTest_Success()
{
new DatabaseScript(
"mytable",
$@"SELECT EXISTS (SELECT 1 FROM mytable WHERE = 'myname');",
$@"SELECT EXISTS (SELECT 1 FROM mytable WHERE name = 'myname');",
DatabaseTestType.RecordExist,
DatabaseKind.PostgreSQL)
}.ToExpectedObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public async Task SqlServerRecordExistScriptTest_Success()
new DatabaseRule()
{
TableName = "mytable",
PredicateValue = "= 'myname'"
PredicateValue = "name = 'myname'"
}
});
});
Expand All @@ -182,7 +182,7 @@ public async Task SqlServerRecordExistScriptTest_Success()
{
new DatabaseScript(
"mytable",
$@"IF EXISTS(SELECT 1 FROM mytable WHERE = 'myname') BEGIN Select 1 END ELSE BEGIN Select 0 END",
$@"IF EXISTS(SELECT 1 FROM mytable WHERE name = 'myname') BEGIN Select 1 END ELSE BEGIN Select 0 END",
DatabaseTestType.RecordExist,
DatabaseKind.SQLServer)
}.ToExpectedObject();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using QAToolKit.Engine.Database.Interfaces;
using QAToolKit.Core.Interfaces;
using QAToolKit.Engine.Database.Models;
using System;
using System.Collections.Generic;
Expand Down
17 changes: 0 additions & 17 deletions src/QAToolKit.Engine.Database/Interfaces/IDatabaseTestGenerator.cs

This file was deleted.

18 changes: 0 additions & 18 deletions src/QAToolKit.Engine.Database/Interfaces/IDatabaseTestRunner.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PackageReference Include="MySql.Data" Version="8.0.22" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Npgsql" Version="5.0.0" />
<PackageReference Include="QAToolKit.Core" Version="0.3.0" />
<PackageReference Include="QAToolKit.Core" Version="0.3.2" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Dapper;
using QAToolKit.Engine.Database.Interfaces;
using QAToolKit.Engine.Database.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using QAToolKit.Core.Interfaces;

namespace QAToolKit.Engine.Database.Runners
{
Expand Down

0 comments on commit ff25884

Please sign in to comment.