using System.Data.Common;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests;
///
/// Records the text of every reader command a context executes, so a query-shape test can
/// assert what a repository method actually reads (e.g. that a delete never SELECTs a fat
/// JSON column).
///
public sealed class SqlReaderCommandRecorder : DbCommandInterceptor
{
private readonly List _commands = new();
/// The text of every reader command recorded since the last .
public IReadOnlyList Commands => _commands;
/// Clears all recorded commands.
public void Reset() => _commands.Clear();
///
/// True when any recorded reader command is a SELECT that projects .
///
/// Column name to look for.
/// Whether a recorded SELECT read the column.
public bool AnySelectReads(string column) =>
_commands.Any(c => c.TrimStart().StartsWith("SELECT", StringComparison.OrdinalIgnoreCase)
&& c.Contains(column, StringComparison.Ordinal));
///
public override InterceptionResult ReaderExecuting(
DbCommand command, CommandEventData eventData, InterceptionResult result)
{
_commands.Add(command.CommandText);
return base.ReaderExecuting(command, eventData, result);
}
///
public override ValueTask> ReaderExecutingAsync(
DbCommand command, CommandEventData eventData, InterceptionResult result,
CancellationToken cancellationToken = default)
{
_commands.Add(command.CommandText);
return base.ReaderExecutingAsync(command, eventData, result, cancellationToken);
}
}