from unittest.mock import Mock, patch

from django.test import TestCase

from newsreader.news.collection.exceptions import (
    StreamDeniedException,
    StreamException,
    StreamForbiddenException,
    StreamNotFoundException,
    StreamParseException,
    StreamTimeOutException,
)
from newsreader.news.collection.feed import FeedStream
from newsreader.news.collection.tests.factories import FeedFactory

from .mocks import simple_mock, simple_mock_parsed


class FeedStreamTestCase(TestCase):
    def setUp(self):
        self.maxDiff = None

        self.patched_fetch = patch("newsreader.news.collection.feed.fetch")
        self.mocked_fetch = self.patched_fetch.start()

    def tearDown(self):
        patch.stopall()

    def test_simple_stream(self):
        self.mocked_fetch.return_value = Mock(content=simple_mock)

        rule = FeedFactory()
        stream = FeedStream(rule)

        data, stream = stream.read()

        self.mocked_fetch.assert_called_once_with(rule.url)
        self.assertEquals(data, simple_mock_parsed)
        self.assertEquals(stream.rule, rule)

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

        rule = FeedFactory()
        stream = FeedStream(rule)

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

        self.mocked_fetch.assert_called_once_with(rule.url)

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

        rule = FeedFactory()
        stream = FeedStream(rule)

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

        self.mocked_fetch.assert_called_once_with(rule.url)

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

        rule = FeedFactory()
        stream = FeedStream(rule)

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

        self.mocked_fetch.assert_called_once_with(rule.url)

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

        rule = FeedFactory()
        stream = FeedStream(rule)

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

        self.mocked_fetch.assert_called_once_with(rule.url)

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

        rule = FeedFactory()
        stream = FeedStream(rule)

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

        self.mocked_fetch.assert_called_once_with(rule.url)

    @patch("newsreader.news.collection.feed.parse")
    def test_stream_raises_parse_exception(self, mocked_parse):
        self.mocked_fetch.return_value = Mock()
        mocked_parse.side_effect = TypeError

        rule = FeedFactory()
        stream = FeedStream(rule)

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