<commit_before>RSpec::Matchers.define :convert_to_sql do |expected|
  match do |actual|
    actual.to_sql == convert_expected_sql(expected)
  end
  
  description do
    "convert to the following SQL: #{convert_expected_sql(expected)}"
  end
  
  failure_message_for_should do |actual|
    "expected generated SQL to be \n  #{convert_expected_sql(expected)}\ngot\n  #{actual.to_sql}"
  end
  
  def convert_expected_sql(expected)
    case SexyScopesSpec.database_system
    when 'mysql'
      expected.gsub('"', '`')
    when 'postgresql'
      expected.gsub('LIKE', 'ILIKE')
    else
      expected
    end<commit_msg>Normalize generated SQL in convert_to_sql matcher to avoid false positives<commit_after>RSpec::Matchers.define :convert_to_sql do |expected|
  case SexyScopesSpec.database_system
  when 'mysql'
    expected.gsub!('"', '`')
  when 'postgresql'
    expected.gsub!('LIKE', 'ILIKE')
  end

  match do |actual|
    convert_to_sql(actual) == expected
  end

  description do
    "convert to the following SQL: #{expected}"
  end

  failure_message_for_should do |actual|
    "expected generated SQL to be \n  #{expected}\ngot\n  #{convert_to_sql(actual)}"
  end

  def convert_to_sql(actual)
    normalize_sql(actual.to_sql)
  end

  # Arel < 6 seems to inserts an additionnal (harmless) space before the WHERE clause.
  # We ensure this space is removed to avoid false positives.
  def normalize_sql(sql)
    sql.gsub('  WHERE ', ' WHERE ')
{'fraction_non_alphanumeric': 0.10178817056396149, 'fraction_numerical': 0.000687757909215956, 'mean_word_length': 3.898989898989899, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4646117', 'n_tokens_mistral': 485, 'n_tokens_neox': 478, 'n_words': 126}