|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data.Common; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using System.Reflection; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Text; |
| 8 | +using Microsoft.Data.SqlClient; |
| 9 | +using RepoDb.Enumerations; |
| 10 | +using RepoDb.Extensions; |
| 11 | +using RepoDb.IntegrationTests; |
| 12 | +using RepoDb.IntegrationTests.Models; |
| 13 | +using RepoDb.IntegrationTests.Setup; |
| 14 | +using RepoDb.Trace; |
| 15 | + |
| 16 | +namespace RepoDb.IntegrationTests.Operations; |
| 17 | + |
| 18 | +[TestClass] |
| 19 | +public class CallThemAllTests : TestBase |
| 20 | +{ |
| 21 | + protected override void InitializeCore() |
| 22 | + { |
| 23 | + base.InitializeCore(); |
| 24 | + Database.Cleanup(); |
| 25 | + |
| 26 | + using var db = CreateOpenConnection(); |
| 27 | + db.Insert(new IdentityTable()); |
| 28 | + } |
| 29 | + public static IEnumerable<MethodInfo> AllDbOperations = typeof(DbConnectionExtension).GetMethods() |
| 30 | + .Where(x => x.IsPublic && x.IsStatic) |
| 31 | + .Where(x => x.GetParameters()[0].ParameterType.IsAssignableFrom(typeof(DbConnection))); |
| 32 | + |
| 33 | + |
| 34 | + public static IEnumerable<object[]> AllDbOperationsNonAsync => |
| 35 | + AllDbOperations.Where(x => x.Name.EndsWith("Async") == false) |
| 36 | + .Select(x => new[] { x }); |
| 37 | + |
| 38 | + public static IEnumerable<object[]> AllDbOperationsAsync => |
| 39 | + AllDbOperations.Where(x => x.Name.EndsWith("Async") == true) |
| 40 | + .Select(x => new[] { x }); |
| 41 | + |
| 42 | + public static string GetDisplayName(MethodInfo call, object[] info) |
| 43 | + { |
| 44 | + var mi = (MethodInfo)info[0]; |
| 45 | + return $"{call.Name} {mi.Name}({string.Join(", ", mi.GetParameters().Select(x => x.ParameterType))})"; |
| 46 | + } |
| 47 | + |
| 48 | + [TestMethod, DynamicData(nameof(AllDbOperationsNonAsync), DynamicDataDisplayName = nameof(GetDisplayName))] |
| 49 | + public void DbConnectionOperation(MethodInfo mi) |
| 50 | + { |
| 51 | + using var db = mi.Name.StartsWith("Truncate") ? CreateOpenConnection() : CreateOpenLimitedConnection(); |
| 52 | + var parameters = mi.GetParameters(); |
| 53 | + var args = new object?[parameters.Length]; |
| 54 | + mi = SetupCall(mi, db, ref parameters, args); |
| 55 | + |
| 56 | + var r = mi.Invoke(null, args); |
| 57 | + |
| 58 | + if (r is System.Collections.IEnumerable enumerable) |
| 59 | + { |
| 60 | + foreach (var i in enumerable) |
| 61 | + { |
| 62 | + GC.KeepAlive(i); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + [TestMethod, DynamicData(nameof(AllDbOperationsAsync), DynamicDataDisplayName = nameof(GetDisplayName))] |
| 69 | + public async Task DbConnectionOperationAsync(MethodInfo mi) |
| 70 | + { |
| 71 | + using var db = mi.Name.StartsWith("Truncate") ? await CreateOpenConnectionAsync() : await CreateOpenLimitedConnectionAsync(); |
| 72 | + |
| 73 | + var parameters = mi.GetParameters(); |
| 74 | + var args = new object?[parameters.Length]; |
| 75 | + mi = SetupCall(mi, db, ref parameters, args); |
| 76 | + |
| 77 | + var r = mi.Invoke(null, args); |
| 78 | + |
| 79 | + if (r is not Task tsk) |
| 80 | + { |
| 81 | + var type = r.GetType(); |
| 82 | + if (type.GetInterfaces().FirstOrDefault(x=>x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IAsyncEnumerable<>)) is { } enumType) |
| 83 | + { |
| 84 | + tsk = (Task)typeof(CallThemAllTests).GetMethod(nameof(Walk)).MakeGenericMethod(enumType.GetGenericArguments()).Invoke(this, [r]); |
| 85 | + } |
| 86 | + else |
| 87 | + tsk = (Task)type.GetMethod("AsTask").Invoke(r, []); |
| 88 | + } |
| 89 | + await tsk; |
| 90 | + } |
| 91 | + |
| 92 | + public async Task Walk<T>(IAsyncEnumerable<T> v) |
| 93 | + { |
| 94 | + await foreach(var i in v) |
| 95 | + { |
| 96 | + GC.KeepAlive(i); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + public static IEnumerable<MethodInfo> AllRepoOperations = typeof(DbRepository<SqlConnection>).GetMethods() |
| 102 | + .Where(x=>x.IsPublic && !x.IsStatic); |
| 103 | + |
| 104 | + public static IEnumerable<object[]> AllRepoOperationsNonAync => |
| 105 | + AllRepoOperations.Where(x => x.Name.EndsWith("Async") == false) |
| 106 | + .Select(x => new[] { x }); |
| 107 | + |
| 108 | + public static IEnumerable<object[]> AllRepoOperationsAsync => |
| 109 | + AllRepoOperations.Where(x => x.Name.EndsWith("Async") == true) |
| 110 | + .Select(x => new[] { x }); |
| 111 | + |
| 112 | + |
| 113 | + [TestMethod, DynamicData(nameof(AllRepoOperationsNonAync), DynamicDataDisplayName = nameof(GetDisplayName))] |
| 114 | + public void RepoConnectionOperation(MethodInfo mi) |
| 115 | + { |
| 116 | + using var repo = new DbRepository<SqlConnection>(mi.Name.StartsWith("Truncate") ? DbInstance.ConnectionString : DbInstance.LimitedConnectionString); |
| 117 | + var parameters = mi.GetParameters(); |
| 118 | + var args = new object?[parameters.Length]; |
| 119 | + mi = SetupCall(mi, null, ref parameters, args); |
| 120 | + |
| 121 | + mi.Invoke(repo, args); |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + [TestMethod, DynamicData(nameof(AllRepoOperationsAsync), DynamicDataDisplayName = nameof(GetDisplayName))] |
| 126 | + public async Task RepoConnectionOperationAsync(MethodInfo mi) |
| 127 | + { |
| 128 | + using var repo = new DbRepository<SqlConnection>(mi.Name.StartsWith("Truncate") ? DbInstance.ConnectionString : DbInstance.LimitedConnectionString); |
| 129 | + var parameters = mi.GetParameters(); |
| 130 | + var args = new object?[parameters.Length]; |
| 131 | + mi = SetupCall(mi, null, ref parameters, args); |
| 132 | + |
| 133 | + |
| 134 | + var r = mi.Invoke(repo, args); |
| 135 | + |
| 136 | + if (r is not Task tsk) |
| 137 | + { |
| 138 | + var type = r.GetType(); |
| 139 | + if (type.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IAsyncEnumerable<>)) is { } enumType) |
| 140 | + { |
| 141 | + tsk = (Task)typeof(CallThemAllTests).GetMethod(nameof(Walk)).MakeGenericMethod(enumType.GetGenericArguments()).Invoke(this, [r]); |
| 142 | + } |
| 143 | + else |
| 144 | + tsk = (Task)type.GetMethod("AsTask").Invoke(r, []); |
| 145 | + } |
| 146 | + await tsk; |
| 147 | + } |
| 148 | + |
| 149 | + public static IEnumerable<MethodInfo> AllBaseRepoOperations = typeof(BaseRepository<IdentityTable, SqlConnection>).GetMethods() |
| 150 | + .Where(x => x.IsPublic && !x.IsStatic); |
| 151 | + |
| 152 | + public static IEnumerable<object[]> AllBaseRepoOperationsNonAync => |
| 153 | + AllBaseRepoOperations.Where(x => x.Name.EndsWith("Async") == false) |
| 154 | + .Select(x => new[] { x }); |
| 155 | + |
| 156 | + public static IEnumerable<object[]> AllBaseRepoOperationsAsync => |
| 157 | + AllBaseRepoOperations.Where(x => x.Name.EndsWith("Async") == true) |
| 158 | + .Select(x => new[] { x }); |
| 159 | + |
| 160 | + |
| 161 | + class MyRepository<TEntity, TConnection> : BaseRepository<IdentityTable, SqlConnection> |
| 162 | + where TEntity : class |
| 163 | + where TConnection : DbConnection, new() |
| 164 | + { |
| 165 | + public MyRepository(string connectionString, ConnectionPersistency connectionPersistency) |
| 166 | + : base(connectionString, null, connectionPersistency, null, Constant.DefaultCacheItemExpirationInMinutes, null, null) |
| 167 | + { |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + [TestMethod, DynamicData(nameof(AllBaseRepoOperationsNonAync), DynamicDataDisplayName = nameof(GetDisplayName))] |
| 172 | + public void BaseRepoConnectionOperation(MethodInfo mi) |
| 173 | + { |
| 174 | + var BaseRepo = new MyRepository<IdentityTable, SqlConnection>(mi.Name.StartsWith("Truncate") ? DbInstance.ConnectionString : DbInstance.LimitedConnectionString, ConnectionPersistency.PerCall); |
| 175 | + var parameters = mi.GetParameters(); |
| 176 | + var args = new object?[parameters.Length]; |
| 177 | + mi = SetupCall(mi, null, ref parameters, args); |
| 178 | + |
| 179 | + mi.Invoke(BaseRepo, args); |
| 180 | + } |
| 181 | + |
| 182 | + |
| 183 | + [TestMethod, DynamicData(nameof(AllBaseRepoOperationsAsync), DynamicDataDisplayName = nameof(GetDisplayName))] |
| 184 | + public async Task BaseRepoConnectionOperationAsync(MethodInfo mi) |
| 185 | + { |
| 186 | + var BaseRepo = new MyRepository<IdentityTable, SqlConnection>(mi.Name.StartsWith("Truncate") ? DbInstance.ConnectionString : DbInstance.LimitedConnectionString, ConnectionPersistency.PerCall); |
| 187 | + var parameters = mi.GetParameters(); |
| 188 | + var args = new object?[parameters.Length]; |
| 189 | + mi = SetupCall(mi, null, ref parameters, args); |
| 190 | + |
| 191 | + var r = mi.Invoke(BaseRepo, args); |
| 192 | + |
| 193 | + if (r is not Task tsk) |
| 194 | + { |
| 195 | + var type = r.GetType(); |
| 196 | + if (type.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IAsyncEnumerable<>)) is { } enumType) |
| 197 | + { |
| 198 | + tsk = (Task)typeof(CallThemAllTests).GetMethod(nameof(Walk)).MakeGenericMethod(enumType.GetGenericArguments()).Invoke(this, [r]); |
| 199 | + } |
| 200 | + else |
| 201 | + tsk = (Task)type.GetMethod("AsTask").Invoke(r, []); |
| 202 | + } |
| 203 | + await tsk; |
| 204 | + } |
| 205 | + |
| 206 | + private static MethodInfo SetupCall(MethodInfo mi, DbConnection? db, ref ParameterInfo[] parameters, object?[] args) |
| 207 | + { |
| 208 | + if (mi.IsGenericMethod) |
| 209 | + { |
| 210 | + var ga = mi.GetGenericArguments(); |
| 211 | + |
| 212 | + if (ga.Length == 1 && ga[0].Name is "TEntity" or "TWhat") |
| 213 | + mi = mi.MakeGenericMethod(typeof(IdentityTable)); |
| 214 | + else if (ga.Length == 1 && ga[0].Name is "TResult" or "TKey") |
| 215 | + mi = mi.MakeGenericMethod(typeof(int?)); |
| 216 | + else if (ga.Length == 2 && ga[0].Name == "TEntity" && ga[1].Name is "TResult" or "TKey") |
| 217 | + mi = mi.MakeGenericMethod(typeof(IdentityTable), typeof(int?)); |
| 218 | + else if (ga.Length == 2 && ga[0].Name == "TEntity" && ga[1].Name == "TWhat") |
| 219 | + mi = mi.MakeGenericMethod(typeof(IdentityTable), typeof(IdentityTable)); |
| 220 | + else if (ga.All(x => x.Name.StartsWith("T") && x.Name.Substring(1).All(char.IsDigit))) |
| 221 | + { |
| 222 | + mi = mi.MakeGenericMethod(ga.Select(x => typeof(IdentityTable)).ToArray()); |
| 223 | + } |
| 224 | + parameters = mi.GetParameters(); |
| 225 | + } |
| 226 | + if (db is { }) |
| 227 | + { |
| 228 | + args[0] = db; |
| 229 | + } |
| 230 | + |
| 231 | + for (int i = 0; i < parameters.Length; i++) |
| 232 | + { |
| 233 | + var qp = parameters[i]; |
| 234 | + |
| 235 | + object? value = Convert.IsDBNull(qp.DefaultValue) ? null : qp.DefaultValue; |
| 236 | + |
| 237 | + var an = qp.Name.ToLowerInvariant(); |
| 238 | + |
| 239 | + while (an.Length > 0 && char.IsDigit(an[an.Length - 1])) |
| 240 | + an = an.Substring(0, an.Length - 1); |
| 241 | + |
| 242 | + switch (an) |
| 243 | + { |
| 244 | + case "tablename": |
| 245 | + value = nameof(IdentityTable); |
| 246 | + break; |
| 247 | + case "commandtext": |
| 248 | + value = "SELECT 1 AS Id;SELECT 1 AS Id;SELECT 1 AS Id;SELECT 1 AS Id;SELECT 1 AS Id;SELECT 1 AS Id;SELECT 1 AS Id;SELECT 1 AS Id;"; |
| 249 | + break; |
| 250 | + case "connection": |
| 251 | + value = db; |
| 252 | + break; |
| 253 | + case "entities": |
| 254 | + value = new[] |
| 255 | + { |
| 256 | + new IdentityTable(), |
| 257 | + new IdentityTable() |
| 258 | + }; |
| 259 | + break; |
| 260 | + case "keys" when qp.ParameterType.IsAssignableFrom(typeof(int?[])): |
| 261 | + value = new int?[] { 1, 2, 3 }; |
| 262 | + break; |
| 263 | + case "keys" when qp.ParameterType.IsAssignableFrom(typeof(object?[])): |
| 264 | + value = new object?[] { 1, 2, 3 }; |
| 265 | + break; |
| 266 | + case "entity": |
| 267 | + value = new IdentityTable(); |
| 268 | + break; |
| 269 | + case "qualifier" when !qp.HasDefaultValue: |
| 270 | + value = new Field(nameof(IdentityTable.Id)); |
| 271 | + break; |
| 272 | + case "qualifiers" when !qp.HasDefaultValue && qp.ParameterType.IsAssignableFrom(typeof(FieldSet)): |
| 273 | + value = new Field(nameof(IdentityTable.Id)).AsEnumerable(); |
| 274 | + break; |
| 275 | + case "qualifiers" when !qp.HasDefaultValue && qp.ParameterType.IsAssignableFrom(typeof(Expression<Func<IdentityTable, object?>>)): |
| 276 | + value = (Expression<Func<IdentityTable, object?>>)(x => x.Id); |
| 277 | + break; |
| 278 | + case "field" when !qp.HasDefaultValue && qp.ParameterType.IsAssignableFrom(typeof(Field)): |
| 279 | + value = new Field(nameof(IdentityTable.ColumnInt)); |
| 280 | + break; |
| 281 | + case "field" when !qp.HasDefaultValue && qp.ParameterType.IsAssignableFrom(typeof(Expression<Func<IdentityTable, object?>>)): |
| 282 | + value = (Expression<Func<IdentityTable, object?>>)(x => x.ColumnInt); |
| 283 | + break; |
| 284 | + case "field" when !qp.HasDefaultValue && qp.ParameterType.IsAssignableFrom(typeof(Expression<Func<IdentityTable, int?>>)): |
| 285 | + value = (Expression<Func<IdentityTable, int?>>)(x => x.ColumnInt); |
| 286 | + break; |
| 287 | + } |
| 288 | + |
| 289 | + args[i] = value; |
| 290 | + } |
| 291 | + |
| 292 | + return mi; |
| 293 | + } |
| 294 | +} |
0 commit comments