模拟IDocumentQuery具有使用查询表达式的能力

时间:2019-02-28 16:02:13

标签: linq azure-cosmosdb nsubstitute

我需要能够模拟IDocumentQuery,以便能够测试代码段,查询文档集合并可能使用谓词来过滤它们:

IQueryable<T> documentQuery = client
                .CreateDocumentQuery<T>(collectionUri, options);

if (predicate != null)
{
   documentQuery = documentQuery.Where(predicate);
}

var list = documentQuery.AsDocumentQuery();
var documents = new List<T>();           

while (list.HasMoreResults)
{
   documents.AddRange(await list.ExecuteNextAsync<T>());
}

我使用了https://stackoverflow.com/a/49911733/212121的答案来编写以下方法:

public static IDocumentClient Create<T>(params T[] collectionDocuments)
{
    var query = Substitute.For<IFakeDocumentQuery<T>>();

    var provider = Substitute.For<IQueryProvider>();

    provider
        .CreateQuery<T>(Arg.Any<Expression>())                                                
        .Returns(x => query);

    query.Provider.Returns(provider);
    query.ElementType.Returns(collectionDocuments.AsQueryable().ElementType);
    query.Expression.Returns(collectionDocuments.AsQueryable().Expression);
    query.GetEnumerator().Returns(collectionDocuments.AsQueryable().GetEnumerator());

    query.ExecuteNextAsync<T>().Returns(x => new FeedResponse<T>(collectionDocuments));
    query.HasMoreResults.Returns(true, false);

    var client = Substitute.For<IDocumentClient>();

    client
        .CreateDocumentQuery<T>(Arg.Any<Uri>(), Arg.Any<FeedOptions>())
        .Returns(query);

    return client;
}

只要不使用IQueryable.Where进行过滤,哪种方法就可以正常工作。

我的问题:

  

是否有任何捕获谓词的方法,这些谓词用于创建documentQuery并将谓词应用于collectionDocuments参数?

1 个答案:

答案 0 :(得分:1)

从查询提供程序访问表达式,以便将其传递到支持集合以应用所需的过滤器。

查看以下内容

public static IDocumentClient Create<T>(params T[] collectionDocuments) {
    var query = Substitute.For<IFakeDocumentQuery<T>>();

    var queryable = collectionDocuments.AsQueryable();

    var provider = Substitute.For<IQueryProvider>();
    provider.CreateQuery<T>(Arg.Any<Expression>())
        .Returns(x => {
            var expression = x.Arg<Expression>();
            if (expression != null) {
                queryable = queryable.Provider.CreateQuery<T>(expression);
            }
            return query;
        });

    query.Provider.Returns(_ => provider);
    query.ElementType.Returns(_ => queryable.ElementType);
    query.Expression.Returns(_ => queryable.Expression);
    query.GetEnumerator().Returns(_ => queryable.GetEnumerator());

    query.ExecuteNextAsync<T>().Returns(x => new FeedResponse<T>(query));
    query.HasMoreResults.Returns(true, true, false);

    var client = Substitute.For<IDocumentClient>();

    client
        .CreateDocumentQuery<T>(Arg.Any<Uri>(), Arg.Any<FeedOptions>())
        .Returns(query);

    return client;
}

重要的部分是传递给查询的表达式用于在后备数据源(数组)上创建另一个查询的地方。

使用下面的示例受试者进行演示。

public class SubjectUnderTest {
    private readonly IDocumentClient client;

    public SubjectUnderTest(IDocumentClient client) {
        this.client = client;
    }

    public async Task<List<T>> Query<T>(Expression<Func<T, bool>> predicate = null) {
        FeedOptions options = null; //for dummy purposes only
        Uri collectionUri = null;  //for dummy purposes only
        IQueryable<T> documentQuery = client.CreateDocumentQuery<T>(collectionUri, options);

        if (predicate != null) {
            documentQuery = documentQuery.Where(predicate);
        }

        var list = documentQuery.AsDocumentQuery();
        var documents = new List<T>();

        while (list.HasMoreResults) {
            documents.AddRange(await list.ExecuteNextAsync<T>());
        }

        return documents;
    }
}

下面的示例测试何时将表达式传递给查询

[TestMethod]
public async Task Should_Filter_DocumentQuery() {
    //Arrange
    var dataSource = Enumerable.Range(0, 3)
        .Select(_ => new Document() { Key = _ }).ToArray();
    var client = Create(dataSource);
    var subject = new SubjectUnderTest(client);

    Expression<Func<Document, bool>> predicate = _ => _.Key == 1;
    var expected = dataSource.Where(predicate.Compile());

    //Act
    var actual = await subject.Query<Document>(predicate);

    //Assert
    actual.Should().BeEquivalentTo(expected);
}

public class Document {
    public int Key { get; set; }
}