|
| 1 | +using Microsoft.Data.SqlClient; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Mid-session <c>USE</c> over the wire. The database-change ENVCHANGE (+ |
| 8 | +/// INFO 5701) must precede the response's final DONE — SqlClient's token |
| 9 | +/// reader stalls until command timeout on an ENVCHANGE that arrives after |
| 10 | +/// the last DONE (probe-confirmed 2026-07-15; go-mssqldb tolerates the late |
| 11 | +/// position, so only a real-SqlClient oracle catches the ordering). |
| 12 | +/// </summary> |
| 13 | +[TestClass] |
| 14 | +public sealed class UseDatabaseTests |
| 15 | +{ |
| 16 | + public TestContext TestContext { get; set; } = null!; |
| 17 | + |
| 18 | + [TestMethod] |
| 19 | + public async Task UseMaster_ExecuteNonQuery_SwitchesDatabase() |
| 20 | + { |
| 21 | + var simulation = new Simulation(); |
| 22 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 23 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 24 | + |
| 25 | + await using (var use = new SqlCommand("use [master]", connection)) |
| 26 | + _ = await use.ExecuteNonQueryAsync(TestContext.CancellationToken); |
| 27 | + |
| 28 | + AreEqual("master", connection.Database); |
| 29 | + await using var query = new SqlCommand("select db_name()", connection); |
| 30 | + AreEqual("master", await query.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 31 | + } |
| 32 | + |
| 33 | + [TestMethod] |
| 34 | + public async Task UseMaster_RaisesChangedDatabaseContextInfoMessage() |
| 35 | + { |
| 36 | + var simulation = new Simulation(); |
| 37 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 38 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 39 | + |
| 40 | + var messages = new List<string>(); |
| 41 | + connection.InfoMessage += (_, e) => messages.Add(e.Message); |
| 42 | + await using var use = new SqlCommand("use [master]", connection); |
| 43 | + _ = await use.ExecuteNonQueryAsync(TestContext.CancellationToken); |
| 44 | + |
| 45 | + Contains("Changed database context to 'master'.", messages); |
| 46 | + } |
| 47 | + |
| 48 | + [TestMethod] |
| 49 | + public async Task UseInMultiStatementBatch_WithResultSet() |
| 50 | + { |
| 51 | + var simulation = new Simulation(); |
| 52 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 53 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 54 | + |
| 55 | + await using var command = new SqlCommand("use [master] select db_name()", connection); |
| 56 | + AreEqual("master", await command.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 57 | + AreEqual("master", connection.Database); |
| 58 | + } |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + public async Task UseViaRpc_ParameterizedCommand_SwitchesDatabase() |
| 62 | + { |
| 63 | + var simulation = new Simulation(); |
| 64 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 65 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 66 | + |
| 67 | + // A parameter forces the sp_executesql RPC path instead of SQLBatch. |
| 68 | + await using var command = new SqlCommand("use [master] select @x", connection); |
| 69 | + _ = command.Parameters.AddWithValue("@x", 1); |
| 70 | + AreEqual(1, await command.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 71 | + AreEqual("master", connection.Database); |
| 72 | + } |
| 73 | + |
| 74 | + [TestMethod] |
| 75 | + public async Task UseThenError_StillSwitchesDatabase() |
| 76 | + { |
| 77 | + var simulation = new Simulation(); |
| 78 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 79 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 80 | + |
| 81 | + await using (var command = new SqlCommand("use [master] select * from nosuchtable", connection)) |
| 82 | + { |
| 83 | + var ex = await ThrowsExactlyAsync<SqlException>(() => command.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 84 | + AreEqual(208, ex.Number); |
| 85 | + } |
| 86 | + |
| 87 | + AreEqual("master", connection.Database); |
| 88 | + await using var query = new SqlCommand("select db_name()", connection); |
| 89 | + AreEqual("master", await query.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 90 | + } |
| 91 | + |
| 92 | + [TestMethod] |
| 93 | + public async Task UseRoundTrip_MasterAndBack() |
| 94 | + { |
| 95 | + var simulation = new Simulation(); |
| 96 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 97 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 98 | + |
| 99 | + await using (var toMaster = new SqlCommand("use [master]", connection)) |
| 100 | + _ = await toMaster.ExecuteNonQueryAsync(TestContext.CancellationToken); |
| 101 | + await using (var back = new SqlCommand("use [simulated]", connection)) |
| 102 | + _ = await back.ExecuteNonQueryAsync(TestContext.CancellationToken); |
| 103 | + |
| 104 | + AreEqual("simulated", connection.Database); |
| 105 | + await using var query = new SqlCommand("select db_name()", connection); |
| 106 | + AreEqual("simulated", await query.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 107 | + } |
| 108 | + |
| 109 | + [TestMethod] |
| 110 | + public async Task UseMissingDatabase_RaisesMsg911_SessionSurvives() |
| 111 | + { |
| 112 | + var simulation = new Simulation(); |
| 113 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 114 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 115 | + |
| 116 | + await using (var command = new SqlCommand("use [nosuchdb]", connection)) |
| 117 | + { |
| 118 | + var ex = await ThrowsExactlyAsync<SqlException>(() => command.ExecuteNonQueryAsync(TestContext.CancellationToken)); |
| 119 | + AreEqual(911, ex.Number); |
| 120 | + } |
| 121 | + |
| 122 | + AreEqual("simulated", connection.Database); |
| 123 | + await using var query = new SqlCommand("select db_name()", connection); |
| 124 | + AreEqual("simulated", await query.ExecuteScalarAsync(TestContext.CancellationToken)); |
| 125 | + } |
| 126 | +} |
0 commit comments