from studio.models import StudioPage
from tests.conftest import CustomImageFactory, StudioPageFactory


def test_studio_page_persists_via_factory(home_page):
    logo = CustomImageFactory()
    page = StudioPageFactory(
        parent=home_page,
        title="STUDiO",
        slug="studio",
        lead="<p>Ingress.</p>",
        body=[("paragraph", "<p>Body content.</p>")],
        pig="drink",
        logo=logo,
    )

    reloaded = StudioPage.objects.get(pk=page.pk)
    assert reloaded.title == "STUDiO"
    assert reloaded.slug == "studio"
    assert "Ingress." in reloaded.lead
    assert reloaded.pig == "drink"
    assert reloaded.body[0].block_type == "paragraph"
    assert reloaded.logo == logo


def test_studio_page_is_singleton(home_page):
    StudioPageFactory(parent=home_page, slug="studio")

    assert StudioPage.can_create_at(home_page) is False


def test_graphql_studio_page_query(home_page, graphql_post):
    logo = CustomImageFactory(alt="STUDiO-logo")
    StudioPageFactory(
        parent=home_page,
        title="STUDiO",
        slug="studio",
        lead="<p>Ingress text.</p>",
        body=[("paragraph", "<p>Body content.</p>")],
        pig="drink",
        logo=logo,
    )

    response, body = graphql_post(
        """
        query {
          page: studioPage {
            ... on StudioPage {
              title
              slug
              lead
              pig
              logo {
                alt
              }
              body {
                blockType
                field
                ... on RichTextBlock {
                  value
                }
              }
            }
          }
        }
        """
    )

    assert response.status_code == 200
    assert "errors" not in body, body
    data = body["data"]["page"]
    assert data["title"] == "STUDiO"
    assert data["slug"] == "studio"
    assert "Ingress text." in data["lead"]
    assert data["pig"] == "drink"
    assert data["logo"]["alt"] == "STUDiO-logo"
    assert data["body"][0]["blockType"] == "RichTextBlock"
    assert "Body content." in data["body"][0]["value"]
