Skip to content

Commit

Permalink
Better Parameters serialization (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jun 20, 2024
1 parent 6e0668c commit 87c868c
Show file tree
Hide file tree
Showing 29 changed files with 626 additions and 76 deletions.
1 change: 1 addition & 0 deletions src/.gitattributes → .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.verified.txt text eol=lf working-tree-encoding=UTF-8
*.verified.xml text eol=lf working-tree-encoding=UTF-8
*.verified.json text eol=lf working-tree-encoding=UTF-8
*.verified.sql text eol=lf working-tree-encoding=UTF-8

.editorconfig text eol=lf working-tree-encoding=UTF-8
Shared.sln.DotSettings text eol=lf working-tree-encoding=UTF-8
16 changes: 9 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ This test:
```cs
await Verify(connection);
```
<sup><a href='/src/Tests/Tests.cs#L77-L81' title='Snippet source file'>snippet source</a> | <a href='#snippet-SqlServerSchema' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Tests.cs#L79-L83' title='Snippet source file'>snippet source</a> | <a href='#snippet-SqlServerSchema' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will result in the following verified file:

<!-- snippet: Tests.SqlServerSchema.verified.sql -->
<a id='snippet-Tests.SqlServerSchema.verified.sql'></a>
<!-- snippet: Tests.Schema.verified.sql -->
<a id='snippet-Tests.Schema.verified.sql'></a>
```sql
-- Tables

Expand Down Expand Up @@ -102,7 +102,7 @@ END;

CREATE SYNONYM [dbo].[synonym1] FOR [MyTable]
```
<sup><a href='/src/Tests/Tests.SqlServerSchema.verified.sql#L1-L57' title='Snippet source file'>snippet source</a> | <a href='#snippet-Tests.SqlServerSchema.verified.sql' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Tests.Schema.verified.sql#L1-L57' title='Snippet source file'>snippet source</a> | <a href='#snippet-Tests.Schema.verified.sql' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -123,7 +123,7 @@ command.CommandText = "select Value from MyTable";
var value = await command.ExecuteScalarAsync();
await Verify(value!);
```
<sup><a href='/src/Tests/Tests.cs#L158-L168' title='Snippet source file'>snippet source</a> | <a href='#snippet-Recording' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Tests.cs#L163-L173' title='Snippet source file'>snippet source</a> | <a href='#snippet-Recording' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will result in the following verified file:
Expand Down Expand Up @@ -154,7 +154,9 @@ Recording.Start();
await using var command = connection.CreateCommand();
command.CommandText = "select Value from MyTable";
var value = await command.ExecuteScalarAsync();
var entries = Recording.Stop().Select(_ => _.Data);
var entries = Recording
.Stop()
.Select(_ => _.Data);
//TODO: optionally filter the results
await Verify(
new
Expand All @@ -163,7 +165,7 @@ await Verify(
sql = entries
});
```
<sup><a href='/src/Tests/Tests.cs#L193-L210' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordingSpecific' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Tests.cs#L280-L299' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordingSpecific' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions src/Tests/Tests.MsParameterCollectionFull.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
name: {
Value: DateTime_1,
DbType: Date,
Direction: InputOutput,
Offset: 5,
Precision: 2,
Scale: 3,
Size: 4,
LocaleId: 10,
SourceColumn: sourceColumn,
SourceVersion: Proposed
}
}
4 changes: 4 additions & 0 deletions src/Tests/Tests.MsParameterEmpty.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
Name: name,
DbType: Date
}
13 changes: 13 additions & 0 deletions src/Tests/Tests.MsParameterFull.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
Name: name,
Value: DateTime_1,
Direction: InputOutput,
Offset: 5,
Precision: 2,
Scale: 3,
Size: 4,
LocaleId: 10,
SourceColumn: sourceColumn,
SourceVersion: Proposed,
ForceColumnEncryption: true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Tables
-- Tables

CREATE TABLE [dbo].[MyTable](
[Value] [int] NULL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Tables
-- Tables

CREATE TABLE [dbo].[MyTable](
[Value] [int] NULL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Tables
-- Tables

CREATE TABLE [dbo].[MyTable](
[Value] [int] NULL
Expand Down
14 changes: 14 additions & 0 deletions src/Tests/Tests.SysParameterCollectionFull.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
name: {
Value: DateTime_1,
DbType: Date,
Direction: InputOutput,
Offset: 5,
Precision: 2,
Scale: 3,
Size: 4,
LocaleId: 10,
SourceColumn: sourceColumn,
SourceVersion: Proposed
}
}
4 changes: 4 additions & 0 deletions src/Tests/Tests.SysParameterEmpty.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
Name: name,
DbType: Date
}
13 changes: 13 additions & 0 deletions src/Tests/Tests.SysParameterFull.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
Name: name,
Value: DateTime_1,
DbType: Date,
Direction: InputOutput,
Offset: 5,
Precision: 2,
Scale: 3,
Size: 4,
LocaleId: 10,
SourceColumn: sourceColumn,
SourceVersion: Proposed
}
103 changes: 96 additions & 7 deletions src/Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Data;
using System.Data.SqlTypes;
using Microsoft.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
Expand Down Expand Up @@ -69,7 +71,7 @@ RETURNS DEC(10,2)
});

[Test]
public async Task SqlServerSchema()
public async Task Schema()
{
await using var database = await sqlInstance.Build();
var connection = database.Connection;
Expand All @@ -86,11 +88,14 @@ public async Task SqlServerSchemaInDynamic()
{
await using var database = await sqlInstance.Build();
var connection = database.Connection;
await Verify(new {connection});
await Verify(new
{
connection
});
}

[Test]
public async Task SqlServerSchemaLegacy()
public async Task SchemaLegacy()
{
var database = await sqlInstance.Build();
var connectionString = database.ConnectionString;
Expand Down Expand Up @@ -121,7 +126,7 @@ await Verify()
}

[Test]
public async Task SqlException()
public async Task Exception()
{
await using var database = await sqlInstance.Build();
await using var connection = new SqlConnection(database.ConnectionString);
Expand All @@ -132,7 +137,7 @@ public async Task SqlException()
}

[Test]
public async Task SqlError()
public async Task Error()
{
await using var database = await sqlInstance.Build();
await using var connection = new SqlConnection(database.ConnectionString);
Expand Down Expand Up @@ -184,6 +189,88 @@ public async Task RecordingWithParameter()
await Verify(value!);
}

[Test]
public Task MsParameterEmpty()
{
var parameter = new SqlParameter("name", SqlDbType.Date);
return Verify(parameter);
}

[Test]
public Task MsParameterFull()
{
var parameter = BuildFullMsParameter();

return Verify(parameter);
}
[Test]
public Task MsParameterCollectionFull()
{
var parameter = BuildFullSysParameter();

var parameters = new System.Data.SqlClient.SqlCommand().Parameters;
parameters.Add(parameter);
return Verify(parameters);
}

static SqlParameter BuildFullMsParameter() =>
new("name", SqlDbType.DateTime)
{
Direction = ParameterDirection.InputOutput,
Offset = 5,
Precision = 2,
Scale = 3,
Value = DateTime.Now,
CompareInfo = SqlCompareOptions.BinarySort2,
LocaleId = 10,
Size = 4,
IsNullable = false,
SourceVersion = DataRowVersion.Proposed,
ForceColumnEncryption = true,
SourceColumn = "sourceColumn"
};

[Test]
public Task SysParameterFull()
{
var parameter = BuildFullSysParameter();

return Verify(parameter);
}

[Test]
public Task SysParameterCollectionFull()
{
var parameter = BuildFullSysParameter();

var parameters = new System.Data.SqlClient.SqlCommand().Parameters;
parameters.Add(parameter);
return Verify(parameters);
}

static System.Data.SqlClient.SqlParameter BuildFullSysParameter() =>
new("name", SqlDbType.Date)
{
Direction = ParameterDirection.InputOutput,
Offset = 5,
Precision = 2,
Scale = 3,
Value = DateTime.Now,
CompareInfo = SqlCompareOptions.BinarySort2,
LocaleId = 10,
Size = 4,
IsNullable = false,
SourceVersion = DataRowVersion.Proposed,
SourceColumn = "sourceColumn"
};

[Test]
public Task SysParameterEmpty()
{
var parameter = new System.Data.SqlClient.SqlParameter("name", SqlDbType.Date);
return Verify(parameter);
}

[Test]
public async Task RecordingSpecific()
{
Expand All @@ -198,7 +285,9 @@ public async Task RecordingSpecific()
await using var command = connection.CreateCommand();
command.CommandText = "select Value from MyTable";
var value = await command.ExecuteScalarAsync();
var entries = Recording.Stop().Select(_ => _.Data);
var entries = Recording
.Stop()
.Select(_ => _.Data);
//TODO: optionally filter the results
await Verify(
new
Expand Down Expand Up @@ -232,7 +321,7 @@ static async Task Execute(SqlConnection sqlConnection)
}

[Test]
public async Task SqlServerSchemaSettings()
public async Task SchemaSettings()
{
await using var database = await sqlInstance.Build();
var connection = database.Connection;
Expand Down
8 changes: 3 additions & 5 deletions src/Verify.SqlServer/Converters/MsConnectionConverter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Microsoft.Data.SqlClient;

class MsConnectionConverter :
WriteOnlyJsonConverter<SqlConnection>
class MsConnectionConverter :
WriteOnlyJsonConverter<MsConnection>
{
public override void Write(VerifyJsonWriter writer, SqlConnection connection)
public override void Write(VerifyJsonWriter writer, MsConnection connection)
{
var schemaSettings = writer.Context.GetSchemaSettings();
var builder = new SqlScriptBuilder(schemaSettings);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Microsoft.Data.SqlClient;

class MsSqlErrorConverter :
WriteOnlyJsonConverter<SqlError>
class MsErrorConverter :
WriteOnlyJsonConverter<MsError>
{
public override void Write(VerifyJsonWriter writer, SqlError error)
public override void Write(VerifyJsonWriter writer, MsError error)
{
writer.WriteStartObject();
writer.WriteMember(error, error.Message, "Message");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Microsoft.Data.SqlClient;

class MsSqlExceptionConverter :
WriteOnlyJsonConverter<SqlException>
class MsExceptionConverter :
WriteOnlyJsonConverter<MsException>
{
public override void Write(VerifyJsonWriter writer, SqlException exception)
public override void Write(VerifyJsonWriter writer, MsException exception)
{
writer.WriteStartObject();

Expand Down
29 changes: 29 additions & 0 deletions src/Verify.SqlServer/Converters/MsParameterCollectionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class MsParameterCollectionConverter :
WriteOnlyJsonConverter<MsParameterCollection>
{
public override void Write(VerifyJsonWriter writer, MsParameterCollection parameters)
{
writer.WriteStartObject();

MsParameterConverter.OmitName();
foreach (MsParameter parameter in parameters)
{
var name = parameter.ParameterName;
object? value;
if (MsParameterConverter.IsOnlyValue(parameter))
{
value = parameter.Value;
}
else
{
value = parameter;
}

writer.WriteMember(parameters, value, name);
}

MsParameterConverter.ClearOmitName();

writer.WriteEndObject();
}
}
Loading

0 comments on commit 87c868c

Please sign in to comment.