From 148d67a4d4f921379b4d8cbd48ff1f1d85078f58 Mon Sep 17 00:00:00 2001 From: Oliver Peng Date: Wed, 10 Aug 2011 10:05:32 -0400 Subject: [PATCH 1/2] Add new comprehensive test cases for rdbi-mysql --- test/setup_test_db.sql | 129 +++++++++++++++++++ test/test_dbaccess_mysql.rb | 240 ++++++++++++++++++++++++++++++++++++ 2 files changed, 369 insertions(+) create mode 100644 test/setup_test_db.sql create mode 100644 test/test_dbaccess_mysql.rb diff --git a/test/setup_test_db.sql b/test/setup_test_db.sql new file mode 100644 index 0000000..fe4d69e --- /dev/null +++ b/test/setup_test_db.sql @@ -0,0 +1,129 @@ +create database mysql_test; + +use mysql_test; + +-- ----------------------------------------------------------------------- +-- Table with all kinds of numeric columns +-- ----------------------------------------------------------------------- +CREATE TABLE table1 ( + table1_id int NOT NULL, + + column1 TINYINT, + + column2 SMALLINT, + + column3 MEDIUMINT NOT NULL, + + column4 INT, + + column5 BIGINT, + + column6 DECIMAL(10,5), + + column7 FLOAT NOT NULL, + + column8 DOUBLE, + + column9 BIT(8), + + CONSTRAINT PK_table1 PRIMARY KEY(table1_id), + CONSTRAINT UQ_table1 UNIQUE( column3, column7 ) +); + + +-- ----------------------------------------------------------------------- +-- Table with all kinds of string columns +-- ----------------------------------------------------------------------- +CREATE TABLE table2 ( + table2_id int NOT NULL, + + table1_id int NOT NULL, + + column1 CHAR(10), + + column2 VARCHAR(10), + + column3 BINARY(100) NOT NULL, + + column4 VARBINARY(100), + + column5 TINYBLOB, + + column6 BLOB, + + column7 MEDIUMBLOB, + + column8 LONGBLOB, + + column9 TINYTEXT, + + column10 TEXT, + + column11 MEDIUMTEXT, + + column12 LONGTEXT, + + CONSTRAINT PK_table2 PRIMARY KEY(table2_id), + CONSTRAINT UQ_table2 UNIQUE( column2, column3 ), + CONSTRAINT FK_table2_table1 + FOREIGN KEY (table1_id) REFERENCES table1(table1_id) + ON DELETE CASCADE +); + +-- ----------------------------------------------------------------------- +-- Table with all kinds of enum and date columns +-- ----------------------------------------------------------------------- +CREATE TABLE table3 ( + table3_id int NOT NULL, + + table2_id int NOT NULL, + + column1 enum('ABC', 'DEF', 'HIJ', 'KLM', 'OPQ') NOT NULL, + + column2 DATE, + + column3 TIME, + + column4 DATETIME NOT NULL, + + column5 TIMESTAMP NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, + + column6 YEAR, + + column7 enum('ABC', 'DEF', 'HIJ', 'KLM', 'OPQ') NOT NULL, + + CONSTRAINT PK_table3 PRIMARY KEY(table3_id), + CONSTRAINT UQ_table3 UNIQUE( column4, column5 ), + CONSTRAINT FK_table3_table2 + FOREIGN KEY (table2_id) REFERENCES table2(table2_id) + ON DELETE CASCADE +); + +-- ----------------------------------------------------------------------- +-- Bridge table between table2 and table3 +-- ----------------------------------------------------------------------- +CREATE TABLE table4( + + table4_id int NOT NULL, + + -- Reference to table2 + table2_id int NOT NULL, + + -- Reference to table3 + table3_id int NOT NULL, + + column1 enum('ABC', 'DEF', 'HIJ', 'KLM', 'OPQ') NOT NULL, + + column2 VARCHAR(10), + + CONSTRAINT PK_table4 + PRIMARY KEY (table2_id, table3_id, column1), + CONSTRAINT UQ_table4_id UNIQUE(table4_id), + CONSTRAINT FK_table4_table2 + FOREIGN KEY (table2_id) REFERENCES table2(table2_id) + ON DELETE CASCADE, + CONSTRAINT FK_table4_table3 + FOREIGN KEY (table3_id) REFERENCES table3(table3_id) + ON DELETE CASCADE +); + diff --git a/test/test_dbaccess_mysql.rb b/test/test_dbaccess_mysql.rb new file mode 100644 index 0000000..e215cef --- /dev/null +++ b/test/test_dbaccess_mysql.rb @@ -0,0 +1,240 @@ + +require 'rubygems' +require 'test/unit' +require 'rdbi/driver/mysql' +require 'test/unit' + +class Test_DbaccessMySQL < Test::Unit::TestCase + # Compare one row with expecting hash data + def compare_row(data, row) + + data.each do |key, value| + + if value == "NULL" + expecting_value = nil + else + expecting_value = value + end + + if row[key].instance_of?(BigDecimal) + assert_equal(BigDecimal.new(expecting_value), row[key], "#{key} column mismatch: #{value.to_s} <> #{row[key].to_s}") + elsif row[key].instance_of?(Float) + assert_equal(expecting_value.to_f, row[key], "#{key} column mismatch: #{value.to_s} <> #{row[key].to_s}") + elsif row[key].instance_of?(Time) or row[key].instance_of?(DateTime) + assert_equal(expecting_value.to_s, row[key].strftime("%Y-%m-%d %H:%M:%S")[0, expecting_value.to_s.size], "#{key} column mismatch: #{value.to_s} <> #{row[key].strftime("'%Y-%m-%d %H:%M:%S'")}") + else + assert_equal(expecting_value.to_s, row[key].to_s.strip[0, expecting_value.to_s.size], "#{key} column mismatch: #{value.class.to_s}->#{value.to_s} <> #{row[key].class.to_s}->#{row[key].to_s}") + end + end + end + + def setup + %x{mysql -u root -e "drop database mysql_test"} + %x{mysql -u root -e "source setup_test_db.sql;"} + + base_args = { + :host => "localhost", + :hostname => "localhost", + :port => 3306, + :username => "root", + :password => "", + :database => "mysql_test", + } + + @dba = RDBI.connect(:MySQL, base_args) + end + + def test_all + data = { + "table1_id" => "1", + "column1" => "-1", + "column2" => "-1", + "column3" => "-1", + "column4" => "-1", + "column5" => "-1", + "column6" => "-1", +# "column7" => "-1.11", + "column8" => "-1.11", + "column9" => "1" + } + + @dba.execute("INSERT INTO table1 (column5, column6, table1_id, column7, column8, column9, column1, column2, column3, column4) values (-1, -1, 1, -1.11, -1.11, 1, -1, -1, -1, -1)") + + result_set = @dba.execute("select * from table1 where table1_id = 1") + assert_equal(1, result_set.count) + data.delete("column9") + compare_row(data, result_set.as(:Struct).fetch(:first)) + + data = { + "table1_id" => "1", + "column1" => "1", + "column2" => "1", + "column3" => "1", + "column4" => "1", + "column5" => "1", + "column6" => "1", +# "column7" => "1.11", + "column8" => "1.11", + "column9" => "11" + } + + @dba.execute("UPDATE table1 SET column5=1, column6=1, column7=1.11, column8=1.11, column9=11, column1=1, column2=1, column3=1, column4=1 WHERE table1_id=1") + result_set = @dba.execute("select * from table1 where table1_id = 1") + assert_equal(1, result_set.count) + data.delete("column9") + compare_row(data, result_set.as(:Struct).fetch(:first)) + + data = { + "table2_id" => "1", + "table1_id" => "1", + "column1" => "-1", + "column2" => "NULL", + "column3" => "-1", + "column4" => "-1", + "column5" => "-1", + "column6" => "-1", + "column7" => "-1.11", + "column8" => "-1.11", + "column9" => "1", + "column10" => "1", + "column11" => "1", + "column12" => "1" + } + + @dba.execute("INSERT INTO table2 (column5, column10, column6, column11, table1_id, column7, column12, table2_id, column8, column9, column1, column2, column3, column4) values ('-1', '1', '-1', '1', 1, '-1.11', '1', 1, '-1.11', '1', '-1', NULL, '-1', '-1')") + + result_set = @dba.execute("select * from table2 where table2_id = 1") + assert_equal(1, result_set.count) + compare_row(data, result_set.as(:Struct).fetch(:first)) + + data = { + "table2_id" => "1", + "column1" => "1", + "column2" => "1", + "column3" => "1", + "column4" => "1", + "column5" => "1", + "column6" => "1", + "column7" => "1.11", + "column8" => "1.11", + "column9" => "-1", + "column10" => "NULL", + "column11" => "-1", + "column12" => "-1" + } + + @dba.execute("UPDATE table2 SET column5='1', column10=NULL, column11='-1', column6='1', column12='-1', column7='1.11', column8='1.11', column9='-1', column1='1', column2='1', column3='1', column4='1' WHERE table2_id=1") + result_set = @dba.execute("select * from table2 where table2_id = 1") + assert_equal(1, result_set.count) + compare_row(data, result_set.as(:Struct).fetch(:first)) + + data = { + "table3_id" => "1", + "table2_id" => "1", + "column1" => "ABC", + "column2" => "2011-02-12", + "column3" => "12:13:16", + "column4" => "2011-02-12 12:13:16", + "column5" => "2011-02-12 12:13:16", + "column6" => "2011", + "column7" => "2" + } + + @dba.execute("INSERT INTO table3 (column5, column6, column7, table2_id, column1, column2, column3, table3_id, column4) values ('2011-02-12 12:13:16', '2011', 2, 1, 'ABC', '2011-02-12', '12:13:16', 1, '2011-02-12 12:13:16')") + +# result_set = @dba.execute("select * from table3 where table3_id = 1") +# assert_equal(1, result_set.count) +# data["column7"] = "DEF" +# data["column3"] = Time.now.strftime("%Y-%m-%d ") + data["column3"] +# compare_row(data, result_set.as(:Struct).fetch(:first)) + + data = { + "table3_id" => "1", + "table2_id" => "1", + "column1" => "3", + "column2" => "2010-02-12", + "column3" => "02:13:16", + "column4" => "2011-01-12 12:13:16", + "column5" => "2011-01-12 12:13:16", + "column6" => "2010", + "column7" => "ABC" + } + + @dba.execute("UPDATE table3 SET column5='2011-01-12 12:13:16', column6='2010', column7='ABC', table2_id=1, column1=3, column2='2010-02-12', column3='02:13:16', column4='2011-01-12 12:13:16' WHERE table3_id=1") + +# result_set = @dba.execute("select * from table3 where table3_id = 1") +# assert_equal(1, result_set.count) +# data["column1"] = "HIJ" +# data["column3"] = Time.now.strftime("%Y-%m-%d ") + data["column3"] +# compare_row(data, result_set.as(:Struct).fetch(:first)) +# + + data = { + "table4_id" => "1", + "table3_id" => "1", + "table2_id" => "1", + "column1" => "ABC", + "column2" => "abc\"\'" + } + +# @dba.execute("INSERT INTO table4 (table2_id, column1, column2, table4_id, table3_id) values (1, 'ABC', 'abc\"\'', 1, 1)") +# +# result_set = @dba.execute("select * from table4 where table4_id = 1") +# assert_equal(1, result_set.count) +# compare_row(data, result_set.as(:Struct).fetch(:first)) +# +# data = { +# "table3_id" => "1", +# "table2_id" => "1", +# "column1" => "1", +# "column2" => "def" +# } +# +# @dba.execute("UPDATE table4 SET column2='def' WHERE table2_id=1 AND column1=1 AND table3_id=1") +# +# result_set = @dba.execute("select * from table4 where table4_id = 1") +# assert_equal(1, result_set.count) +# data["column1"] = "ABC" +# compare_row(data, result_set.as(:Struct).fetch(:first)) +# +# data = { +# "table3_id" => "1", +# "table2_id" => "1", +# "column1" => "1" +# } + + @dba.execute("DELETE FROM table4 WHERE table2_id=1 AND column1=1 AND table3_id=1") + + assert_equal(0, @dba.execute("select * from table4 where table4_id = 1").count) + + data = { + "table3_id" => "1" + } + + @dba.execute("DELETE FROM table3 WHERE table3_id=1") + + assert_equal(0, @dba.execute("select * from table3 where table3_id = 1").count) + + data = { + "table2_id" => "1" + } + + @dba.execute("DELETE FROM table2 WHERE table2_id=1") + + assert_equal(0, @dba.execute("select * from table2 where table2_id = 1").count) + + data = { + "table1_id" => "1" + } + + @dba.execute("DELETE FROM table1 WHERE table1_id=1") + + assert_equal(0, @dba.execute("select * from table1 where table1_id = 1").count) + end + + def teardown + if @dba + @dba.disconnect() + end + end +end From f2baca05b8a583c61b19820c21eb5f160d016b2b Mon Sep 17 00:00:00 2001 From: Oliver Peng Date: Thu, 11 Aug 2011 10:36:41 -0400 Subject: [PATCH 2/2] Updated version --- test/test_dbaccess_mysql.rb | 76 ++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/test/test_dbaccess_mysql.rb b/test/test_dbaccess_mysql.rb index e215cef..d1517f3 100644 --- a/test/test_dbaccess_mysql.rb +++ b/test/test_dbaccess_mysql.rb @@ -53,7 +53,7 @@ def test_all "column4" => "-1", "column5" => "-1", "column6" => "-1", -# "column7" => "-1.11", + "column7" => "-1.11", "column8" => "-1.11", "column9" => "1" } @@ -73,7 +73,7 @@ def test_all "column4" => "1", "column5" => "1", "column6" => "1", -# "column7" => "1.11", + "column7" => "1.11", "column8" => "1.11", "column9" => "11" } @@ -142,11 +142,11 @@ def test_all @dba.execute("INSERT INTO table3 (column5, column6, column7, table2_id, column1, column2, column3, table3_id, column4) values ('2011-02-12 12:13:16', '2011', 2, 1, 'ABC', '2011-02-12', '12:13:16', 1, '2011-02-12 12:13:16')") -# result_set = @dba.execute("select * from table3 where table3_id = 1") -# assert_equal(1, result_set.count) -# data["column7"] = "DEF" -# data["column3"] = Time.now.strftime("%Y-%m-%d ") + data["column3"] -# compare_row(data, result_set.as(:Struct).fetch(:first)) + result_set = @dba.execute("select * from table3 where table3_id = 1") + assert_equal(1, result_set.count) + data["column7"] = "DEF" + data["column3"] = Time.now.strftime("%Y-%m-%d ") + data["column3"] + compare_row(data, result_set.as(:Struct).fetch(:first)) data = { "table3_id" => "1", @@ -162,12 +162,12 @@ def test_all @dba.execute("UPDATE table3 SET column5='2011-01-12 12:13:16', column6='2010', column7='ABC', table2_id=1, column1=3, column2='2010-02-12', column3='02:13:16', column4='2011-01-12 12:13:16' WHERE table3_id=1") -# result_set = @dba.execute("select * from table3 where table3_id = 1") -# assert_equal(1, result_set.count) -# data["column1"] = "HIJ" -# data["column3"] = Time.now.strftime("%Y-%m-%d ") + data["column3"] -# compare_row(data, result_set.as(:Struct).fetch(:first)) -# + result_set = @dba.execute("select * from table3 where table3_id = 1") + assert_equal(1, result_set.count) + data["column1"] = "HIJ" + data["column3"] = Time.now.strftime("%Y-%m-%d ") + data["column3"] + compare_row(data, result_set.as(:Struct).fetch(:first)) + data = { "table4_id" => "1", @@ -177,31 +177,31 @@ def test_all "column2" => "abc\"\'" } -# @dba.execute("INSERT INTO table4 (table2_id, column1, column2, table4_id, table3_id) values (1, 'ABC', 'abc\"\'', 1, 1)") -# -# result_set = @dba.execute("select * from table4 where table4_id = 1") -# assert_equal(1, result_set.count) -# compare_row(data, result_set.as(:Struct).fetch(:first)) -# -# data = { -# "table3_id" => "1", -# "table2_id" => "1", -# "column1" => "1", -# "column2" => "def" -# } -# -# @dba.execute("UPDATE table4 SET column2='def' WHERE table2_id=1 AND column1=1 AND table3_id=1") -# -# result_set = @dba.execute("select * from table4 where table4_id = 1") -# assert_equal(1, result_set.count) -# data["column1"] = "ABC" -# compare_row(data, result_set.as(:Struct).fetch(:first)) -# -# data = { -# "table3_id" => "1", -# "table2_id" => "1", -# "column1" => "1" -# } + @dba.execute("INSERT INTO table4 (table2_id, column1, column2, table4_id, table3_id) values (1, 'ABC', 'abc\\\"\\\'', 1, 1)") + + result_set = @dba.execute("select * from table4 where table4_id = 1") + assert_equal(1, result_set.count) + compare_row(data, result_set.as(:Struct).fetch(:first)) + + data = { + "table3_id" => "1", + "table2_id" => "1", + "column1" => "1", + "column2" => "def" + } + + @dba.execute("UPDATE table4 SET column2='def' WHERE table2_id=1 AND column1=1 AND table3_id=1") + + result_set = @dba.execute("select * from table4 where table4_id = 1") + assert_equal(1, result_set.count) + data["column1"] = "ABC" + compare_row(data, result_set.as(:Struct).fetch(:first)) + + data = { + "table3_id" => "1", + "table2_id" => "1", + "column1" => "1" + } @dba.execute("DELETE FROM table4 WHERE table2_id=1 AND column1=1 AND table3_id=1")