https://github.com/verifytests/verify.sqlserver
Extends Verify to allow verification of SqlServer bits.
https://github.com/verifytests/verify.sqlserver
Last synced: about 2 months ago
JSON representation
Extends Verify to allow verification of SqlServer bits.
- Host: GitHub
- URL: https://github.com/verifytests/verify.sqlserver
- Owner: VerifyTests
- License: mit
- Created: 2020-01-08T04:41:29.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2025-09-05T23:33:48.000Z (10 months ago)
- Last Synced: 2025-09-06T01:18:04.662Z (10 months ago)
- Language: C#
- Homepage:
- Size: 898 KB
- Stars: 18
- Watchers: 1
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
- Funding: .github/FUNDING.yml
- License: license.txt
- Code of conduct: code_of_conduct.md
Awesome Lists containing this project
README
#
Verify.SqlServer
[](https://github.com/orgs/VerifyTests/discussions)
[](https://ci.appveyor.com/project/SimonCropp/verify-sqlserver)
[](https://www.nuget.org/packages/Verify.SqlServer/)
Extends [Verify](https://github.com/VerifyTests/Verify) to allow verification of SqlServer bits.
**See [Milestones](../../milestones?state=closed) for release notes.**
## Sponsors
### Entity Framework Extensions
[Entity Framework Extensions](https://entityframework-extensions.net/?utm_source=simoncropp&utm_medium=Verify.SqlServer) is a major sponsor and is proud to contribute to the development this project.
[](https://entityframework-extensions.net/?utm_source=simoncropp&utm_medium=Verify.SqlServer)
### Developed using JetBrains IDEs
[](https://jb.gg/OpenSourceSupport)
## NuGet
* https://nuget.org/packages/Verify.SqlServer
## Usage
```cs
[ModuleInitializer]
public static void Init() =>
VerifySqlServer.Initialize();
```
snippet source | anchor
### SqlServer Schema
This test:
```cs
await Verify(connection);
```
snippet source | anchor
Will result in the following verified file:
#### Object types to include
```cs
await Verify(connection)
// include only tables and views
.SchemaIncludes(DbObjects.Tables | DbObjects.Views);
```
snippet source | anchor
Available values:
```cs
namespace VerifyTests.SqlServer;
[Flags]
public enum DbObjects
{
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:
```cs
await Verify(connection)
// include tables & views, or named MyTrigger
.SchemaFilter(
_ => _ is TableViewBase ||
_.Name == "MyTrigger");
```
snippet source | anchor
### Recording
Recording allows all commands executed to be captured and then (optionally) verified.
Call `SqlRecording.StartRecording()`:
```cs
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!);
```
snippet source | anchor
Will result in the following verified file:
```txt
{
target: 42,
sql: {
Text:
select Value
from MyTable,
HasTransaction: false
}
}
```
snippet source | anchor
Sql entries can be explicitly read using `SqlRecording.FinishRecording`, optionally filtered, and passed to Verify:
```cs
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
});
```
snippet source | anchor
#### Interpreting recording results
Recording results can be interpreted in a a variety of ways:
```cs
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();
// failed Commands via Type
var sqlErrorsViaType = entries
.Select(_ => _.Data)
.OfType();
```
snippet source | anchor
## Icon
[Database](https://thenounproject.com/term/database/310841/) designed by [Creative Stall](https://thenounproject.com/creativestall/) from [The Noun Project](https://thenounproject.com).