from twisted.internet.defer import inlineCallbacks, maybeDeferred
from twisted.trial import unittest

from zope.interface.verify import verifyObject

from scrapyd.interfaces import ISpiderQueue
from scrapyd import spiderqueue

class SpiderQueueTest(unittest.TestCase):
    """This test case can be used easily for testing other SpiderQueue's by
    just changing the _get_queue() method. It also supports queues with
    deferred methods.
    """

    def setUp(self):
        self.q = self._get_queue()
        self.name = 'spider1'
        self.args = {
            'arg1': 'val1',
            'arg2': 2,
            'arg3': u'\N{SNOWMAN}',
        }
        self.msg = self.args.copy()
        self.msg['name'] = self.name

    def _get_queue(self):
        return spiderqueue.SqliteSpiderQueue(':memory:')

    def test_interface(self):
        verifyObject(ISpiderQueue, self.q)

    @inlineCallbacks
    def test_add_pop_count(self):
        c = yield maybeDeferred(self.q.count)
        self.assertEqual(c, 0)

        yield maybeDeferred(self.q.add, self.name, **self.args)

        c = yield maybeDeferred(self.q.count)
        self.assertEqual(c, 1)

        m = yield maybeDeferred(self.q.pop)
        self.assertEqual(m, self.msg)

        c = yield maybeDeferred(self.q.count)
        self.assertEqual(c, 0)

    @inlineCallbacks
    def test_list(self):
        l = yield maybeDeferred(self.q.list)
        self.assertEqual(l, [])

        yield maybeDeferred(self.q.add, self.name, **self.args)
        yield maybeDeferred(self.q.add, self.name, **self.args)

        l = yield maybeDeferred(self.q.list)
        self.assertEqual(l, [self.msg, self.msg])

    @inlineCallbacks
    def test_clear(self):
        yield maybeDeferred(self.q.add, self.name, **self.args)
        yield maybeDeferred(self.q.add, self.name, **self.args)

        c = yield maybeDeferred(self.q.count)
        self.assertEqual(c, 2)

        yield maybeDeferred(self.q.clear)

        c = yield maybeDeferred(self.q.count)
        self.assertEqual(c, 0)


class JsonSpiderQueueTest(unittest.TestCase):
    def _get_queue(self):
        return spiderqueue.JsonSqliteSpiderQueue(':memory:')


class PickleSpiderQueueTest(unittest.TestCase):
    def _get_queue(self):
        return spiderqueue.PickleSqliteSpiderQueue(':memory:')

@@ -288,108 +288,8 @@
 ase 
-can be used easily for testing other SpiderQueue's by%0A    just changing the _get_queue() method. It 
 also
@@ -309,20 +309,16 @@
 ues with
-%0A   
  deferre
@@ -380,23 +380,47 @@
  = s
-elf._get_queue(
+piderqueue.SqliteSpiderQueue(':memory:'
 )%0A  
@@ -644,91 +644,8 @@
 me%0A%0A
-    def _get_queue(self):%0A        return spiderqueue.SqliteSpiderQueue(':memory:')%0A
 %0A   
@@ -1879,278 +1879,4 @@
  0)%0A
-%0A%0Aclass JsonSpiderQueueTest(unittest.TestCase):%0A    def _get_queue(self):%0A        return spiderqueue.JsonSqliteSpiderQueue(':memory:')%0A%0A%0Aclass PickleSpiderQueueTest(unittest.TestCase):%0A    def _get_queue(self):%0A        return spiderqueue.PickleSqliteSpiderQueue(':memory:')%0A

drop tests for plaintext & pickle spider queue