def test_where(self, test_db):
a = test_db.test.find()
with pytest.raises(TypeError):
a.where(5)
with pytest.raises(TypeError):
a.where(None)
with pytest.raises(TypeError):
a.where({})
await test_db.test.insert_many([{'x': i} for i in range(10)])
assert 3 == len(await test_db.test.find().where('this.x < 3').to_list())
assert 3 == len(await test_db.test.find().where(Code('this.x < 3')).to_list())
assert 3 == len(await test_db.test.find().where(Code('this.x < i', {'i': 3})).to_list())
assert 10 == len(await test_db.test.find().to_list())
assert 3 == await test_db.test.find().where('this.x < 3').count()
assert 10 == await test_db.test.find().count()
assert 3 == await test_db.test.find().where(u'this.x < 3').count()
assert [0, 1, 2] == [a['x'] for a in await test_db.test.find().where('this.x < 3').to_list()]
assert [] == [a['x'] for a in await test_db.test.find({'x': 5}).where('this.x < 3').to_list()]
assert [5] == [a['x'] for a in await test_db.test.find({'x': 5}).where('this.x > 3').to_list()]
cursor = test_db.test.find().where('this.x < 3').where('this.x > 7')
assert [8, 9] == [a['x'] for a in await cursor.to_list()]
a = test_db.test.find()
b = a.where('this.x > 3')
async for _ in a:
break
with pytest.raises(InvalidOperation):
a.where('this.x < 3')
评论列表
文章目录