0

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?

1 Answer 1

1

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'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.