lundi 22 mai 2017

How do you test a method in django that delegates tasks within itself?

Say I have 2 celery tasks:

@app.task(name='process_one_line')
def process_one_line(line):
    do_alot(line)

@app.task(name='process_one_file')
def process_one_file(file_id):
    for line in get_file_by_id(file_id):
        process_one_line.delay(line)

and say I have a TestCase that looks like this:

def test_processing_a_file(self):
    process_one_file(self.file_id)

This will run fine, but the delegated process_one_line methods actually get pushed off to my other celery instance outside of the test cases. In short, my redis server running on port 6379 receives the tasks, and then pushes them off to the "main" workers. So whatever changes do_alot makes, they get reflected in the actual database instead of the test database created when you run django tests.

I can test things by having it test process_one_line by itself, but it would be nice if I could test the whole thing altogether.

Aucun commentaire:

Enregistrer un commentaire