Exemple #1
0
def django_tests(verbosity, interactive, test_labels):
    from django.conf import settings

    import deseb
    deseb.add_aka_support()

    old_installed_apps = settings.INSTALLED_APPS
    old_test_database_name = settings.TEST_DATABASE_NAME
    old_root_urlconf = settings.ROOT_URLCONF
    old_template_dirs = settings.TEMPLATE_DIRS
    old_use_i18n = settings.USE_I18N
    old_middleware_classes = settings.MIDDLEWARE_CLASSES

    # Redirect some settings for the duration of these tests.
    settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
    settings.ROOT_URLCONF = 'urls'
    settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__),
                                           TEST_TEMPLATE_DIR), )
    settings.USE_I18N = True
    settings.MIDDLEWARE_CLASSES = (
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.middleware.common.CommonMiddleware',
    )
    if not hasattr(settings, 'SITE_ID'):
        settings.SITE_ID = 1

    # Load all the ALWAYS_INSTALLED_APPS.
    # (This import statement is intentionally delayed until after we
    # access settings because of the USE_I18N dependency.)
    from django.db.models.loading import get_apps, load_app
    get_apps()

    # Load all the test model apps.
    test_models = []
    for model_dir, model_name in get_test_models():
        model_label = '.'.join([model_dir, model_name])
        try:
            # if the model was named on the command line, or
            # no models were named (i.e., run all), import
            # this model and add it to the list to test.
            if not test_labels or model_name in set(
                [label.split('.')[0] for label in test_labels]):
                if verbosity >= 1:
                    print "Importing model %s" % model_name
                mod = load_app(model_label)
                if mod:
                    if model_label not in settings.INSTALLED_APPS:
                        settings.INSTALLED_APPS.append(model_label)
        except Exception, e:
            sys.stderr.write(
                "Error while importing %s:" % model_name +
                ''.join(traceback.format_exception(*sys.exc_info())[1:]))
            continue
Exemple #2
0
def django_tests(verbosity, interactive, test_labels):
    from django.conf import settings

    import deseb

    deseb.add_aka_support()

    old_installed_apps = settings.INSTALLED_APPS
    old_test_database_name = settings.TEST_DATABASE_NAME
    old_root_urlconf = settings.ROOT_URLCONF
    old_template_dirs = settings.TEMPLATE_DIRS
    old_use_i18n = settings.USE_I18N
    old_middleware_classes = settings.MIDDLEWARE_CLASSES

    # Redirect some settings for the duration of these tests.
    settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
    settings.ROOT_URLCONF = "urls"
    settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), TEST_TEMPLATE_DIR),)
    settings.USE_I18N = True
    settings.MIDDLEWARE_CLASSES = (
        "django.contrib.sessions.middleware.SessionMiddleware",
        "django.contrib.auth.middleware.AuthenticationMiddleware",
        "django.middleware.common.CommonMiddleware",
    )
    if not hasattr(settings, "SITE_ID"):
        settings.SITE_ID = 1

    # Load all the ALWAYS_INSTALLED_APPS.
    # (This import statement is intentionally delayed until after we
    # access settings because of the USE_I18N dependency.)
    from django.db.models.loading import get_apps, load_app

    get_apps()

    # Load all the test model apps.
    test_models = []
    for model_dir, model_name in get_test_models():
        model_label = ".".join([model_dir, model_name])
        try:
            # if the model was named on the command line, or
            # no models were named (i.e., run all), import
            # this model and add it to the list to test.
            if not test_labels or model_name in set([label.split(".")[0] for label in test_labels]):
                if verbosity >= 1:
                    print "Importing model %s" % model_name
                mod = load_app(model_label)
                if mod:
                    if model_label not in settings.INSTALLED_APPS:
                        settings.INSTALLED_APPS.append(model_label)
        except Exception, e:
            sys.stderr.write(
                "Error while importing %s:" % model_name + "".join(traceback.format_exception(*sys.exc_info())[1:])
            )
            continue
Exemple #3
0
def modeltest(app_name, use_aka=True):
    if not app_name:
        raise Exception("No test name given")
    if not os.path.exists('settings.py'):
        raise Exception('Oops... file settings.py does not exist! Please copy your settings there!')
    from django.conf import settings
    from django.db.models.loading import get_apps, get_app
    from deseb.schema_evolution import evolvediff
    from django.core.management.color import no_style
    from deseb.actions import get_introspected_evolution_options
    from django.core.management.sql import sql_create, sql_indexes
    from django.db.transaction import commit_on_success
    from django.db import connection
    from deseb.actions import get_schemas, show_evolution_plan
    if DEBUG:
        print "Test %s" % app_name
    #reset on post state and pre state
    from deseb import add_aka_support
    if use_aka:
        add_aka_support()
    
    style = no_style()
    settings.INSTALLED_APPS = tuple(list(settings.INSTALLED_APPS[:5]) + [app_name])
    
    write_file(app_name+"/models.py", '') # re-init models.py
    write_file(app_name+"/errdiff.%s.actual" % settings.DATABASE_ENGINE, "")
    write_file(app_name+"/errors.%s.actual" % settings.DATABASE_ENGINE, "")
    get_apps()
    
    drop_all_tables()
    
    reload_models(app_name, 'pre')    
    app = get_app(app_name)
    create = sql_create(app, style) + sql_indexes(app, style)
    write_file(app_name+"/init.%s.actual" % settings.DATABASE_ENGINE, create)
    #FIXME: compare to init.correct later instead of copying
    write_file(app_name+"/init.%s.planned" % settings.DATABASE_ENGINE, create)
    
    reset = sql_create(app, style)
    #print 'SQL:', '\n'.join(reset)
    commit_on_success(run_sql)(reset)
    
    reset_idx = sql_indexes(app, style)
    run_sql(reset_idx)
    
    reload_models(app_name, 'post')
    if use_aka:
        from deseb.storage import update_with_aka, save_renames
        update_with_aka(app_name)
        save_renames(app_name)

    cursor = connection.cursor()
    db_schema, model_schema = get_schemas(cursor, app, style)
    diff = show_evolution_plan(cursor, app, style, db_schema, model_schema)
    write_file(app_name+"/diff.%s.actual" % settings.DATABASE_ENGINE, diff)
    #FIXME: compare to diff.correct later instead of copying
    write_file(app_name+"/diff.%s.planned" % settings.DATABASE_ENGINE, diff)
    
    actions = get_introspected_evolution_options(app, style, db_schema, model_schema)
    write_file(app_name+"/actions.%s.actual" % settings.DATABASE_ENGINE, actions)
    #FIXME: compare to diff.correct later instead of copying
    write_file(app_name+"/actions.%s.planned" % settings.DATABASE_ENGINE, actions)
    try:
        commit_on_success(run_sql)(actions)
    except:
        #print 'changes rolled back'
        from django.db import transaction
        transaction.rollback()
        raise
    #else:
        #print 'changes committed'

    cursor = connection.cursor()
    db_schema, model_schema = get_schemas(cursor, app, style, model_schema=model_schema)
    # due to sqlite3/pysqlite bug, caused deferred index creation, we reget db schema.
    # this is f*****g weird, but i've no any explanation, why getting indxes
    # doesn't work correctly first time  
    db_schema, model_schema = get_schemas(cursor, app, style, model_schema=model_schema)
    diff = show_evolution_plan(cursor, app, style, db_schema, model_schema)
    write_file(app_name+"/errdiff.%s.actual" % settings.DATABASE_ENGINE, diff)
    diff1 = diff.split('\n',1)[1]
    if diff1:
        print "Errors:"
        print diff1

    try:
        actions, db_schema, model_schema = get_introspected_evolution_options(app, style, db_schema, model_schema)
    except Exception:
        actions = ['Was unable to generate error diff SQL commands']
    write_file(app_name+"/errors.%s.actual" % settings.DATABASE_ENGINE, actions)
    #FIXME: compare to diff.correct later instead of copying
    #write_file(app_name+"/errors.%s.planned" % settings.DATABASE_ENGINE, actions)
    return diff1