Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Koed00 committed May 8, 2015
1 parent 4c7d2b2 commit 444cd8e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,5 @@ target/

rqjob
db.sqlite3
manage.py
manage.py
cover
3 changes: 0 additions & 3 deletions django_rq_jobs/tests.py

This file was deleted.

1 change: 1 addition & 0 deletions django_rq_jobs/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'ilan'
13 changes: 13 additions & 0 deletions django_rq_jobs/tests/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django_rq import job
from django.core import management


@job
def django_check():
return management.call_command('check')


@job
def django_arg_check(verbosity):
if not verbosity:
raise ValueError
86 changes: 86 additions & 0 deletions django_rq_jobs/tests/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from datetime import timedelta

from django.core.exceptions import ImproperlyConfigured

from django.test import TestCase, override_settings
from django.utils import timezone

from django.utils.translation import activate
from django.core import management

from django_rq_jobs.models import underscore_to_camelcase, task_list, queue_index_by_name, Job


class RQJobsTestCase(TestCase):
@override_settings(RQ_JOBS_MODULE='django_rq_jobs.tests.tasks')
def setUp(self):
activate('en-en')
Job.objects.create(task='django_check', schedule_type=Job.HOURLY)
Job.objects.create(task='django_check', schedule_type=Job.WEEKLY)
Job.objects.create(task='django_check', schedule_type=Job.MONTHLY)
Job.objects.create(task='django_check', schedule_type=Job.QUARTERLY)
Job.objects.create(task='django_check', schedule_type=Job.YEARLY)
Job.objects.create(task='django_arg_check', schedule_type=Job.ONCE, args={'verbosity': 3})

def test_camel_case(self):
self.assertEqual(underscore_to_camelcase('this_is_a_function'), 'This Is A Function')

@override_settings(RQ_JOBS_MODULE='django_rq_jobs.tests.tasks')
def test_task_list(self):
self.assertEqual(task_list(), [('django_arg_check', 'Django Arg Check'), ('django_check', 'Django Check')])
with self.settings(RQ_JOBS_MODULE=None):
self.assertRaises(ImproperlyConfigured, task_list)


@override_settings(RQ_QUEUES="{'high': {},'default': {}, 'low': {}")
def test_queue_index_by_name(self):
self.assertEqual(queue_index_by_name('default'), 1)
self.assertEqual(queue_index_by_name('nomnom'), 0)

@override_settings(RQ_JOBS_MODULE='django_rq_jobs.tests.tasks')
def test_create_job(self):
"""test simple job creation """
test_job = Job.objects.get(task='django_check', schedule_type=Job.HOURLY)
self.assertEqual(test_job.task, 'django_check')
self.assertEqual(test_job.args, None)
self.assertEqual(test_job.schedule_type, Job.HOURLY)
self.assertEqual(test_job.repeats, -1)
self.assertEqual(test_job.last_run, None)
self.assertTrue(test_job.next_run < timezone.now())
self.assertEqual(test_job.rq_id, None)
self.assertEqual(test_job.rq_job, None)
self.assertEqual(test_job.rq_link(), None)
self.assertEqual(test_job.rq_origin, None)
self.assertEqual(test_job.rq_status(), None)

@override_settings(RQ_JOBS_MODULE='django_rq_jobs.tests.tasks')
def test_run_job(self):
"""run a job and check if it's rescheduled properly"""
management.call_command('rqjobs')
test_job = Job.objects.get(task='django_check', schedule_type=Job.HOURLY)
self.assertNotEqual(test_job.rq_id, None)
self.assertNotEqual(test_job.rq_origin, None)
self.assertNotEqual(test_job.rq_job, None)
self.assertNotEqual(test_job.rq_status(), None)
self.assertNotEqual(test_job.rq_origin, None)
self.assertNotEqual(test_job.rq_link(), None)
self.assertNotEqual(test_job.last_run, None)
self.assertTrue(test_job.next_run > timezone.now())

@override_settings(RQ_JOBS_MODULE='django_rq_jobs.tests.tasks')
def test_run_once_job(self):
"""run an single run job with arguments and check if it gets deleted"""
test_job = Job.objects.get(task='django_arg_check')
management.call_command('rqjobs')
self.assertFalse(Job.objects.filter(pk=test_job.pk).exists())

@override_settings(RQ_JOBS_MODULE='django_rq_jobs.tests.tasks')
def test_run_limited_job(self):
"""run a limited run job twice to see if it counts down and gets deleted"""
test_job = Job.objects.create(task='django_check', schedule_type=Job.HOURLY, repeats=2,
next_run=timezone.now() + timedelta(hours=-2))
management.call_command('rqjobs')
self.assertEqual(Job.objects.get(pk=test_job.pk).repeats, 1)
management.call_command('rqjobs')
self.assertFalse(Job.objects.filter(pk=test_job.pk).exists())

0 comments on commit 444cd8e

Please sign in to comment.