import pytest
from aiohttp.test_utils import make_mocked_coro


async def test_create(spawn_client, test_random_alphanumeric, static_time):
    client = await spawn_client(authorize=True, permissions=["create_ref"])

    default_source_type = [
        "strain",
        "isolate"
    ]

    client.app["settings"] = {
        "default_source_types": default_source_type
    }

    data = {
        "name": "Test Viruses",
        "description": "A bunch of viruses used for testing",
        "data_type": "genome",
        "organism": "virus",
        "public": True
    }

    resp = await client.post("/api/refs", data)

    assert resp.status == 201

    assert resp.headers["Location"] == "/api/refs/" + test_random_alphanumeric.history[0]

    assert await resp.json() == dict(
        data,
        id=test_random_alphanumeric.history[0],
        created_at="2015-10-06T20:00:00Z",
        user={
            "id": "test"
        },
        users=[{
            "build": True,
            "id": "test",
            "modify": True,
            "modify_otu": True,
            "remove": True
        }],
        groups=[],
        contributors=[],
        internal_control=None,
        restrict_source_types=False,
        source_types=default_source_type,
        latest_build=None
    )


@pytest.mark.parametrize("error", [None, 404])
@pytest.mark.parametrize("field", ["group", "user"])
async def test_add_group_or_user(error, field, mocker, spawn_client, resp_is):
    client = await spawn_client(authorize=True, permissions=["create_ref"])

    expected = {"id": "test"}

    if error == 404:
        expected = None

    m_add_group_or_user = mocker.patch("virtool.db.references.add_group_or_user", make_mocked_coro(expected))

    url = "/api/refs/foobar/{}s".format(field)

    resp = await client.post(url, {
        field + "_id": "baz",
        "modify": True
    })

    if error == 404:
        assert await resp_is.not_found(resp)

    else:
        assert resp.status == 201

        assert await resp.json() == expected

        m_add_group_or_user.assert_called_with(client.db, "foobar", field + "s", {
            "build": False,
            "modify": True,
            "modify_otu": False,
            "remove": False,
            field + "_id": "baz"
        })


@pytest.mark.parametrize("error", [None, 404])
@pytest.mark.parametrize("field", ["group", "user"])
async def test_edit_group_or_user(error, field, mocker, spawn_client, resp_is):
    client = await spawn_client(authorize=True, permissions=["create_ref"])

    expected = {"id": "test"}

    if error == 404:
        expected = None

    m_edit_group_or_user = mocker.patch("virtool.db.references.edit_group_or_user", make_mocked_coro(expected))

    url = "/api/refs/foobar/{}s/baz".format(field)

    resp = await client.patch(url, {
        "remove": True
    })

    if error == 404:
        assert await resp_is.not_found(resp)

    else:
        assert resp.status == 200

        assert await resp.json() == expected

        m_edit_group_or_user.assert_called_with(client.db, "foobar", "baz", field + "s", {
            "build": False,
            "modify": False,
            "modify_otu": False,
            "remove": True
        })


@pytest.mark.parametrize("error", [None, 404])
@pytest.mark.parametrize("field", ["group", "user"])
async def test_delete_group_or_user(error, field, mocker, spawn_client, resp_is):
    client = await spawn_client(authorize=True, permissions=["create_ref"])

    expected = "test"

    if error == 404:
        expected = None

    m_edit_group_or_user = mocker.patch("virtool.db.references.delete_group_or_user", make_mocked_coro(expected))

    url = "/api/refs/foobar/{}s/baz".format(field)

    resp = await client.delete(url)

    if error == 404:
        assert await resp_is.not_found(resp)

    else:
        assert resp.status == 204

        m_edit_group_or_user.assert_called_with(client.db, "foobar", "baz", field + "s")

@@ -79,16 +79,24 @@
 _create(
+mocker, 
 spawn_cl
@@ -141,16 +141,16 @@
 _time):%0A
-
     clie
@@ -572,24 +572,110 @@
 True%0A    %7D%0A%0A
+    m = mocker.patch(%22virtool.db.references.get_unbuilt_count%22, make_mocked_coro(5))%0A%0A
     resp = a
@@ -1304,24 +1304,56 @@
 ypes=False,%0A
+        unbuilt_change_count=5,%0A
         sour
@@ -1382,16 +1382,16 @@
 e_type,%0A
-
         
@@ -1415,16 +1415,112 @@
 %0A    )%0A%0A
+    m.assert_called_with(%0A        client.db,%0A        test_random_alphanumeric.history%5B0%5D%0A    )%0A%0A
 %0A@pytest

Fix failing test