Skip to content

Commit

Permalink
Query: Move some of the Sql Assertion to use AssertSql function (#16916)
Browse files Browse the repository at this point in the history
  • Loading branch information
smitpatel authored Aug 2, 2019
1 parent 252f5c5 commit 3c5e998
Show file tree
Hide file tree
Showing 8 changed files with 600 additions and 646 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ var results
.ToList();

Assert.Equal(0, results.Count);
Assert.Equal(

AssertSql(
@"SELECT [m].[Int]
FROM [MappedNullableDataTypes] AS [m]
WHERE ([m].[TimeSpanAsTime] = '00:01:02') AND [m].[TimeSpanAsTime] IS NOT NULL",
Sql,
ignoreLineEndingDifferences: true);
WHERE ([m].[TimeSpanAsTime] = '00:01:02') AND [m].[TimeSpanAsTime] IS NOT NULL");
}
}

Expand All @@ -65,12 +64,10 @@ public void Translate_array_length()
.Select(p => p.BytesAsImage.Length)
.FirstOrDefault();

Assert.Equal(
AssertSql(
@"SELECT TOP(1) CAST(DATALENGTH([p].[BytesAsImage]) AS int)
FROM [MappedDataTypesWithIdentity] AS [p]
WHERE CAST(DATALENGTH([p].[BytesAsImage]) AS int) = 0",
Sql,
ignoreLineEndingDifferences: true);
WHERE CAST(DATALENGTH([p].[BytesAsImage]) AS int) = 0");
}
}

Expand All @@ -88,14 +85,12 @@ var results
.ToList();

Assert.Equal(0, results.Count);
Assert.Equal(
AssertSql(
@"@__timeSpan_0='02:01:00' (Nullable = true)
SELECT [m].[Int]
FROM [MappedNullableDataTypes] AS [m]
WHERE (([m].[TimeSpanAsTime] = @__timeSpan_0) AND ([m].[TimeSpanAsTime] IS NOT NULL AND @__timeSpan_0 IS NOT NULL)) OR ([m].[TimeSpanAsTime] IS NULL AND @__timeSpan_0 IS NULL)",
Sql,
ignoreLineEndingDifferences: true);
WHERE (([m].[TimeSpanAsTime] = @__timeSpan_0) AND ([m].[TimeSpanAsTime] IS NOT NULL AND @__timeSpan_0 IS NOT NULL)) OR ([m].[TimeSpanAsTime] IS NULL AND @__timeSpan_0 IS NULL)");
}
}

Expand All @@ -113,14 +108,12 @@ var results
.ToList();

Assert.Equal(0, results.Count);
Assert.Equal(
AssertSql(
@"@__timeSpan_1='02:01:00' (Nullable = true)
SELECT [m].[Int]
FROM [MappedNullableDataTypes] AS [m]
WHERE (DATEDIFF(HOUR, [m].[TimeSpanAsTime], @__timeSpan_1) = 0) AND DATEDIFF(HOUR, [m].[TimeSpanAsTime], @__timeSpan_1) IS NOT NULL",
Sql,
ignoreLineEndingDifferences: true);
WHERE (DATEDIFF(HOUR, [m].[TimeSpanAsTime], @__timeSpan_1) = 0) AND DATEDIFF(HOUR, [m].[TimeSpanAsTime], @__timeSpan_1) IS NOT NULL");
}
}

Expand All @@ -138,14 +131,12 @@ var results
.ToList();

Assert.Equal(0, results.Count);
Assert.Equal(
AssertSql(
@"@__timeSpan_1='02:01:00' (Nullable = true)
SELECT [m].[Int]
FROM [MappedNullableDataTypes] AS [m]
WHERE (DATEDIFF(MINUTE, [m].[TimeSpanAsTime], @__timeSpan_1) = 0) AND DATEDIFF(MINUTE, [m].[TimeSpanAsTime], @__timeSpan_1) IS NOT NULL",
Sql,
ignoreLineEndingDifferences: true);
WHERE (DATEDIFF(MINUTE, [m].[TimeSpanAsTime], @__timeSpan_1) = 0) AND DATEDIFF(MINUTE, [m].[TimeSpanAsTime], @__timeSpan_1) IS NOT NULL");
}
}

Expand All @@ -163,14 +154,12 @@ var results
.ToList();

Assert.Equal(0, results.Count);
Assert.Equal(
AssertSql(
@"@__timeSpan_1='02:01:00' (Nullable = true)
SELECT [m].[Int]
FROM [MappedNullableDataTypes] AS [m]
WHERE (DATEDIFF(SECOND, [m].[TimeSpanAsTime], @__timeSpan_1) = 0) AND DATEDIFF(SECOND, [m].[TimeSpanAsTime], @__timeSpan_1) IS NOT NULL",
Sql,
ignoreLineEndingDifferences: true);
WHERE (DATEDIFF(SECOND, [m].[TimeSpanAsTime], @__timeSpan_1) = 0) AND DATEDIFF(SECOND, [m].[TimeSpanAsTime], @__timeSpan_1) IS NOT NULL");
}
}

Expand All @@ -188,14 +177,12 @@ var results
.ToList();

Assert.Equal(0, results.Count);
Assert.Equal(
AssertSql(
@"@__timeSpan_1='02:01:00' (Nullable = true)
SELECT [m].[Int]
FROM [MappedNullableDataTypes] AS [m]
WHERE (DATEDIFF(MILLISECOND, [m].[TimeSpanAsTime], @__timeSpan_1) = 0) AND DATEDIFF(MILLISECOND, [m].[TimeSpanAsTime], @__timeSpan_1) IS NOT NULL",
Sql,
ignoreLineEndingDifferences: true);
WHERE (DATEDIFF(MILLISECOND, [m].[TimeSpanAsTime], @__timeSpan_1) = 0) AND DATEDIFF(MILLISECOND, [m].[TimeSpanAsTime], @__timeSpan_1) IS NOT NULL");
}
}

Expand All @@ -213,14 +200,12 @@ var results
.ToList();

Assert.Equal(0, results.Count);
Assert.Equal(
AssertSql(
@"@__timeSpan_1='02:01:00' (Nullable = true)
SELECT [m].[Int]
FROM [MappedNullableDataTypes] AS [m]
WHERE (DATEDIFF(MICROSECOND, [m].[TimeSpanAsTime], @__timeSpan_1) = 0) AND DATEDIFF(MICROSECOND, [m].[TimeSpanAsTime], @__timeSpan_1) IS NOT NULL",
Sql,
ignoreLineEndingDifferences: true);
WHERE (DATEDIFF(MICROSECOND, [m].[TimeSpanAsTime], @__timeSpan_1) = 0) AND DATEDIFF(MICROSECOND, [m].[TimeSpanAsTime], @__timeSpan_1) IS NOT NULL");
}
}

Expand All @@ -238,14 +223,12 @@ var results
.ToList();

Assert.Equal(0, results.Count);
Assert.Equal(
AssertSql(
@"@__timeSpan_1='02:01:00' (Nullable = true)
SELECT [m].[Int]
FROM [MappedNullableDataTypes] AS [m]
WHERE (DATEDIFF(NANOSECOND, [m].[TimeSpanAsTime], @__timeSpan_1) = 0) AND DATEDIFF(NANOSECOND, [m].[TimeSpanAsTime], @__timeSpan_1) IS NOT NULL",
Sql,
ignoreLineEndingDifferences: true);
WHERE (DATEDIFF(NANOSECOND, [m].[TimeSpanAsTime], @__timeSpan_1) = 0) AND DATEDIFF(NANOSECOND, [m].[TimeSpanAsTime], @__timeSpan_1) IS NOT NULL");
}
}

Expand Down Expand Up @@ -3051,7 +3034,8 @@ const string query
return actual;
}

private string Sql => Fixture.TestSqlLoggerFactory.Sql;
private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

public class BuiltInDataTypesSqlServerFixture : BuiltInDataTypesFixtureBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public override void ConcurrencyCheckAttribute_throws_if_value_in_database_chang
base.ConcurrencyCheckAttribute_throws_if_value_in_database_changed();

// Issue #15285
/*Assert.Equal(
/*AssertSql(
@"SELECT TOP(1) [r].[UniqueNo], [r].[MaxLengthProperty], [r].[Name], [r].[RowVersion], [t].[UniqueNo], [t].[Details_Name], [t0].[UniqueNo], [t0].[AdditionalDetails_Name]
FROM [Sample] AS [r]
LEFT JOIN (
Expand Down Expand Up @@ -194,16 +194,14 @@ WHERE [r.AdditionalDetails].[AdditionalDetails_Name] IS NOT NULL
SET NOCOUNT ON;
UPDATE [Sample] SET [Name] = @p0, [RowVersion] = @p1
WHERE [UniqueNo] = @p2 AND [RowVersion] = @p3;
SELECT @@ROWCOUNT;",
Sql,
ignoreLineEndingDifferences: true);*/
SELECT @@ROWCOUNT;");*/
}

public override void DatabaseGeneratedAttribute_autogenerates_values_when_set_to_identity()
{
base.DatabaseGeneratedAttribute_autogenerates_values_when_set_to_identity();

Assert.Equal(
AssertSql(
@"@p0='' (Size = 10)
@p1='Third' (Nullable = false) (Size = 4000)
@p2='00000000-0000-0000-0000-000000000003'
Expand All @@ -215,16 +213,14 @@ INSERT INTO [Sample] ([MaxLengthProperty], [Name], [RowVersion], [AdditionalDeta
VALUES (@p0, @p1, @p2, @p3, @p4);
SELECT [UniqueNo]
FROM [Sample]
WHERE @@ROWCOUNT = 1 AND [UniqueNo] = scope_identity();",
Sql,
ignoreLineEndingDifferences: true);
WHERE @@ROWCOUNT = 1 AND [UniqueNo] = scope_identity();");
}

public override void MaxLengthAttribute_throws_while_inserting_value_longer_than_max_length()
{
base.MaxLengthAttribute_throws_while_inserting_value_longer_than_max_length();

Assert.Equal(
AssertSql(
@"@p0='Short' (Size = 10)
@p1='ValidString' (Nullable = false) (Size = 4000)
@p2='00000000-0000-0000-0000-000000000001'
Expand All @@ -236,9 +232,9 @@ INSERT INTO [Sample] ([MaxLengthProperty], [Name], [RowVersion], [AdditionalDeta
VALUES (@p0, @p1, @p2, @p3, @p4);
SELECT [UniqueNo]
FROM [Sample]
WHERE @@ROWCOUNT = 1 AND [UniqueNo] = scope_identity();
@p0='VeryVeryVeryVeryVeryVeryLongString' (Size = -1)
WHERE @@ROWCOUNT = 1 AND [UniqueNo] = scope_identity();",
//
@"@p0='VeryVeryVeryVeryVeryVeryLongString' (Size = -1)
@p1='ValidString' (Nullable = false) (Size = 4000)
@p2='00000000-0000-0000-0000-000000000002'
@p3='Third Additional Name' (Size = 4000)
Expand All @@ -249,29 +245,40 @@ INSERT INTO [Sample] ([MaxLengthProperty], [Name], [RowVersion], [AdditionalDeta
VALUES (@p0, @p1, @p2, @p3, @p4);
SELECT [UniqueNo]
FROM [Sample]
WHERE @@ROWCOUNT = 1 AND [UniqueNo] = scope_identity();",
Sql,
ignoreLineEndingDifferences: true);
WHERE @@ROWCOUNT = 1 AND [UniqueNo] = scope_identity();");
}

public override void RequiredAttribute_for_navigation_throws_while_inserting_null_value()
{
base.RequiredAttribute_for_navigation_throws_while_inserting_null_value();

Assert.Contains(
"@p1='1'" + _eol,
Sql);
AssertSql(
@"@p0='' (DbType = Int32)
@p1='1'
Assert.Contains(
"@p1='' (Nullable = false) (DbType = Int32)" + _eol,
Sql);
SET NOCOUNT ON;
INSERT INTO [BookDetails] ([AdditionalBookDetailsId], [AnotherBookId])
VALUES (@p0, @p1);
SELECT [Id]
FROM [BookDetails]
WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();",
//
@"@p0='' (DbType = Int32)
@p1='' (Nullable = false) (DbType = Int32)
SET NOCOUNT ON;
INSERT INTO [BookDetails] ([AdditionalBookDetailsId], [AnotherBookId])
VALUES (@p0, @p1);
SELECT [Id]
FROM [BookDetails]
WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();");
}

public override void RequiredAttribute_for_property_throws_while_inserting_null_value()
{
base.RequiredAttribute_for_property_throws_while_inserting_null_value();

Assert.Equal(
AssertSql(
@"@p0='' (Size = 10)
@p1='ValidString' (Nullable = false) (Size = 4000)
@p2='00000000-0000-0000-0000-000000000001'
Expand All @@ -283,9 +290,9 @@ INSERT INTO [Sample] ([MaxLengthProperty], [Name], [RowVersion], [AdditionalDeta
VALUES (@p0, @p1, @p2, @p3, @p4);
SELECT [UniqueNo]
FROM [Sample]
WHERE @@ROWCOUNT = 1 AND [UniqueNo] = scope_identity();
@p0='' (Size = 10)
WHERE @@ROWCOUNT = 1 AND [UniqueNo] = scope_identity();",
//
@"@p0='' (Size = 10)
@p1='' (Nullable = false) (Size = 4000)
@p2='00000000-0000-0000-0000-000000000002'
@p3='Two' (Size = 4000)
Expand All @@ -296,35 +303,31 @@ INSERT INTO [Sample] ([MaxLengthProperty], [Name], [RowVersion], [AdditionalDeta
VALUES (@p0, @p1, @p2, @p3, @p4);
SELECT [UniqueNo]
FROM [Sample]
WHERE @@ROWCOUNT = 1 AND [UniqueNo] = scope_identity();",
Sql,
ignoreLineEndingDifferences: true);
WHERE @@ROWCOUNT = 1 AND [UniqueNo] = scope_identity();");
}

public override void StringLengthAttribute_throws_while_inserting_value_longer_than_max_length()
{
base.StringLengthAttribute_throws_while_inserting_value_longer_than_max_length();

Assert.Equal(
AssertSql(
@"@p0='ValidString' (Size = 16)
SET NOCOUNT ON;
INSERT INTO [Two] ([Data])
VALUES (@p0);
SELECT [Id], [Timestamp]
FROM [Two]
WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
@p0='ValidButLongString' (Size = -1)
WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();",
//
@"@p0='ValidButLongString' (Size = -1)
SET NOCOUNT ON;
INSERT INTO [Two] ([Data])
VALUES (@p0);
SELECT [Id], [Timestamp]
FROM [Two]
WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();",
Sql,
ignoreLineEndingDifferences: true);
WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();");
}

public override void TimestampAttribute_throws_if_value_in_database_changed()
Expand All @@ -336,8 +339,8 @@ public override void TimestampAttribute_throws_if_value_in_database_changed()
}

private static readonly string _eol = Environment.NewLine;

private string Sql => Fixture.TestSqlLoggerFactory.Sql;
private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

public class DataAnnotationSqlServerFixture : DataAnnotationFixtureBase
{
Expand Down
Loading

0 comments on commit 3c5e998

Please sign in to comment.