Exemple #1
0
        /// <summary>
        ///     Sets up FromSqlRaw invocations containing a specified sql string and parameters to return a specified result.
        /// </summary>
        /// <typeparam name="T">The queryable source type.</typeparam>
        /// <param name="mockedQueryProvider">The mocked query provider.</param>
        /// <param name="sql">The FromSqlRaw sql string. Set up supports case insensitive partial matches.</param>
        /// <param name="parameters">The FromSqlRaw parameters. Set up supports case insensitive partial parameter sequence matching.</param>
        /// <param name="fromSqlResult">The sequence to return when FromSqlRaw is invoked.</param>
        /// <returns>The mocked query provider.</returns>
        public static IQueryProvider AddFromSqlRawResult <T>(this IQueryProvider mockedQueryProvider, string sql, IEnumerable <object> parameters, IEnumerable <T> fromSqlResult)
            where T : class
        {
            EnsureArgument.IsNotNull(mockedQueryProvider, nameof(mockedQueryProvider));
            EnsureArgument.IsNotNull(sql, nameof(sql));
            EnsureArgument.IsNotNull(parameters, nameof(parameters));
            EnsureArgument.IsNotNull(fromSqlResult, nameof(fromSqlResult));

            Logger.LogDebug("Setting up '{sql}'", sql);

            var createQueryResult = new AsyncEnumerable <T>(fromSqlResult);

            mockedQueryProvider.Configure()
            .CreateQuery <T>(Arg.Is <FromSqlQueryRootExpression>(fsqre => ProjectExpressionHelper.SqlAndParametersMatchFromSqlExpression(sql, parameters, fsqre)))
            .Returns(callInfo =>
            {
                ProjectExpressionHelper.ThrowIfExpressionIsNotSupported(callInfo.Arg <Expression>());

                var fsqre = (FromSqlQueryRootExpression)callInfo.Arg <Expression>();
                var parts = new List <string>();
                parts.Add("FromSql inputs:");
                parts.Add(ProjectExpressionHelper.StringifyFromSqlExpression(fsqre));
                Logger.LogDebug(string.Join(Environment.NewLine, parts));

                return(createQueryResult);
            });

            return(mockedQueryProvider);
        }
Exemple #2
0
        /// <inheritdoc />
        public virtual IQueryable <TElement> CreateQuery <TElement>(Expression expression)
        {
            Logger.LogDebug("CreateQuery<TElement>: invoked");

            if (expression is FromSqlQueryRootExpression)
            {
                Logger.LogDebug("CreateQuery: catch all exception invoked");
                throw new NotSupportedException();
            }

            ProjectExpressionHelper.ThrowIfExpressionIsNotSupported(expression);

            return(new AsyncEnumerable <TElement>(Source.Provider.CreateQuery <TElement>(EnsureExpressionCanBeResolvedBySourceProvider(expression))));
        }
Exemple #3
0
        private void SetUpReadOnlyModel <TEntity>(Mock <TDbContext> dbContextMock) where TEntity : class
        {
            var mockedReadOnlyDbSet = _dbContext.Set <TEntity>().CreateMockedReadOnlyDbSet();

            var property = typeof(TDbContext).GetProperties().SingleOrDefault(p => p.PropertyType == typeof(DbSet <TEntity>));

            if (property != null)
            {
                var propertyExpression = ProjectExpressionHelper.CreatePropertyExpression <TDbContext, DbSet <TEntity> >(property);
                dbContextMock.Setup(propertyExpression).Returns(() => mockedReadOnlyDbSet);
            }
            else
            {
                Logger.LogDebug("Could not find a DbContext property for type '{type}'", typeof(TEntity));
            }

            dbContextMock.Setup(m => m.Set <TEntity>()).Returns(() => mockedReadOnlyDbSet);
        }
Exemple #4
0
        private void SetUpModel <TEntity>(Mock <TDbContext> dbContextMock) where TEntity : class
        {
            var mockedDbSet = _dbContext.Set <TEntity>().CreateMockedDbSet();

            var property = typeof(TDbContext).GetProperties().SingleOrDefault(p => p.PropertyType == typeof(DbSet <TEntity>));

            if (property != null)
            {
                var propertyExpression = ProjectExpressionHelper.CreatePropertyExpression <TDbContext, DbSet <TEntity> >(property);
                dbContextMock.Setup(propertyExpression).Returns(() => mockedDbSet);
            }
            else
            {
                Logger.LogDebug("Could not find a DbContext property for type '{type}'", typeof(TEntity));
            }

            dbContextMock.Setup(m => m.Set <TEntity>()).Returns(() => mockedDbSet);

            dbContextMock.Setup(m => m.Add(It.IsAny <TEntity>())).Returns((TEntity providedEntity) => _dbContext.Add(providedEntity));
            dbContextMock.Setup(m => m.AddAsync(It.IsAny <TEntity>(), It.IsAny <CancellationToken>()))
            .Returns((TEntity providedEntity, CancellationToken providedCancellationToken) => _dbContext.AddAsync(providedEntity, providedCancellationToken));

            dbContextMock.Setup(m => m.Attach(It.IsAny <TEntity>())).Returns((TEntity providedEntity) => _dbContext.Attach(providedEntity));

            dbContextMock.Setup(m => m.Entry(It.IsAny <TEntity>())).Returns((TEntity providedEntity) => _dbContext.Entry(providedEntity));

            dbContextMock.Setup(m => m.Find <TEntity>(It.IsAny <object[]>())).Returns((object[] providedKeyValues) => _dbContext.Find <TEntity>(providedKeyValues));

            dbContextMock.Setup(m => m.FindAsync <TEntity>(It.IsAny <object[]>())).Returns((object[] providedKeyValues) => _dbContext.FindAsync <TEntity>(providedKeyValues));
            dbContextMock.Setup(m => m.FindAsync <TEntity>(It.IsAny <object[]>(), It.IsAny <CancellationToken>()))
            .Returns((object[] providedKeyValues, CancellationToken providedCancellationToken) => _dbContext.FindAsync <TEntity>(providedKeyValues, providedCancellationToken));

            dbContextMock.Setup(m => m.Remove(It.IsAny <TEntity>())).Returns((TEntity providedEntity) => _dbContext.Remove(providedEntity));

            dbContextMock.Setup(m => m.Update(It.IsAny <TEntity>())).Returns((TEntity providedEntity) => _dbContext.Update(providedEntity));
        }
Exemple #5
0
 /// <inheritdoc />
 public virtual TResult Execute <TResult>(Expression expression)
 {
     Logger.LogDebug("Execute<TResult>: invoked");
     ProjectExpressionHelper.ThrowIfExpressionIsNotSupported(expression);
     return(Source.Provider.Execute <TResult>(EnsureExpressionCanBeResolvedBySourceProvider(expression)));
 }