public async Task Should_delete_staging_table_when_empty()
        {
            var sut = new SqlTTimeoutsTarget(new TestLoggingAdapter(), connectionString, schema);

            await using var connection = new SqlConnection(connectionString);
            await connection.OpenAsync();

            await SqlTQueueCreator.CreateStagingQueue(connection, SqlConstants.TimeoutMigrationStagingTable, schema, connection.Database, preview : false);

            await sut.Complete(ExistingEndpointName);

            var sqlStatement = $"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = '{SqlConstants.TimeoutMigrationStagingTable}' AND TABLE_CATALOG = '{databaseName}'";

            await using var command = new SqlCommand(sqlStatement, connection)
                        {
                            CommandType = CommandType.Text
                        };
            var result = await command.ExecuteScalarAsync().ConfigureAwait(false) as int?;

            Assert.That(result, Is.EqualTo(0));
        }
        public async Task Should_delete_staging_queue_when_completing()
        {
            var sut          = new SqlTTimeoutsTarget(new TestLoggingAdapter(), connectionString, schema);
            var endpointName = "FakeEndpoint";

            await using var endpointTarget = await sut.PrepareTargetEndpointBatchMigrator(endpointName);

            await sut.Complete(endpointName);

            await using var connection = new SqlConnection(connectionString);
            await connection.OpenAsync();

            await using var command = connection.CreateCommand();
            command.CommandText     = string.Format(@"
   SELECT COUNT(*)
   FROM INFORMATION_SCHEMA.TABLES
   WHERE TABLE_SCHEMA = '{1}' AND TABLE_NAME = '{0}' AND TABLE_CATALOG = '{2}'
", SqlConstants.TimeoutMigrationStagingTable, schema, databaseName);
            var result = await command.ExecuteScalarAsync() as int?;

            Assert.That(Convert.ToBoolean(result), Is.False);
        }
        public async Task Should_throw_when_staging_table_not_empty()
        {
            var sut = new SqlTTimeoutsTarget(new TestLoggingAdapter(), connectionString, schema);

            await using var connection = new SqlConnection(connectionString);
            await connection.OpenAsync();

            await SqlTQueueCreator.CreateStagingQueue(connection, SqlConstants.TimeoutMigrationStagingTable, schema, connection.Database, preview : false);

            var sql =
                $"INSERT INTO {schema}.{SqlConstants.TimeoutMigrationStagingTable} VALUES('headers', NULL, DATEADD(DAY, 1, GETDATE()))";

            await using var command = new SqlCommand(sql, connection)
                        {
                            CommandType = CommandType.Text
                        };
            await command.ExecuteNonQueryAsync();

            Assert.ThrowsAsync <Exception>(async() =>
            {
                await sut.Complete(ExistingEndpointName);
            });
        }