Skip to content

Commit a3475a2

Browse files
authored
Add savepoint placeholder for each savepoint created (#1179)
1 parent af26fc9 commit a3475a2

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

test/support/query_assertions.rb

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ def assert_queries_count(count = nil, include_schema: false, &block)
1010
queries = include_schema ? counter.log_all : counter.log
1111

1212
# Start of monkey-patch
13-
# Rails tests expect a save-point to be released at the end of the test. SQL Server does not release
14-
# save-points and so the number of queries will be off by one. This monkey patch adds a placeholder query
15-
# to the end of the queries array to account for the missing save-point release.
16-
queries.append "/* release savepoint placeholder for testing */" if queries.first =~ /SAVE TRANSACTION \S+/
13+
queries = include_release_savepoint_placeholder_queries(queries)
1714
# End of monkey-patch
1815

1916
if count
@@ -24,6 +21,29 @@ def assert_queries_count(count = nil, include_schema: false, &block)
2421
result
2522
end
2623
end
24+
25+
private
26+
27+
# Rails tests expect a save-point to be created and released. SQL Server does not release
28+
# save-points and so the number of queries will be off. This monkey patch adds a placeholder queries
29+
# to replace the missing save-point releases.
30+
def include_release_savepoint_placeholder_queries(queries)
31+
grouped_queries = [[]]
32+
33+
queries.each do |query|
34+
if query =~ /SAVE TRANSACTION \S+/
35+
grouped_queries << [query]
36+
else
37+
grouped_queries.last << query
38+
end
39+
end
40+
41+
grouped_queries.each do |group|
42+
group.append "/* release savepoint placeholder for testing */" if group.first =~ /SAVE TRANSACTION \S+/
43+
end
44+
45+
grouped_queries.flatten
46+
end
2747
end
2848
end
2949
end

0 commit comments

Comments
 (0)