pytest / mongoengine是否在测试过程中删除了文档收集?

时间:2018-09-05 23:06:07

标签: python mongodb pytest mongoengine shapely

假设我在localhost上运行mongodb。我正在尝试使用py.test数据库在项目上运行tmpdb套件,该数据库在拆解期间将被删除。我的conftest.py是:

import pytest
import mongoengine as me
from pymongo import MongoClient


@pytest.fixture(autouse=True)
def tmpdb():
    cc: MongoClient = me.connect('tmpdb')
    yield cc
    cc.drop_database('tmpdb')
    cc.close()

和一个简单的测试:

import pytest
import random
import typing
import mongoengine as me
import shapely.geometry as geo


class Points(me.Document):
    p = me.PointField()
    mp = me.MultiPointField()


Pair = typing.Tuple[float, float]
IterPair = typing.Iterable[Pair]


def generator(i: int) -> IterPair:
    for _ in range(i):
        x = random.uniform(-179, 180)
        y = random.uniform(-89, 90)
        yield x, y


@pytest.fixture
def point() -> geo.Point:
    return geo.Point(list(generator(1)))


@pytest.fixture
def multipoint() -> geo.MultiPoint:
    return geo.MultiPoint(list(generator(2)))


def test_init_and_save(point: geo.Point, multipoint: geo.MultiPoint):
    # me.connect('tmpdb')
    # Points.drop_collection()
    new = Points()
    new.p = geo.mapping(point)
    new.mp = geo.mapping(multipoint)
    new.save()


def test_query_within_box():
    box = [[-180, -90], [180, 90]]
    qry = Points.objects(p__geo_within_box=box)
    obj = qry.first()
    assert obj is not None

由于某些原因,test_query_within_box失败;也就是说,查询无法返回结果(就像在两次测试之间删除了数据库一样)。这应该不会发生,因为tmpdb夹具不应该被拆除直到所有测试完成使用tmpdb夹具退出范围的至少一个测试功能?

如果我注释掉conftest.py并取消注释test_init_and_save中的行,则所有测试都通过。

这是怎么回事?

0 个答案:

没有答案