Skip to content
43 changes: 43 additions & 0 deletions test/Integration/SqlTriggerBindingIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,5 +612,48 @@ void TestExceptionMessageSeen(object sender, DataReceivedEventArgs e)
await changesTask;

}

/// <summary>
/// Tests that the GlobalState table has LastAccessTime column.
/// </summary>
/// <remarks>We call StartAsync which initializes the GlobalState and then drop the LastAccessTime column from the table and recall the StartAsync to check if the GlobalState has the column.</remarks>
[Fact]
public async void LastAccessTimeColumnTest()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good to try and give tests names that say what the test is doing

Suggested change
public async void LastAccessTimeColumnTest()
public async void LastAccessTimeColumn_Created_OnStartup()

{

this.SetChangeTrackingForTable("Products");
string userFunctionId = "func-id";
IConfiguration configuration = new ConfigurationBuilder().Build();
var listener = new SqlTriggerListener<Product>(this.DbConnectionString, "dbo.Products", userFunctionId, Mock.Of<ITriggeredFunctionExecutor>(), Mock.Of<ILogger>(), configuration);
await listener.StartAsync(CancellationToken.None);
// Cancel immediately so the listener doesn't start processing the changes
await listener.StopAsync(CancellationToken.None);
//Check if LastAccessTime column exists in the GlobalState table
Assert.Equal(1, this.ExecuteScalar("SELECT 1 FROM sys.columns WHERE Name = N'LastAccessTime' AND Object_ID = Object_ID(N'[az_func].[GlobalState]')"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Assert.True so you can give it a message in case of failure

// Delete default constraint(s) on LastAccessTime column before dropping it
string deleteDefaultContraint = @"DECLARE @sql NVARCHAR(MAX)
WHILE 1=1
BEGIN
SELECT TOP 1 @sql = N'ALTER TABLE [az_func].[GlobalState] DROP CONSTRAINT ['+dc.NAME+N']'
FROM sys.default_constraints dc
JOIN sys.columns c
ON c.default_object_id = dc.object_id
WHERE dc.parent_object_id = OBJECT_ID('[az_func].[GlobalState]')
AND c.name = N'LastAccessTime'
IF @@ROWCOUNT = 0 BREAK
EXEC (@sql)
END";
this.ExecuteNonQuery(deleteDefaultContraint);
// Delete the LastAccessTime column from GlobalState table.
this.ExecuteNonQuery($"ALTER TABLE [az_func].[GlobalState] DROP COLUMN LastAccessTime");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the constant value from SqlTriggerConstants for the table name everywhere you reference it.

I would also suggest creating a constant for the LastAccessTime column name


await listener.StartAsync(CancellationToken.None);
// Cancel immediately so the listener doesn't start processing the changes
await listener.StopAsync(CancellationToken.None);

//Check if LastAccessTime column exists in the GlobalState table
Assert.Equal(1, this.ExecuteScalar("SELECT 1 FROM sys.columns WHERE Name = N'LastAccessTime' AND Object_ID = Object_ID(N'[az_func].[GlobalState]')"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert.True


}
}
}