22require 'models/car'
33
44class ShowplanTestSqlserver < ActiveRecord ::TestCase
5-
5+
66 fixtures :cars
7-
7+
88 context 'Unprepare previously prepared SQL' do
9-
9+
1010 should 'from simple statement' do
11- plan = Car . where ( id : 1 ) . explain
11+ plan = Car . where ( id : 1 ) . explain
1212 assert plan . starts_with? ( "EXPLAIN for: SELECT [cars].* FROM [cars] WHERE [cars].[id] = 1" )
1313 assert plan . include? ( "Clustered Index Seek" ) , 'make sure we do not showplan the sp_executesql'
1414 end
@@ -18,81 +18,56 @@ class ShowplanTestSqlserver < ActiveRecord::TestCase
1818 assert plan . starts_with? ( "EXPLAIN for: SELECT [cars].* FROM [cars] WHERE (\n id = 1 \n )" )
1919 assert plan . include? ( "Clustered Index Seek" ) , 'make sure we do not showplan the sp_executesql'
2020 end
21-
21+
2222 should 'from prepared statement ...' do
23- plan = capture_logger do
24- Car . where ( :name => ',' ) . limit ( 1 ) . explain
25- end
23+ plan = Car . where ( :name => ',' ) . limit ( 1 ) . explain
2624 assert plan . include? ( "SELECT TOP (1) [cars].* FROM [cars] WHERE [cars].[name] = N','" )
2725 assert plan . include? ( "TOP EXPRESSION" ) , 'make sure we do not showplan the sp_executesql'
2826 assert plan . include? ( "Clustered Index Scan" ) , 'make sure we do not showplan the sp_executesql'
2927 end
30-
28+
3129 end
32-
30+
3331 context 'With SHOWPLAN_TEXT option' do
34-
32+
3533 should 'use simple table printer' do
3634 with_showplan_option ( 'SHOWPLAN_TEXT' ) do
3735 plan = Car . where ( :id => 1 ) . explain
38- assert plan . starts_with? ( "EXPLAIN for: SELECT [cars].* FROM [cars] WHERE [cars].[id] = 1" )
36+ assert plan . starts_with? ( "EXPLAIN for: SELECT [cars].* FROM [cars] WHERE [cars].[id] = 1" )
3937 assert plan . include? ( "Clustered Index Seek" ) , 'make sure we do not showplan the sp_executesql'
4038 end
4139 end
42-
40+
4341 end
44-
42+
4543 context 'With SHOWPLAN_XML option' do
46-
44+
4745 should 'show formatted xml' do
4846 with_showplan_option ( 'SHOWPLAN_XML' ) do
4947 plan = Car . where ( :id => 1 ) . explain
5048 assert plan . include? ( 'ShowPlanXML' )
5149 end
5250 end
53-
51+
5452 end
55-
56-
53+
54+
5755 private
58-
56+
5957 def base
6058 ActiveRecord ::Base
6159 end
6260
6361 def connection
6462 base . connection
6563 end
66-
64+
6765 def with_showplan_option ( option )
6866 old_option = ActiveRecord ::ConnectionAdapters ::SQLServerAdapter . showplan_option
6967 ActiveRecord ::ConnectionAdapters ::SQLServerAdapter . showplan_option = option
7068 yield
7169 ensure
7270 ActiveRecord ::ConnectionAdapters ::SQLServerAdapter . showplan_option = old_option
7371 end
74-
75-
76- def capture_logger
77- original_logger = base . logger
78- log = StringIO . new
79- base . logger = Logger . new ( log )
80- base . logger . level = Logger ::WARN
81- yield
82- log . string
83- ensure
84- base . logger = original_logger
85- end
8672
87- def capture_queries
88- queries = Thread . current [ :available_queries_for_explain ] = [ ]
89- with_threshold ( 0 ) do
90- yield
91- end
92- queries
93- ensure
94- Thread . current [ :available_queries_for_explain ] = nil
95- end
96-
97-
9873end
0 commit comments