Skip to content

Commit ff8f9f0

Browse files
committed
Add a test case for save(validate:false) with invalid foreign key.
It is supposed to raise ActiveRecord::InvalidForeignKey when the database has a fk constraint.
1 parent 8a081db commit ff8f9f0

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

activerecord/test/cases/adapter_test.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test_uniqueness_violations_are_translated_to_specific_exception
146146
end
147147

148148
def test_foreign_key_violations_are_translated_to_specific_exception
149-
unless @connection.adapter_name == 'SQLite'
149+
unless current_adapter?(:SQLite3Adapter)
150150
assert_raises(ActiveRecord::InvalidForeignKey) do
151151
# Oracle adapter uses prefetched primary key values from sequence and passes them to connection adapter insert method
152152
if @connection.prefetch_primary_key?
@@ -159,6 +159,20 @@ def test_foreign_key_violations_are_translated_to_specific_exception
159159
end
160160
end
161161

162+
def test_foreign_key_violations_are_translated_to_specific_exception_with_validate_false
163+
unless current_adapter?(:SQLite3Adapter)
164+
klass_has_fk = Class.new(ActiveRecord::Base) do
165+
self.table_name = 'fk_test_has_fk'
166+
end
167+
168+
assert_raises(ActiveRecord::InvalidForeignKey) do
169+
has_fk = klass_has_fk.new
170+
has_fk.fk_id = 1231231231
171+
has_fk.save(validate: false)
172+
end
173+
end
174+
end
175+
162176
def test_disable_referential_integrity
163177
assert_nothing_raised do
164178
@connection.disable_referential_integrity do

0 commit comments

Comments
 (0)