# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4

import asyncio
from unittest import mock

import pytest

from loafer.conf import Settings
from loafer.exceptions import ConfigurationError, ConsumerError
from loafer.dispatcher import LoaferDispatcher
from loafer.manager import LoaferManager


def test_routes(event_loop):
    manager = LoaferManager()
    assert manager.routes
    assert len(manager.routes) == 1
    # this value is set on envvar in pytest.ini
    assert manager.routes[0].name == 'test'


def test_routes_raise_error_if_not_configured(event_loop):
    settings = Settings()
    settings.LOAFER_ROUTES = None

    with pytest.raises(ConfigurationError):
        manager = LoaferManager(settings)
        manager.routes

    settings.LOAFER_ROUTES = []

    with pytest.raises(ConfigurationError):
        manager = LoaferManager(settings)

        manager.routes


def test_routes_stop_manager_if_loop_is_running(event_loop):
    settings = Settings()
    settings.LOAFER_ROUTES = None
    manager = LoaferManager(settings)
    manager.stop = mock.Mock()

    with mock.patch.object(event_loop, 'is_running', return_value=True):
        with pytest.raises(ConfigurationError):
            manager.routes

        assert manager.stop.called
        assert manager.stop.called_once_with()


def test_consumers(event_loop):
    manager = LoaferManager()
    assert manager.consumers
    assert len(manager.consumers) == 1


def test_consumers_returns_empty_if_not_configure(event_loop):
    settings = Settings()
    settings.LOAFER_CONSUMERS = None
    manager = LoaferManager(settings)
    assert manager.consumers == []


def test_dispatcher(event_loop):
    manager = LoaferManager()
    assert manager.dispatcher
    assert isinstance(manager.dispatcher, LoaferDispatcher)


def test_on_future_errors(event_loop):
    manager = LoaferManager()
    manager.stop = mock.Mock()
    future = asyncio.Future()
    future.set_exception(ConsumerError)
    manager.on_future__errors(future)

    assert manager.stop.called
    assert manager.stop.called_once_with()

@@ -289,16 +289,43 @@
 nager%0A%0A%0A
+@pytest.mark.asyncio%0Aasync 
 def test
@@ -332,26 +332,16 @@
 _routes(
-event_loop
 ):%0A    m
@@ -521,16 +521,43 @@
 test'%0A%0A%0A
+@pytest.mark.asyncio%0Aasync 
 def test
@@ -594,26 +594,16 @@
 figured(
-event_loop
 ):%0A    s
@@ -913,16 +913,43 @@
 outes%0A%0A%0A
+@pytest.mark.asyncio%0Aasync 
 def test
@@ -988,26 +988,16 @@
 running(
-event_loop
 ):%0A    s
@@ -1148,21 +1148,24 @@
 .object(
-event
+manager.
 _loop, '
@@ -1353,24 +1353,51 @@
 ce_with()%0A%0A%0A
+@pytest.mark.asyncio%0Aasync 
 def test_con
@@ -1403,26 +1403,16 @@
 nsumers(
-event_loop
 ):%0A    m
@@ -1506,16 +1506,43 @@
  == 1%0A%0A%0A
+@pytest.mark.asyncio%0Aasync 
 def test
@@ -1583,26 +1583,16 @@
 nfigure(
-event_loop
 ):%0A    s
@@ -1724,16 +1724,43 @@
 == %5B%5D%0A%0A%0A
+@pytest.mark.asyncio%0Aasync 
 def test
@@ -1771,26 +1771,16 @@
 patcher(
-event_loop
 ):%0A    m
@@ -1896,16 +1896,43 @@
 cher)%0A%0A%0A
+@pytest.mark.asyncio%0Aasync 
 def test
@@ -1953,18 +1953,8 @@
 ors(
-event_loop
 ):%0A 

Update tests: usage of asyncio fixtures