Skip to content

Commit

Permalink
test: Add daemonstatus test
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Jul 20, 2024
1 parent 787f733 commit c83f50b
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/test_webservice.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import io
import os
import re
Expand All @@ -12,6 +13,8 @@
from scrapyd.webservice import UtilsCache, get_spider_list
from tests import get_egg_data, root_add_version

job1 = Job("p1", "s1", end_time=datetime.datetime(2001, 2, 3, 4, 5, 6, 7))


@pytest.fixture()
def app(chdir):
Expand All @@ -22,6 +25,14 @@ def add_test_version(app, project, version, basename):
app.getComponent(IEggStorage).put(io.BytesIO(get_egg_data(basename)), project, version)


def assert_webservice(txrequest, root, basename, args, expected):
txrequest.args = args.copy()
content = root.children[b"%b.json" % basename.encode()].render_GET(txrequest)

assert content.pop("node_name")
assert content == expected


def test_get_spider_list_log_stdout(app):
add_test_version(app, "logstdout", "logstdout", "logstdout")
spiders = get_spider_list("logstdout")
Expand Down Expand Up @@ -90,6 +101,26 @@ def test_utils_cache_repr():
assert repr(cache) == "UtilsCache(cache_manager=JsonSqliteDict({'key': 'value'}))"


def test_daemonstatus(txrequest, root_with_egg):
expected = {"status": "ok", "running": 0, "pending": 0, "finished": 0}
assert_webservice(txrequest, root_with_egg, "daemonstatus", {}, expected)

root_with_egg.launcher.finished.add(job1)

expected["finished"] += 1
assert_webservice(txrequest, root_with_egg, "daemonstatus", {}, expected)

root_with_egg.launcher.processes[0] = job1

expected["running"] += 1
assert_webservice(txrequest, root_with_egg, "daemonstatus", {}, expected)

root_with_egg.scheduler.queues["quotesbot"].add("quotesbot")

expected["pending"] += 1
assert_webservice(txrequest, root_with_egg, "daemonstatus", {}, expected)


def test_list_spiders(txrequest, root):
UtilsCache.invalid_cache("myproject") # test_get_spider_list fills cache

Expand Down

0 comments on commit c83f50b

Please sign in to comment.