From a5dff6659ce02047fa8ca65dc41d7ef1c2895081 Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Fri, 19 Apr 2013 21:18:23 +0200 Subject: [PATCH] Replace the Calculator fixture by a Number fixture. This makes the tests a little more realistic, since I want to add a test for class methods. --- tests/fixtures.py | 15 +++++++++------ tests/test_job.py | 12 ++++++------ tests/test_queue.py | 10 +++++----- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/tests/fixtures.py b/tests/fixtures.py index 77774d9..337fc70 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -49,13 +49,16 @@ def access_self(): return job.id -class Calculator(object): - """Test instance methods.""" - def __init__(self, denominator): - self.denominator = denominator +class Number(object): + def __init__(self, value): + self.value = value - def calculate(self, x, y): - return x * y / self.denominator + @classmethod + def divide(cls, x, y): + return x * y + + def div(self, y): + return self.value / y with Connection(): diff --git a/tests/test_job.py b/tests/test_job.py index 8b1d137..8f59472 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -1,7 +1,7 @@ import times from datetime import datetime from tests import RQTestCase -from tests.fixtures import Calculator, some_calculation, say_hello, access_self +from tests.fixtures import Number, some_calculation, say_hello, access_self from tests.helpers import strip_milliseconds from cPickle import loads from rq.job import Job, get_current_job @@ -51,13 +51,13 @@ class TestJob(RQTestCase): def test_create_instance_method_job(self): """Creation of jobs for instance methods.""" - c = Calculator(2) - job = Job.create(func=c.calculate, args=(3, 4)) + n = Number(2) + job = Job.create(func=n.div, args=(4,)) # Job data is set - self.assertEquals(job.func, c.calculate) - self.assertEquals(job.instance, c) - self.assertEquals(job.args, (3, 4)) + self.assertEquals(job.func, n.div) + self.assertEquals(job.instance, n) + self.assertEquals(job.args, (4,)) def test_create_job_from_string_function(self): """Creation of jobs using string specifier.""" diff --git a/tests/test_queue.py b/tests/test_queue.py index 061c984..e5bd7fd 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -1,5 +1,5 @@ from tests import RQTestCase -from tests.fixtures import Calculator, div_by_zero, say_hello, some_calculation +from tests.fixtures import Number, div_by_zero, say_hello, some_calculation from rq import Queue, get_failed_queue from rq.job import Job, Status from rq.exceptions import InvalidJobOperationError @@ -161,14 +161,14 @@ class TestQueue(RQTestCase): def test_dequeue_instance_method(self): """Dequeueing instance method jobs from queues.""" q = Queue() - c = Calculator(2) - result = q.enqueue(c.calculate, 3, 4) + n = Number(2) + q.enqueue(n.div, 4) job = q.dequeue() # The instance has been pickled and unpickled, so it is now a separate # object. Test for equality using each object's __dict__ instead. - self.assertEquals(job.instance.__dict__, c.__dict__) - self.assertEquals(job.func.__name__, 'calculate') + self.assertEquals(job.instance.__dict__, Number.__dict__) + self.assertEquals(job.func.__name__, 'divide') self.assertEquals(job.args, (3, 4)) def test_dequeue_ignores_nonexisting_jobs(self):