# Path: virtool/db/utils.py
# @asynccontextmanager
# async def buffered_bulk_writer(collection, batch_size=100):
#     """
#     A context manager for bulk writing to MongoDB.
# 
#     Returns a :class:``BufferedBulkWriter`` object. Automatically flushes the buffer
#     when the context manager exits.
# 
#     :param collection: the MongoDB collection to write against
#     :param batch_size: the number of requests to be sent in each bulk operation
# 
#     """
#     writer = BufferedBulkWriter(collection, batch_size)
# 
#     try:
#         yield writer
#     finally:
#         await writer.flush()


import pytest
import virtool.db
import virtool.db.utils
from virtool.db.utils import buffered_bulk_writer

    assert await dbi.analyses.find().to_list(None) == [{"_id": 1, "ready": True}]


async def test_check_missing_ids(dbi):
    await dbi.subtraction.insert_many(
        [
            {
                "_id": "foo",
                "name": "Foo",
            },
            {
                "_id": "bar",
                "name": "Bar",
            },
        ]
    )

    non_existent_subtractions = await virtool.db.utils.check_missing_ids(
        dbi.subtraction, ["foo", "bar", "baz"]
    )

    assert non_existent_subtractions == {"baz"}


@pytest.mark.parametrize("batch_size", (60, 100))
async def test_buffered_bulk_writer(batch_size, data_regression, snapshot, mocker):
    collection = mocker.Mock()
    collection.bulk_write = mocker.AsyncMock()


# Path: virtool/db/utils.py
# @asynccontextmanager
# async def buffered_bulk_writer(collection, batch_size=100):
#     """
#     A context manager for bulk writing to MongoDB.
# 
#     Returns a :class:``BufferedBulkWriter`` object. Automatically flushes the buffer
#     when the context manager exits.
# 
#     :param collection: the MongoDB collection to write against
#     :param batch_size: the number of requests to be sent in each bulk operation
# 
#     """
#     writer = BufferedBulkWriter(collection, batch_size)
# 
#     try:
#         yield writer
#     finally:
#         await writer.flush()


# Path: tests/db/test_utils.py
import pytest
import virtool.db
import virtool.db.utils
from virtool.db.utils import buffered_bulk_writer


    assert await dbi.analyses.find().to_list(None) == [{"_id": 1, "ready": True}]


async def test_check_missing_ids(dbi):
    await dbi.subtraction.insert_many(
        [
            {
                "_id": "foo",
                "name": "Foo",
            },
            {
                "_id": "bar",
                "name": "Bar",
            },
        ]
    )

    non_existent_subtractions = await virtool.db.utils.check_missing_ids(
        dbi.subtraction, ["foo", "bar", "baz"]
    )

    assert non_existent_subtractions == {"baz"}


@pytest.mark.parametrize("batch_size", (60, 100))
async def test_buffered_bulk_writer(batch_size, data_regression, snapshot, mocker):
    collection = mocker.Mock()
    collection.bulk_write = mocker.AsyncMock()


    async with buffered_bulk_writer(collection, batch_size=batch_size) as writer: