from celery import current_app
from celery.task import task
from redis import StrictRedis

from celery_dedupe import DedupeTask
from celery_dedupe.storage.redis import RedisStorage
from tests import settings

redis = StrictRedis(db=9)
storage = RedisStorage(redis, expiry=60)


@task(base=DedupeTask, storage=storage)
def noop_task(*a, **kw):
    return None


@task(base=DedupeTask, storage=storage)
def raise_exception(*a, **kw):
    raise AttributeError()


class TestDedupeTask(object):

    def setup_class(self):
        current_app.config_from_object(settings)

    def test_create_key(self):
        task = DedupeTask()

        key = task._create_key(tuple(), {})
        assert key == 'cd:celery_dedupe.tasks.DedupeTask:d41d8cd98f00b204e9800998ecf8427e'

        key = task._create_key((True, False), {})
        assert key == 'cd:celery_dedupe.tasks.DedupeTask:85eadac9a27eb1f6dccf00145df6003b'

        key = task._create_key(tuple(), {'something': 2})
        assert key == 'cd:celery_dedupe.tasks.DedupeTask:a758dbbdf7ba75fc39732ab4cca53049'

        key = task._create_key(tuple(), {'something_else': 2})
        assert key == 'cd:celery_dedupe.tasks.DedupeTask:271a7d4a14d7892b87b8fc96d50507a9'

    def test_apply_task_twice(self):
        result1 = noop_task.apply_async(countdown=10)
        result2 = noop_task.apply_async()
        assert result1.task_id == result2.task_id

    def test_apply_task_once(self):
        result = noop_task.delay(1)
        assert result.get() == None

    def test_key_cleared_on_exception(self):
        key = raise_exception._create_key(tuple(), dict())
        assert not redis.exists(key)
        try:
            raise_exception.delay().get()
        except AttributeError:
            pass

        assert not redis.exists(key)

@@ -388,24 +388,35 @@
 rage=storage
+, bind=True
 )%0Adef raise_
@@ -425,16 +425,22 @@
 ception(
+self, 
 *a, **kw
@@ -521,32 +521,56 @@
 up_class(self):%0A
+        redis.flushdb()%0A
         current_
@@ -1337,10 +1337,11 @@
 own=
-1
 0
+.5
 )%0A  
@@ -1366,27 +1366,21 @@
 op_task.
-apply_async
+delay
 ()%0A     
@@ -1423,16 +1423,129 @@
 .task_id
+%0A        assert result1.get() == result2.get()%0A        assert not redis.exists(noop_task._create_key(None, None))
 %0A%0A    de
@@ -1645,16 +1645,81 @@
  == None
+%0A        assert not redis.exists(noop_task._create_key((1,), %7B%7D))
 %0A%0A    de
@@ -1803,23 +1803,18 @@
 key(
-tuple(), dict()
+None, None
 )%0A  
@@ -1950,12 +1950,58 @@
     
-p
 ass
+ert True%0A        else:%0A            assert False
 %0A%0A  

Improve tests