|
| 1 | +using System.Data; |
| 2 | +using System.Data.Common; |
| 3 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 4 | + |
| 5 | +namespace SqlServerSimulator; |
| 6 | + |
| 7 | +[TestClass] |
| 8 | +public class CommandTests |
| 9 | +{ |
| 10 | + private static DbCommand CreateCommand() => new Simulation().CreateDbConnection().CreateCommand(); |
| 11 | + |
| 12 | + private static DbCommand CreateCommand(string? commandText) => new Simulation().CreateCommand(commandText); |
| 13 | + |
| 14 | + private static DbConnection CreateOpenConnection() => new Simulation().CreateOpenConnection(); |
| 15 | + |
| 16 | + [TestMethod] |
| 17 | + public void DbConnectionPassThrough() |
| 18 | + { |
| 19 | + using var connection = new Simulation().CreateDbConnection(); |
| 20 | + using var command = connection.CreateCommand(); |
| 21 | + AreSame(connection, command.Connection); |
| 22 | + } |
| 23 | + |
| 24 | + [TestMethod] |
| 25 | + public void CreateCommandIsNotNull() => IsNotNull(CreateCommand()); |
| 26 | + |
| 27 | + [TestMethod] |
| 28 | + public void CommandTypeDefaultsText() => AreEqual(System.Data.CommandType.Text, CreateCommand().CommandType); |
| 29 | + |
| 30 | + [TestMethod] |
| 31 | + public void CreateCommandWithCommandText() |
| 32 | + { |
| 33 | + const string commandText = "select 1"; |
| 34 | + using var command = CreateCommand(commandText); |
| 35 | + AreEqual(commandText, command.CommandText); |
| 36 | + } |
| 37 | + |
| 38 | + [TestMethod] |
| 39 | + public void NullCommandTextConvertedToEmptyString() => AreSame(string.Empty, CreateCommand("").CommandText); |
| 40 | + |
| 41 | + [TestMethod] |
| 42 | + public void CommandTimeoutRangeCheck() |
| 43 | + { |
| 44 | + using var command = CreateCommand(); |
| 45 | + AreEqual(30, command.CommandTimeout); |
| 46 | + var exception = ThrowsExactly<ArgumentException>(() => command.CommandTimeout = -1); |
| 47 | + AreEqual(nameof(command.CommandTimeout), exception.ParamName); |
| 48 | + |
| 49 | + command.CommandTimeout = 5; |
| 50 | + AreEqual(5, command.CommandTimeout); |
| 51 | + } |
| 52 | + |
| 53 | + [TestMethod] |
| 54 | + public void DesignTimeVisibleBehavior() |
| 55 | + { |
| 56 | + using var command = CreateCommand(); |
| 57 | + IsTrue(command.DesignTimeVisible); |
| 58 | + command.DesignTimeVisible = false; |
| 59 | + IsFalse(command.DesignTimeVisible); |
| 60 | + command.DesignTimeVisible = true; |
| 61 | + IsTrue(command.DesignTimeVisible); |
| 62 | + } |
| 63 | + |
| 64 | + [TestMethod] |
| 65 | + public void TransactionPassThrough() |
| 66 | + { |
| 67 | + using var connection = CreateOpenConnection(); |
| 68 | + using var transaction = connection.BeginTransaction(); |
| 69 | + AreSame(transaction.Connection, connection); |
| 70 | + using var command = connection.CreateCommand(); |
| 71 | + IsNull(command.Transaction); |
| 72 | + |
| 73 | + command.Transaction = transaction; |
| 74 | + AreSame(transaction, command.Transaction); |
| 75 | + command.Transaction = null; |
| 76 | + IsNull(command.Transaction); |
| 77 | + } |
| 78 | + |
| 79 | + sealed class TestTransaction : DbTransaction |
| 80 | + { |
| 81 | + public override IsolationLevel IsolationLevel => throw new NotImplementedException(); |
| 82 | + protected override DbConnection? DbConnection => throw new NotImplementedException(); |
| 83 | + public override void Commit() => throw new NotImplementedException(); |
| 84 | + public override void Rollback() => throw new NotImplementedException(); |
| 85 | + } |
| 86 | + |
| 87 | + [TestMethod] |
| 88 | + public void TransactionRestrictions() |
| 89 | + { |
| 90 | + var simulation = new Simulation(); |
| 91 | + using var connection = simulation.CreateOpenConnection(); |
| 92 | + using (var sim1con2 = simulation.CreateOpenConnection()) |
| 93 | + { |
| 94 | + using var otherConnectionTransaction = sim1con2.BeginTransaction(); |
| 95 | + |
| 96 | + using var doomedCommand = connection.CreateCommand(); |
| 97 | + |
| 98 | + _ = Throws<Exception>(() => doomedCommand.Transaction = otherConnectionTransaction); |
| 99 | + } |
| 100 | + |
| 101 | + using var connection2 = CreateOpenConnection(); |
| 102 | + using var transaction = connection.BeginTransaction(); |
| 103 | + using var command2 = connection2.CreateCommand(); |
| 104 | + _ = Throws<Exception>(() => command2.Transaction = transaction); |
| 105 | + _ = Throws<Exception>(() => command2.Transaction = new TestTransaction()); |
| 106 | + } |
| 107 | +} |
0 commit comments