rails-dbd-mysql 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,26 @@
1
+ DBDConfig.set_testbase(:mysql, Class.new(Test::Unit::TestCase) do
2
+ def dbtype
3
+ "mysql"
4
+ end
5
+
6
+ def test_base
7
+ assert_equal(@dbh.driver_name, "Mysql")
8
+ assert_kind_of(DBI::DBD::Mysql::Database, @dbh.instance_variable_get(:@handle))
9
+ end
10
+
11
+ def set_base_dbh
12
+ config = DBDConfig.get_config["mysql"]
13
+ @dbh = DBI.connect("dbi:Mysql:"+config["dbname"], config["username"], config["password"], { })
14
+ end
15
+
16
+ def setup
17
+ set_base_dbh
18
+ DBDConfig.inject_sql(@dbh, dbtype, "dbd/mysql/up.sql")
19
+ end
20
+
21
+ def teardown
22
+ DBDConfig.inject_sql(@dbh, dbtype, "dbd/mysql/down.sql")
23
+ @dbh.disconnect
24
+ end
25
+ end
26
+ )
@@ -0,0 +1,19 @@
1
+ drop view view_names;
2
+ ---
3
+ drop table names;
4
+ ---
5
+ drop table blob_test;
6
+ ---
7
+ drop table boolean_test;
8
+ ---
9
+ drop table time_test;
10
+ ---
11
+ drop table timestamp_test;
12
+ ---
13
+ drop table bit_test;
14
+ ---
15
+ drop table field_types_test;
16
+ ---
17
+ drop table db_specific_types_test;
18
+ ---
19
+ drop table precision_test;
@@ -0,0 +1,18 @@
1
+ class TestMySQLBlob < DBDConfig.testbase(:mysql)
2
+ def test_blob_round_trip
3
+ data =(0..255).collect{|n| n.chr}.join("")
4
+ sql = "INSERT INTO blob_test (name, data) VALUES (?, ?)"
5
+
6
+ @dbh.do(sql, 'test1', DBI::Binary.new(data))
7
+ @dbh.do(sql, 'test2', data)
8
+
9
+ @dbh.prepare(sql) do |sth|
10
+ sth.execute('test3', DBI::Binary.new(data))
11
+ sth.execute('test4', data)
12
+ end
13
+
14
+ @dbh.select_all("SELECT name, data FROM blob_test") do |name, fetch_data|
15
+ assert_equal fetch_data, data
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ class TestNewMethods < DBDConfig.testbase(:mysql)
2
+ def test_database_name
3
+ assert_nothing_raised do
4
+ assert_equal DBDConfig.get_config[dbtype]['dbname'], @dbh.database_name
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,111 @@
1
+ class TestMysqlPatches < DBDConfig.testbase(:mysql)
2
+ def test_exception_on_aggregate
3
+ assert_nothing_raised do
4
+ sth = @dbh.prepare("select sum(age) from names")
5
+ sth.execute
6
+ row = sth.fetch
7
+ assert_equal(70.0, row[0])
8
+ sth.finish
9
+
10
+ sth = @dbh.prepare("select count(*) from names")
11
+ sth.execute
12
+ assert_equal([3], sth.fetch)
13
+ sth.finish
14
+ end
15
+ end
16
+
17
+ def test_timestamps
18
+ timestamp = "04-06-1978 06:00:00"
19
+ datestamp = "04-06-1978"
20
+ date = Date.strptime(datestamp, "%m-%d-%Y")
21
+ stamp = DateTime.strptime(timestamp, "%m-%d-%Y %H:%M:%S")
22
+ assert_nothing_raised do
23
+ @sth = @dbh.prepare("insert into db_specific_types_test (ts) values (?)")
24
+ @sth.execute(stamp)
25
+ @sth.finish
26
+ end
27
+
28
+ assert_nothing_raised do
29
+ @sth = @dbh.prepare("select ts from db_specific_types_test where ts is not null")
30
+ @sth.execute
31
+
32
+ newstamp = @sth.fetch[0]
33
+
34
+ assert_equal(newstamp, stamp)
35
+ assert_equal(newstamp.strftime("%m-%d-%Y %H:%M:%S"), timestamp)
36
+ @sth.finish
37
+ end
38
+
39
+ assert_nothing_raised do
40
+ @sth = @dbh.prepare("insert into db_specific_types_test (dt) values (?)")
41
+ @sth.execute(date)
42
+ @sth.finish
43
+ end
44
+
45
+ assert_nothing_raised do
46
+ @sth = @dbh.prepare("select dt from db_specific_types_test where dt is not null")
47
+ @sth.execute
48
+
49
+ newdate = @sth.fetch[0]
50
+
51
+ assert_equal(newdate, date)
52
+ assert_equal(newdate.strftime("%m-%d-%Y"), datestamp)
53
+ @sth.finish
54
+ end
55
+ end
56
+
57
+ # FIXME when the spec is more solid, this should be in the general tests
58
+ def test_columns
59
+ assert_nothing_raised do
60
+ assert_equal [
61
+ {
62
+ :name =>"foo",
63
+ :default =>"1",
64
+ :primary =>true,
65
+ :scale =>nil,
66
+ :sql_type =>4,
67
+ :nullable =>false,
68
+ :indexed =>true,
69
+ :precision =>11,
70
+ :type_name =>"int",
71
+ :unique =>true
72
+ }
73
+ ], @dbh.columns("field_types_test")
74
+ end
75
+
76
+ assert_nothing_raised do
77
+ sth = @dbh.prepare("insert into field_types_test (foo) values (?)")
78
+ sth.execute(2)
79
+ sth.finish
80
+ end
81
+
82
+ assert_nothing_raised do
83
+ sth = @dbh.prepare("select * from field_types_test")
84
+ sth.execute
85
+ row = sth.fetch
86
+ columns = sth.column_info
87
+ sth.finish
88
+
89
+ assert_equal [2], row
90
+ assert_equal [
91
+ {
92
+ :dbi_type => DBI::Type::Integer,
93
+ :name =>"foo",
94
+ :mysql_type_name =>"INT",
95
+ :mysql_max_length =>1,
96
+ :primary =>true,
97
+ :scale =>0,
98
+ :mysql_flags =>49155,
99
+ :sql_type =>4,
100
+ :nullable =>false,
101
+ :mysql_type =>3,
102
+ :indexed =>true,
103
+ :mysql_length =>11,
104
+ :precision =>11,
105
+ :type_name =>"INTEGER",
106
+ :unique =>true
107
+ }
108
+ ], columns
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,28 @@
1
+ create table names (
2
+ name varchar(255),
3
+ age integer
4
+ ) Engine=InnoDB;
5
+ ---
6
+ insert into names (name, age) values ('Joe', 19);
7
+ ---
8
+ insert into names (name, age) values ('Jim', 30);
9
+ ---
10
+ insert into names (name, age) values ('Bob', 21);
11
+ ---
12
+ create table precision_test (text_field varchar(20) primary key not null, integer_field integer, decimal_field decimal(2,1), numeric_field numeric(30,6));
13
+ ---
14
+ CREATE TABLE blob_test (name VARCHAR(30), data BLOB) Engine=InnoDB;
15
+ ---
16
+ create view view_names as select * from names;
17
+ ---
18
+ create table boolean_test (num integer, mybool boolean) Engine=InnoDB;
19
+ ---
20
+ create table time_test (mytime time) Engine=InnoDB;
21
+ ---
22
+ create table timestamp_test (mytimestamp timestamp) Engine=InnoDB;
23
+ ---
24
+ create table bit_test (mybit bit) Engine=InnoDB;
25
+ ---
26
+ create table field_types_test (foo integer not null primary key default 1);
27
+ ---
28
+ create table db_specific_types_test (ts timestamp, dt date);
@@ -0,0 +1,131 @@
1
+ require 'rubygems'
2
+ gem 'test-unit'
3
+ # figure out what tests to run
4
+ require 'yaml'
5
+ require 'test/unit/testsuite'
6
+ require 'test/unit/ui/console/testrunner'
7
+
8
+ if File.basename(Dir.pwd) == "test"
9
+ $:.unshift('../lib')
10
+ else
11
+ $:.unshift('lib')
12
+ end
13
+
14
+ module Test::Unit::Assertions
15
+ def build_message(head, template=nil, *arguments)
16
+ template += "\n" + "DATABASE: " + dbtype
17
+ template &&= template.chomp
18
+ return AssertionMessage.new(head, template, arguments)
19
+ end
20
+ end
21
+
22
+ class Class
23
+ def name=(klass_name)
24
+ @name = klass_name
25
+ end
26
+
27
+ def name
28
+ return @name || super
29
+ end
30
+ end
31
+
32
+ module DBDConfig
33
+ @testbase = { }
34
+ @current_dbtype = nil
35
+
36
+ def self.get_config
37
+ config = nil
38
+
39
+ begin
40
+ config = YAML.load_file(File.join(ENV["HOME"], ".ruby-dbi.test-config.yaml"))
41
+ rescue Exception => e
42
+ config = { }
43
+ config["dbtypes"] = [ ]
44
+ end
45
+
46
+ return config
47
+ end
48
+
49
+ def self.inject_sql(dbh, dbtype, file)
50
+ # splits by --- in the file, strips newlines and the semicolons.
51
+ # this way we can still manually import the file, but use it with our
52
+ # drivers for client-independent injection.
53
+ File.open(file).read.split(/\n*---\n*/, -1).collect { |x| x.gsub!(/\n/, ''); x.sub(/;\z/, '') }.each do |stmt|
54
+ tmp = STDERR.dup
55
+ STDERR.reopen('sql.log', 'a')
56
+ begin
57
+ dbh.commit rescue nil
58
+ dbh["AutoCommit"] = true rescue nil
59
+ dbh.do(stmt)
60
+ dbh.commit unless dbtype == 'sqlite3'
61
+ rescue Exception => e
62
+ tmp.puts "Error injecting '#{stmt}' for db #{dbtype}"
63
+ tmp.puts "Error: #{e.message}"
64
+ end
65
+ STDERR.reopen(tmp)
66
+ end
67
+ end
68
+
69
+ def self.current_dbtype
70
+ @current_dbtype
71
+ end
72
+
73
+ def self.current_dbtype=(setting)
74
+ @current_dbtype = setting
75
+ end
76
+
77
+ def self.testbase(klass_name)
78
+ return @testbase[klass_name]
79
+ end
80
+
81
+ def self.set_testbase(klass_name, klass)
82
+ @testbase[klass_name] = klass
83
+ klass.name = klass_name.to_s
84
+ end
85
+
86
+ def self.suite
87
+ @suite ||= []
88
+ end
89
+ end
90
+
91
+ if __FILE__ == $0
92
+ Dir.chdir("..") if File.basename(Dir.pwd) == "test"
93
+ $LOAD_PATH.unshift(File.join(Dir.pwd, "lib"))
94
+ Dir.chdir("test") rescue nil
95
+
96
+ begin
97
+ require 'dbi'
98
+ rescue LoadError => e
99
+ begin
100
+ require 'rubygems'
101
+ gem 'dbi'
102
+ require 'dbi'
103
+ rescue LoadError => e
104
+ abort "DBI must already be installed or must come with this package for tests to work."
105
+ end
106
+ end
107
+
108
+ Deprecate.set_action(proc { })
109
+
110
+ config = DBDConfig.get_config
111
+
112
+ config["dbtypes"] = ENV["DBTYPES"].split(/\s+/) if ENV["DBTYPES"]
113
+
114
+ if config and config["dbtypes"]
115
+ config["dbtypes"].each do |dbtype|
116
+ unless config[dbtype]
117
+ warn "#{dbtype} is selected for testing but not configured; see test/DBD_TESTS"
118
+ next
119
+ end
120
+
121
+ # base.rb is special, see DBD_TESTS
122
+ require "dbd/#{dbtype}/base.rb"
123
+ Dir["dbd/#{dbtype}/test*.rb"].each { |file| require file }
124
+ # run the general tests
125
+ DBDConfig.current_dbtype = dbtype.to_sym
126
+ Dir["dbd/general/test*.rb"].each { |file| load file; @class.name = file; DBDConfig.suite << @class }
127
+ end
128
+ elsif !config["dbtypes"]
129
+ warn "Please see test/DBD_TESTS for information on configuring DBD tests."
130
+ end
131
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-dbd-mysql
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Raphael Costa
14
+ - Erik Hollensbe
15
+ - Christopher Maujean
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2010-08-10 00:00:00 -03:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: rails-dbi
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ hash: 31
32
+ segments:
33
+ - 0
34
+ - 1
35
+ - 2
36
+ version: 0.1.2
37
+ type: :runtime
38
+ version_requirements: *id001
39
+ - !ruby/object:Gem::Dependency
40
+ name: mysql
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description: MySQL DBI DBD, Leverages 'mysql' low-level driver
54
+ email: ruby-dbi-users@rubyforge.org
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - README
61
+ - LICENSE
62
+ - ChangeLog
63
+ files:
64
+ - test/dbd/general/test_database.rb
65
+ - test/dbd/general/test_statement.rb
66
+ - test/dbd/general/test_types.rb
67
+ - test/dbd/mysql/test_blob.rb
68
+ - test/dbd/mysql/test_new_methods.rb
69
+ - test/dbd/mysql/up.sql
70
+ - test/dbd/mysql/base.rb
71
+ - test/dbd/mysql/down.sql
72
+ - test/dbd/mysql/test_patches.rb
73
+ - lib/dbd/Mysql.rb
74
+ - lib/dbd/mysql/driver.rb
75
+ - lib/dbd/mysql/statement.rb
76
+ - lib/dbd/mysql/database.rb
77
+ - test/DBD_TESTS
78
+ - README
79
+ - LICENSE
80
+ - ChangeLog
81
+ - test/ts_dbd.rb
82
+ has_rdoc: true
83
+ homepage: http://www.rubyforge.org/projects/ruby-dbi
84
+ licenses: []
85
+
86
+ post_install_message:
87
+ rdoc_options: []
88
+
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 55
97
+ segments:
98
+ - 1
99
+ - 8
100
+ - 0
101
+ version: 1.8.0
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.3.7
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: MySQL DBI DBD, Leverages 'mysql' low-level driver
118
+ test_files:
119
+ - test/ts_dbd.rb