aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2016-01-27 10:47:15 +0900
committerRyuta Kamizono <kamipo@gmail.com>2016-02-12 23:57:15 +0900
commit811a4fa8eb6ceea841e61e8ac05747ffb69595ae (patch)
tree6ada1cfcf71a644d731f9ba7b55b7c06d260ee26 /activerecord/test/cases
parentf7aa4c92a0a0dd609b6e07a5db256c417d0aa235 (diff)
downloadrails-811a4fa8eb6ceea841e61e8ac05747ffb69595ae.tar.gz
rails-811a4fa8eb6ceea841e61e8ac05747ffb69595ae.tar.bz2
rails-811a4fa8eb6ceea841e61e8ac05747ffb69595ae.zip
Avoid a string value truncation in uniqueness validation
In MySQL, PostgreSQL, Oracle and SQLServer, a value over the limit cannot be inserted or updated (See #23522). In SQLite3, a value is inserted or updated regardless of the limit. We should avoid a string value truncation in uniqueness validation.
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/validations/uniqueness_validation_test.rb32
1 files changed, 22 insertions, 10 deletions
diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb
index e601c53dbf..6264ec15d0 100644
--- a/activerecord/test/cases/validations/uniqueness_validation_test.rb
+++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb
@@ -336,19 +336,31 @@ class UniquenessValidationTest < ActiveRecord::TestCase
end
def test_validate_uniqueness_with_limit
- # Event.title is limited to 5 characters
- e1 = Event.create(:title => "abcde")
- assert e1.valid?, "Could not create an event with a unique, 5 character title"
- e2 = Event.create(:title => "abcdefgh")
- assert !e2.valid?, "Created an event whose title, with limit taken into account, is not unique"
+ if current_adapter?(:SQLite3Adapter)
+ # Event.title has limit 5, but does not affected in SQLite.
+ e1 = Event.create(title: "abcdefgh")
+ assert e1.valid?, "Could not create an event with a unique 8 characters title"
+ e2 = Event.create(title: "abcdefgh")
+ assert !e2.valid?, "Created an event whose title is not unique"
+ elsif current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter, :OracleAdapter, :SQLServerAdapter)
+ assert_raise(ActiveRecord::StatementInvalid) do
+ Event.create(title: "abcdefgh")
+ end
+ end
end
def test_validate_uniqueness_with_limit_and_utf8
- # Event.title is limited to 5 characters
- e1 = Event.create(:title => "一二三四五")
- assert e1.valid?, "Could not create an event with a unique, 5 character title"
- e2 = Event.create(:title => "一二三四五六七八")
- assert !e2.valid?, "Created an event whose title, with limit taken into account, is not unique"
+ if current_adapter?(:SQLite3Adapter)
+ # Event.title has limit 5, but does not affected in SQLite.
+ e1 = Event.create(title: "一二三四五六七八")
+ assert e1.valid?, "Could not create an event with a unique 8 characters title"
+ e2 = Event.create(title: "一二三四五六七八")
+ assert !e2.valid?, "Created an event whose title is not unique"
+ elsif current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter, :OracleAdapter, :SQLServerAdapter)
+ assert_raise(ActiveRecord::StatementInvalid) do
+ Event.create(title: "一二三四五六七八")
+ end
+ end
end
def test_validate_straight_inheritance_uniqueness