An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# Verify.SqlServer

[![Discussions](https://img.shields.io/badge/Verify-Discussions-yellow?svg=true&label=)](https://github.com/orgs/VerifyTests/discussions)
[![Build status](https://img.shields.io/appveyor/build/SimonCropp/verify-sqlserver)](https://ci.appveyor.com/project/SimonCropp/verify-sqlserver)
[![NuGet Status](https://img.shields.io/nuget/v/Verify.SqlServer.svg)](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.

[![Entity Framework Extensions](https://raw.githubusercontent.com/VerifyTests/Verify.SqlServer/refs/heads/main/docs/zzz.png)](https://entityframework-extensions.net/?utm_source=simoncropp&utm_medium=Verify.SqlServer)

### Developed using JetBrains IDEs

[![JetBrains logo.](https://raw.githubusercontent.com/VerifyTests/Verify.SqlServer/main/docs/jetbrains.png)](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).