提交 90e396ce 编写于 作者: Y Yves Senn

pg tests, move uniqueness validation test to array tests.

上级 b8ec014b
......@@ -271,6 +271,21 @@ def test_assigning_non_array_value
assert_instance_of PG::InvalidTextRepresentation, e.original_exception
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
def assert_cycle field, array
# test creation
......
......@@ -30,11 +30,6 @@ class ReplyWithTitleObject < Reply
def title; ReplyTitle.new; end
end
class Employee < ActiveRecord::Base
self.table_name = 'postgresql_arrays'
validates_uniqueness_of :nicknames
end
class TopicWithUniqEvent < Topic
belongs_to :event, foreign_key: :parent_id
validates :event, uniqueness: true
......@@ -378,18 +373,6 @@ def test_validate_uniqueness_with_non_callable_conditions_is_not_supported
}
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
event = Event.create
assert TopicWithUniqEvent.create(event: event).valid?
......
ActiveRecord::Schema.define do
%w(postgresql_arrays postgresql_times
postgresql_oids defaults postgresql_timestamp_with_zones
%w(postgresql_times postgresql_oids defaults postgresql_timestamp_with_zones
postgresql_partitioned_table postgresql_partitioned_table_parent).each do |table_name|
execute "DROP TABLE IF EXISTS #{quote_table_name table_name}"
end
......@@ -43,14 +42,6 @@
execute "CREATE DOMAIN schema_1.varchar AS varchar"
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
CREATE TABLE postgresql_times (
id SERIAL PRIMARY KEY,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册