mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
update tests for mysql2 support
This commit is contained in:
1
Gemfile
1
Gemfile
@@ -35,6 +35,7 @@ platforms :ruby do
|
||||
group :db do
|
||||
gem "pg", ">= 0.9.0"
|
||||
gem "mysql", ">= 2.8.1"
|
||||
gem "mysql2", :path => 'git://github.com/brianmario/mysql2.git'
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -24,14 +24,14 @@ def run_without_aborting(*tasks)
|
||||
abort "Errors running #{errors.join(', ')}" if errors.any?
|
||||
end
|
||||
|
||||
desc 'Run mysql, sqlite, and postgresql tests by default'
|
||||
desc 'Run mysql, mysql2, sqlite, and postgresql tests by default'
|
||||
task :default => :test
|
||||
|
||||
desc 'Run mysql, sqlite, and postgresql tests'
|
||||
desc 'Run mysql, mysql2, sqlite, and postgresql tests'
|
||||
task :test do
|
||||
tasks = defined?(JRUBY_VERSION) ?
|
||||
%w(test_jdbcmysql test_jdbcsqlite3 test_jdbcpostgresql) :
|
||||
%w(test_mysql test_sqlite3 test_postgresql)
|
||||
%w(test_mysql test_mysql2 test_sqlite3 test_postgresql)
|
||||
run_without_aborting(*tasks)
|
||||
end
|
||||
|
||||
@@ -39,15 +39,15 @@ namespace :test do
|
||||
task :isolated do
|
||||
tasks = defined?(JRUBY_VERSION) ?
|
||||
%w(isolated_test_jdbcmysql isolated_test_jdbcsqlite3 isolated_test_jdbcpostgresql) :
|
||||
%w(isolated_test_mysql isolated_test_sqlite3 isolated_test_postgresql)
|
||||
%w(isolated_test_mysql isolated_test_mysql2 isolated_test_sqlite3 isolated_test_postgresql)
|
||||
run_without_aborting(*tasks)
|
||||
end
|
||||
end
|
||||
|
||||
%w( mysql postgresql sqlite3 firebird db2 oracle sybase openbase frontbase jdbcmysql jdbcpostgresql jdbcsqlite3 jdbcderby jdbch2 jdbchsqldb ).each do |adapter|
|
||||
%w( mysql mysql2 postgresql sqlite3 firebird db2 oracle sybase openbase frontbase jdbcmysql jdbcpostgresql jdbcsqlite3 jdbcderby jdbch2 jdbchsqldb ).each do |adapter|
|
||||
Rake::TestTask.new("test_#{adapter}") { |t|
|
||||
connection_path = "test/connections/#{adapter =~ /jdbc/ ? 'jdbc' : 'native'}_#{adapter}"
|
||||
adapter_short = adapter == 'db2' ? adapter : adapter[/^[a-z]+/]
|
||||
adapter_short = adapter == 'db2' ? adapter : adapter[/^[a-z0-9]+/]
|
||||
t.libs << "test" << connection_path
|
||||
t.test_files = (Dir.glob( "test/cases/**/*_test.rb" ).reject {
|
||||
|x| x =~ /\/adapters\//
|
||||
@@ -59,7 +59,7 @@ end
|
||||
|
||||
task "isolated_test_#{adapter}" do
|
||||
connection_path = "test/connections/#{adapter =~ /jdbc/ ? 'jdbc' : 'native'}_#{adapter}"
|
||||
adapter_short = adapter == 'db2' ? adapter : adapter[/^[a-z]+/]
|
||||
adapter_short = adapter == 'db2' ? adapter : adapter[/^[a-z0-9]+/]
|
||||
puts [adapter, adapter_short, connection_path].inspect
|
||||
ruby = File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME'))
|
||||
(Dir["test/cases/**/*_test.rb"].reject {
|
||||
|
||||
@@ -42,7 +42,7 @@ class ActiveSchemaTest < ActiveRecord::TestCase
|
||||
assert_equal "DROP TABLE `people`", drop_table(:people)
|
||||
end
|
||||
|
||||
if current_adapter?(:MysqlAdapter)
|
||||
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
||||
def test_create_mysql_database_with_encoding
|
||||
assert_equal "CREATE DATABASE `matt` DEFAULT CHARACTER SET `utf8`", create_database(:matt)
|
||||
assert_equal "CREATE DATABASE `aimonetti` DEFAULT CHARACTER SET `latin1`", create_database(:aimonetti, {:charset => 'latin1'})
|
||||
|
||||
125
activerecord/test/cases/adapters/mysql2/active_schema_test.rb
Normal file
125
activerecord/test/cases/adapters/mysql2/active_schema_test.rb
Normal file
@@ -0,0 +1,125 @@
|
||||
require "cases/helper"
|
||||
|
||||
class ActiveSchemaTest < ActiveRecord::TestCase
|
||||
def setup
|
||||
ActiveRecord::ConnectionAdapters::Mysql2Adapter.class_eval do
|
||||
alias_method :execute_without_stub, :execute
|
||||
remove_method :execute
|
||||
def execute(sql, name = nil) return sql end
|
||||
end
|
||||
end
|
||||
|
||||
def teardown
|
||||
ActiveRecord::ConnectionAdapters::Mysql2Adapter.class_eval do
|
||||
remove_method :execute
|
||||
alias_method :execute, :execute_without_stub
|
||||
end
|
||||
end
|
||||
|
||||
def test_add_index
|
||||
# add_index calls index_name_exists? which can't work since execute is stubbed
|
||||
ActiveRecord::ConnectionAdapters::Mysql2Adapter.send(:define_method, :index_name_exists?) do |*|
|
||||
false
|
||||
end
|
||||
expected = "CREATE INDEX `index_people_on_last_name` ON `people` (`last_name`)"
|
||||
assert_equal expected, add_index(:people, :last_name, :length => nil)
|
||||
|
||||
expected = "CREATE INDEX `index_people_on_last_name` ON `people` (`last_name`(10))"
|
||||
assert_equal expected, add_index(:people, :last_name, :length => 10)
|
||||
|
||||
expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`(15))"
|
||||
assert_equal expected, add_index(:people, [:last_name, :first_name], :length => 15)
|
||||
|
||||
expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`)"
|
||||
assert_equal expected, add_index(:people, [:last_name, :first_name], :length => {:last_name => 15})
|
||||
|
||||
expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`(10))"
|
||||
assert_equal expected, add_index(:people, [:last_name, :first_name], :length => {:last_name => 15, :first_name => 10})
|
||||
ActiveRecord::ConnectionAdapters::Mysql2Adapter.send(:remove_method, :index_name_exists?)
|
||||
end
|
||||
|
||||
def test_drop_table
|
||||
assert_equal "DROP TABLE `people`", drop_table(:people)
|
||||
end
|
||||
|
||||
if current_adapter?(:Mysql2Adapter)
|
||||
def test_create_mysql_database_with_encoding
|
||||
assert_equal "CREATE DATABASE `matt` DEFAULT CHARACTER SET `utf8`", create_database(:matt)
|
||||
assert_equal "CREATE DATABASE `aimonetti` DEFAULT CHARACTER SET `latin1`", create_database(:aimonetti, {:charset => 'latin1'})
|
||||
assert_equal "CREATE DATABASE `matt_aimonetti` DEFAULT CHARACTER SET `big5` COLLATE `big5_chinese_ci`", create_database(:matt_aimonetti, {:charset => :big5, :collation => :big5_chinese_ci})
|
||||
end
|
||||
|
||||
def test_recreate_mysql_database_with_encoding
|
||||
create_database(:luca, {:charset => 'latin1'})
|
||||
assert_equal "CREATE DATABASE `luca` DEFAULT CHARACTER SET `latin1`", recreate_database(:luca, {:charset => 'latin1'})
|
||||
end
|
||||
end
|
||||
|
||||
def test_add_column
|
||||
assert_equal "ALTER TABLE `people` ADD `last_name` varchar(255)", add_column(:people, :last_name, :string)
|
||||
end
|
||||
|
||||
def test_add_column_with_limit
|
||||
assert_equal "ALTER TABLE `people` ADD `key` varchar(32)", add_column(:people, :key, :string, :limit => 32)
|
||||
end
|
||||
|
||||
def test_drop_table_with_specific_database
|
||||
assert_equal "DROP TABLE `otherdb`.`people`", drop_table('otherdb.people')
|
||||
end
|
||||
|
||||
def test_add_timestamps
|
||||
with_real_execute do
|
||||
begin
|
||||
ActiveRecord::Base.connection.create_table :delete_me do |t|
|
||||
end
|
||||
ActiveRecord::Base.connection.add_timestamps :delete_me
|
||||
assert column_present?('delete_me', 'updated_at', 'datetime')
|
||||
assert column_present?('delete_me', 'created_at', 'datetime')
|
||||
ensure
|
||||
ActiveRecord::Base.connection.drop_table :delete_me rescue nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_remove_timestamps
|
||||
with_real_execute do
|
||||
begin
|
||||
ActiveRecord::Base.connection.create_table :delete_me do |t|
|
||||
t.timestamps
|
||||
end
|
||||
ActiveRecord::Base.connection.remove_timestamps :delete_me
|
||||
assert !column_present?('delete_me', 'updated_at', 'datetime')
|
||||
assert !column_present?('delete_me', 'created_at', 'datetime')
|
||||
ensure
|
||||
ActiveRecord::Base.connection.drop_table :delete_me rescue nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def with_real_execute
|
||||
#we need to actually modify some data, so we make execute point to the original method
|
||||
ActiveRecord::ConnectionAdapters::Mysql2Adapter.class_eval do
|
||||
alias_method :execute_with_stub, :execute
|
||||
remove_method :execute
|
||||
alias_method :execute, :execute_without_stub
|
||||
end
|
||||
yield
|
||||
ensure
|
||||
#before finishing, we restore the alias to the mock-up method
|
||||
ActiveRecord::ConnectionAdapters::Mysql2Adapter.class_eval do
|
||||
remove_method :execute
|
||||
alias_method :execute, :execute_with_stub
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def method_missing(method_symbol, *arguments)
|
||||
ActiveRecord::Base.connection.send(method_symbol, *arguments)
|
||||
end
|
||||
|
||||
def column_present?(table_name, column_name, type)
|
||||
results = ActiveRecord::Base.connection.select_all("SHOW FIELDS FROM #{table_name} LIKE '#{column_name}'")
|
||||
results.first && results.first['Type'] == type
|
||||
end
|
||||
end
|
||||
42
activerecord/test/cases/adapters/mysql2/connection_test.rb
Normal file
42
activerecord/test/cases/adapters/mysql2/connection_test.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
require "cases/helper"
|
||||
|
||||
class MysqlConnectionTest < ActiveRecord::TestCase
|
||||
def setup
|
||||
super
|
||||
@connection = ActiveRecord::Base.connection
|
||||
end
|
||||
|
||||
def test_no_automatic_reconnection_after_timeout
|
||||
assert @connection.active?
|
||||
@connection.update('set @@wait_timeout=1')
|
||||
sleep 2
|
||||
assert !@connection.active?
|
||||
end
|
||||
|
||||
def test_successful_reconnection_after_timeout_with_manual_reconnect
|
||||
assert @connection.active?
|
||||
@connection.update('set @@wait_timeout=1')
|
||||
sleep 2
|
||||
@connection.reconnect!
|
||||
assert @connection.active?
|
||||
end
|
||||
|
||||
def test_successful_reconnection_after_timeout_with_verify
|
||||
assert @connection.active?
|
||||
@connection.update('set @@wait_timeout=1')
|
||||
sleep 2
|
||||
@connection.verify!
|
||||
assert @connection.active?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def run_without_connection
|
||||
original_connection = ActiveRecord::Base.remove_connection
|
||||
begin
|
||||
yield original_connection
|
||||
ensure
|
||||
ActiveRecord::Base.establish_connection(original_connection)
|
||||
end
|
||||
end
|
||||
end
|
||||
176
activerecord/test/cases/adapters/mysql2/reserved_word_test.rb
Normal file
176
activerecord/test/cases/adapters/mysql2/reserved_word_test.rb
Normal file
@@ -0,0 +1,176 @@
|
||||
require "cases/helper"
|
||||
|
||||
class Group < ActiveRecord::Base
|
||||
Group.table_name = 'group'
|
||||
belongs_to :select, :class_name => 'Select'
|
||||
has_one :values
|
||||
end
|
||||
|
||||
class Select < ActiveRecord::Base
|
||||
Select.table_name = 'select'
|
||||
has_many :groups
|
||||
end
|
||||
|
||||
class Values < ActiveRecord::Base
|
||||
Values.table_name = 'values'
|
||||
end
|
||||
|
||||
class Distinct < ActiveRecord::Base
|
||||
Distinct.table_name = 'distinct'
|
||||
has_and_belongs_to_many :selects
|
||||
has_many :values, :through => :groups
|
||||
end
|
||||
|
||||
# a suite of tests to ensure the ConnectionAdapters#MysqlAdapter can handle tables with
|
||||
# reserved word names (ie: group, order, values, etc...)
|
||||
class MysqlReservedWordTest < ActiveRecord::TestCase
|
||||
def setup
|
||||
@connection = ActiveRecord::Base.connection
|
||||
|
||||
# we call execute directly here (and do similar below) because ActiveRecord::Base#create_table()
|
||||
# will fail with these table names if these test cases fail
|
||||
|
||||
create_tables_directly 'group'=>'id int auto_increment primary key, `order` varchar(255), select_id int',
|
||||
'select'=>'id int auto_increment primary key',
|
||||
'values'=>'id int auto_increment primary key, group_id int',
|
||||
'distinct'=>'id int auto_increment primary key',
|
||||
'distincts_selects'=>'distinct_id int, select_id int'
|
||||
end
|
||||
|
||||
def teardown
|
||||
drop_tables_directly ['group', 'select', 'values', 'distinct', 'distincts_selects', 'order']
|
||||
end
|
||||
|
||||
# create tables with reserved-word names and columns
|
||||
def test_create_tables
|
||||
assert_nothing_raised {
|
||||
@connection.create_table :order do |t|
|
||||
t.column :group, :string
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
# rename tables with reserved-word names
|
||||
def test_rename_tables
|
||||
assert_nothing_raised { @connection.rename_table(:group, :order) }
|
||||
end
|
||||
|
||||
# alter column with a reserved-word name in a table with a reserved-word name
|
||||
def test_change_columns
|
||||
assert_nothing_raised { @connection.change_column_default(:group, :order, 'whatever') }
|
||||
#the quoting here will reveal any double quoting issues in change_column's interaction with the column method in the adapter
|
||||
assert_nothing_raised { @connection.change_column('group', 'order', :Int, :default => 0) }
|
||||
assert_nothing_raised { @connection.rename_column(:group, :order, :values) }
|
||||
end
|
||||
|
||||
# dump structure of table with reserved word name
|
||||
def test_structure_dump
|
||||
assert_nothing_raised { @connection.structure_dump }
|
||||
end
|
||||
|
||||
# introspect table with reserved word name
|
||||
def test_introspect
|
||||
assert_nothing_raised { @connection.columns(:group) }
|
||||
assert_nothing_raised { @connection.indexes(:group) }
|
||||
end
|
||||
|
||||
#fixtures
|
||||
self.use_instantiated_fixtures = true
|
||||
self.use_transactional_fixtures = false
|
||||
|
||||
#fixtures :group
|
||||
|
||||
def test_fixtures
|
||||
f = create_test_fixtures :select, :distinct, :group, :values, :distincts_selects
|
||||
|
||||
assert_nothing_raised {
|
||||
f.each do |x|
|
||||
x.delete_existing_fixtures
|
||||
end
|
||||
}
|
||||
|
||||
assert_nothing_raised {
|
||||
f.each do |x|
|
||||
x.insert_fixtures
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
#activerecord model class with reserved-word table name
|
||||
def test_activerecord_model
|
||||
create_test_fixtures :select, :distinct, :group, :values, :distincts_selects
|
||||
x = nil
|
||||
assert_nothing_raised { x = Group.new }
|
||||
x.order = 'x'
|
||||
assert_nothing_raised { x.save }
|
||||
x.order = 'y'
|
||||
assert_nothing_raised { x.save }
|
||||
assert_nothing_raised { y = Group.find_by_order('y') }
|
||||
assert_nothing_raised { y = Group.find(1) }
|
||||
x = Group.find(1)
|
||||
end
|
||||
|
||||
# has_one association with reserved-word table name
|
||||
def test_has_one_associations
|
||||
create_test_fixtures :select, :distinct, :group, :values, :distincts_selects
|
||||
v = nil
|
||||
assert_nothing_raised { v = Group.find(1).values }
|
||||
assert_equal 2, v.id
|
||||
end
|
||||
|
||||
# belongs_to association with reserved-word table name
|
||||
def test_belongs_to_associations
|
||||
create_test_fixtures :select, :distinct, :group, :values, :distincts_selects
|
||||
gs = nil
|
||||
assert_nothing_raised { gs = Select.find(2).groups }
|
||||
assert_equal gs.length, 2
|
||||
assert(gs.collect{|x| x.id}.sort == [2, 3])
|
||||
end
|
||||
|
||||
# has_and_belongs_to_many with reserved-word table name
|
||||
def test_has_and_belongs_to_many
|
||||
create_test_fixtures :select, :distinct, :group, :values, :distincts_selects
|
||||
s = nil
|
||||
assert_nothing_raised { s = Distinct.find(1).selects }
|
||||
assert_equal s.length, 2
|
||||
assert(s.collect{|x|x.id}.sort == [1, 2])
|
||||
end
|
||||
|
||||
# activerecord model introspection with reserved-word table and column names
|
||||
def test_activerecord_introspection
|
||||
assert_nothing_raised { Group.table_exists? }
|
||||
assert_nothing_raised { Group.columns }
|
||||
end
|
||||
|
||||
# Calculations
|
||||
def test_calculations_work_with_reserved_words
|
||||
assert_nothing_raised { Group.count }
|
||||
end
|
||||
|
||||
def test_associations_work_with_reserved_words
|
||||
assert_nothing_raised { Select.find(:all, :include => [:groups]) }
|
||||
end
|
||||
|
||||
#the following functions were added to DRY test cases
|
||||
|
||||
private
|
||||
# custom fixture loader, uses Fixtures#create_fixtures and appends base_path to the current file's path
|
||||
def create_test_fixtures(*fixture_names)
|
||||
Fixtures.create_fixtures(FIXTURES_ROOT + "/reserved_words", fixture_names)
|
||||
end
|
||||
|
||||
# custom drop table, uses execute on connection to drop a table if it exists. note: escapes table_name
|
||||
def drop_tables_directly(table_names, connection = @connection)
|
||||
table_names.each do |name|
|
||||
connection.execute("DROP TABLE IF EXISTS `#{name}`")
|
||||
end
|
||||
end
|
||||
|
||||
# custom create table, uses execute on connection to create a table, note: escapes table_name, does NOT escape columns
|
||||
def create_tables_directly (tables, connection = @connection)
|
||||
tables.each do |table_name, column_properties|
|
||||
connection.execute("CREATE TABLE `#{table_name}` ( #{column_properties} )")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -32,7 +32,7 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
|
||||
|
||||
def test_belongs_to_with_primary_key_joins_on_correct_column
|
||||
sql = Client.joins(:firm_with_primary_key).to_sql
|
||||
if current_adapter?(:MysqlAdapter)
|
||||
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
||||
assert_no_match(/`firm_with_primary_keys_companies`\.`id`/, sql)
|
||||
assert_match(/`firm_with_primary_keys_companies`\.`name`/, sql)
|
||||
elsif current_adapter?(:OracleAdapter)
|
||||
|
||||
@@ -109,8 +109,13 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
||||
record = con.select_rows(sql).last
|
||||
assert_not_nil record[2]
|
||||
assert_not_nil record[3]
|
||||
assert_match %r{\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}}, record[2]
|
||||
assert_match %r{\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}}, record[3]
|
||||
if current_adapter?(:Mysql2Adapter)
|
||||
assert_match %r{\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}}, record[2].to_s(:db)
|
||||
assert_match %r{\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}}, record[3].to_s(:db)
|
||||
else
|
||||
assert_match %r{\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}}, record[2]
|
||||
assert_match %r{\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}}, record[3]
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_record_timestamp_for_join_table_only_if_timestamp_should_be_recorded
|
||||
|
||||
@@ -52,6 +52,263 @@ class BasicsTest < ActiveRecord::TestCase
|
||||
assert Topic.table_exists?
|
||||
end
|
||||
|
||||
def test_set_attributes
|
||||
topic = Topic.find(1)
|
||||
topic.attributes = { "title" => "Budget", "author_name" => "Jason" }
|
||||
topic.save
|
||||
assert_equal("Budget", topic.title)
|
||||
assert_equal("Jason", topic.author_name)
|
||||
assert_equal(topics(:first).author_email_address, Topic.find(1).author_email_address)
|
||||
end
|
||||
|
||||
def test_set_attributes_without_hash
|
||||
topic = Topic.new
|
||||
assert_nothing_raised { topic.attributes = '' }
|
||||
end
|
||||
|
||||
def test_integers_as_nil
|
||||
test = AutoId.create('value' => '')
|
||||
assert_nil AutoId.find(test.id).value
|
||||
end
|
||||
|
||||
def test_set_attributes_with_block
|
||||
topic = Topic.new do |t|
|
||||
t.title = "Budget"
|
||||
t.author_name = "Jason"
|
||||
end
|
||||
|
||||
assert_equal("Budget", topic.title)
|
||||
assert_equal("Jason", topic.author_name)
|
||||
end
|
||||
|
||||
def test_respond_to?
|
||||
topic = Topic.find(1)
|
||||
assert_respond_to topic, "title"
|
||||
assert_respond_to topic, "title?"
|
||||
assert_respond_to topic, "title="
|
||||
assert_respond_to topic, :title
|
||||
assert_respond_to topic, :title?
|
||||
assert_respond_to topic, :title=
|
||||
assert_respond_to topic, "author_name"
|
||||
assert_respond_to topic, "attribute_names"
|
||||
assert !topic.respond_to?("nothingness")
|
||||
assert !topic.respond_to?(:nothingness)
|
||||
end
|
||||
|
||||
def test_array_content
|
||||
topic = Topic.new
|
||||
topic.content = %w( one two three )
|
||||
topic.save
|
||||
|
||||
assert_equal(%w( one two three ), Topic.find(topic.id).content)
|
||||
end
|
||||
|
||||
def test_read_attributes_before_type_cast
|
||||
category = Category.new({:name=>"Test categoty", :type => nil})
|
||||
category_attrs = {"name"=>"Test categoty", "type" => nil, "categorizations_count" => nil}
|
||||
assert_equal category_attrs , category.attributes_before_type_cast
|
||||
end
|
||||
|
||||
if current_adapter?(:MysqlAdapter)
|
||||
def test_read_attributes_before_type_cast_on_boolean
|
||||
bool = Booleantest.create({ "value" => false })
|
||||
assert_equal "0", bool.reload.attributes_before_type_cast["value"]
|
||||
end
|
||||
end
|
||||
|
||||
if current_adapter?(:Mysql2Adapter)
|
||||
def test_read_attributes_before_type_cast_on_boolean
|
||||
bool = Booleantest.create({ "value" => false })
|
||||
assert_equal 0, bool.reload.attributes_before_type_cast["value"]
|
||||
end
|
||||
end
|
||||
|
||||
def test_read_attributes_before_type_cast_on_datetime
|
||||
developer = Developer.find(:first)
|
||||
# Oracle adapter returns Time before type cast
|
||||
if current_adapter?(:OracleAdapter)
|
||||
assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"].to_s(:db)
|
||||
|
||||
developer.created_at = "345643456"
|
||||
assert_equal developer.created_at_before_type_cast, "345643456"
|
||||
assert_equal developer.created_at, nil
|
||||
|
||||
developer.created_at = "2010-03-21T21:23:32+01:00"
|
||||
assert_equal developer.created_at_before_type_cast, "2010-03-21T21:23:32+01:00"
|
||||
assert_equal developer.created_at, Time.parse("2010-03-21T21:23:32+01:00")
|
||||
elsif current_adapter?(:Mysql2Adapter)
|
||||
assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"].to_s(:db)
|
||||
else
|
||||
assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"]
|
||||
end
|
||||
end
|
||||
|
||||
def test_hash_content
|
||||
topic = Topic.new
|
||||
topic.content = { "one" => 1, "two" => 2 }
|
||||
topic.save
|
||||
|
||||
assert_equal 2, Topic.find(topic.id).content["two"]
|
||||
|
||||
topic.content_will_change!
|
||||
topic.content["three"] = 3
|
||||
topic.save
|
||||
|
||||
assert_equal 3, Topic.find(topic.id).content["three"]
|
||||
end
|
||||
|
||||
def test_update_array_content
|
||||
topic = Topic.new
|
||||
topic.content = %w( one two three )
|
||||
|
||||
topic.content.push "four"
|
||||
assert_equal(%w( one two three four ), topic.content)
|
||||
|
||||
topic.save
|
||||
|
||||
topic = Topic.find(topic.id)
|
||||
topic.content << "five"
|
||||
assert_equal(%w( one two three four five ), topic.content)
|
||||
end
|
||||
|
||||
def test_case_sensitive_attributes_hash
|
||||
# DB2 is not case-sensitive
|
||||
return true if current_adapter?(:DB2Adapter)
|
||||
|
||||
assert_equal @loaded_fixtures['computers']['workstation'].to_hash, Computer.find(:first).attributes
|
||||
end
|
||||
|
||||
def test_hashes_not_mangled
|
||||
new_topic = { :title => "New Topic" }
|
||||
new_topic_values = { :title => "AnotherTopic" }
|
||||
|
||||
topic = Topic.new(new_topic)
|
||||
assert_equal new_topic[:title], topic.title
|
||||
|
||||
topic.attributes= new_topic_values
|
||||
assert_equal new_topic_values[:title], topic.title
|
||||
end
|
||||
|
||||
def test_create_through_factory
|
||||
topic = Topic.create("title" => "New Topic")
|
||||
topicReloaded = Topic.find(topic.id)
|
||||
assert_equal(topic, topicReloaded)
|
||||
end
|
||||
|
||||
def test_write_attribute
|
||||
topic = Topic.new
|
||||
topic.send(:write_attribute, :title, "Still another topic")
|
||||
assert_equal "Still another topic", topic.title
|
||||
|
||||
topic.send(:write_attribute, "title", "Still another topic: part 2")
|
||||
assert_equal "Still another topic: part 2", topic.title
|
||||
end
|
||||
|
||||
def test_read_attribute
|
||||
topic = Topic.new
|
||||
topic.title = "Don't change the topic"
|
||||
assert_equal "Don't change the topic", topic.send(:read_attribute, "title")
|
||||
assert_equal "Don't change the topic", topic["title"]
|
||||
|
||||
assert_equal "Don't change the topic", topic.send(:read_attribute, :title)
|
||||
assert_equal "Don't change the topic", topic[:title]
|
||||
end
|
||||
|
||||
def test_read_attribute_when_false
|
||||
topic = topics(:first)
|
||||
topic.approved = false
|
||||
assert !topic.approved?, "approved should be false"
|
||||
topic.approved = "false"
|
||||
assert !topic.approved?, "approved should be false"
|
||||
end
|
||||
|
||||
def test_read_attribute_when_true
|
||||
topic = topics(:first)
|
||||
topic.approved = true
|
||||
assert topic.approved?, "approved should be true"
|
||||
topic.approved = "true"
|
||||
assert topic.approved?, "approved should be true"
|
||||
end
|
||||
|
||||
def test_read_write_boolean_attribute
|
||||
topic = Topic.new
|
||||
# puts ""
|
||||
# puts "New Topic"
|
||||
# puts topic.inspect
|
||||
topic.approved = "false"
|
||||
# puts "Expecting false"
|
||||
# puts topic.inspect
|
||||
assert !topic.approved?, "approved should be false"
|
||||
topic.approved = "false"
|
||||
# puts "Expecting false"
|
||||
# puts topic.inspect
|
||||
assert !topic.approved?, "approved should be false"
|
||||
topic.approved = "true"
|
||||
# puts "Expecting true"
|
||||
# puts topic.inspect
|
||||
assert topic.approved?, "approved should be true"
|
||||
topic.approved = "true"
|
||||
# puts "Expecting true"
|
||||
# puts topic.inspect
|
||||
assert topic.approved?, "approved should be true"
|
||||
# puts ""
|
||||
end
|
||||
|
||||
def test_query_attribute_string
|
||||
[nil, "", " "].each do |value|
|
||||
assert_equal false, Topic.new(:author_name => value).author_name?
|
||||
end
|
||||
|
||||
assert_equal true, Topic.new(:author_name => "Name").author_name?
|
||||
end
|
||||
|
||||
def test_query_attribute_number
|
||||
[nil, 0, "0"].each do |value|
|
||||
assert_equal false, Developer.new(:salary => value).salary?
|
||||
end
|
||||
|
||||
assert_equal true, Developer.new(:salary => 1).salary?
|
||||
assert_equal true, Developer.new(:salary => "1").salary?
|
||||
end
|
||||
|
||||
def test_query_attribute_boolean
|
||||
[nil, "", false, "false", "f", 0].each do |value|
|
||||
assert_equal false, Topic.new(:approved => value).approved?
|
||||
end
|
||||
|
||||
[true, "true", "1", 1].each do |value|
|
||||
assert_equal true, Topic.new(:approved => value).approved?
|
||||
end
|
||||
end
|
||||
|
||||
def test_query_attribute_with_custom_fields
|
||||
object = Company.find_by_sql(<<-SQL).first
|
||||
SELECT c1.*, c2.ruby_type as string_value, c2.rating as int_value
|
||||
FROM companies c1, companies c2
|
||||
WHERE c1.firm_id = c2.id
|
||||
AND c1.id = 2
|
||||
SQL
|
||||
|
||||
assert_equal "Firm", object.string_value
|
||||
assert object.string_value?
|
||||
|
||||
object.string_value = " "
|
||||
assert !object.string_value?
|
||||
|
||||
assert_equal 1, object.int_value.to_i
|
||||
assert object.int_value?
|
||||
|
||||
object.int_value = "0"
|
||||
assert !object.int_value?
|
||||
end
|
||||
|
||||
def test_non_attribute_access_and_assignment
|
||||
topic = Topic.new
|
||||
assert !topic.respond_to?("mumbo")
|
||||
assert_raise(NoMethodError) { topic.mumbo }
|
||||
assert_raise(NoMethodError) { topic.mumbo = 5 }
|
||||
end
|
||||
|
||||
def test_preserving_date_objects
|
||||
if current_adapter?(:SybaseAdapter)
|
||||
# Sybase ctlib does not (yet?) support the date type; use datetime instead.
|
||||
@@ -284,7 +541,7 @@ class BasicsTest < ActiveRecord::TestCase
|
||||
end
|
||||
|
||||
|
||||
if current_adapter?(:MysqlAdapter)
|
||||
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
||||
def test_update_all_with_order_and_limit
|
||||
assert_equal 1, Topic.update_all("content = 'bulk updated!'", nil, :limit => 1, :order => 'id DESC')
|
||||
end
|
||||
|
||||
@@ -325,7 +325,7 @@ class CalculationsTest < ActiveRecord::TestCase
|
||||
end
|
||||
|
||||
def test_from_option_with_specified_index
|
||||
if Edge.connection.adapter_name == 'MySQL'
|
||||
if Edge.connection.adapter_name == 'MySQL' or Edge.connection.adapter_name == 'Mysql2'
|
||||
assert_equal Edge.count(:all), Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)')
|
||||
assert_equal Edge.count(:all, :conditions => 'sink_id < 5'),
|
||||
Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)', :conditions => 'sink_id < 5')
|
||||
|
||||
@@ -68,6 +68,40 @@ class ColumnDefinitionTest < ActiveRecord::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
if current_adapter?(:Mysql2Adapter)
|
||||
def test_should_set_default_for_mysql_binary_data_types
|
||||
binary_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", "a", "binary(1)")
|
||||
assert_equal "a", binary_column.default
|
||||
|
||||
varbinary_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", "a", "varbinary(1)")
|
||||
assert_equal "a", varbinary_column.default
|
||||
end
|
||||
|
||||
def test_should_not_set_default_for_blob_and_text_data_types
|
||||
assert_raise ArgumentError do
|
||||
ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", "a", "blob")
|
||||
end
|
||||
|
||||
assert_raise ArgumentError do
|
||||
ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", "Hello", "text")
|
||||
end
|
||||
|
||||
text_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", nil, "text")
|
||||
assert_equal nil, text_column.default
|
||||
|
||||
not_null_text_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", nil, "text", false)
|
||||
assert_equal "", not_null_text_column.default
|
||||
end
|
||||
|
||||
def test_has_default_should_return_false_for_blog_and_test_data_types
|
||||
blob_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", nil, "blob")
|
||||
assert !blob_column.has_default?
|
||||
|
||||
text_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", nil, "text")
|
||||
assert !text_column.has_default?
|
||||
end
|
||||
end
|
||||
|
||||
if current_adapter?(:PostgreSQLAdapter)
|
||||
def test_bigint_column_should_map_to_integer
|
||||
bigint_column = ActiveRecord::ConnectionAdapters::PostgreSQLColumn.new('number', nil, "bigint")
|
||||
|
||||
@@ -39,7 +39,7 @@ class DefaultTest < ActiveRecord::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
if current_adapter?(:MysqlAdapter)
|
||||
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
||||
class DefaultsTestWithoutTransactionalFixtures < ActiveRecord::TestCase
|
||||
# ActiveRecord::Base#create! (and #save and other related methods) will
|
||||
# open a new transaction. When in transactional fixtures mode, this will
|
||||
|
||||
@@ -256,7 +256,7 @@ if ActiveRecord::Base.connection.supports_migrations?
|
||||
|
||||
def test_create_table_with_defaults
|
||||
# MySQL doesn't allow defaults on TEXT or BLOB columns.
|
||||
mysql = current_adapter?(:MysqlAdapter)
|
||||
mysql = current_adapter?(:MysqlAdapter) || current_adapter?(:Mysql2Adapter)
|
||||
|
||||
Person.connection.create_table :testings do |t|
|
||||
t.column :one, :string, :default => "hello"
|
||||
@@ -313,7 +313,7 @@ if ActiveRecord::Base.connection.supports_migrations?
|
||||
assert_equal 'integer', four.sql_type
|
||||
assert_equal 'bigint', eight.sql_type
|
||||
assert_equal 'integer', eleven.sql_type
|
||||
elsif current_adapter?(:MysqlAdapter)
|
||||
elsif current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
||||
assert_match 'int(11)', default.sql_type
|
||||
assert_match 'tinyint', one.sql_type
|
||||
assert_match 'int', four.sql_type
|
||||
@@ -581,7 +581,7 @@ if ActiveRecord::Base.connection.supports_migrations?
|
||||
assert_kind_of BigDecimal, bob.wealth
|
||||
end
|
||||
|
||||
if current_adapter?(:MysqlAdapter)
|
||||
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
||||
def test_unabstracted_database_dependent_types
|
||||
Person.delete_all
|
||||
|
||||
@@ -621,7 +621,7 @@ if ActiveRecord::Base.connection.supports_migrations?
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
end
|
||||
|
||||
if current_adapter?(:MysqlAdapter)
|
||||
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
||||
def testing_table_for_positioning
|
||||
Person.connection.create_table :testings, :id => false do |t|
|
||||
t.column :first, :integer
|
||||
@@ -1447,7 +1447,7 @@ if ActiveRecord::Base.connection.supports_migrations?
|
||||
columns = Person.connection.columns(:binary_testings)
|
||||
data_column = columns.detect { |c| c.name == "data" }
|
||||
|
||||
if current_adapter?(:MysqlAdapter)
|
||||
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
||||
assert_equal '', data_column.default
|
||||
else
|
||||
assert_nil data_column.default
|
||||
@@ -1748,7 +1748,7 @@ if ActiveRecord::Base.connection.supports_migrations?
|
||||
end
|
||||
|
||||
def integer_column
|
||||
if current_adapter?(:MysqlAdapter)
|
||||
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
||||
'int(11)'
|
||||
elsif current_adapter?(:OracleAdapter)
|
||||
'NUMBER(38)'
|
||||
|
||||
@@ -57,7 +57,7 @@ class QueryCacheTest < ActiveRecord::TestCase
|
||||
# Oracle adapter returns count() as Fixnum or Float
|
||||
if current_adapter?(:OracleAdapter)
|
||||
assert_kind_of Numeric, Task.connection.select_value("SELECT count(*) AS count_all FROM tasks")
|
||||
elsif current_adapter?(:SQLite3Adapter) && SQLite3::Version::VERSION > '1.2.5'
|
||||
elsif current_adapter?(:SQLite3Adapter) && SQLite3::Version::VERSION > '1.2.5' or current_adapter?(:Mysql2Adapter)
|
||||
# Future versions of the sqlite3 adapter will return numeric
|
||||
assert_instance_of Fixnum,
|
||||
Task.connection.select_value("SELECT count(*) AS count_all FROM tasks")
|
||||
|
||||
@@ -93,7 +93,7 @@ class SchemaDumperTest < ActiveRecord::TestCase
|
||||
|
||||
assert_match %r{c_int_4.*}, output
|
||||
assert_no_match %r{c_int_4.*:limit}, output
|
||||
elsif current_adapter?(:MysqlAdapter)
|
||||
elsif current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
||||
assert_match %r{c_int_1.*:limit => 1}, output
|
||||
assert_match %r{c_int_2.*:limit => 2}, output
|
||||
assert_match %r{c_int_3.*:limit => 3}, output
|
||||
@@ -169,7 +169,7 @@ class SchemaDumperTest < ActiveRecord::TestCase
|
||||
assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
|
||||
end
|
||||
|
||||
if current_adapter?(:MysqlAdapter)
|
||||
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
||||
def test_schema_dump_should_not_add_default_value_for_mysql_text_field
|
||||
output = standard_dump
|
||||
assert_match %r{t.text\s+"body",\s+:null => false$}, output
|
||||
|
||||
25
activerecord/test/connections/native_mysql2/connection.rb
Normal file
25
activerecord/test/connections/native_mysql2/connection.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
print "Using native Mysql2\n"
|
||||
require_dependency 'models/course'
|
||||
require 'logger'
|
||||
|
||||
ActiveRecord::Base.logger = Logger.new("debug.log")
|
||||
|
||||
# GRANT ALL PRIVILEGES ON activerecord_unittest.* to 'rails'@'localhost';
|
||||
# GRANT ALL PRIVILEGES ON activerecord_unittest2.* to 'rails'@'localhost';
|
||||
|
||||
ActiveRecord::Base.configurations = {
|
||||
'arunit' => {
|
||||
:adapter => 'mysql2',
|
||||
:username => 'rails',
|
||||
:encoding => 'utf8',
|
||||
:database => 'activerecord_unittest',
|
||||
},
|
||||
'arunit2' => {
|
||||
:adapter => 'mysql2',
|
||||
:username => 'rails',
|
||||
:database => 'activerecord_unittest2'
|
||||
}
|
||||
}
|
||||
|
||||
ActiveRecord::Base.establish_connection 'arunit'
|
||||
Course.establish_connection 'arunit2'
|
||||
24
activerecord/test/schema/mysql2_specific_schema.rb
Normal file
24
activerecord/test/schema/mysql2_specific_schema.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
ActiveRecord::Schema.define do
|
||||
create_table :binary_fields, :force => true, :options => 'CHARACTER SET latin1' do |t|
|
||||
t.binary :tiny_blob, :limit => 255
|
||||
t.binary :normal_blob, :limit => 65535
|
||||
t.binary :medium_blob, :limit => 16777215
|
||||
t.binary :long_blob, :limit => 2147483647
|
||||
t.text :tiny_text, :limit => 255
|
||||
t.text :normal_text, :limit => 65535
|
||||
t.text :medium_text, :limit => 16777215
|
||||
t.text :long_text, :limit => 2147483647
|
||||
end
|
||||
|
||||
ActiveRecord::Base.connection.execute <<-SQL
|
||||
DROP PROCEDURE IF EXISTS ten;
|
||||
SQL
|
||||
|
||||
ActiveRecord::Base.connection.execute <<-SQL
|
||||
CREATE PROCEDURE ten() SQL SECURITY INVOKER
|
||||
BEGIN
|
||||
select 10;
|
||||
END
|
||||
SQL
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user