Skip to content

Commit 0d04de7

Browse files
committed
test: introduce assert_same_elements where order doesn't matter
instead of explicitly sorting arrays in the test for comparison
1 parent 2a2f282 commit 0d04de7

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

test/test_helper.rb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,20 @@ module Tenanted
1717
class TestCase < ActiveSupport::TestCase
1818
extend Minitest::Spec::DSL
1919

20-
# When used with Minitest::Spec's `describe`, ActiveSupport::Testing's `test` creates methods
21-
# that may be inherited by subsequent describe blocks and run multiple times. Warn us if this
22-
# happens. (Note that Minitest::Spec's `it` doesn't do this.)
23-
def self.test(name, &block)
24-
if self.children.any?
25-
c = caller_locations[0]
26-
path = Pathname.new(c.path).relative_path_from(Pathname.new(Dir.pwd))
27-
puts "WARNING: #{path}:#{__LINE__}: test #{name.inspect} is being inherited"
28-
end
29-
30-
super
31-
end
20+
class << self
21+
# When used with Minitest::Spec's `describe`, ActiveSupport::Testing's `test` creates methods
22+
# that may be inherited by subsequent describe blocks and run multiple times. Warn us if this
23+
# happens. (Note that Minitest::Spec's `it` doesn't do this.)
24+
def test(name, &block)
25+
if self.children.any?
26+
c = caller_locations[0]
27+
path = Pathname.new(c.path).relative_path_from(Pathname.new(Dir.pwd))
28+
puts "WARNING: #{path}:#{__LINE__}: test #{name.inspect} is being inherited"
29+
end
3230

31+
super
32+
end
3333

34-
class << self
3534
def for_each_scenario(s = all_scenarios, &block)
3635
s.each do |db_scenario, model_scenarios|
3736
with_db_scenario(db_scenario) do
@@ -143,6 +142,10 @@ def with_new_migration_file
143142
File.join(db_path, "tenanted_migrations")
144143
end
145144

145+
def assert_same_elements(expected, actual)
146+
assert_equal(expected.sort, actual.sort, "Elements don't match")
147+
end
148+
146149
def capture_log
147150
StringIO.new.tap do |log|
148151
logger_was, ActiveRecord::Base.logger = ActiveRecord::Base.logger, ActiveSupport::Logger.new(log)

test/unit/base_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@
9191
test "models can be created" do
9292
user = User.new
9393

94-
assert_equal([ "id", "email", "created_at", "updated_at" ].sort,
95-
user.attributes.keys.sort)
96-
assert_equal([ "id", "email", "created_at", "updated_at" ].sort,
97-
User.column_names.sort)
94+
assert_same_elements([ "id", "email", "created_at", "updated_at" ],
95+
user.attributes.keys)
96+
assert_same_elements([ "id", "email", "created_at", "updated_at" ],
97+
User.column_names)
9898
end
9999

100100
test "schema cache can be loaded" do

test/unit/database_configurations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
TenantedApplicationRecord.while_tenanted("bar") { User.count }
5656

57-
assert_equal([ "foo", "bar" ].sort, tenanted_config.tenants.sort)
57+
assert_same_elements([ "foo", "bar" ], tenanted_config.tenants)
5858

5959
TenantedApplicationRecord.destroy_tenant("foo")
6060

test/unit/tenant_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208

209209
TenantedApplicationRecord.while_tenanted("bar") { User.count }
210210

211-
assert_equal([ "foo", "bar" ].sort, TenantedApplicationRecord.tenants.sort)
211+
assert_same_elements([ "foo", "bar" ], TenantedApplicationRecord.tenants)
212212

213213
TenantedApplicationRecord.destroy_tenant("foo")
214214

@@ -292,8 +292,8 @@
292292
User.count
293293
end
294294
assert_equal(20250213005959, User.connection_pool.migration_context.current_version)
295-
assert_equal([ "id", "email", "created_at", "updated_at", "age" ].sort,
296-
User.new.attributes.keys.sort)
295+
assert_same_elements([ "id", "email", "created_at", "updated_at", "age" ],
296+
User.new.attributes.keys)
297297
end
298298
end
299299
end
@@ -305,8 +305,8 @@
305305

306306
describe "before a connection is made" do
307307
test "models can be created but migration is not applied" do
308-
assert_equal([ "id", "email", "created_at", "updated_at" ].sort,
309-
User.new.attributes.keys.sort)
308+
assert_same_elements([ "id", "email", "created_at", "updated_at" ],
309+
User.new.attributes.keys)
310310
end
311311
end
312312

@@ -319,8 +319,8 @@
319319
User.count
320320
end
321321
assert_equal(20250213005959, User.connection_pool.migration_context.current_version)
322-
assert_equal([ "id", "email", "created_at", "updated_at", "age" ].sort,
323-
User.new.attributes.keys.sort)
322+
assert_same_elements([ "id", "email", "created_at", "updated_at", "age" ],
323+
User.new.attributes.keys)
324324
end
325325
end
326326
end

0 commit comments

Comments
 (0)