summaryrefslogblamecommitdiffstatshomepage
path: root/tests/api/test_comic_tag.py
blob: f536b7939ba7c0b302e82cee03bddc8b4130066f (plain) (tree)





































































































































                                                                               
from functools import partial

import pytest
from conftest import DB, Response
from hircine.db.models import Namespace, Tag


@pytest.fixture
def query_comic_tags(schema_execute):
    query = """
    query comicTags($forFilter: Boolean) {
        comicTags(forFilter: $forFilter) {
            edges {
                __typename
                id
                name
            }
            count
        }
    }
    """

    def wrapper(q):
        async def _execute(for_filter=False):
            return await schema_execute(q, {"forFilter": for_filter})

        return _execute

    return wrapper(query)


def build_item(namespace, tag):
    nid, tid = "", ""
    nname, tname = "", ""

    if namespace:
        nid, nname = namespace.id, namespace.name

    if tag:
        tid, tname = tag.id, tag.name

    item = {
        "__typename": "ComicTag",
        "id": f"{nid}:{tid}",
        "name": f"{nname}:{tname}",
    }

    return item


@pytest.mark.anyio
async def test_query_comic_tags_cross(query_comic_tags):
    ns_foo = Namespace(id=1, name="foo")
    ns_bar = Namespace(id=2, name="bar")
    tag_qoo = Tag(id=1, name="qoo", namespaces=[ns_foo, ns_bar])
    tag_qar = Tag(id=2, name="qar", namespaces=[ns_foo, ns_bar])

    await DB.add_all(ns_foo, ns_bar)
    await DB.add_all(tag_qoo, tag_qar)

    builder = partial(build_item)

    response = Response(await query_comic_tags())
    assert response.data["edges"] == [
        builder(ns_bar, tag_qar),
        builder(ns_bar, tag_qoo),
        builder(ns_foo, tag_qar),
        builder(ns_foo, tag_qoo),
    ]


@pytest.mark.anyio
async def test_query_comic_tags_restricted_namespace(query_comic_tags):
    ns_foo = Namespace(id=1, name="foo")
    ns_bar = Namespace(id=2, name="bar")
    tag_qoo = Tag(id=1, name="qoo", namespaces=[ns_bar])
    tag_qar = Tag(id=2, name="qar", namespaces=[ns_foo])

    await DB.add_all(ns_foo, ns_bar)
    await DB.add_all(tag_qoo, tag_qar)

    builder = partial(build_item)

    response = Response(await query_comic_tags())
    assert response.data["edges"] == [
        builder(ns_bar, tag_qoo),
        builder(ns_foo, tag_qar),
    ]


@pytest.mark.anyio
async def test_query_comic_tag_matchers_cross(query_comic_tags):
    ns_foo = Namespace(id=1, name="foo")
    ns_bar = Namespace(id=2, name="bar")
    tag_qoo = Tag(id=1, name="qoo", namespaces=[ns_foo, ns_bar])
    tag_qar = Tag(id=2, name="qar", namespaces=[ns_foo, ns_bar])

    await DB.add_all(ns_foo, ns_bar, tag_qoo, tag_qar)

    builder = partial(build_item)

    response = Response(await query_comic_tags(for_filter=True))
    assert response.data["edges"] == [
        builder(ns_bar, None),
        builder(ns_foo, None),
        builder(None, tag_qar),
        builder(None, tag_qoo),
        builder(ns_bar, tag_qar),
        builder(ns_bar, tag_qoo),
        builder(ns_foo, tag_qar),
        builder(ns_foo, tag_qoo),
    ]


@pytest.mark.anyio
async def test_query_comic_tag_matchers_restricted_namespace(query_comic_tags):
    ns_foo = Namespace(id=1, name="foo")
    ns_bar = Namespace(id=2, name="bar")
    tag_qoo = Tag(id=1, name="qoo", namespaces=[ns_bar])
    tag_qar = Tag(id=2, name="qar", namespaces=[ns_foo])

    await DB.add_all(ns_foo, ns_bar, tag_qoo, tag_qar)

    builder = partial(build_item)

    response = Response(await query_comic_tags(for_filter=True))
    assert response.data["edges"] == [
        builder(ns_bar, None),
        builder(ns_foo, None),
        builder(None, tag_qar),
        builder(None, tag_qoo),
        builder(ns_bar, tag_qoo),
        builder(ns_foo, tag_qar),
    ]