猫鼬聚合查询在Jest/Mockgoose测试中失败,可在其他地方使用 [英] Mongoose aggregation query fails in Jest/Mockgoose test, works elsewhere

查看:78
本文介绍了猫鼬聚合查询在Jest/Mockgoose测试中失败,可在其他地方使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的Mongoose模型创建一些测试,但我不知道如何让Jest/Mockgoose测试通过速记查询/聚合管道(请参见下面的第一个代码块)创建它是为了从一个集合中检索一个随机文档,而该文档在另一个集合中没有被引用.

I'm trying to create some tests for my Mongoose models, and I can't figure out how to get the Jest/Mockgoose test to pass for a shorthand query/aggregation pipeline (see below in first code block) that I created to retrieve a random document from one collection that isn't referenced in another collection.

activitySchema.query.getUnused = function() {
    return Activity.aggregate()
        .lookup({
            from: 'userActivity',
            localField: '_id',
            foreignField: 'activity',
            as: 'matched_docs',
        })
        .match({ matched_docs: { $eq: [] } })
        .sample(1)
        .project({ matched_docs: 0, __v: 0 })
        .exec()
}

笑话

describe('Activity methods', () => {
    test('Activity unused query executes', (done) => {
        function createActivity() {
            return Activity
                .create({ activity: 'running' })
                .then(activity => {
                    console.log('Activity is generated')
                    return Promise.resolve(true)
                })
        }
        async function retrieveActivity() {
            await createActivity()
            Activity
                .find()
                .getUnused()
                .then(unused => {
                    console.log(unused);
                    expect(unused).toHaveLength(1)
                    done()
                })
                .catch(x => {
                console.log(x)
                })
        }


        return retrieveActivity()
    })

})

沙盒节点JS代码:

Activity.find().getUnused()
    .then((x) => {
     console.log(x);
})

当我在沙盒节点文件中尝试时,机智正常工作并检索到一个典型的查询集,例如:

When I try it in a sandbox node file, wit works normally and retrieves a typical queryset like:

   [ { _id: 58f3dee3b0346910a69e6e5d, activity: 'running', __v: 0 } ]

运行测试时,出现此MongoError:

When I run the test, I get this MongoError:

The 'cursor' option is required, except for aggregation explain

如何以一种在两种情况下都可以使用的通用方式解决此问题?我希望该方法在可能的情况下返回承诺.我已经尝试了链式聚合方法(参见上文)和更原生的管道Mongo聚合数组,但是都返回了错误.如果相关的话,我的mongo版本是3.4.2.

How do I fix this with a universal manner that works in both contexts? I'd like for the method to return a promise if possible. I've tried both the chained aggregate methods (see above) and the more native Mongo aggregate array for pipelines, but both return errors. My mongo version is 3.4.2 if that's relevant.

推荐答案

最后弄清楚了这一点:我必须向聚合管道添加一个游标调用,并将其转换为流.为了维护Promise,我使用了查询方法 返回一个Promise,一旦流结束,该Promise将使用数据进行解析,如下所示:

Finally figured this out: I had to add a cursor call to the aggregation pipeline, and translate it into a stream. To maintain the Promise I had the query method return a Promise that resolves with data once the stream has ended, as below:

activitySchema.query.getUnused = function() {
    return new Promise((res, rej) => {
        let data = []
        return Activity.aggregate()
            .lookup({
                from: 'userActivity',
                localField: '_id',
                foreignField: 'activity',
                as: 'matched_docs',
            })
            .match({ matched_docs: { $eq: [] } })
            .sample(1)
            .project({ matched_docs: 0, __v: 0 })
            .cursor({})
            .exec()
            .on('data', doc => data.push(doc))
            .on('end', () => res(data))

    })
}

这篇关于猫鼬聚合查询在Jest/Mockgoose测试中失败,可在其他地方使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆