|
| 1 | +using System; |
| 2 | +using System.Data.SQLite; |
| 3 | +using System.Linq; |
| 4 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 5 | +using RepoDb.SqLite.IntegrationTests.Models; |
| 6 | +using RepoDb.SqLite.IntegrationTests.Setup; |
| 7 | + |
| 8 | +namespace RepoDb.SqLite.IntegrationTests.SDS |
| 9 | +{ |
| 10 | + [TestClass] |
| 11 | + public class DbHelperTests |
| 12 | + { |
| 13 | + [TestInitialize] |
| 14 | + public void Initialize() |
| 15 | + { |
| 16 | + Database.Initialize(); |
| 17 | + Cleanup(); |
| 18 | + } |
| 19 | + |
| 20 | + [TestCleanup] |
| 21 | + public void Cleanup() |
| 22 | + { |
| 23 | + Database.Cleanup(); |
| 24 | + } |
| 25 | + |
| 26 | + #region GetFields |
| 27 | + |
| 28 | + #region Sync |
| 29 | + |
| 30 | + [TestMethod] |
| 31 | + public void TestDbHelperGetFields() |
| 32 | + { |
| 33 | + using (var connection = new SQLiteConnection(Database.ConnectionStringSDS)) |
| 34 | + { |
| 35 | + // Setup |
| 36 | + var helper = connection.GetDbHelper(); |
| 37 | + var tables = Database.CreateSdsCompleteTables(10, connection); |
| 38 | + |
| 39 | + // Act |
| 40 | + var fields = helper.GetFields(connection, "SdsCompleteTable", null); |
| 41 | + |
| 42 | + // Assert |
| 43 | + using (var reader = connection.ExecuteReader("pragma table_info([SdsCompleteTable]);")) |
| 44 | + { |
| 45 | + var fieldCount = 0; |
| 46 | + |
| 47 | + while (reader.Read()) |
| 48 | + { |
| 49 | + var name = reader.GetString(1); |
| 50 | + var field = fields.FirstOrDefault(f => string.Equals(f.Name, name, StringComparison.OrdinalIgnoreCase)); |
| 51 | + |
| 52 | + // Assert |
| 53 | + Assert.IsNotNull(field); |
| 54 | + |
| 55 | + fieldCount++; |
| 56 | + } |
| 57 | + |
| 58 | + // Assert |
| 59 | + Assert.AreEqual(fieldCount, fields.Count()); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + [TestMethod] |
| 65 | + public void TestDbHelperGetFieldsPrimary() |
| 66 | + { |
| 67 | + using (var connection = new SQLiteConnection(Database.ConnectionStringSDS)) |
| 68 | + { |
| 69 | + // Setup |
| 70 | + var helper = connection.GetDbHelper(); |
| 71 | + var tables = Database.CreateSdsCompleteTables(10, connection); |
| 72 | + |
| 73 | + // Act |
| 74 | + var fields = helper.GetFields(connection, "SdsCompleteTable", null); |
| 75 | + var primary = fields.FirstOrDefault(f => f.IsPrimary == true); |
| 76 | + |
| 77 | + // Assert |
| 78 | + Assert.IsNotNull(primary); |
| 79 | + Assert.AreEqual("Id", primary.Name); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + [TestMethod] |
| 84 | + public void TestDbHelperGetFieldsIdentity() |
| 85 | + { |
| 86 | + using (var connection = new SQLiteConnection(Database.ConnectionStringSDS)) |
| 87 | + { |
| 88 | + // Setup |
| 89 | + var helper = connection.GetDbHelper(); |
| 90 | + var tables = Database.CreateSdsCompleteTables(10, connection); |
| 91 | + |
| 92 | + // Act |
| 93 | + var fields = helper.GetFields(connection, "SdsCompleteTable", null); |
| 94 | + var primary = fields.FirstOrDefault(f => f.IsIdentity == true); |
| 95 | + |
| 96 | + // Assert |
| 97 | + Assert.IsNotNull(primary); |
| 98 | + Assert.AreEqual("Id", primary.Name); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + #endregion |
| 103 | + |
| 104 | + #region Async |
| 105 | + |
| 106 | + [TestMethod] |
| 107 | + public void TestDbHelperGetFieldsAsync() |
| 108 | + { |
| 109 | + using (var connection = new SQLiteConnection(Database.ConnectionStringSDS)) |
| 110 | + { |
| 111 | + // Setup |
| 112 | + var helper = connection.GetDbHelper(); |
| 113 | + var tables = Database.CreateSdsCompleteTables(10, connection); |
| 114 | + |
| 115 | + // Act |
| 116 | + var fields = helper.GetFieldsAsync(connection, "SdsCompleteTable", null).Result; |
| 117 | + |
| 118 | + // Assert |
| 119 | + using (var reader = connection.ExecuteReader("pragma table_info([SdsCompleteTable]);")) |
| 120 | + { |
| 121 | + var fieldCount = 0; |
| 122 | + |
| 123 | + while (reader.Read()) |
| 124 | + { |
| 125 | + var name = reader.GetString(1); |
| 126 | + var field = fields.FirstOrDefault(f => string.Equals(f.Name, name, StringComparison.OrdinalIgnoreCase)); |
| 127 | + |
| 128 | + // Assert |
| 129 | + Assert.IsNotNull(field); |
| 130 | + |
| 131 | + fieldCount++; |
| 132 | + } |
| 133 | + |
| 134 | + // Assert |
| 135 | + Assert.AreEqual(fieldCount, fields.Count()); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + [TestMethod] |
| 141 | + public void TestDbHelperGetFieldsAsyncPrimary() |
| 142 | + { |
| 143 | + using (var connection = new SQLiteConnection(Database.ConnectionStringSDS)) |
| 144 | + { |
| 145 | + // Setup |
| 146 | + var helper = connection.GetDbHelper(); |
| 147 | + var tables = Database.CreateSdsCompleteTables(10, connection); |
| 148 | + |
| 149 | + // Act |
| 150 | + var fields = helper.GetFieldsAsync(connection, "SdsCompleteTable", null).Result; |
| 151 | + var primary = fields.FirstOrDefault(f => f.IsPrimary == true); |
| 152 | + |
| 153 | + // Assert |
| 154 | + Assert.IsNotNull(primary); |
| 155 | + Assert.AreEqual("Id", primary.Name); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + [TestMethod] |
| 160 | + public void TestDbHelperGetFieldsAsyncIdentity() |
| 161 | + { |
| 162 | + using (var connection = new SQLiteConnection(Database.ConnectionStringSDS)) |
| 163 | + { |
| 164 | + // Setup |
| 165 | + var helper = connection.GetDbHelper(); |
| 166 | + var tables = Database.CreateSdsCompleteTables(10, connection); |
| 167 | + |
| 168 | + // Act |
| 169 | + var fields = helper.GetFieldsAsync(connection, "SdsCompleteTable", null).Result; |
| 170 | + var primary = fields.FirstOrDefault(f => f.IsIdentity == true); |
| 171 | + |
| 172 | + // Assert |
| 173 | + Assert.IsNotNull(primary); |
| 174 | + Assert.AreEqual("Id", primary.Name); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + #endregion |
| 179 | + |
| 180 | + #endregion |
| 181 | + |
| 182 | + #region GetScopeIdentity |
| 183 | + |
| 184 | + #region Sync |
| 185 | + |
| 186 | + [TestMethod] |
| 187 | + public void TestDbHelperGetScopeIdentity() |
| 188 | + { |
| 189 | + using (var connection = new SQLiteConnection(Database.ConnectionStringSDS)) |
| 190 | + { |
| 191 | + // Create the tables |
| 192 | + Database.CreateSdsTables(connection); |
| 193 | + |
| 194 | + // Setup |
| 195 | + var helper = connection.GetDbHelper(); |
| 196 | + var table = Helper.CreateSdsCompleteTables(1).First(); |
| 197 | + |
| 198 | + // Act |
| 199 | + var insertResult = connection.Insert<SdsCompleteTable>(table); |
| 200 | + |
| 201 | + // Assert |
| 202 | + Assert.IsTrue(Convert.ToInt64(insertResult) > 0); |
| 203 | + Assert.IsTrue(table.Id > 0); |
| 204 | + |
| 205 | + // Act |
| 206 | + var result = helper.GetScopeIdentity(connection, null); |
| 207 | + |
| 208 | + // Assert |
| 209 | + Assert.AreEqual(insertResult, result); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + #endregion |
| 214 | + |
| 215 | + #region Async |
| 216 | + |
| 217 | + [TestMethod] |
| 218 | + public void TestDbHelperGetScopeIdentityAsync() |
| 219 | + { |
| 220 | + using (var connection = new SQLiteConnection(Database.ConnectionStringSDS)) |
| 221 | + { |
| 222 | + // Create the tables |
| 223 | + Database.CreateSdsTables(connection); |
| 224 | + |
| 225 | + // Setup |
| 226 | + var helper = connection.GetDbHelper(); |
| 227 | + var table = Helper.CreateSdsCompleteTables(1).First(); |
| 228 | + |
| 229 | + // Act |
| 230 | + var insertResult = connection.Insert<SdsCompleteTable>(table); |
| 231 | + |
| 232 | + // Assert |
| 233 | + Assert.IsTrue(Convert.ToInt64(insertResult) > 0); |
| 234 | + Assert.IsTrue(table.Id > 0); |
| 235 | + |
| 236 | + // Act |
| 237 | + var result = helper.GetScopeIdentityAsync(connection, null).Result; |
| 238 | + |
| 239 | + // Assert |
| 240 | + Assert.AreEqual(insertResult, result); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + #endregion |
| 245 | + |
| 246 | + #endregion |
| 247 | + } |
| 248 | +} |
0 commit comments