Skip to content

Repository files navigation

Verify.SqlServer

DiscussionsBuild statusNuGet Status

Extends Verify to allow verification of SqlServer bits.

See Milestones for release notes.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

Developed using JetBrains IDEs

JetBrains logo.

NuGet

Usage

[ModuleInitializer]publicstaticvoidInit()=>VerifySqlServer.Initialize();

snippet source | anchor

SqlServer Schema

This test:

awaitVerify(connection);

snippet source | anchor

Will result in the following verified file:

Object types to include

awaitVerify(connection)// include only tables and views.SchemaIncludes(DbObjects.Tables|DbObjects.Views);

snippet source | anchor

Available values:

namespaceVerifyTests.SqlServer;[Flags]publicenumDbObjects{StoredProcedures=1,Synonyms=2,Tables=4,UserDefinedFunctions=8,Views=16,All=StoredProcedures|Synonyms|Tables|UserDefinedFunctions|Views}

snippet source | anchor

Filtering

Objects can be dynamically filtered:

awaitVerify(connection)// include tables & views, or named MyTrigger.SchemaFilter(
_ =>_isTableViewBase||_.Name=="MyTrigger");

snippet source | anchor

Recording

Recording allows all commands executed to be captured and then (optionally) verified.

Call Recording.Start():

awaitusingvarconnection=newSqlConnection(connectionString);awaitconnection.OpenAsync();Recording.Start();awaitusingvarcommand=connection.CreateCommand();command.CommandText="select Value from MyTable";varvalue=awaitcommand.ExecuteScalarAsync();awaitVerify(value!);

snippet source | anchor

Will result in the following verified file:

{
target: 42,
sql: {
Text:
select Value
from MyTable,
HasTransaction: false
}
}

snippet source | anchor

Sql entries can be explicitly read using Recording.Stop(), optionally filtered, and passed to Verify:

awaitusingvarconnection=newSqlConnection(connectionString);awaitconnection.OpenAsync();Recording.Start();awaitusingvarcommand=connection.CreateCommand();command.CommandText="select Value from MyTable";varvalue=awaitcommand.ExecuteScalarAsync();awaitusingvarerrorCommand=connection.CreateCommand();errorCommand.CommandText="select Value from BadTable";try{awaiterrorCommand.ExecuteScalarAsync();}catch{}varentries=Recording.Stop().Select(_ =>_.Data);//Optionally filter resultsawaitVerify(new{value,sqlEntries=entries});

snippet source | anchor

Interpreting recording results

Recording results can be interpreted in a variety of ways:

varentries=Recording.Stop();// all sql entries via keyvarsqlEntries=entries.Where(_ =>_.Name=="sql").Select(_ =>_.Data);// successful Commands via TypevarsqlCommandsViaType=entries.Select(_ =>_.Data).OfType<SqlCommand>();// failed Commands via TypevarsqlErrorsViaType=entries.Select(_ =>_.Data).OfType<ErrorEntry>();

snippet source | anchor

Disabling recording

Recording is enabled by VerifySqlServer.Initialize(), which subscribes to the Microsoft.Data.SqlClient diagnostic listener. Pass recordCommands: false to leave that listener unsubscribed:

[ModuleInitializer]publicstaticvoidInit()=>VerifySqlServer.Initialize(recordCommands:false);

snippet source | anchor

Only recording is disabled. The converters, and the SqlConnection schema file converter, are still registered.

This is useful when another package records the same commands. Verify.EntityFramework records EF Core commands under the name ef, and EF Core executes those commands through SqlCommand. So with both packages recording, every command EF executes is captured twice: once as sql and once as ef.

Two constraints are worth knowing:

  • VerifierSettings.InitializePlugins() discovers this package and calls Initialize(), which enables recording. So Initialize(recordCommands: false) has to run beforeInitializePlugins(), otherwise discovery initializes first and the explicit call throws Already Initialized.
  • Recording.IgnoreNames("sql") is an alternative that needs no ordering, since it can be called at any point before recording starts. It discards sql entries as they are added, but the listener stays subscribed, so each command is still cloned and then thrown away.

Icon

Database designed by Creative Stall from The Noun Project.

About

Extends Verify to allow verification of SqlServer bits.

Resources

Code of conduct

Stars

18 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages