mysql2 0.1.4 → 0.1.5

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.
@@ -3,13 +3,17 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
3
3
  require 'active_record'
4
4
  require 'active_record/connection_adapters/mysql2_adapter'
5
5
 
6
+ class Mysql2Test2 < ActiveRecord::Base
7
+ set_table_name "mysql2_test2"
8
+ end
9
+
6
10
  describe ActiveRecord::ConnectionAdapters::Mysql2Adapter do
7
11
  it "should be able to connect" do
8
12
  lambda {
9
13
  ActiveRecord::Base.establish_connection(:adapter => 'mysql2')
10
14
  }.should_not raise_error(Mysql2::Error)
11
15
  end
12
-
16
+
13
17
  context "once connected" do
14
18
  before(:each) do
15
19
  @connection = ActiveRecord::Base.connection
@@ -20,4 +24,123 @@ describe ActiveRecord::ConnectionAdapters::Mysql2Adapter do
20
24
  @connection.execute("SELECT NOW() as n").first['n'].class.should eql(Time)
21
25
  end
22
26
  end
27
+
28
+ context "columns" do
29
+ before(:all) do
30
+ ActiveRecord::Base.establish_connection(:adapter => 'mysql2', :database => 'test')
31
+ Mysql2Test2.connection.execute %[
32
+ CREATE TABLE IF NOT EXISTS mysql2_test2 (
33
+ `id` mediumint(9) NOT NULL AUTO_INCREMENT,
34
+ `null_test` varchar(10) DEFAULT NULL,
35
+ `bit_test` bit(64) NOT NULL DEFAULT b'1',
36
+ `boolean_test` tinyint(1) DEFAULT 0,
37
+ `tiny_int_test` tinyint(4) NOT NULL DEFAULT '1',
38
+ `small_int_test` smallint(6) NOT NULL DEFAULT '1',
39
+ `medium_int_test` mediumint(9) NOT NULL DEFAULT '1',
40
+ `int_test` int(11) NOT NULL DEFAULT '1',
41
+ `big_int_test` bigint(20) NOT NULL DEFAULT '1',
42
+ `float_test` float(10,3) NOT NULL DEFAULT '1.000',
43
+ `double_test` double(10,3) NOT NULL DEFAULT '1.000',
44
+ `decimal_test` decimal(10,3) NOT NULL DEFAULT '1.000',
45
+ `date_test` date NOT NULL DEFAULT '2010-01-01',
46
+ `date_time_test` datetime NOT NULL DEFAULT '2010-01-01 00:00:00',
47
+ `timestamp_test` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
48
+ `time_test` time NOT NULL DEFAULT '00:00:00',
49
+ `year_test` year(4) NOT NULL DEFAULT '2010',
50
+ `char_test` char(10) NOT NULL DEFAULT 'abcdefghij',
51
+ `varchar_test` varchar(10) NOT NULL DEFAULT 'abcdefghij',
52
+ `binary_test` binary(10) NOT NULL DEFAULT 'abcdefghij',
53
+ `varbinary_test` varbinary(10) NOT NULL DEFAULT 'abcdefghij',
54
+ `tiny_blob_test` tinyblob NOT NULL,
55
+ `tiny_text_test` tinytext,
56
+ `blob_test` blob,
57
+ `text_test` text,
58
+ `medium_blob_test` mediumblob,
59
+ `medium_text_test` mediumtext,
60
+ `long_blob_test` longblob,
61
+ `long_text_test` longtext,
62
+ `enum_test` enum('val1','val2') NOT NULL DEFAULT 'val1',
63
+ `set_test` set('val1','val2') NOT NULL DEFAULT 'val1,val2',
64
+ PRIMARY KEY (`id`)
65
+ )
66
+ ]
67
+ Mysql2Test2.connection.execute "INSERT INTO mysql2_test2 (null_test) VALUES (NULL)"
68
+ @test_result = Mysql2Test2.connection.execute("SELECT * FROM mysql2_test2 ORDER BY id DESC LIMIT 1").first
69
+ end
70
+
71
+ after(:all) do
72
+ Mysql2Test2.connection.execute("DELETE FROM mysql2_test WHERE id=#{@test_result['id']}")
73
+ end
74
+
75
+ it "default value should be cast to the expected type of the field" do
76
+ test = Mysql2Test2.new
77
+ test.null_test.should be_nil
78
+ test.bit_test.should eql("b'1'")
79
+ test.boolean_test.should eql(false)
80
+ test.tiny_int_test.should eql(1)
81
+ test.small_int_test.should eql(1)
82
+ test.medium_int_test.should eql(1)
83
+ test.int_test.should eql(1)
84
+ test.big_int_test.should eql(1)
85
+ test.float_test.should eql('1.0000'.to_f)
86
+ test.double_test.should eql('1.0000'.to_f)
87
+ test.decimal_test.should eql(BigDecimal.new('1.0000'))
88
+ test.date_test.should eql(Date.parse('2010-01-01'))
89
+ test.date_time_test.should eql(Time.parse('2010-01-01 00:00:00'))
90
+ test.timestamp_test.should be_nil
91
+ test.time_test.class.should eql(Time)
92
+ test.year_test.should eql(2010)
93
+ test.char_test.should eql('abcdefghij')
94
+ test.varchar_test.should eql('abcdefghij')
95
+ test.binary_test.should eql('abcdefghij')
96
+ test.varbinary_test.should eql('abcdefghij')
97
+ test.tiny_blob_test.should eql("")
98
+ test.tiny_text_test.should be_nil
99
+ test.blob_test.should be_nil
100
+ test.text_test.should be_nil
101
+ test.medium_blob_test.should be_nil
102
+ test.medium_text_test.should be_nil
103
+ test.long_blob_test.should be_nil
104
+ test.long_text_test.should be_nil
105
+ test.long_blob_test.should be_nil
106
+ test.enum_test.should eql('val1')
107
+ test.set_test.should eql('val1,val2')
108
+ test.save
109
+ end
110
+
111
+ it "should have correct values when pulled from a db record" do
112
+ test = Mysql2Test2.last
113
+ test.null_test.should be_nil
114
+ test.bit_test.class.should eql(String)
115
+ test.boolean_test.should eql(false)
116
+ test.tiny_int_test.should eql(1)
117
+ test.small_int_test.should eql(1)
118
+ test.medium_int_test.should eql(1)
119
+ test.int_test.should eql(1)
120
+ test.big_int_test.should eql(1)
121
+ test.float_test.should eql('1.0000'.to_f)
122
+ test.double_test.should eql('1.0000'.to_f)
123
+ test.decimal_test.should eql(BigDecimal.new('1.0000'))
124
+ test.date_test.should eql(Date.parse('2010-01-01'))
125
+ test.date_time_test.should eql(Time.parse('2010-01-01 00:00:00'))
126
+ test.timestamp_test.class.should eql(Time)
127
+ test.time_test.class.should eql(Time)
128
+ test.year_test.should eql(2010)
129
+ test.char_test.should eql('abcdefghij')
130
+ test.varchar_test.should eql('abcdefghij')
131
+ test.binary_test.should eql('abcdefghij')
132
+ test.varbinary_test.should eql('abcdefghij')
133
+ test.tiny_blob_test.should eql("")
134
+ test.tiny_text_test.should be_nil
135
+ test.blob_test.should be_nil
136
+ test.text_test.should be_nil
137
+ test.medium_blob_test.should be_nil
138
+ test.medium_text_test.should be_nil
139
+ test.long_blob_test.should be_nil
140
+ test.long_text_test.should be_nil
141
+ test.long_blob_test.should be_nil
142
+ test.enum_test.should eql('val1')
143
+ test.set_test.should eql('val1,val2')
144
+ end
145
+ end
23
146
  end
@@ -34,6 +34,21 @@ describe Mysql2::Client do
34
34
  results[1]['Value'].class.should eql(String)
35
35
  end
36
36
 
37
+ it "should respond to #close" do
38
+ @client.should respond_to :close
39
+ end
40
+
41
+ it "should be able to close properly" do
42
+ @client.close.should be_nil
43
+ end
44
+
45
+ it "should raise an exception when closed twice" do
46
+ @client.close.should be_nil
47
+ lambda {
48
+ @client.close
49
+ }.should raise_error(Mysql2::Error)
50
+ end
51
+
37
52
  it "should respond to #query" do
38
53
  @client.should respond_to :query
39
54
  end
@@ -52,8 +52,9 @@ describe Mysql2::Result do
52
52
  @client.query "USE test"
53
53
  @client.query %[
54
54
  CREATE TABLE IF NOT EXISTS mysql2_test (
55
+ id MEDIUMINT NOT NULL AUTO_INCREMENT,
55
56
  null_test VARCHAR(10),
56
- bit_test BIT,
57
+ bit_test BIT(64),
57
58
  tiny_int_test TINYINT,
58
59
  small_int_test SMALLINT,
59
60
  medium_int_test MEDIUMINT,
@@ -80,7 +81,8 @@ describe Mysql2::Result do
80
81
  long_blob_test LONGBLOB,
81
82
  long_text_test LONGTEXT,
82
83
  enum_test ENUM('val1', 'val2'),
83
- set_test SET('val1', 'val2')
84
+ set_test SET('val1', 'val2'),
85
+ PRIMARY KEY (id)
84
86
  )
85
87
  ]
86
88
  @client.query %[
@@ -93,14 +95,18 @@ describe Mysql2::Result do
93
95
  )
94
96
 
95
97
  VALUES (
96
- NULL, 1, 1, 10, 10, 10, 10,
98
+ NULL, b'101', 1, 10, 10, 10, 10,
97
99
  10.3, 10.3, 10.3, '2010-4-4', '2010-4-4 11:44:00', '2010-4-4 11:44:00', '11:44:00',
98
100
  2009, "test", "test", "test", "test", "test",
99
101
  "test", "test", "test", "test", "test",
100
102
  "test", "test", 'val1', 'val1,val2'
101
103
  )
102
104
  ]
103
- @test_result = @client.query("SELECT * FROM mysql2_test LIMIT 1").first
105
+ @test_result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
106
+ end
107
+
108
+ after(:all) do
109
+ @client.query("DELETE FROM mysql2_test WHERE id=#{@test_result['id']}")
104
110
  end
105
111
 
106
112
  it "should return nil for a NULL value" do
@@ -108,52 +114,94 @@ describe Mysql2::Result do
108
114
  @test_result['null_test'].should eql(nil)
109
115
  end
110
116
 
111
- {
112
- 'bit_test' => 'BIT',
113
- 'tiny_int_test' => 'TINYINT',
114
- 'small_int_test' => 'SMALLINT',
115
- 'medium_int_test' => 'MEDIUMINT',
116
- 'int_test' => 'INT',
117
- 'big_int_test' => 'BIGINT',
118
- 'year_test' => 'YEAR'
119
- }.each do |field, type|
120
- it "should return a Fixnum for #{type}" do
121
- [Fixnum, Bignum].should include(@test_result[field].class)
122
- end
117
+ it "should return Fixnum for a BIT value" do
118
+ @test_result['bit_test'].class.should eql(String)
119
+ @test_result['bit_test'].should eql("\000\000\000\000\000\000\000\005")
123
120
  end
124
121
 
125
- {
126
- 'decimal_test' => 'DECIMAL'
127
- }.each do |field, type|
128
- it "should return a Fixnum for #{type}" do
129
- @test_result[field].class.should eql(BigDecimal)
130
- end
122
+ it "should return Fixnum for a TINYINT value" do
123
+ [Fixnum, Bignum].should include(@test_result['tiny_int_test'].class)
124
+ @test_result['tiny_int_test'].should eql(1)
131
125
  end
132
126
 
133
- {
134
- 'float_test' => 'FLOAT',
135
- 'double_test' => 'DOUBLE'
136
- }.each do |field, type|
137
- it "should return a Float for #{type}" do
138
- @test_result[field].class.should eql(Float)
139
- end
127
+ it "should return Fixnum for a SMALLINT value" do
128
+ [Fixnum, Bignum].should include(@test_result['small_int_test'].class)
129
+ @test_result['small_int_test'].should eql(10)
140
130
  end
141
131
 
142
- {
143
- 'date_test' => 'DATE',
144
- 'date_time_test' => 'DATETIME',
145
- 'timestamp_test' => 'TIMESTAMP',
146
- 'time_test' => 'TIME'
147
- }.each do |field, type|
148
- it "should return a Time for #{type}" do
149
- @test_result[field].class.should eql(Time)
150
- end
132
+ it "should return Fixnum for a MEDIUMINT value" do
133
+ [Fixnum, Bignum].should include(@test_result['medium_int_test'].class)
134
+ @test_result['medium_int_test'].should eql(10)
135
+ end
136
+
137
+ it "should return Fixnum for an INT value" do
138
+ [Fixnum, Bignum].should include(@test_result['int_test'].class)
139
+ @test_result['int_test'].should eql(10)
140
+ end
141
+
142
+ it "should return Fixnum for a BIGINT value" do
143
+ [Fixnum, Bignum].should include(@test_result['big_int_test'].class)
144
+ @test_result['big_int_test'].should eql(10)
145
+ end
146
+
147
+ it "should return Fixnum for a YEAR value" do
148
+ [Fixnum, Bignum].should include(@test_result['year_test'].class)
149
+ @test_result['year_test'].should eql(2009)
150
+ end
151
+
152
+ it "should return BigDecimal for a DECIMAL value" do
153
+ @test_result['decimal_test'].class.should eql(BigDecimal)
154
+ @test_result['decimal_test'].should eql(10.3)
155
+ end
156
+
157
+ it "should return Float for a FLOAT value" do
158
+ @test_result['float_test'].class.should eql(Float)
159
+ @test_result['float_test'].should eql(10.3)
160
+ end
161
+
162
+ it "should return Float for a DOUBLE value" do
163
+ @test_result['double_test'].class.should eql(Float)
164
+ @test_result['double_test'].should eql(10.3)
165
+ end
166
+
167
+ it "should return Time for a DATETIME value" do
168
+ @test_result['date_time_test'].class.should eql(Time)
169
+ @test_result['date_time_test'].strftime("%F %T").should eql('2010-04-04 11:44:00')
170
+ end
171
+
172
+ it "should return Time for a TIMESTAMP value" do
173
+ @test_result['timestamp_test'].class.should eql(Time)
174
+ @test_result['timestamp_test'].strftime("%F %T").should eql('2010-04-04 11:44:00')
175
+ end
176
+
177
+ it "should return Time for a TIME value" do
178
+ @test_result['time_test'].class.should eql(Time)
179
+ @test_result['time_test'].strftime("%F %T").should eql('2000-01-01 11:44:00')
180
+ end
181
+
182
+ it "should return Date for a DATE value" do
183
+ @test_result['date_test'].class.should eql(Date)
184
+ @test_result['date_test'].strftime("%F").should eql('2010-04-04')
185
+ end
186
+
187
+ it "should return String for an ENUM value" do
188
+ @test_result['enum_test'].class.should eql(String)
189
+ @test_result['enum_test'].should eql('val1')
190
+ end
191
+
192
+ it "should return String for a SET value" do
193
+ @test_result['set_test'].class.should eql(String)
194
+ @test_result['set_test'].should eql('val1,val2')
195
+ end
196
+
197
+ it "should return String for a BINARY value" do
198
+ @test_result['binary_test'].class.should eql(String)
199
+ @test_result['binary_test'].should eql("test#{"\000"*6}")
151
200
  end
152
201
 
153
202
  {
154
203
  'char_test' => 'CHAR',
155
204
  'varchar_test' => 'VARCHAR',
156
- 'binary_test' => 'BINARY',
157
205
  'varbinary_test' => 'VARBINARY',
158
206
  'tiny_blob_test' => 'TINYBLOB',
159
207
  'tiny_text_test' => 'TINYTEXT',
@@ -162,12 +210,11 @@ describe Mysql2::Result do
162
210
  'medium_blob_test' => 'MEDIUMBLOB',
163
211
  'medium_text_test' => 'MEDIUMTEXT',
164
212
  'long_blob_test' => 'LONGBLOB',
165
- 'long_text_test' => 'LONGTEXT',
166
- 'enum_test' => 'ENUM',
167
- 'set_test' => 'SET'
213
+ 'long_text_test' => 'LONGTEXT'
168
214
  }.each do |field, type|
169
215
  it "should return a String for #{type}" do
170
216
  @test_result[field].class.should eql(String)
217
+ @test_result[field].should eql("test")
171
218
  end
172
219
  end
173
220
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 4
9
- version: 0.1.4
8
+ - 5
9
+ version: 0.1.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brian Lopez
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-23 00:00:00 -07:00
17
+ date: 2010-05-12 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -35,7 +35,9 @@ files:
35
35
  - VERSION
36
36
  - benchmark/active_record.rb
37
37
  - benchmark/escape.rb
38
- - benchmark/query.rb
38
+ - benchmark/query_with_mysql_casting.rb
39
+ - benchmark/query_without_mysql_casting.rb
40
+ - benchmark/sequel.rb
39
41
  - benchmark/setup_db.rb
40
42
  - examples/eventmachine.rb
41
43
  - ext/extconf.rb
@@ -45,6 +47,7 @@ files:
45
47
  - lib/arel/engines/sql/compilers/mysql2_compiler.rb
46
48
  - lib/mysql2.rb
47
49
  - lib/mysql2/em.rb
50
+ - lib/sequel/adapters/mysql2.rb
48
51
  - mysql2.gemspec
49
52
  - spec/active_record/active_record_spec.rb
50
53
  - spec/em/em_spec.rb