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.
var generator = new SqlServerTestGenerator(options =>
{
options.AddDatabaseObjectExitsRule(new string[] { "mytable" }, DatabaseObjectType.Table);
options.AddDatabaseRecordExitsRule(new List<DatabaseRecordExistRule>()
{
new DatabaseRecordExistRule()
{
TableName = "mytable",
ColumnName = "name",
Operator = "=",
Value = "myname"
}
});
options.AddDatabaseRecordsCountRule(new List<DatabaseRecordCountRule>()
{
new DatabaseRecordCountRule()
{
TableName = "mytable",
Count = 100,
Operator = "="
}
});
});
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 tablemytableexists in the database.AddDatabaseRecordExitsRule: will check if a record in tablemytablewithnameequalsmynameexists.AddDatabaseRecordsCountRule: will check if there is exactly 100 records in themytabletable.
Alternatively if you want to use MySQL or PostgreSQL generators, you can use MySqlTestGeneratororPostgresqlTestGenerator` respectively.
To run the tests, we create a SqlServerTestRunner runner:
var runner = new SqlServerTestRunner(scripts, options =>
{
options.AddSQLServerConnection("server=localhost;user=user;password=mypassword;Initial Catalog=myDatabase");
});
List<DatabaseScriptResult> results = await runner.Run();Alternatively if you want to use MySQL or PostgreSQL runners, you can use MySqlTestRunnerorPostgresqlTestRunner` 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.
- Implement asserters for processing the
DatabaseScriptResultlist. - Add more test types if necessary.
MIT License
Copyright (c) 2020 Miha Jakovac
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.