Skip to content

Commit bdd549a

Browse files
committed
Refactor tests to be less brittle
1 parent 5f43a2a commit bdd549a

File tree

3 files changed

+74
-28
lines changed

3 files changed

+74
-28
lines changed

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -538,15 +538,27 @@ def test_adding_a_collection
538538
end
539539

540540
def test_transactions_when_adding_to_persisted
541-
force_signal37_to_load_all_clients_of_firm
542-
Client.expects(:transaction)
543-
companies(:first_firm).clients_of_firm.concat(Client.new("name" => "Natural Company"))
541+
good = Client.new(:name => "Good")
542+
bad = Client.new(:name => "Bad", :raise_on_save => true)
543+
544+
begin
545+
companies(:first_firm).clients_of_firm.concat(good, bad)
546+
rescue Client::RaisedOnSave
547+
end
548+
549+
assert !companies(:first_firm).clients_of_firm(true).include?(good)
544550
end
545551

546552
def test_transactions_when_adding_to_new_record
547-
Client.expects(:transaction).never
548-
firm = Firm.new
549-
firm.clients_of_firm.concat(Client.new("name" => "Natural Company"))
553+
prev_ignored_sql = ActiveRecord::SQLCounter.ignored_sql
554+
ActiveRecord::SQLCounter.ignored_sql = []
555+
556+
assert_no_queries do
557+
firm = Firm.new
558+
firm.clients_of_firm.concat(Client.new("name" => "Natural Company"))
559+
end
560+
ensure
561+
ActiveRecord::SQLCounter.ignored_sql = prev_ignored_sql
550562
end
551563

552564
def test_new_aliased_to_build
@@ -791,18 +803,31 @@ def test_delete_all_with_not_yet_loaded_association_collection
791803
end
792804

793805
def test_transaction_when_deleting_persisted
794-
force_signal37_to_load_all_clients_of_firm
795-
client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
796-
Client.expects(:transaction)
797-
companies(:first_firm).clients_of_firm.delete(client)
806+
good = Client.new(:name => "Good")
807+
bad = Client.new(:name => "Bad", :raise_on_destroy => true)
808+
809+
companies(:first_firm).clients_of_firm = [good, bad]
810+
811+
begin
812+
companies(:first_firm).clients_of_firm.destroy(good, bad)
813+
rescue Client::RaisedOnDestroy
814+
end
815+
816+
assert_equal [good, bad], companies(:first_firm).clients_of_firm(true)
798817
end
799818

800819
def test_transaction_when_deleting_new_record
801-
client = Client.new("name" => "New Client")
802-
firm = Firm.new
803-
firm.clients_of_firm << client
804-
Client.expects(:transaction).never
805-
firm.clients_of_firm.delete(client)
820+
prev_ignored_sql = ActiveRecord::SQLCounter.ignored_sql
821+
ActiveRecord::SQLCounter.ignored_sql = []
822+
823+
assert_no_queries do
824+
firm = Firm.new
825+
client = Client.new("name" => "New Client")
826+
firm.clients_of_firm << client
827+
firm.clients_of_firm.destroy(client)
828+
end
829+
ensure
830+
ActiveRecord::SQLCounter.ignored_sql = prev_ignored_sql
806831
end
807832

808833
def test_clearing_an_association_collection
@@ -1139,21 +1164,29 @@ def test_replace_failure
11391164
end
11401165

11411166
def test_transactions_when_replacing_on_persisted
1142-
firm = Firm.find(:first, :order => "id")
1143-
firm.clients = [companies(:first_client)]
1144-
assert firm.save, "Could not save firm"
1145-
firm.reload
1167+
good = Client.new(:name => "Good")
1168+
bad = Client.new(:name => "Bad", :raise_on_save => true)
11461169

1147-
Client.expects(:transaction)
1148-
firm.clients_of_firm = [Client.new("name" => "Natural Company")]
1170+
companies(:first_firm).clients_of_firm = [good]
1171+
1172+
begin
1173+
companies(:first_firm).clients_of_firm = [bad]
1174+
rescue Client::RaisedOnSave
1175+
end
1176+
1177+
assert_equal [good], companies(:first_firm).clients_of_firm(true)
11491178
end
11501179

11511180
def test_transactions_when_replacing_on_new_record
1152-
firm = Firm.new
1153-
firm.clients_of_firm << Client.new("name" => "Natural Company")
1181+
prev_ignored_sql = ActiveRecord::SQLCounter.ignored_sql
1182+
ActiveRecord::SQLCounter.ignored_sql = []
11541183

1155-
Client.expects(:transaction).never
1156-
firm.clients_of_firm = [Client.new("name" => "New Client")]
1184+
assert_no_queries do
1185+
firm = Firm.new
1186+
firm.clients_of_firm = [Client.new("name" => "New Client")]
1187+
end
1188+
ensure
1189+
ActiveRecord::SQLCounter.ignored_sql = prev_ignored_sql
11571190
end
11581191

11591192
def test_get_ids

activerecord/test/cases/helper.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ def with_active_record_default_timezone(zone)
5757

5858
module ActiveRecord
5959
class SQLCounter
60-
IGNORED_SQL = [/^PRAGMA (?!(table_info))/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /^SHOW max_identifier_length/, /^BEGIN/, /^COMMIT/]
60+
cattr_accessor :ignored_sql
61+
self.ignored_sql = [/^PRAGMA (?!(table_info))/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /^SHOW max_identifier_length/, /^BEGIN/, /^COMMIT/]
6162

6263
# FIXME: this needs to be refactored so specific database can add their own
6364
# ignored SQL. This ignored SQL is for Oracle.
64-
IGNORED_SQL.concat [/^select .*nextval/i, /^SAVEPOINT/, /^ROLLBACK TO/, /^\s*select .* from all_triggers/im]
65+
ignored_sql.concat [/^select .*nextval/i, /^SAVEPOINT/, /^ROLLBACK TO/, /^\s*select .* from all_triggers/im]
6566

6667
def initialize
6768
$queries_executed = []
@@ -73,7 +74,7 @@ def call(name, start, finish, message_id, values)
7374
# FIXME: this seems bad. we should probably have a better way to indicate
7475
# the query was cached
7576
unless 'CACHE' == values[:name]
76-
$queries_executed << sql unless IGNORED_SQL.any? { |r| sql =~ r }
77+
$queries_executed << sql unless self.class.ignored_sql.any? { |r| sql =~ r }
7778
end
7879
end
7980
end

activerecord/test/models/company.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ class Client < Company
124124
has_many :accounts, :through => :firm
125125
belongs_to :account
126126

127+
class RaisedOnSave < RuntimeError; end
128+
attr_accessor :raise_on_save
129+
before_save do
130+
raise RaisedOnSave if raise_on_save
131+
end
132+
133+
class RaisedOnDestroy < RuntimeError; end
134+
attr_accessor :raise_on_destroy
135+
before_destroy do
136+
raise RaisedOnDestroy if raise_on_destroy
137+
end
138+
127139
# Record destruction so we can test whether firm.clients.clear has
128140
# is calling client.destroy, deleting from the database, or setting
129141
# foreign keys to NULL.

0 commit comments

Comments
 (0)