Richard
Richard

Reputation: 65510

Running Django management command from tests?

I have a Django management command to create materialized views on my database, that runs as follows (I store my database login details in environment variables):

python manage.py create_db_matviews $DB_NAME $DB_USER $DB_PASS

The management command code looks like this:

 class Command(BaseCommand):
   def handle(self, *args, **options):
    db_name = args[0]
    db_user = args[1]
    db_pass = args[2]
    self.conn = psycopg2.connect(database=db_name, user=db_user,
                                 password=db_pass)

Now I want to run the management command from inside my tests, so that the materialized views get created on my test data. However this does not work:

def setUpModule():
    # load fixtures, then... 
    management.call_command('create_db_matviews',
                        ['test', 'test', 'test'], verbosity=0)

It fails as follows:

.... 
db_user = args[1]
IndexError: tuple index out of range

How can I supply arguments to the management script in the way that it wants?

Also, what credentials should I use to get access to the test database?

Upvotes: 0

Views: 200

Answers (1)

Dzarafata
Dzarafata

Reputation: 558

It fails because in tuple of args You set only one argument, with is list of 3 values. You should use:

management.call_command('create_db_matviews', 'test', 'test', 'test', verbosity=0)

*agrs is pythons way to send multiple parameters and all of them will be send as tuple. You send (['test', 'test', 'test', ],) so args[0] was ['test', 'test', 'test', ], not 'test'

Upvotes: 1

Related Questions