from json import JSONDecodeError
from unittest.mock import patch

from django.test import TestCase

from newsreader.news.collection.exceptions import (
    StreamDeniedException,
    StreamException,
    StreamForbiddenException,
    StreamNotFoundException,
    StreamParseException,
    StreamTimeOutException,
)
from newsreader.news.collection.tests.factories import TwitterTimelineFactory
from newsreader.news.collection.tests.twitter.stream.mocks import simple_mock
from newsreader.news.collection.twitter import TwitterStream


class TwitterStreamTestCase(TestCase):
    def setUp(self):
        self.patched_fetch = patch("newsreader.news.collection.twitter.fetch")
        self.mocked_fetch = self.patched_fetch.start()

    def tearDown(self):
        patch.stopall()

    def test_simple_stream(self):
        self.mocked_fetch.return_value.json.return_value = simple_mock

        timeline = TwitterTimelineFactory()
        stream = TwitterStream(timeline)

        data, stream = stream.read()

        self.assertEquals(data, simple_mock)
        self.assertEquals(stream, stream)

        self.mocked_fetch.assert_called()

    def test_stream_raises_exception(self):
        self.mocked_fetch.side_effect = StreamException

        timeline = TwitterTimelineFactory()
        stream = TwitterStream(timeline)

        with self.assertRaises(StreamException):
            stream.read()

        self.mocked_fetch.assert_called()

    def test_stream_raises_denied_exception(self):
        self.mocked_fetch.side_effect = StreamDeniedException

        timeline = TwitterTimelineFactory()
        stream = TwitterStream(timeline)

        with self.assertRaises(StreamDeniedException):
            stream.read()

        self.mocked_fetch.assert_called()

    def test_stream_raises_not_found_exception(self):
        self.mocked_fetch.side_effect = StreamNotFoundException

        timeline = TwitterTimelineFactory()
        stream = TwitterStream(timeline)

        with self.assertRaises(StreamNotFoundException):
            stream.read()

        self.mocked_fetch.assert_called()

    def test_stream_raises_time_out_exception(self):
        self.mocked_fetch.side_effect = StreamTimeOutException

        timeline = TwitterTimelineFactory()
        stream = TwitterStream(timeline)

        with self.assertRaises(StreamTimeOutException):
            stream.read()

        self.mocked_fetch.assert_called()

    def test_stream_raises_forbidden_exception(self):
        self.mocked_fetch.side_effect = StreamForbiddenException

        timeline = TwitterTimelineFactory()
        stream = TwitterStream(timeline)

        with self.assertRaises(StreamForbiddenException):
            stream.read()

        self.mocked_fetch.assert_called()

    def test_stream_raises_parse_exception(self):
        self.mocked_fetch.return_value.json.side_effect = JSONDecodeError(
            "No json found", "{}", 5
        )

        timeline = TwitterTimelineFactory()
        stream = TwitterStream(timeline)

        with self.assertRaises(StreamParseException):
            stream.read()

        self.mocked_fetch.assert_called()
