Skip to content

Instantly share code, notes, and snippets.

@fractaledmind
Created August 4, 2023 13:44
Show Gist options
  • Save fractaledmind/9379e53530786edea70e17289e3792dd to your computer and use it in GitHub Desktop.
Save fractaledmind/9379e53530786edea70e17289e3792dd to your computer and use it in GitHub Desktop.
A reproducible bug script demonstrating that WHERE IIF doesn't behave as expected.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "sqlite3"
end
require "sqlite3"
$db = SQLite3::Database.new(":memory:")
$db.execute <<-SQL
CREATE TABLE queues(
name TEXT,
value TEXT
);
SQL
class WhereIIFTest < Minitest::Test
def test_where_iif
$db.execute("INSERT INTO queues(name, value) VALUES (?, ?), (?, ?)", ['default', 1], ['urgent', 10])
where_iif = "SELECT COUNT(*) FROM queues WHERE iif(?, name = ?, 1)"
assert_equal 2, $db.get_first_value(where_iif, nil)
assert_equal 1, $db.get_first_value(where_iif, 'default')
assert_equal 1, $db.get_first_value(where_iif, 'urgent')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment