Todd O'Bryan wrote:
> I'm having a chicken-and-egg problem.
> 
> So, I want to drop the test database, recreate it, sync it to the  
> models, and then populate it with test data, ideally automatically  
> because automatic tests are far more likely to get run.
> 
> If I want the drop and create commands to be generic, they need  
> access to the current DATABASE_NAME, so they have to be called while  
> the server or shell is running so that they pick up any --settings=  
> options from the command line and know which database to use.
> 
> But then I have to run syncdb, which I can't figure out how to run  
> under program control. Well, I can, but it prints out all the stuff  
> it would print if it was called on the command line and stops to ask  
> if I want to create a superuser, which is kind of annoying in the  
> middle of a test script.
> 
> And then I need to be running under program control again to add all  
> my test data to the the db.
> 
> Please tell me that I'm overthinking this and there's some easy way  
> out of this!

Here's one hackish version for db initialization, which is still work in 
progress and you'll want to adopt it to your needs:

def createdb():
    from StringIO import StringIO
    from django.contrib.auth.models import User

    if settings.DATABASE_ENGINE == "sqlite3":
        settings.DATABASE_NAME = ":memory:"
    else:
        cursor = db.connection.cursor()
        try:
            db.connection.connection.autocommit(1)
        except:
            pass
        try:
            cursor.execute("CREATE DATABASE %s" % settings.TEST_DATABASE_NAME)
        except Exception, e:

            sys.stderr.write("Got an error creating the test database: %s\n" % 
e)
            if settings.TEST_DATABASE_NAME.startswith('test_'):
                confirm = "yes"
            else:
                confirm = raw_input("It appears the test database, %s, already 
exists. Type 'yes' to delete it, or 'no' to cancel: " % 
settings.TEST_DATABASE_NAME)
            if confirm == 'yes':
                cursor.execute("DROP DATABASE %s" % settings.TEST_DATABASE_NAME)
                cursor.execute("CREATE DATABASE %s" % 
settings.TEST_DATABASE_NAME)
            else:
                abort_test()
    db.connection.close()
    settings.DATABASE_NAME = settings.TEST_DATABASE_NAME

    fake_in=StringIO("no\n")
    stdin = sys.stdin
    sys.stdin = fake_in
    syncdb()
    sys.stdin = stdin
    admin=User(username="...", is_staff=True, is_superuser=True)
    admin.set_password("...")
    admin.save()


Another version that avoids that hackish io redirection works like this, from a 
posting of mamcxyz:

        # disable the "create a super user" question
        dispatcher.disconnect(create_superuser, sender=auth_app,
signal=signals.post_syncdb)
        management.syncdb()

Michael


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to