diff options
Diffstat (limited to 'activerecord/test/cases')
6 files changed, 166 insertions, 4 deletions
diff --git a/activerecord/test/cases/adapters/postgresql/intrange_test.rb b/activerecord/test/cases/adapters/postgresql/intrange_test.rb new file mode 100644 index 0000000000..5f6a64619d --- /dev/null +++ b/activerecord/test/cases/adapters/postgresql/intrange_test.rb @@ -0,0 +1,106 @@ +# encoding: utf-8 + +require "cases/helper" +require 'active_record/base' +require 'active_record/connection_adapters/postgresql_adapter' + +class PostgresqlIntrangesTest < ActiveRecord::TestCase + class IntRangeDataType < ActiveRecord::Base + self.table_name = 'intrange_data_type' + end + + def setup + @connection = ActiveRecord::Base.connection + begin + @connection.transaction do + @connection.create_table('intrange_data_type') do |t| + t.intrange 'int_range', :default => (1..10) + t.intrange 'long_int_range', :limit => 8, :default => (1..100) + end + end + rescue ActiveRecord::StatementInvalid + return skip "do not test on PG without ranges" + end + @int_range_column = IntRangeDataType.columns.find { |c| c.name == 'int_range' } + @long_int_range_column = IntRangeDataType.columns.find { |c| c.name == 'long_int_range' } + end + + def teardown + @connection.execute 'drop table if exists intrange_data_type' + end + + def test_columns + assert_equal :intrange, @int_range_column.type + assert_equal :intrange, @long_int_range_column.type + end + + def test_type_cast_intrange + assert @int_range_column + assert_equal(true, @int_range_column.has_default?) + assert_equal((1..10), @int_range_column.default) + assert_equal("int4range", @int_range_column.sql_type) + + data = "[1,10)" + hash = @int_range_column.class.string_to_intrange data + assert_equal((1..9), hash) + assert_equal((1..9), @int_range_column.type_cast(data)) + + assert_equal((nil..nil), @int_range_column.type_cast("empty")) + assert_equal((1..5), @int_range_column.type_cast('[1,5]')) + assert_equal((2..4), @int_range_column.type_cast('(1,5)')) + assert_equal((2..39), @int_range_column.type_cast('[2,40)')) + assert_equal((10..20), @int_range_column.type_cast('(9,20]')) + end + + def test_type_cast_long_intrange + assert @long_int_range_column + assert_equal(true, @long_int_range_column.has_default?) + assert_equal((1..100), @long_int_range_column.default) + assert_equal("int8range", @long_int_range_column.sql_type) + end + + def test_rewrite + @connection.execute "insert into intrange_data_type (int_range) VALUES ('(1, 6)')" + x = IntRangeDataType.first + x.int_range = (1..100) + assert x.save! + end + + def test_select + @connection.execute "insert into intrange_data_type (int_range) VALUES ('(1, 4]')" + x = IntRangeDataType.first + assert_equal((2..4), x.int_range) + end + + def test_empty_range + @connection.execute %q|insert into intrange_data_type (int_range) VALUES('empty')| + x = IntRangeDataType.first + assert_equal((nil..nil), x.int_range) + end + + def test_rewrite_to_nil + @connection.execute %q|insert into intrange_data_type (int_range) VALUES('(1, 4]')| + x = IntRangeDataType.first + x.int_range = nil + assert x.save! + assert_equal(nil, x.int_range) + end + + def test_invalid_intrange + assert IntRangeDataType.create!(int_range: ('a'..'d')) + x = IntRangeDataType.first + assert_equal(nil, x.int_range) + end + + def test_save_empty_range + assert IntRangeDataType.create!(int_range: (nil..nil)) + x = IntRangeDataType.first + assert_equal((nil..nil), x.int_range) + end + + def test_save_invalid_data + assert_raises(ActiveRecord::StatementInvalid) do + IntRangeDataType.create!(int_range: "empty1") + end + end +end diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 8e52ce1d91..2b96b42032 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -330,6 +330,17 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase end end + def test_update_counter_caches_on_replace_association + post = posts(:welcome) + tag = post.tags.create!(:name => 'doomed') + tag.tagged_posts << posts(:thinking) + + tag.tagged_posts = [] + post.reload + + assert_equal(post.taggings.count, post.taggings_count) + end + def test_replace_association assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)} diff --git a/activerecord/test/cases/log_subscriber_test.rb b/activerecord/test/cases/log_subscriber_test.rb index 70d00aecf9..345e83a102 100644 --- a/activerecord/test/cases/log_subscriber_test.rb +++ b/activerecord/test/cases/log_subscriber_test.rb @@ -1,4 +1,5 @@ require "cases/helper" +require "models/binary" require "models/developer" require "models/post" require "active_support/log_subscriber/test_helper" @@ -100,4 +101,12 @@ class LogSubscriberTest < ActiveRecord::TestCase def test_initializes_runtime Thread.new { assert_equal 0, ActiveRecord::LogSubscriber.runtime }.join end + + def test_binary_data_is_not_logged + skip if current_adapter?(:Mysql2Adapter) + + Binary.create(:data => 'some binary data') + wait + assert_match(/<16 bytes of binary data>/, @logger.logged(:debug).join) + end end diff --git a/activerecord/test/cases/migration/rename_column_test.rb b/activerecord/test/cases/migration/rename_column_test.rb index d1a85ee5e4..318d61263a 100644 --- a/activerecord/test/cases/migration/rename_column_test.rb +++ b/activerecord/test/cases/migration/rename_column_test.rb @@ -173,6 +173,16 @@ module ActiveRecord refute TestModel.new.administrator? end + def test_change_column_with_custom_index_name + add_column "test_models", "category", :string + add_index :test_models, :category, name: 'test_models_categories_idx' + + assert_equal ['test_models_categories_idx'], connection.indexes('test_models').map(&:name) + change_column "test_models", "category", :string, null: false, default: 'article' + + assert_equal ['test_models_categories_idx'], connection.indexes('test_models').map(&:name) + end + def test_change_column_default add_column "test_models", "first_name", :string connection.change_column_default "test_models", "first_name", "Tester" diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index 6962da298e..295c7e13fa 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -1,5 +1,6 @@ -require "cases/helper" +require 'cases/helper' require 'models/topic' +require 'models/person' require 'bcrypt' class SerializedAttributeTest < ActiveRecord::TestCase @@ -225,4 +226,12 @@ class SerializedAttributeTest < ActiveRecord::TestCase ensure ActiveRecord::Base.time_zone_aware_attributes = false end + + def test_serialize_attribute_can_be_serialized_in_an_integer_column + insures = ['life'] + person = SerializedPerson.new(first_name: 'David', insures: insures) + assert person.save + person = person.reload + assert_equal(insures, person.insures) + end end diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb index 46212e49b6..46e767af1a 100644 --- a/activerecord/test/cases/validations/uniqueness_validation_test.rb +++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb @@ -30,6 +30,11 @@ class ReplyWithTitleObject < Reply def title; ReplyTitle.new; end end +class Employee < ActiveRecord::Base + self.table_name = 'postgresql_arrays' + validates_uniqueness_of :nicknames +end + class UniquenessValidationTest < ActiveRecord::TestCase fixtures :topics, 'warehouse-things', :developers @@ -341,16 +346,28 @@ class UniquenessValidationTest < ActiveRecord::TestCase assert w6.errors[:city].any?, "Should have errors for city" assert_equal ["has already been taken"], w6.errors[:city], "Should have uniqueness message for city" end - + def test_validate_uniqueness_with_conditions Topic.validates_uniqueness_of(:title, :conditions => Topic.where('approved = ?', true)) Topic.create("title" => "I'm a topic", "approved" => true) Topic.create("title" => "I'm an unapproved topic", "approved" => false) - + t3 = Topic.new("title" => "I'm a topic", "approved" => true) assert !t3.valid?, "t3 shouldn't be valid" - + t4 = Topic.new("title" => "I'm an unapproved topic", "approved" => false) assert t4.valid?, "t4 should be valid" end + + def test_validate_uniqueness_with_array_column + return skip "Uniqueness on arrays has only been tested in PostgreSQL so far." if !current_adapter? :PostgreSQLAdapter + + e1 = Employee.create("nicknames" => ["john", "johnny"], "commission_by_quarter" => [1000, 1200]) + assert e1.persisted?, "Saving e1" + + e2 = Employee.create("nicknames" => ["john", "johnny"], "commission_by_quarter" => [2200]) + assert !e2.persisted?, "e2 shouldn't be valid" + assert e2.errors[:nicknames].any?, "Should have errors for nicknames" + assert_equal ["has already been taken"], e2.errors[:nicknames], "Should have uniqueness message for nicknames" + end end |