|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using Avalonia.Controls; |
| 5 | +using Avalonia.Input; |
| 6 | +using Avalonia.Interactivity; |
| 7 | +using Avalonia.LogicalTree; |
| 8 | +using PlanViewer.App.Controls; |
| 9 | +using PlanViewer.Core.Models; |
| 10 | +using PlanViewer.Core.Services; |
| 11 | + |
| 12 | +namespace PlanViewer.Core.Tests; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// #502: SQL Server caps StatementText at 4,000 characters when it writes showplan XML, so for a |
| 16 | +/// long query every consumer of the plan — advice, Copy Query Text, Open in Query Editor — was |
| 17 | +/// handing back a query that stops mid-statement. The reporter's symptom was the second-order one: |
| 18 | +/// the shortened text is not valid T-SQL, so re-running or formatting it failed with a syntax error |
| 19 | +/// that pointed nowhere near the truncation. |
| 20 | +/// |
| 21 | +/// <para>Two halves. The plan says so (Rule 39), and where the full query was captured alongside the |
| 22 | +/// plan it is handed out instead of the plan's short copy.</para> |
| 23 | +/// </summary> |
| 24 | +public class TruncatedStatementTextTests |
| 25 | +{ |
| 26 | + private const string CapturedQuery = "SELECT 'the full query the session actually ran';"; |
| 27 | + |
| 28 | + // --------------------------------------------------------------- |
| 29 | + // Rule 39: say so on the plan |
| 30 | + // --------------------------------------------------------------- |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void Rule39_FlagsAStatementThatHitTheCap() |
| 34 | + { |
| 35 | + var plan = AnalyzeStatementOfLength(PlanStatement.TruncationLengthThreshold); |
| 36 | + |
| 37 | + var warning = Assert.Single(PlanTestHelper.WarningsOfType(plan, "Truncated Query Text")); |
| 38 | + Assert.Equal(PlanWarningSeverity.Info, warning.Severity); |
| 39 | + Assert.Contains("4,000 characters", warning.Message); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void Rule39_SaysNothingAboutAQueryThatFits() |
| 44 | + { |
| 45 | + var plan = AnalyzeStatementOfLength(PlanStatement.TruncationLengthThreshold - 1); |
| 46 | + |
| 47 | + Assert.Empty(PlanTestHelper.WarningsOfType(plan, "Truncated Query Text")); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void Rule39_CanBeTurnedOff() |
| 52 | + { |
| 53 | + var stmt = StatementOfLength(PlanStatement.TruncationLengthThreshold); |
| 54 | + var plan = new ParsedPlan(); |
| 55 | + plan.Batches.Add(new PlanBatch { Statements = { stmt } }); |
| 56 | + |
| 57 | + PlanAnalyzer.Analyze(plan, new AnalyzerConfig { Rules = new RulesConfig { Disabled = { 39 } } }); |
| 58 | + |
| 59 | + Assert.Empty(PlanTestHelper.WarningsOfType(plan, "Truncated Query Text")); |
| 60 | + } |
| 61 | + |
| 62 | + // --------------------------------------------------------------- |
| 63 | + // Hand back the captured query instead of the plan's short copy |
| 64 | + // --------------------------------------------------------------- |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void SingleStatement_OpenInQueryEditor_HandsBackTheCapturedQuery() |
| 68 | + { |
| 69 | + HeadlessUi.Run(() => |
| 70 | + { |
| 71 | + var viewer = LoadPlan("isnull_plan.sqlplan", Truncated, CapturedQuery); |
| 72 | + |
| 73 | + Assert.Equal(CapturedQuery, OpenInEditorText(viewer)); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// A plan opened from a file has no captured text, and nothing can invent it — the short copy is |
| 79 | + /// all there is. Rule 39 is what tells the user why. |
| 80 | + /// </summary> |
| 81 | + [Fact] |
| 82 | + public void SingleStatement_WithNothingCaptured_StillHandsBackThePlanText() |
| 83 | + { |
| 84 | + HeadlessUi.Run(() => |
| 85 | + { |
| 86 | + var viewer = LoadPlan("isnull_plan.sqlplan", Truncated, queryText: null); |
| 87 | + |
| 88 | + var text = OpenInEditorText(viewer); |
| 89 | + Assert.NotEqual(CapturedQuery, text); |
| 90 | + Assert.True(text!.Length >= PlanStatement.TruncationLengthThreshold); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// The captured text is the whole batch and showplan records no statement offsets, so for a |
| 96 | + /// multi-statement plan there is no way to tell which slice belongs to the selected row. Handing |
| 97 | + /// back a confidently wrong statement would be worse than handing back a short one. |
| 98 | + /// </summary> |
| 99 | + [Fact] |
| 100 | + public void MultiStatement_KeepsThePlanTextEvenThoughItIsTruncated() |
| 101 | + { |
| 102 | + HeadlessUi.Run(() => |
| 103 | + { |
| 104 | + var viewer = LoadPlan("eager_table_spool_plan.sqlplan", Truncated, CapturedQuery); |
| 105 | + |
| 106 | + Assert.NotEqual(CapturedQuery, OpenInEditorText(viewer)); |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public void UntruncatedPlan_IsUnaffectedByAnyOfThis() |
| 112 | + { |
| 113 | + HeadlessUi.Run(() => |
| 114 | + { |
| 115 | + var viewer = LoadPlan("isnull_plan.sqlplan", xml => xml, CapturedQuery); |
| 116 | + |
| 117 | + Assert.NotEqual(CapturedQuery, OpenInEditorText(viewer)); |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + // --------------------------------------------------------------- |
| 122 | + // Helpers |
| 123 | + // --------------------------------------------------------------- |
| 124 | + |
| 125 | + private static PlanStatement StatementOfLength(int length) => |
| 126 | + new() { StatementText = new string('x', length), StatementType = "SELECT" }; |
| 127 | + |
| 128 | + private static ParsedPlan AnalyzeStatementOfLength(int length) |
| 129 | + { |
| 130 | + var plan = new ParsedPlan(); |
| 131 | + plan.Batches.Add(new PlanBatch { Statements = { StatementOfLength(length) } }); |
| 132 | + PlanAnalyzer.Analyze(plan); |
| 133 | + return plan; |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Pads the plan's first recorded statement past the cap, so the fixture looks the way a real |
| 138 | + /// capped plan looks. Letters and a space only — this goes back into an XML attribute. |
| 139 | + /// </summary> |
| 140 | + private static string Truncated(string xml) |
| 141 | + { |
| 142 | + const string marker = "StatementText=\""; |
| 143 | + var start = xml.IndexOf(marker, StringComparison.Ordinal); |
| 144 | + Assert.True(start >= 0, "fixture has no StatementText to pad"); |
| 145 | + |
| 146 | + var valueEnd = xml.IndexOf('"', start + marker.Length); |
| 147 | + Assert.True(valueEnd > start, "fixture's StatementText is not terminated"); |
| 148 | + |
| 149 | + return xml[..valueEnd] |
| 150 | + + " " + new string('x', PlanStatement.TruncationLengthThreshold) |
| 151 | + + xml[valueEnd..]; |
| 152 | + } |
| 153 | + |
| 154 | + private static PlanViewerControl LoadPlan(string planFileName, Func<string, string> shape, string? queryText) |
| 155 | + { |
| 156 | + var path = Path.Combine("Plans", planFileName); |
| 157 | + Assert.True(File.Exists(path), $"Test plan not found: {path}"); |
| 158 | + var xml = shape(File.ReadAllText(path).Replace("encoding=\"utf-16\"", "encoding=\"utf-8\"")); |
| 159 | + |
| 160 | + var viewer = new PlanViewerControl(); |
| 161 | + Assert.True(viewer.LoadPlan(xml, planFileName, queryText), $"Plan failed to load: {viewer.LastLoadError}"); |
| 162 | + |
| 163 | + return viewer; |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// Drives the menu entry the reporter used and returns the text it handed over. Goes through the |
| 168 | + /// real right-click so the Opening event configures the menu first, the way #467 established. |
| 169 | + /// </summary> |
| 170 | + private static string? OpenInEditorText(PlanViewerControl viewer) |
| 171 | + { |
| 172 | + var window = new Window { Content = viewer, Width = 1400, Height = 900 }; |
| 173 | + window.Show(); |
| 174 | + window.UpdateLayout(); |
| 175 | + |
| 176 | + var grid = viewer.GetLogicalDescendants().OfType<DataGrid>().First(g => g.Name == "StatementsGrid"); |
| 177 | + grid.RaiseEvent(new ContextRequestedEventArgs()); |
| 178 | + |
| 179 | + string? opened = null; |
| 180 | + viewer.OpenInEditorRequested += (_, text) => opened = text; |
| 181 | + |
| 182 | + grid.ContextMenu!.Items.OfType<MenuItem>().First(i => i.Name == "OpenStatementInEditorItem") |
| 183 | + .RaiseEvent(new RoutedEventArgs(MenuItem.ClickEvent)); |
| 184 | + |
| 185 | + return opened; |
| 186 | + } |
| 187 | +} |
0 commit comments