|
| 1 | +namespace Belin.Sql; |
| 2 | + |
| 3 | +using Belin.Sql.Fixtures; |
| 4 | +using System.Data.SQLite; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Tests the features of the <see cref="CommandBuilder"/> class. |
| 8 | +/// </summary> |
| 9 | +[TestClass] |
| 10 | +public sealed class CommandBuilderTests { |
| 11 | + |
| 12 | + /// <summary> |
| 13 | + /// The connection to the data source. |
| 14 | + /// </summary> |
| 15 | + private SQLiteConnection connection = default!; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// The test data. |
| 19 | + /// </summary> |
| 20 | + private readonly Character record = new() { Id = 1000, FirstName = "Cédric", Gender = CharacterGender.DarkLord }; |
| 21 | + |
| 22 | + [TestInitialize] |
| 23 | + public void TestInitialize() => connection = That.CreateInMemoryDatabase(); |
| 24 | + |
| 25 | + [TestCleanup] |
| 26 | + public void TestCleanup() => connection.Close(); |
| 27 | + |
| 28 | + [TestMethod] |
| 29 | + public void GetDeleteCommand() { |
| 30 | + var command = new CommandBuilder(connection).GetDeleteCommand(record); |
| 31 | + StartsWith(@"DELETE FROM ""main"".""Characters""", command.Text); |
| 32 | + EndsWith(@"WHERE ""ID"" = @ID", command.Text); |
| 33 | + |
| 34 | + var parameter = command.Parameters.Single(); |
| 35 | + AreEqual("@ID", parameter.Name); |
| 36 | + AreEqual(1000, parameter.Value); |
| 37 | + } |
| 38 | + |
| 39 | + [TestMethod] |
| 40 | + public void GetExistsCommand() { |
| 41 | + var command = new CommandBuilder(connection).GetExistsCommand<Character>(record.Id); |
| 42 | + StartsWith("SELECT 1", command.Text); |
| 43 | + Contains(@"FROM ""main"".""Characters""", command.Text); |
| 44 | + EndsWith(@"WHERE ""ID"" = @ID", command.Text); |
| 45 | + |
| 46 | + var parameter = command.Parameters.Single(); |
| 47 | + AreEqual("@ID", parameter.Name); |
| 48 | + AreEqual(1000, parameter.Value); |
| 49 | + } |
| 50 | + |
| 51 | + [TestMethod] |
| 52 | + public void GetFindCommand() { |
| 53 | + var builder = new CommandBuilder(connection); |
| 54 | + |
| 55 | + var command = builder.GetFindCommand<Character>(record.Id); |
| 56 | + StartsWith(@"SELECT """, command.Text); |
| 57 | + DoesNotContain("*", command.Text); |
| 58 | + Contains(@"FROM ""main"".""Characters""", command.Text); |
| 59 | + EndsWith(@"WHERE ""ID"" = @ID", command.Text); |
| 60 | + |
| 61 | + var parameter = command.Parameters.Single(); |
| 62 | + AreEqual("@ID", parameter.Name); |
| 63 | + AreEqual(1000, parameter.Value); |
| 64 | + |
| 65 | + command = builder.GetFindCommand<Character>(record.Id, ["firstName"]); |
| 66 | + StartsWith(@"SELECT ""firstName""", command.Text); |
| 67 | + DoesNotContain("gender", command.Text); |
| 68 | + DoesNotContain("lastName", command.Text); |
| 69 | + } |
| 70 | + |
| 71 | + [TestMethod] |
| 72 | + public void GetInsertCommand() { |
| 73 | + var command = new CommandBuilder(connection).GetInsertCommand(record); |
| 74 | + StartsWith(@"INSERT INTO ""main"".""Characters"" (", command.Text); |
| 75 | + Contains("VALUES (", command.Text); |
| 76 | + |
| 77 | + var parameters = command.Parameters; |
| 78 | + HasCount(3, parameters); |
| 79 | + AreEqual("Cédric", parameters["firstName"].Value); |
| 80 | + AreEqual(CharacterGender.DarkLord, parameters["gender"].Value); |
| 81 | + AreEqual("", parameters["lastName"].Value); |
| 82 | + } |
| 83 | + |
| 84 | + [TestMethod] |
| 85 | + public void GetUpdateCommand() { |
| 86 | + var builder = new CommandBuilder(connection); |
| 87 | + |
| 88 | + var command = builder.GetUpdateCommand(record); |
| 89 | + StartsWith(@"UPDATE ""main"".""Characters""", command.Text); |
| 90 | + Contains(@"SET """, command.Text); |
| 91 | + EndsWith(@"WHERE ""ID"" = @ID", command.Text); |
| 92 | + |
| 93 | + var parameters = command.Parameters; |
| 94 | + HasCount(4, parameters); |
| 95 | + AreEqual("Cédric", parameters["firstName"].Value); |
| 96 | + AreEqual(CharacterGender.DarkLord, parameters["gender"].Value); |
| 97 | + AreEqual(1000, parameters["ID"].Value); |
| 98 | + AreEqual("", parameters["lastName"].Value); |
| 99 | + |
| 100 | + HasCount(2, builder.GetUpdateCommand(record, "lastName").Parameters); |
| 101 | + } |
| 102 | +} |
0 commit comments