from django.test import override_settings, SimpleTestCase
from pillowtop import get_all_pillow_instances, get_all_pillow_classes, get_pillow_by_name
from pillowtop.checkpoints.manager import PillowCheckpoint
from pillowtop.dao.mock import MockDocumentStore
from pillowtop.exceptions import PillowNotFoundError
from pillowtop.feed.mock import RandomChangeFeed
from pillowtop.feed.interface import Change
from pillowtop.listener import BasicPillow
from inspect import isclass
from pillowtop.pillow.interface import ConstructedPillow
from pillowtop.processor import LoggingProcessor


class FakePillow(BasicPillow):
    _couch_db = object()  # hack to make tests pass since this is required


@override_settings(PILLOWTOPS={'test': ['pillowtop.tests.FakePillow']})
class PillowImportTestCase(SimpleTestCase):

    def test_get_pillow_classes(self):
        pillows = get_all_pillow_classes()
        self.assertEquals(len(pillows), 1)
        self.assertTrue(isclass(pillows[0]))

    def test_get_pillow_instances(self):
        pillows = get_all_pillow_instances()
        self.assertEquals(len(pillows), 1)
        self.assertFalse(isclass(pillows[0]))

    def test_get_pillow_by_name(self):
        self.assertEqual(FakePillow, get_pillow_by_name('FakePillow', instantiate=False))

    def test_get_pillow_by_name_instantiate(self):
        self.assertEqual(FakePillow, type(get_pillow_by_name('FakePillow', instantiate=True)))

    def test_get_pillow_by_name_missing(self):
        with self.assertRaises(PillowNotFoundError):
            get_pillow_by_name('MissingPillow')


class FakeConstructedPillow(ConstructedPillow):
    pass


def make_fake_constructed_pillow():
    fake_dao = MockDocumentStore()
    pillow = FakeConstructedPillow(
        name='FakeConstructedPillowName',
        document_store=fake_dao,
        checkpoint=PillowCheckpoint(fake_dao, 'fake-constructed-pillow'),
        change_feed=RandomChangeFeed(10),
        processor=LoggingProcessor(),
    )
    return pillow


PILLOWTOPS_OVERRIDE = {
    'test': [
        {
            'name': 'FakeConstructedPillowName',
            'class': 'pillowtop.tests.FakeConstructedPillow',
            'instance': 'pillowtop.tests.make_fake_constructed_pillow'
        }
    ]
}

@override_settings(PILLOWTOPS=PILLOWTOPS_OVERRIDE)
class PillowFactoryFunctionTestCase(SimpleTestCase):

    def test_get_pillow_classes(self):
        pillows = get_all_pillow_classes()
        self.assertEquals(len(pillows), 1)
        pillow_class = pillows[0]
        self.assertTrue(isclass(pillow_class))
        self.assertEqual(FakeConstructedPillow, pillow_class)

    def test_get_pillow_instances(self):
        pillows = get_all_pillow_instances()
        self.assertEquals(len(pillows), 1)
        pillow = pillows[0]
        self.assertFalse(isclass(pillow))
        self.assertEqual(FakeConstructedPillow, type(pillow))
        self.assertEqual(FakeConstructedPillow.get_name(), 'FakeConstructedPillowName')

    def test_get_pillow_class_by_name(self):
        pillow = get_pillow_by_name('FakeConstructedPillowName', instantiate=False)
        self.assertEqual(FakeConstructedPillow, pillow)

    def test_get_pillow_by_name_instantiate(self):
        pillow = get_pillow_by_name('FakeConstructedPillowName', instantiate=True)
        self.assertFalse(isclass(pillow))
        self.assertEqual(FakeConstructedPillow, type(pillow))


@override_settings(PILLOWTOPS=PILLOWTOPS_OVERRIDE)
class PillowTestCase(SimpleTestCase):

    def test_pillow_reset_checkpoint(self):
        pillow = make_fake_constructed_pillow()
        seq_id = '456'
        pillow.set_checkpoint(Change('123', seq_id))
        self.assertEqual(pillow.checkpoint.get_or_create()['seq'], seq_id)
        pillow.reset_checkpoint()
        self.assertEqual(pillow.checkpoint.get_or_create()['seq'], '0')

@@ -2904,32 +2904,17 @@
 rtEqual(
-FakeConstructedP
+p
 illow.ge
@@ -3377,16 +3377,89 @@
 illow))%0A
+        self.assertEqual(pillow.get_name(), 'FakeConstructedPillowName')%0A
 %0A%0A@overr

fix tests