b42490781c | ||
---|---|---|
.github | ||
src | ||
.gitattributes | ||
.gitignore | ||
code_of_conduct.md | ||
license.txt | ||
readme.md |
readme.md
Verify.SqlServer
Extends Verify to allow verification of SqlServer bits.
See Milestones for release notes.
NuGet package
https://nuget.org/packages/Verify.SqlServer/
Usage
[ModuleInitializer]
public static void Init() =>
VerifySqlServer.Initialize();
SqlServer Schema
This test:
await Verify(connection);
Will result in the following verified file:
Object types to include
await Verify(connection)
// include only tables and views
.SchemaIncludes(DbObjects.Tables | DbObjects.Views);
Available values:
namespace VerifyTests.SqlServer;
[Flags]
public enum DbObjects
{
StoredProcedures = 1,
Synonyms = 2,
Tables = 4,
UserDefinedFunctions = 8,
Views = 16,
All = StoredProcedures | Synonyms | Tables | UserDefinedFunctions | Views
}
Filtering
Objects can be dynamically filtered:
await Verify(connection)
// include tables & views, or named MyTrigger
.SchemaFilter(
_ => _ is TableViewBase ||
_.Name == "MyTrigger");
Recording
Recording allows all commands executed to be captured and then (optionally) verified.
Call SqlRecording.StartRecording()
:
await using var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
Recording.Start();
await using var command = connection.CreateCommand();
command.CommandText = "select Value from MyTable";
var value = await command.ExecuteScalarAsync();
await Verify(value!);
Will result in the following verified file:
{
target: 42,
sql: {
Text: select Value from MyTable,
HasTransaction: false
}
}
Sql entries can be explicitly read using SqlRecording.FinishRecording
, optionally filtered, and passed to Verify:
await using var connection = new SqlConnection(connectionString);
await connection.OpenAsync();
Recording.Start();
await using var command = connection.CreateCommand();
command.CommandText = "select Value from MyTable";
var value = await command.ExecuteScalarAsync();
await using var errorCommand = connection.CreateCommand();
errorCommand.CommandText = "select Value from BadTable";
try
{
await errorCommand.ExecuteScalarAsync();
}
catch
{
}
var entries = Recording
.Stop()
.Select(_ => _.Data);
//Optionally filter results
await Verify(
new
{
value,
sqlEntries = entries
});
Interpreting recording results
Recording results can be interpreted in a a variety of ways:
var entries = Recording.Stop();
// all sql entries via key
var sqlEntries = entries
.Where(_ => _.Name == "sql")
.Select(_ => _.Data);
// successful Commands via Type
var sqlCommandsViaType = entries
.Select(_ => _.Data)
.OfType<SqlCommand>();
// failed Commands via Type
var sqlErrorsViaType = entries
.Select(_ => _.Data)
.OfType<ErrorEntry>();
Icon
Database designed by Creative Stall from The Noun Project.