Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--nodb not safe for database-backed apps! #13

Open
arowla opened this issue Oct 21, 2010 · 4 comments
Open

--nodb not safe for database-backed apps! #13

arowla opened this issue Oct 21, 2010 · 4 comments

Comments

@arowla
Copy link

arowla commented Oct 21, 2010

If you happen to run tests with the --nodb option on an app that does in fact use the ORM to do CRUD operations on your database, it WILL corrupt whatever your default (not test) database is. You can test this by making a simple app, let's call it "myapp", with a single model, set up to use a SQLite3 database:

from django.db import models

class MyTable(models.Model):
    a = models.IntegerField(primary_key=True)

And a single test:

from django.test import TestCase
from myapp.models import MyTable

class SimpleTest(TestCase):
    def test_blow_away_everything(self):
        MyTable.objects.all().delete()

Now run a syncdb and then manually insert a few rows into "myapp_mytable", and pretend this is your production DB. Run ./manage.py test normally and it is fine, of course. But run ./manage.py test --nodb; all your rows will have been deleted.

I presume this happens because --nodb doesn't do anything to prevent database access, nor does it set up a test database to replace/act as a proxy for your dev/production/whatever database.

Oddly enough, the problem does not seem to exist if using django.db.connection directly, instead of the ORM. I haven't looked into why that is.

I'd suggest 1) a strongly worded warning in the README and anywhere else appropriate, not to use this option, ever, on a database-backed app, 2) if possible, a modification to disable the ORM or access to the default database with this option turned on. Perhaps the runner could just quit if it encounters a test trying to use the ORM/database. --nodb is very useful for certain sets of tests that don't hit the database, but the user would need to make sure they specify the particular tests to run correctly every time, or else suffer potentially dire consequences. (As I unfortunately did.)

@garethr
Copy link
Owner

garethr commented Oct 24, 2010

I've added something to the readme just now. Thanks for the issue report and sorry it caused you some pain. I'll also have a look when I get chance about something that throws and exception if you do hit the database.

@arowla
Copy link
Author

arowla commented Oct 26, 2010

Thanks. I'm not seeing anything in the README, however. Did that change get pushed to Github?

@garethr
Copy link
Owner

garethr commented Oct 26, 2010

Whoops. Didn't push the changes. Done now. Thanks for the issue report

@arowla
Copy link
Author

arowla commented Oct 26, 2010

There it is. :) Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants