from news.admin import NewsSidebarViewSet
from news.models import NewsPage
from tests.conftest import NewsPageFactory


def test_news_page_persists_via_factory(news_index):
    page = NewsPageFactory(parent=news_index, title="Big news", excerpt="Short summary")

    reloaded = NewsPage.objects.get(pk=page.pk)
    assert reloaded.title == "Big news"
    assert reloaded.excerpt == "Short summary"


def test_news_sidebar_viewset_wired_to_newspage():
    assert NewsSidebarViewSet.model is NewsPage
    assert NewsSidebarViewSet.add_to_admin_menu is True


def test_graphql_news_index_query(news_index, graphql_post):
    response, body = graphql_post(
        """
        query {
          newsIndex {
            title
          }
        }
        """
    )

    assert response.status_code == 200
    assert "errors" not in body, body
    assert body["data"]["newsIndex"]["title"] == news_index.title
