Skip to content

Commit

Permalink
tests: Remove classes from tests (spiderqueue)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Jul 19, 2024
1 parent 3a7311d commit 1a50168
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 44 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"coveralls",
"pytest",
"pytest-cov",
"pytest-twisted",
"requests",
],
"docs": [
Expand Down
91 changes: 47 additions & 44 deletions tests/test_spiderqueue.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,69 @@
import pytest
from twisted.internet.defer import inlineCallbacks, maybeDeferred
from twisted.trial import unittest
from zope.interface.verify import verifyObject

from scrapyd import spiderqueue
from scrapyd.config import Config
from scrapyd.interfaces import ISpiderQueue

name = "spider1"
priority = 5
args = {
"arg1": "val1",
"arg2": 2,
"arg3": "\N{SNOWMAN}",
}
expected = args.copy()
expected["name"] = name

class SpiderQueueTest(unittest.TestCase):
"""This test case also supports queues with deferred methods."""

def setUp(self):
self.q = spiderqueue.SqliteSpiderQueue(Config(values={"dbs_dir": ":memory:"}), "quotesbot")
self.name = "spider1"
self.priority = 5
self.args = {
"arg1": "val1",
"arg2": 2,
"arg3": "\N{SNOWMAN}",
}
self.msg = self.args.copy()
self.msg["name"] = self.name
@pytest.fixture()
def queue():
return spiderqueue.SqliteSpiderQueue(Config(values={"dbs_dir": ":memory:"}), "quotesbot")

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)
def test_interface(queue):
verifyObject(ISpiderQueue, queue)

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

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

m = yield maybeDeferred(self.q.pop)
self.assertEqual(m, self.msg)
yield maybeDeferred(queue.add, name, priority, **args)

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

@inlineCallbacks
def test_list(self):
actual = yield maybeDeferred(self.q.list)
self.assertEqual(actual, [])
m = yield maybeDeferred(queue.pop)
assert m == expected

yield maybeDeferred(self.q.add, self.name, self.priority, **self.args)
yield maybeDeferred(self.q.add, self.name, self.priority, **self.args)
c = yield maybeDeferred(queue.count)
assert c == 0

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

@inlineCallbacks
def test_clear(self):
yield maybeDeferred(self.q.add, self.name, self.priority, **self.args)
yield maybeDeferred(self.q.add, self.name, self.priority, **self.args)
@inlineCallbacks
def test_list(queue):
actual = yield maybeDeferred(queue.list)
assert actual == []

c = yield maybeDeferred(self.q.count)
self.assertEqual(c, 2)
yield maybeDeferred(queue.add, name, priority, **args)
yield maybeDeferred(queue.add, name, priority, **args)

yield maybeDeferred(self.q.clear)
actual = yield maybeDeferred(queue.list)
assert actual == [expected, expected]

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

@inlineCallbacks
def test_clear(queue):
yield maybeDeferred(queue.add, name, priority, **args)
yield maybeDeferred(queue.add, name, priority, **args)

c = yield maybeDeferred(queue.count)
assert c == 2

yield maybeDeferred(queue.clear)

c = yield maybeDeferred(queue.count)
assert c == 0

0 comments on commit 1a50168

Please sign in to comment.