[Django]-Changing settings for the tests in django (search engine for haystack to be specific)

0๐Ÿ‘

I took the answer from here and modified it slightly. This works great for me:

from contextlib import contextmanager

@contextmanager
def connection(**kwargs):
    from haystack import connections

    for key, new_value in kwargs.items():
        setattr(connections, key, new_value)
        connections['default'].options['URL'] = connections.connections_info['default']['URL']
    yield

My test, then, looks like:

def test_job_detail_by_title_slug_job_id(self):
    with connection(connections_info=solr_settings.HAYSTACK_CONNECTIONS):
        resp = self.client.get('/0/rts-crb-unix-production-engineer/27216666/job/')
        self.assertEqual(resp.status_code, 404)
        resp = self.client.get('/indianapolis/indiana/usa/jobs/')
        self.assertEqual(resp.status_code, 200)
๐Ÿ‘คmattdeboard

Leave a comment