Skip to content

Commit

Permalink
test every endpoint at least once for status code 200 and fix broken …
Browse files Browse the repository at this point in the history
…endpoints
  • Loading branch information
jvllmr committed Oct 29, 2024
1 parent 0919be9 commit 7712e7e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 17 deletions.
22 changes: 11 additions & 11 deletions rq_dashboard/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def workers_overview(instance_number):
"rq_dashboard/workers.html",
current_instance=instance_number,
instance_list=escape_format_instance_list(current_app.config.get("RQ_DASHBOARD_REDIS_URL")),
workers=Worker.all(),
workers=Worker.all(connection=current_app.redis_conn),
rq_url_prefix=url_for(".queues_overview"),
rq_dashboard_version=rq_dashboard_version,
rq_version=rq_version,
Expand Down Expand Up @@ -440,7 +440,7 @@ def requeue_job_view(job_id):
@blueprint.route("/requeue/<queue_name>", methods=["GET", "POST"])
@jsonify
def requeue_all(queue_name):
fq = Queue(queue_name, serializer=config.serializer).failed_job_registry
fq = Queue(queue_name, serializer=config.serializer, connection=current_app.redis_conn).failed_job_registry
job_ids = fq.get_job_ids()
count = len(job_ids)
for job_id in job_ids:
Expand All @@ -453,30 +453,30 @@ def requeue_all(queue_name):
@jsonify
def empty_queue(queue_name, registry_name):
if registry_name == "queued":
q = Queue(queue_name, serializer=config.serializer)
q = Queue(queue_name, serializer=config.serializer, connection=current_app.redis_conn)
q.empty()
elif registry_name == "failed":
registry = FailedJobRegistry(queue_name)
registry = FailedJobRegistry(queue_name, connection=current_app.redis_conn)
for id in registry.get_job_ids():
delete_job_view(id, registry)
elif registry_name == "deferred":
registry = DeferredJobRegistry(queue_name)
registry = DeferredJobRegistry(queue_name, connection=current_app.redis_conn)
for id in registry.get_job_ids():
delete_job_view(id, registry)
elif registry_name == "started":
registry = StartedJobRegistry(queue_name)
registry = StartedJobRegistry(queue_name, connection=current_app.redis_conn)
for id in registry.get_job_ids():
delete_job_view(id, registry)
elif registry_name == "finished":
registry = FinishedJobRegistry(queue_name)
registry = FinishedJobRegistry(queue_name, connection=current_app.redis_conn)
for id in registry.get_job_ids():
delete_job_view(id, registry)
elif registry_name == "canceled":
registry = CanceledJobRegistry(queue_name)
registry = CanceledJobRegistry(queue_name, connection=current_app.redis_conn)
for id in registry.get_job_ids():
delete_job_view(id, registry)
elif registry_name == "scheduled":
registry = ScheduledJobRegistry(queue_name)
registry = ScheduledJobRegistry(queue_name, connection=current_app.redis_conn)
for id in registry.get_job_ids():
delete_job_view(id, registry)

Expand All @@ -486,7 +486,7 @@ def empty_queue(queue_name, registry_name):
@blueprint.route("/queue/<queue_name>/compact", methods=["POST"])
@jsonify
def compact_queue(queue_name):
q = Queue(queue_name, serializer=config.serializer)
q = Queue(queue_name, serializer=config.serializer, connection=current_app.redis_conn)
q.compact()
return dict(status="OK")

Expand Down Expand Up @@ -600,7 +600,7 @@ def list_jobs(instance_number, queue_name, registry_name, per_page, order, page)
@blueprint.route("/<int:instance_number>/data/job/<job_id>.json")
@jsonify
def job_info(instance_number, job_id):
job = Job.fetch(job_id, serializer=config.serializer)
job = Job.fetch(job_id, serializer=config.serializer, connection=current_app.redis_conn)
result = dict(
id=job.id,
created_at=serialize_date(job.created_at),
Expand Down
65 changes: 59 additions & 6 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


HTTP_OK = 200

REGISTRY_NAMES = ["failed", "deferred", "scheduled", "queued", "started", "finished", "canceled"]
class BasicTestCase(unittest.TestCase):
redis_client = None

Expand Down Expand Up @@ -81,11 +81,12 @@ def some_work():
self.assertEqual(response_del.status_code, HTTP_OK)

def test_registry_jobs_list(self):
response = self.client.get('/0/data/jobs/default/failed/8/asc/1.json')
self.assertEqual(response.status_code, HTTP_OK)
data = json.loads(response.data.decode('utf8'))
self.assertIsInstance(data, dict)
self.assertIn('jobs', data)
for registry_name in REGISTRY_NAMES:
response = self.client.get(f'/0/data/jobs/default/{registry_name}/8/asc/1.json')
self.assertEqual(response.status_code, HTTP_OK)
data = json.loads(response.data.decode('utf8'))
self.assertIsInstance(data, dict)
self.assertIn('jobs', data)

def test_worker_python_version_field(self):
w = Worker(['q'], connection=self.app.redis_conn)
Expand Down Expand Up @@ -143,6 +144,58 @@ def some_work():
data_dsc = json.loads(response_dsc.data.decode('utf8'))
self.assertEqual(job_ids[::-1], [job['id'] for job in data_dsc['jobs']])

def test_job_info(self):
def some_work():
return
q = Queue(connection=self.app.redis_conn)
job = q.enqueue(some_work)
job_info_url = f'/0/data/job/{job.id}.json'
response_info = self.client.get(job_info_url)
self.assertEqual(response_info.status_code, HTTP_OK)

def test_compact_queue(self):
def some_work():
return
q = Queue(connection=self.app.redis_conn)
q.enqueue(some_work)
compact_queue_url = f"/queue/{q.name}/compact"
response_compact = self.client.post(compact_queue_url)
self.assertEqual(response_compact.status_code, HTTP_OK)

def test_empty_queue(self):
for registry_name in REGISTRY_NAMES:
response = self.client.post(f'/queue/default/{registry_name}/empty')
self.assertEqual(response.status_code, HTTP_OK)

def test_requeue_all(self):
def some_failing_work():
raise Exception
q = Queue(connection=self.app.redis_conn)
job = q.enqueue(some_failing_work)
worker = Worker([q], connection=self.app.redis_conn)
worker.execute_job(job, q)
requeue_all_url = f"/requeue/{q.name}"
for method in (self.client.post, self.client.get):
response = method(requeue_all_url)
self.assertEqual(response.status_code, HTTP_OK)

def test_requeue_one(self):
def some_failing_work():
raise Exception
q = Queue(connection=self.app.redis_conn)
job = q.enqueue(some_failing_work)
worker = Worker([q], connection=self.app.redis_conn)
worker.execute_job(job, q)
requeue_one_url = f"/job/{job.id}/requeue"
response = self.client.post(requeue_one_url)
self.assertEqual(response.status_code, HTTP_OK)

def test_workers_overview(self):
workers_overview_url = "/0/view/workers"
response = self.client.get(workers_overview_url)
self.assertEqual(response.status_code, HTTP_OK)



__all__ = [
'BasicTestCase',
Expand Down

0 comments on commit 7712e7e

Please sign in to comment.