# Path: virtool/jobs/db.py
# async def acquire(db, job_id: str) -> Dict[str, Any]:
#     """
#     Set the `started` field on a job to `True` and return the complete document.
# 
#     :param db: the application database object
#     :param job_id: the ID of the job to start
#     :return: the complete job document
# 
#     """
#     key, hashed = virtool.utils.generate_key()
# 
#     document = await db.jobs.find_one_and_update(
#         {"_id": job_id},
#         {"$set": {"acquired": True, "key": hashed}},
#         projection=PROJECTION,
#     )
# 
#     document["key"] = key
# 
#     return virtool.utils.base_processor(document)
# 
# async def create(
#     db,
#     workflow: str,
#     job_args: Dict[str, Any],
#     user_id: str,
#     rights: JobRights,
#     job_id: Optional[str] = None,
# ) -> Dict[str, Any]:
#     """
#     Create, insert, and return a job document.
# 
#     :param db: the application database object
#     :param workflow: the name of the workflow to run
#     :param job_args: the arguments required to run the job
#     :param user_id: the user that started the job
#     :param rights: the rights the job will have on Virtool resources
#     :param job_id: an optional ID to use for the new job
# 
#     """
#     document = {
#         "acquired": False,
#         "workflow": workflow,
#         "args": job_args,
#         "key": None,
#         "rights": rights.as_dict(),
#         "state": "waiting",
#         "status": [
#             {
#                 "state": "waiting",
#                 "stage": None,
#                 "error": None,
#                 "progress": 0,
#                 "timestamp": virtool.utils.timestamp(),
#             }
#         ],
#         "user": {"id": user_id},
#     }
# 
#     if job_id:
#         document["_id"] = job_id
# 
#     document = await db.jobs.insert_one(document)
# 
#     return document
# 
# async def force_delete_jobs(app: App):
#     """
#     Force cancellation and deletion of all jobs.
# 
#     :param app: the application object
# 
#     """
#     job_ids = await app["db"].jobs.distinct("_id")
# 
#     await gather(*[app["jobs"].cancel(job_id) for job_id in job_ids])
#     await gather(*[delete(app, job_id) for job_id in job_ids])


from unittest.mock import call
from virtool.jobs.client import JobsClient
from virtool.jobs.db import acquire, create, force_delete_jobs
from virtool.jobs.utils import JobRights
import pytest
import virtool.jobs.db
                    "stage": "foo",
                    "error": None,
                    "progress": 0.33,
                    "timestamp": static_time.datetime,
                }
            ],
        }
    )

    await virtool.jobs.db.cancel(dbi, "foo")

    assert await dbi.jobs.find_one() == snapshot


@pytest.mark.parametrize("with_job_id", [False, True])
async def test_create(
    with_job_id, mocker, snapshot, dbi, test_random_alphanumeric, static_time
):
    mocker.patch("virtool.utils.generate_key", return_value=("key", "hashed"))

    rights = JobRights()
    rights.samples.can_read("foo")
    rights.samples.can_modify("foo")
    rights.samples.can_remove("foo")

    if with_job_id:
        await create(
            dbi, "create_sample", {"sample_id": "foo"}, "bob", rights, job_id="bar"
        )
    else:

# Path: virtool/jobs/db.py
# async def acquire(db, job_id: str) -> Dict[str, Any]:
#     """
#     Set the `started` field on a job to `True` and return the complete document.
# 
#     :param db: the application database object
#     :param job_id: the ID of the job to start
#     :return: the complete job document
# 
#     """
#     key, hashed = virtool.utils.generate_key()
# 
#     document = await db.jobs.find_one_and_update(
#         {"_id": job_id},
#         {"$set": {"acquired": True, "key": hashed}},
#         projection=PROJECTION,
#     )
# 
#     document["key"] = key
# 
#     return virtool.utils.base_processor(document)
# 
# async def create(
#     db,
#     workflow: str,
#     job_args: Dict[str, Any],
#     user_id: str,
#     rights: JobRights,
#     job_id: Optional[str] = None,
# ) -> Dict[str, Any]:
#     """
#     Create, insert, and return a job document.
# 
#     :param db: the application database object
#     :param workflow: the name of the workflow to run
#     :param job_args: the arguments required to run the job
#     :param user_id: the user that started the job
#     :param rights: the rights the job will have on Virtool resources
#     :param job_id: an optional ID to use for the new job
# 
#     """
#     document = {
#         "acquired": False,
#         "workflow": workflow,
#         "args": job_args,
#         "key": None,
#         "rights": rights.as_dict(),
#         "state": "waiting",
#         "status": [
#             {
#                 "state": "waiting",
#                 "stage": None,
#                 "error": None,
#                 "progress": 0,
#                 "timestamp": virtool.utils.timestamp(),
#             }
#         ],
#         "user": {"id": user_id},
#     }
# 
#     if job_id:
#         document["_id"] = job_id
# 
#     document = await db.jobs.insert_one(document)
# 
#     return document
# 
# async def force_delete_jobs(app: App):
#     """
#     Force cancellation and deletion of all jobs.
# 
#     :param app: the application object
# 
#     """
#     job_ids = await app["db"].jobs.distinct("_id")
# 
#     await gather(*[app["jobs"].cancel(job_id) for job_id in job_ids])
#     await gather(*[delete(app, job_id) for job_id in job_ids])


# Path: tests/jobs/test_db.py
from unittest.mock import call
from virtool.jobs.client import JobsClient
from virtool.jobs.db import acquire, create, force_delete_jobs
from virtool.jobs.utils import JobRights
import pytest
import virtool.jobs.db

                    "stage": "foo",
                    "error": None,
                    "progress": 0.33,
                    "timestamp": static_time.datetime,
                }
            ],
        }
    )

    await virtool.jobs.db.cancel(dbi, "foo")

    assert await dbi.jobs.find_one() == snapshot


@pytest.mark.parametrize("with_job_id", [False, True])
async def test_create(
    with_job_id, mocker, snapshot, dbi, test_random_alphanumeric, static_time
):
    mocker.patch("virtool.utils.generate_key", return_value=("key", "hashed"))

    rights = JobRights()
    rights.samples.can_read("foo")
    rights.samples.can_modify("foo")
    rights.samples.can_remove("foo")

    if with_job_id:
        await create(
            dbi, "create_sample", {"sample_id": "foo"}, "bob", rights, job_id="bar"
        )
    else:

        await create(dbi, "create_sample", {"sample_id": "foo"}, "bob", rights)