0

I want a utility to:

  • load up a database with a complete dump (several megabytes)
  • execute some Django code and measurements on that database
  • have the database go away
  • use a command-line tool with arguments to control the behavior of this utility

This is very much like a Django unit test, but where the results are not pass-fail. Rather, there is just some computation run on an ephemeral database.

Also, I will use lots of different data dumps, so using fixtures is not a practical way to load data. (I can load using other utilities, but not using Django unit testing facilities. I've solved this issue.)

What's the easiest way to do the bullet points above?

Should I write a program that is a Django unit test, but somehow is not executed with my normal unit tests? Some other option?

This question is slightly related, in that it describes how to pass command-line arguments to a Django unit test. Looks awkward, but maybe that's the way?

dfrankow
  • 20,191
  • 41
  • 152
  • 214
  • First thing that comes to my mind is to populate an SQLite db with the data, do the test, delete it, then repeat. – lucutzu33 Mar 17 '23 at 16:20
  • Package dbbackup/dbrestore can help to snapshot and restore database. You can then write a script or unit test that control snapshot mounting (using Python API for management command) and perform your measurements. – jlandercy Mar 17 '23 at 16:24
  • @jlandercy I can populate the DB. The problem is automatically pointing my Django code to the new data. The way unit testing handles this is that if you have the "default" database set up, it creates a parallel database (called "default_test"??), but I have no idea how. – dfrankow Mar 17 '23 at 16:55
  • @lucutzu33 Thanks. I know how to populate the data. The problem is pointing the Django code to it. I don't want to blast the database I have, I want an ephemeral one, like unit tests do. – dfrankow Mar 17 '23 at 16:56

1 Answers1

0

I ended up doing this like Django unit tests do (creation.py):

    # create the DB
    # ...

    # re-point the default connection to the new test database:
    connection = connections["default"]
    connection.close()
    settings.DATABASES[connection.alias]["NAME"] = new_db_name
    connection.settings_dict["NAME"] = new_db_name

    # destroy the DB
    # ...

It looks like if I close the connection, then it re-opens with the new settings.

dfrankow
  • 20,191
  • 41
  • 152
  • 214