# Path: virtool/db/migrate_shared.py
# async def add_subtractions_field(collection: AsyncIOMotorCollection):
#     """
#     Transform `subtraction` field to a list and rename it as `subtractions`.
# 
#     :param collection: the Mongo collection to perform the operation on
# 
#     """
#     updates = list()
# 
#     async for document in collection.find({"subtraction": {"$exists": True}}):
#         try:
#             subtractions = [document["subtraction"]["id"]]
#         except TypeError:
#             subtractions = list()
# 
#         update = UpdateOne(
#             {"_id": document["_id"]},
#             {"$set": {"subtractions": subtractions}, "$unset": {"subtraction": ""}},
#         )
# 
#         updates.append(update)
# 
#     if updates:
#         await collection.bulk_write(updates)


from virtool.db.migrate_shared import add_subtractions_field


async def test_migrate_subtractions_list(dbi):
    await dbi.samples.insert_many(
        [
            {"_id": "foo", "subtraction": {"id": "prunus"}},
            {"_id": "bar", "subtraction": {"id": "malus"}},
            {"_id": "baz", "subtraction": None},
        ]
    )


# Path: virtool/db/migrate_shared.py
# async def add_subtractions_field(collection: AsyncIOMotorCollection):
#     """
#     Transform `subtraction` field to a list and rename it as `subtractions`.
# 
#     :param collection: the Mongo collection to perform the operation on
# 
#     """
#     updates = list()
# 
#     async for document in collection.find({"subtraction": {"$exists": True}}):
#         try:
#             subtractions = [document["subtraction"]["id"]]
#         except TypeError:
#             subtractions = list()
# 
#         update = UpdateOne(
#             {"_id": document["_id"]},
#             {"$set": {"subtractions": subtractions}, "$unset": {"subtraction": ""}},
#         )
# 
#         updates.append(update)
# 
#     if updates:
#         await collection.bulk_write(updates)


# Path: tests/db/test_migrate_shared.py
from virtool.db.migrate_shared import add_subtractions_field



async def test_migrate_subtractions_list(dbi):
    await dbi.samples.insert_many(
        [
            {"_id": "foo", "subtraction": {"id": "prunus"}},
            {"_id": "bar", "subtraction": {"id": "malus"}},
            {"_id": "baz", "subtraction": None},
        ]
    )


    await add_subtractions_field(dbi.samples)