from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase

from .models import News


class NewsTests(APITestCase):

    def setUp(self):
        News.objects.create(
            title="Google Article",
            url="www.google.com",
            source="Google",
            community="North Carolina",
            date="Feb 19, 2022",
            topic="Pandemic"
        )
        News.objects.create(
            title="Crisis in Miami",
            url="www.miamiherald.com",
            source="Miami Herald",
            community="Miami",
            date="Feb 18, 2022",
            topic="Extreme Weather"
        )
        News.objects.create(
            title="Life in Miami",
            url="www.miamiherald.com",
            source="Miami Herald",
            community="Miami",
            date="Feb 18, 2022",
            topic="Rising Temperatures"
        )

    def test_get_all_news(self):
        url = reverse("News:news-list")
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 3)

    def test_get_news_for_community(self):
        url = reverse("News:news-list")
        data = {"community": "Miami"}
        response = self.client.get(url, data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 2)
