1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

pg tests, move uniqueness validation test to array tests.

This commit is contained in:
Yves Senn 2014-12-02 11:53:18 +01:00
parent b8ec014b2d
commit 90e396ce65
3 changed files with 16 additions and 27 deletions

View file

@ -271,6 +271,21 @@ class PostgresqlArrayTest < ActiveRecord::TestCase
assert_instance_of PG::InvalidTextRepresentation, e.original_exception assert_instance_of PG::InvalidTextRepresentation, e.original_exception
end end
def test_uniqueness_validation
klass = Class.new(PgArray) do
validates_uniqueness_of :tags
def self.model_name; ActiveModel::Name.new(PgArray) end
end
e1 = klass.create("tags" => ["black", "blue"])
assert e1.persisted?, "Saving e1"
e2 = klass.create("tags" => ["black", "blue"])
assert !e2.persisted?, "e2 shouldn't be valid"
assert e2.errors[:tags].any?, "Should have errors for tags"
assert_equal ["has already been taken"], e2.errors[:tags], "Should have uniqueness message for tags"
end
private private
def assert_cycle field, array def assert_cycle field, array
# test creation # test creation

View file

@ -30,11 +30,6 @@ class ReplyWithTitleObject < Reply
def title; ReplyTitle.new; end def title; ReplyTitle.new; end
end end
class Employee < ActiveRecord::Base
self.table_name = 'postgresql_arrays'
validates_uniqueness_of :nicknames
end
class TopicWithUniqEvent < Topic class TopicWithUniqEvent < Topic
belongs_to :event, foreign_key: :parent_id belongs_to :event, foreign_key: :parent_id
validates :event, uniqueness: true validates :event, uniqueness: true
@ -378,18 +373,6 @@ class UniquenessValidationTest < ActiveRecord::TestCase
} }
end end
if current_adapter? :PostgreSQLAdapter
def test_validate_uniqueness_with_array_column
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
def test_validate_uniqueness_on_existing_relation def test_validate_uniqueness_on_existing_relation
event = Event.create event = Event.create
assert TopicWithUniqEvent.create(event: event).valid? assert TopicWithUniqEvent.create(event: event).valid?

View file

@ -1,7 +1,6 @@
ActiveRecord::Schema.define do ActiveRecord::Schema.define do
%w(postgresql_arrays postgresql_times %w(postgresql_times postgresql_oids defaults postgresql_timestamp_with_zones
postgresql_oids defaults postgresql_timestamp_with_zones
postgresql_partitioned_table postgresql_partitioned_table_parent).each do |table_name| postgresql_partitioned_table postgresql_partitioned_table_parent).each do |table_name|
execute "DROP TABLE IF EXISTS #{quote_table_name table_name}" execute "DROP TABLE IF EXISTS #{quote_table_name table_name}"
end end
@ -43,14 +42,6 @@ _SQL
execute "CREATE DOMAIN schema_1.varchar AS varchar" execute "CREATE DOMAIN schema_1.varchar AS varchar"
execute "CREATE DOMAIN schema_1.bpchar AS bpchar" execute "CREATE DOMAIN schema_1.bpchar AS bpchar"
execute <<_SQL
CREATE TABLE postgresql_arrays (
id SERIAL PRIMARY KEY,
commission_by_quarter INTEGER[],
nicknames TEXT[]
);
_SQL
execute <<_SQL execute <<_SQL
CREATE TABLE postgresql_times ( CREATE TABLE postgresql_times (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,