Django Development and Staging

I have two boxes: dev and production. I need a very simple method for working on the dev machine and then pushing to production.

Environment

Use direnv to maintain environment based on directory.

Use two seperate .envrc files on dev and production. Don’t check-in to

Testing

Create a test case:

# phisaverweb/tests.py

class TestEmails(TestCase):
    fixtures = ['phisaverweb/fixtures/email-tests.json']

    def test_send(self):
        email_weekly_summary()
        print ("hi")

Tests use a seperate database. To get data to test against, it’s best to programmatically create it.

But I used fixtures. First, dump the current database. If you get “django test fixtures contenttypes.ContentType unique constraint duplicate key” type error, I don’t understand why but “natural primary” and “natural foreign” (see below) fixes it:

python manage.py dumpdata --indent 4 --natural-primary --natural-foreign > phisaverweb/fixtures/email-tests.json

Then run the tests via python manage.py tests. In VSCode you can make a launch task:

  {
            "name": "Django Tests",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "test",
                "--noinput",
                "--keepdb", # don't destroy db on exit
                "phisaverweb.tests" # run just these tests
            ],
            "django": true,
            "env": {"PYTHONPATH":"${workspaceFolder}/.."}
        },

Leave a Reply

Your email address will not be published.