<commit_before>import arrow
import pytest
from aiohttp.test_utils import make_mocked_coro


class TestFind:
    @pytest.mark.parametrize("find,per_page,page,d_range,meta", [
        (None, None, None, range(0, 3), {
            "page": 1,
            "per_page": 15,
            "page_count": 1,
            "found_count": 3,
            "total_count": 3
        }),
        # Test ``per_page`` query param.
        (None, 2, 1, range(0, 2), {
            "page": 1,
            "per_page": 2,
            "page_count": 2,
            "found_count": 3,
            "total_count": 3
        }),
        # Test ``per_page`` and ``page`` query param.
        (None, 2, 2, range(2, 3), {
            "page": 2,
            "per_page": 2,
            "page_count": 2,
            "found_count": 3,
            "total_count": 3
        }),
        # Test ``find`` query param and ``found_count`` response field.
        ("gv", None, None, range(1, 3), {
            "page": 1,
            "per_page": 15,
            "page_count": 1,
            "found_count": 2,
            "total_count": 3
        }),
        ("sp", None, None, range(0, 1), {
            "page": 1,
            "per_page": 15,
            "page_count": 1,
            "found_count": 1,
            "total_count": 3
        }),
        ("fred", None, None, [0, 2], {
            "page": 1,
            "per_page": 15,
            "page_count": 1,
            "found_count": 2,
            "total_count": 3
        })
    ])
    async def test(self, find, per_page, page, d_range, meta, spawn_client, static_time):
        client = await spawn_client(authorize=True)

        time_1 = arrow.get(static_time).datetime
        time_2 = arrow.get(static_time).shift(hours=1).datetime
        time_3 = arrow.get(static_time).shift(hours=2).datetime

        await client.db.samples.insert_many([
            {
                "user": {
                    "id": "bob"
                },
                "nuvs": False,
                "host": "",
                "foobar": True,
                "imported": True,
                "isolate": "Thing",
                "created_at": time_2,
                "archived": True,
                "_id": "beb1eb10",
                "name": "16GVP042",
                "pathoscope": False,
                "all_read": True
            },
            {
                "user": {
                    "id": "fred"
                },
                "nuvs": False,
                "host": "",
                "foobar": True,
                "imported": True,
                "isolate": "Test",
                "created_at": time_1,
                "archived": True,
                "_id": "72bb8b31",
                "name": "16GVP043",
                "pathoscope": False,
                "all_read": True
            },
            {
                "user": {
                    "id": "fred"
                },
                "nuvs": False,
                "host": "",
                "foobar": True,
                "imported": True,
                "isolate": "",
                "created_at": time_3,
                "archived": False,
                "_id": "cb400e6d",
                "name": "16SPP044",
                "pathoscope": False,
                "all_read": True
            }
        ])

        path = "/api/samples"
        query = list()

        if find is not None:
            query.append("find={}".format(find))

        if per_page is not None:
            query.append("per_page={}".format(per_page))

        if page is not None:
            query.append("page={}".format(page))

        if len(query):
            path += "?{}".format("&".join(query))

        resp = await client.get(path)

        assert resp.status == 200

        expected_documents = [
            {
                "user": {
                    "id": "fred"
                },
                "nuvs": False,
                "host": "",
                "imported": True,
                "isolate": "",
                "created_at": "2015-10-06T22:00:00Z",
                "archived": False,
                "id": "cb400e6d",
                "name": "16SPP044",
                "pathoscope": False
            },
            {
                "user": {
                    "id": "bob"
                },
                "nuvs": False,
                "host": "",
                "imported": True,
                "isolate": "Thing",
                "created_at": "2015-10-06T21:00:00Z",
                "archived": True,
                "id": "beb1eb10",
                "name": "16GVP042",
                "pathoscope": False
            },
            {
                "user": {
                    "id": "fred"
                },
                "nuvs": False,
                "host": "",
                "imported": True,
                "isolate": "Test",
                "created_at": "2015-10-06T20:00:00Z",
                "archived": True,
                "id": "72bb8b31",
                "name": "16GVP043",
                "pathoscope": False
            }
        ]

        assert await resp.json() == dict(meta, documents=[expected_documents[i] for i in d_range])

    async def test_invalid_query(self, spawn_client, resp_is):
        client = await spawn_client()

        resp = await client.get("/api/samples?per_pag=12")

        assert resp.status == 422

        assert await resp_is.invalid_query(resp, {
            "per_pag": ["unknown field"]
        })


class TestGet:

    async def test(self, mocker, spawn_client, static_time):
        mocker.patch("virtool.sample.get_sample_rights", return_value=(True, True))

        client = await spawn_client()

        await client.db.samples.insert_one({<commit_msg>Fix failing test<commit_after>import arrow
import pytest
from aiohttp.test_utils import make_mocked_coro


class TestFind:
    @pytest.mark.parametrize("find,per_page,page,d_range,meta", [
        (None, None, None, range(0, 3), {
            "page": 1,
            "per_page": 15,
            "page_count": 1,
            "found_count": 3,
            "total_count": 3
        }),
        # Test ``per_page`` query param.
        (None, 2, 1, range(0, 2), {
            "page": 1,
            "per_page": 2,
            "page_count": 2,
            "found_count": 3,
            "total_count": 3
        }),
        # Test ``per_page`` and ``page`` query param.
        (None, 2, 2, range(2, 3), {
            "page": 2,
            "per_page": 2,
            "page_count": 2,
            "found_count": 3,
            "total_count": 3
        }),
        # Test ``find`` query param and ``found_count`` response field.
        ("gv", None, None, range(1, 3), {
            "page": 1,
            "per_page": 15,
            "page_count": 1,
            "found_count": 2,
            "total_count": 3
        }),
        ("sp", None, None, range(0, 1), {
            "page": 1,
            "per_page": 15,
            "page_count": 1,
            "found_count": 1,
            "total_count": 3
        }),
        ("fred", None, None, [0, 2], {
            "page": 1,
            "per_page": 15,
            "page_count": 1,
            "found_count": 2,
            "total_count": 3
        })
    ])
    async def test(self, find, per_page, page, d_range, meta, spawn_client, static_time):
        client = await spawn_client(authorize=True)

        time_1 = arrow.get(static_time).datetime
        time_2 = arrow.get(static_time).shift(hours=1).datetime
        time_3 = arrow.get(static_time).shift(hours=2).datetime

        await client.db.samples.insert_many([
            {
                "user": {
                    "id": "bob"
                },
                "nuvs": False,
                "host": "",
                "foobar": True,
                "imported": True,
                "isolate": "Thing",
                "created_at": time_2,
                "archived": True,
                "_id": "beb1eb10",
                "name": "16GVP042",
                "pathoscope": False,
                "all_read": True
            },
            {
                "user": {
                    "id": "fred"
                },
                "nuvs": False,
                "host": "",
                "foobar": True,
                "imported": True,
                "isolate": "Test",
                "created_at": time_1,
                "archived": True,
                "_id": "72bb8b31",
                "name": "16GVP043",
                "pathoscope": False,
                "all_read": True
            },
            {
                "user": {
                    "id": "fred"
                },
                "nuvs": False,
                "host": "",
                "foobar": True,
                "imported": True,
                "isolate": "",
                "created_at": time_3,
                "archived": False,
                "_id": "cb400e6d",
                "name": "16SPP044",
                "pathoscope": False,
                "all_read": True
            }
        ])

        path = "/api/samples"
        query = list()

        if find is not None:
            query.append("find={}".format(find))

        if per_page is not None:
            query.append("per_page={}".format(per_page))

        if page is not None:
            query.append("page={}".format(page))

        if len(query):
            path += "?{}".format("&".join(query))

        resp = await client.get(path)

        assert resp.status == 200

        expected_documents = [
            {
                "user": {
                    "id": "fred"
                },
                "nuvs": False,
                "host": "",
                "imported": True,
                "isolate": "",
                "created_at": "2015-10-06T22:00:00Z",
                "archived": False,
                "id": "cb400e6d",
                "name": "16SPP044",
                "pathoscope": False
            },
            {
                "user": {
                    "id": "bob"
                },
                "nuvs": False,
                "host": "",
                "imported": True,
                "isolate": "Thing",
                "created_at": "2015-10-06T21:00:00Z",
                "archived": True,
                "id": "beb1eb10",
                "name": "16GVP042",
                "pathoscope": False
            },
            {
                "user": {
                    "id": "fred"
                },
                "nuvs": False,
                "host": "",
                "imported": True,
                "isolate": "Test",
                "created_at": "2015-10-06T20:00:00Z",
                "archived": True,
                "id": "72bb8b31",
                "name": "16GVP043",
                "pathoscope": False
            }
        ]

        assert await resp.json() == dict(meta, documents=[expected_documents[i] for i in d_range])

    async def test_invalid_query(self, spawn_client, resp_is):
        client = await spawn_client()

        resp = await client.get("/api/samples?per_page=five")

        assert resp.status == 422

        assert await resp_is.invalid_query(resp, {
                'per_page': [
                    "field 'per_page' cannot be coerced: invalid literal for int() with base 10: 'five'",
                    'must be of integer type'
                ]
        })


class TestGet:

    async def test(self, mocker, spawn_client, static_time):
        mocker.patch("virtool.sample.get_sample_rights", return_value=(True, True))

        client = await spawn_client()

        await client.db.samples.insert_one({
{'fraction_non_alphanumeric': 0.1399364315780431, 'fraction_numerical': 0.02783266042436217, 'mean_word_length': 1.1547288543401815, 'pattern_counts': {'":': 205, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5876033', 'n_tokens_mistral': 3716, 'n_tokens_neox': 3285, 'n_words': 714}