$: << 'lib' require 'test/unit' require "dbi/sql" # ==================================================================== class TestSqlBind < Test::Unit::TestCase include DBI::SQL::BasicQuote include DBI::SQL::BasicBind def test_one assert_equal "10", bind(self, "?", [10]) assert_equal "'hi'", bind(self, "?", ["hi"]) assert_equal "I 'don''t' know", bind(self, "I ? know", ["don't"]) end def test_many assert_equal "WHERE age=12 AND name='Jim'", bind(self, "WHERE age=? AND name=?", [12, 'Jim']) end def test_too_many assert_raise (RuntimeError) { bind(self, "age=?", [10, 11]) } end def test_too_few assert_raise (RuntimeError) { bind(self, "age in (?, ?, ?)", [10, 11]) } end def test_embedded_questions assert_equal "10 ? 11", bind(self, "? ?? ?", [10, 11]) assert_equal "????", bind(self, "????????", []) end def test_questions_in_param assert_equal "WHERE c='connected?'", bind(self, "WHERE c=?", ["connected?"]) assert_equal "WHERE c='connected?' AND d='???'", bind(self, "WHERE c=? AND d=?", ["connected?", "???"]) end def test_questions_in_quotes assert_equal "WHERE c='connected?' AND d=10", bind(self, "WHERE c='connected?' AND d=?", [10]) end def test_comment_dan sql = %{--Dan's query\n--random comment\nselect column1, column2\nfrom table1\nwhere somevalue = ?} res = %{--Dan's query\n--random comment\nselect column1, column2\nfrom table1\nwhere somevalue = 10} assert_equal res, bind(self, sql, [10]) end def test_minus_bug sql = "SELECT 1 - 3" res = "SELECT 1 - 3" assert_equal res, bind(self, sql, []) end def test_minus2 sql = "SELECT * from test --Dan's query" assert_equal sql, bind(self, sql, []) end def test_slash sql = "SELECT 5 / 4" res = "SELECT 5 / 4" assert_equal res, bind(self, sql, []) end def test_much sql = <