Skip to content

Commit 6dcc1b7

Browse files
authored
Merge pull request #776 from aidanharan/unique-index-fixes
Rails 6: Fix tests that fail because of unique NULL indexes
2 parents e67f033 + c256c33 commit 6dcc1b7

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

test/cases/coerced_tests.rb

+57-2
Original file line numberDiff line numberDiff line change
@@ -879,15 +879,15 @@ def test_relations_dont_load_all_records_in_inspect_coerced
879879
# so we are skipping all together.
880880
coerce_tests! :test_empty_complex_chained_relations
881881

882-
# Can't apply offset withour ORDER
882+
# Can't apply offset without ORDER
883883
coerce_tests! %r{using a custom table affects the wheres}
884884
test 'using a custom table affects the wheres coerced' do
885885
post = posts(:welcome)
886886

887887
assert_equal post, custom_post_relation.where!(title: post.title).order(:id).take
888888
end
889889

890-
# Can't apply offset withour ORDER
890+
# Can't apply offset without ORDER
891891
coerce_tests! %r{using a custom table with joins affects the joins}
892892
test 'using a custom table with joins affects the joins coerced' do
893893
post = posts(:welcome)
@@ -1151,10 +1151,21 @@ class CollectionCacheKeyTest < ActiveRecord::TestCase
11511151

11521152

11531153

1154+
require "models/book"
11541155
module ActiveRecord
11551156
class StatementCacheTest < ActiveRecord::TestCase
11561157
# Getting random failures.
11571158
coerce_tests! :test_find_does_not_use_statement_cache_if_table_name_is_changed
1159+
1160+
# Need to remove index as SQL Server considers NULLs on a unique-index to be equal unlike PostgreSQL/MySQL/SQLite.
1161+
coerce_tests! :test_statement_cache_values_differ
1162+
def test_statement_cache_values_differ_coerced
1163+
Book.connection.remove_index(:books, column: [:author_id, :name])
1164+
1165+
original_test_statement_cache_values_differ
1166+
ensure
1167+
Book.connection.add_index(:books, [:author_id, :name], unique: true)
1168+
end
11581169
end
11591170
end
11601171

@@ -1264,3 +1275,47 @@ class DatabaseTasksTruncateAllTest < ActiveRecord::TestCase
12641275
coerce_tests! :test_truncate_tables
12651276
end
12661277
end
1278+
1279+
1280+
require "models/book"
1281+
class EnumTest < ActiveRecord::TestCase
1282+
# Need to remove index as SQL Server considers NULLs on a unique-index to be equal unlike PostgreSQL/MySQL/SQLite.
1283+
coerce_tests! %r{enums are distinct per class}
1284+
test "enums are distinct per class coerced" do
1285+
Book.connection.remove_index(:books, column: [:author_id, :name])
1286+
1287+
send(:'original_enums are distinct per class')
1288+
ensure
1289+
Book.connection.add_index(:books, [:author_id, :name], unique: true)
1290+
end
1291+
1292+
# Need to remove index as SQL Server considers NULLs on a unique-index to be equal unlike PostgreSQL/MySQL/SQLite.
1293+
coerce_tests! %r{creating new objects with enum scopes}
1294+
test "creating new objects with enum scopes coerced" do
1295+
Book.connection.remove_index(:books, column: [:author_id, :name])
1296+
1297+
send(:'original_creating new objects with enum scopes')
1298+
ensure
1299+
Book.connection.add_index(:books, [:author_id, :name], unique: true)
1300+
end
1301+
1302+
# Need to remove index as SQL Server considers NULLs on a unique-index to be equal unlike PostgreSQL/MySQL/SQLite.
1303+
coerce_tests! %r{enums are inheritable}
1304+
test "enums are inheritable coerced" do
1305+
Book.connection.remove_index(:books, column: [:author_id, :name])
1306+
1307+
send(:'original_enums are inheritable')
1308+
ensure
1309+
Book.connection.add_index(:books, [:author_id, :name], unique: true)
1310+
end
1311+
1312+
# Need to remove index as SQL Server considers NULLs on a unique-index to be equal unlike PostgreSQL/MySQL/SQLite.
1313+
coerce_tests! %r{declare multiple enums at a time}
1314+
test "declare multiple enums at a time coerced" do
1315+
Book.connection.remove_index(:books, column: [:author_id, :name])
1316+
1317+
send(:'original_declare multiple enums at a time')
1318+
ensure
1319+
Book.connection.add_index(:books, [:author_id, :name], unique: true)
1320+
end
1321+
end

test/support/coerceable_test_sqlserver.rb

+12-5
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,28 @@ def coerce_tests!(*methods)
1919
end
2020

2121
def coerce_all_tests!
22-
once = false
2322
instance_methods(false).each do |method|
2423
next unless method.to_s =~ /\Atest/
2524
undef_method(method)
26-
once = true
2725
end
2826
STDOUT.puts "🙉 🙈 🙊 Undefined all tests: #{self.name}"
2927
end
3028

3129
private
3230

33-
def coerced_test_warning(method)
34-
method = instance_methods(false).select { |m| m =~ method } if method.is_a?(Regexp)
31+
def coerced_test_warning(test_to_coerce)
32+
if test_to_coerce.is_a?(Regexp)
33+
method = instance_methods(false).select { |m| m =~ test_to_coerce }
34+
else
35+
method = test_to_coerce
36+
end
37+
3538
Array(method).each do |m|
36-
result = undef_method(m) if m && method_defined?(m)
39+
result = if m && method_defined?(m)
40+
alias_method("original_#{test_to_coerce.inspect.tr('/\:"', '')}", m)
41+
undef_method(m)
42+
end
43+
3744
if result.blank?
3845
STDOUT.puts "🐳 Unfound coerced test: #{self.name}##{m}"
3946
else

0 commit comments

Comments
 (0)