Exemplo n.º 1
0
        public IDataReader ExecuteReader(MockDbCommand command)
        {
            var setup = FindSetup(command, nameof(Query), nameof(QuerySingle));

            var methodCall       = (MethodCallExpression)setup?.Body;
            var method           = methodCall?.Method ?? queryObjectMethod;
            var parametersLookup = command.GetParameterLookup();
            var parametersArray  = (from param in method.GetParameters()
                                    let commandValue = parametersLookup.ContainsKey(param.Name)
                                    ? parametersLookup[param.Name]
                                    : param.DefaultValue
                                                       select commandValue).ToArray();

            var result = method.Invoke(this, parametersArray);
            var reader = result as IDataReader;

            if (result == null)
            {
                if (method.Name == nameof(QuerySingle))
                {
                    return(GetQuerySingleDataReader(command, method.GetGenericArguments()[0]));
                }

                return(GetEmptyDataReader(command));
            }

            return(reader ?? result.GetDataReader());
        }
Exemplo n.º 2
0
        public int ExecuteNonQuery(MockDbCommand command)
        {
            var parametersLookup = command.GetParameterLookup();
            var parametersArray  = (from param in executeMethod.GetParameters()
                                    let commandValue = parametersLookup.ContainsKey(param.Name)
                                     ? parametersLookup[param.Name]
                                     : param.DefaultValue
                                                       select commandValue).ToArray();

            return((int)executeMethod.Invoke(this, parametersArray));
        }
Exemplo n.º 3
0
        internal object ExecuteScalar(MockDbCommand command, bool isAsync, CancellationToken cancellationToken, MethodBase dapperMethod)
        {
            var method           = _methodFinder.FindUserMethodFromMethodInCallStack(dapperMethod, new Type[0]);
            var parametersLookup = command.GetParameterLookup(isAsync, cancellationToken);
            var parametersArray  = method.GetValues(parametersLookup);

            var value = new ScalarValue(isAsync, method, parametersArray, this);

            return(method.IsGenericMethod
                ? value
                : value.ToType(typeof(object), null));
        }
Exemplo n.º 4
0
        internal int ExecuteNonQuery(MockDbCommand command, bool isAsync, CancellationToken cancellationToken, MethodBase dapperMethod, Type dataType)
        {
            var genericArguments = dataType == null
                ? new Type[0]
                : new[] { dataType };
            var method           = _methodFinder.FindUserMethodFromMethodInCallStack(dapperMethod, genericArguments);
            var parametersLookup = command.GetParameterLookup(isAsync, cancellationToken);
            var parametersArray  = method.GetValues(parametersLookup);

            return(isAsync
                ? (int)TaskHelper.GetResultOfTask(method.Invoke(this, parametersArray))
                : (int)method.Invoke(this, parametersArray));
        }
        public bool CommandMatchesExpression(MockDbCommand command)
        {
            var visitor           = new MatchAnonymousObjectExpressionVisitor();
            var comparisonVisitor = (MethodCallExpression)visitor.Visit(methodCallExpression);

            var call = comparisonVisitor.Method.GetParameters()
                       .Zip(comparisonVisitor.Arguments, (methodArg, callArg) => new { methodArg, callArg })
                       .ToDictionary(a => a.methodArg, a => a.callArg);

            var expectedParameters = command.GetParameterLookup();
            var parameterValues    = from arg in call
                                     let match = ResolveToMatch(arg.Value as MethodCallExpression)
                                                 let paramValue = expectedParameters.ContainsKey(arg.Key.Name)
                    ? expectedParameters[arg.Key.Name]
                    : null
                                                                  select new { arg, matches = match.Matches(paramValue) };

            return(parameterValues.All(a => a.matches));
        }
Exemplo n.º 6
0
        internal IDataReader ExecuteReader(MockDbCommand command, bool isAsync, CancellationToken cancellationToken, MethodBase dapperMethod, params Type[] dataTypes)
        {
            var method           = _methodFinder.FindUserMethodFromMethodInCallStack(dapperMethod, dataTypes);
            var parametersLookup = command.GetParameterLookup(isAsync, cancellationToken);
            var parametersArray  = method.GetValues(parametersLookup);

            var result = method.Invoke(this, parametersArray);
            var reader = result as IDataReader;

            if (result == null)
            {
                if (IsSingleResultMethod(method))
                {
                    return(GetQuerySingleDataReader(method.GetGenericArguments()[0]));
                }

                return(GetEmptyDataReader(command));
            }

            return(reader ?? result.GetDataReader());
        }