from unittest import TestCase
from graphene.test import Client

from api.schema import schema
from news.factories import NewsTeaserFactory


class NewsTeaserTests(TestCase):
    def setUp(self):
        self.news_teaser1 = NewsTeaserFactory(order_id=1)
        self.news_teaser2 = NewsTeaserFactory(order_id=2)

        self.client = Client(schema=schema)

    def make_query(self):
        query = '''
            query NewsTeasers {
                newsTeasers {
                  edges {
                    node {
                      id
                      description
                      title
                      imageUrl
                      newsArticleUrl
                      displayDate
                      imageSource
                    }
                  }
                }
            }
        '''

        return self.client.execute(query)

    def test_can_get_news_teaser(self):
        result = self.make_query()
        self.assertIsNone(result.get('errors'))
        news_teasers = result.get('data').get('newsTeasers').get('edges')
        self.assertEqual(news_teasers[0].get('node').get('title'), self.news_teaser1.title)
        self.assertEqual(news_teasers[1].get('node').get('title'), self.news_teaser2.title)

