Forums

Problem Running Tests.py

I was trying to run tests.py for my project and I am getting following Error! python manage.py test

Creating test database for alias 'default'... Got an error creating the test database: (1044, "Access denied for user 'ssdiprojectfall2'@'%' to database 'test_ssdiprojectfall2$default'") Type 'yes' if you would like to try deleting the test database 'test_ssdiprojectfall2$default', or 'no' to cancel: yes Destroying old test database 'default'... Got an error recreating the test database: (1044, "Access denied for user 'ssdiprojectfall2'@'%' to database 'test_ssdiprojectfall2$default'")

I tried creating test database, I tried adding TEST_NAME to setings.py for current database... I tried changing MySQL permissions, nothing works for me. Please Help me out!!

I tried running with Sudo too... But that didn't work either!

Hi there,

Try manually creating a database called "ssdiprojectfall2015$testdb", and then setting the TEST_NAME attribute on the DATABASES entry in settings.py to match?

Alternatively, set your tests to run against sqlite instead of mysql -- they'll be quicker anyway!

Here's the django docs on the test database, that TEST_NAME thing is out of date but there's an equivalent way of doing it in the latest django:

https://docs.djangoproject.com/en/1.8/topics/testing/overview/#the-test-database

https://docs.djangoproject.com/en/1.8/ref/settings/#test

how do I run with sqlite?

It looks like you create a separate dict inside the DATABASES dict called test, and then you give it your settings for the test database. for sqlite you need to set ENGINE to 'django.db.backends.sqlite3', and set any NAME like test.db...

https://docs.djangoproject.com/en/1.8/ref/settings/#test

if 'test' in sys.argv or 'test_coverage' in sys.argv: #Covers regular testing and django-coverage DATABASES['default']['ENGINE'] = 'sqlite3'

appending this to settings.py.... is it the way?

that looks like a hacky way of doing it, it might work. But I would just try the way that's suggested in the docs if I were you.

and what is that? I'm new to testing... could you elaborate more

Sorry, I wasn't being very helpful. Supposing your DATABASES entry in settings.py looks like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'ssdiprojectfall2$default',
        # etc
    }, 
}

What you need to do is add a sub-section called TEST with some overriden settings for your test database. Something like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'ssdiprojectfall2$default',
        # etc
        'TEST': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': 'test.db',
        },
    }, 
}

Try that!

My problem right now is that somehow my test database settings continue to be ignored.

I have even set and sqlite3 backend, still it gets ignored and django tries to create 'test_username$databasename'.

I have tried everything I could to no avail. Django just won't test with the specified database.

Here is the concerned database settings. I did something like this cos I was running deploying same app on heroku as well and I wasn't sure which of the two platforms I'll end up using.

elif deployment_platform == "pythonanywhere":
    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'funnshopp',
        'NAME': 'funnshopp$funn',
        'TEST':{
            'ENGINE': 'django.db.backends.mysql',
            'NAME':'funnshopp$test_default'
            },
        'PASSWORD': get_env_variable('FUNN_PASS'),
        'HOST': 'funnshopp.mysql.pythonanywhere-services.com',
        }
    }

Right now you have this:

    'TEST':{
        'ENGINE': 'django.db.backends.mysql',
        'NAME':'funnshopp$test_default'
        },

What happens if you replace it with this:

    'TEST': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'test.db',
    },

...?

Still got the same result. Django was still trying to test with "test_username$databasename"

Sorry i inadvertently posted a reply using what's to become my other account cos I used it to subscribe to this thread.

Anyway, I got same result when trying to use an sqlite3 backend. Django was still trying to test with

test_username$databasename

That TEST entry definitely should work.

Let's eliminate any other possible causes of problems. How sure are you that this line is working?

elif deployment_platform == "pythonanywhere":

?

Hey Harry.. I'm very sure of that line because I successfully deploy same branch on my pc, heroku, and pythonanywhere. Still I get the same error. I'll show you the whole databases settings.

DEPLOYMENT_PLATFORM = get_env_variable("DEPLOYMENT_PLATFORM")
if DEPLOYMENT_PLATFORM == "heroku-plus-local":
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'funn',
        'USER': 'postgres',
        'PASSWORD': FUNN_PASS,
        'HOST': 'localhost',
        'PORT': 5432
    }
}

else: # DEPLOYMENT_PLATFORM == "pythonanywhere"
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'funnshopp',
        'NAME': 'funnshopp$funn',
        'TEST':{
            'ENGINE': 'django.db.backends.sqlite3', # for sqlite3
            'NAME':'funn.db',
            # 'ENGINE': 'django.db.backends.mysql',
            # 'NAME':'funnshopp$test_default'
            },
        'PASSWORD': get_env_variable('FUNN_PASS'),
        'HOST': 'funnshopp.mysql.pythonanywhere-services.com',
        }
    }

some thoughts:

1) what version of django are you using? 2) is DATABASES redefined somewhere else later on in your code? 3) try printing out what DATABASES['TEST'] is when your code is being run?

I have reworked my settings file according to the layout suggested here Recommended Django Project Layout.

Now test runs fine.

But I have something else to configure and I don't know if its better to start a new thread.

The thing is I just upgraded one of my account. I intend to move all my projects to that one and close down the others. But I don't know how to handle my postactivate script.

Each of my project runs on a different setting and I created a virtual environment for each. So I don't know what to set as DJANGO_SETTINGS_MODULE in my postactivate script.

I have same issue on my local development environment. It's quite tiring to have to append --settings=path/to/settings whenever I run any manage.py command.

Thanks

Just to clarify- previously you had one postactivate script that setup the DJANGO_SETTINGS_MODULE environment variable, and now you are having problems because all your virtualenvs use the same postactivate script?

Instead, you could take a look at your virtualenv bin folder (eg: ~/.virtualenvs/your-virtual-env/bin/activate), and just add export DJANGO_SETTINGS_MODULE=/path/to/your/settings/module to it. This would allow you to have one per virtualenv.

Thanks Conrad. This was really helpful.

BTW, I made a gist about this, just in case someone else find it useful.

You could take a look and let me know if it's clear enough.

Test runs fine now with the specified mysql database.

But when I try to work with the database for another web app I get the 1045 operational error. Access denied to user....

Still working on it. Cheers

Hi funnshopp,

Thanks for making the virtualenv environment variable notes! Good luck working through your error!

Hi Conrad,

All's working fine now. But I do have a question about the suggestion by both giles and harry about running tests with sqlite3.

That setting doesn't work for me. Here's my DATABASE from my production settings

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'funnshopp',
        'NAME': 'funnshopp$funn',
        'PASSWORD': get_env_variable('FUNN_PASS'),
        'HOST': 'funnshopp.mysql.pythonanywhere-services.com',
        'TEST':{
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME':'test.db'
            # 'ENGINE': 'django.db.backends.mysql',
            # 'NAME':'funnshopp$test_default'
            },
        },
    }

The alternative setting works fine. But it takes forever to run the test, which is why I'm exploring sqlite3 as an alternative to running tests. Here's the error I get when I run tests with this settings

Got an error creating the test database: (1044, "Access denied for user 'funnshopp'@'%' to database 'test.db'")

Here's my base.py just in case something might be interfering with my DATABASES which I don't know about yet. Thanks for the help.

import os
from django.urls import reverse_lazy
from django.core.exceptions import ImproperlyConfigured

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

def get_env_variable(var_name):
    """Get the environment variable or return exception"""
    try:
        return os.environ[var_name]
    except KeyError:
        error_msg = "Set the {} environment variable".format(var_name)
        raise ImproperlyConfigured(error_msg)

ALLOWED_HOSTS = ['*']
ROOT_URLCONF = 'funnshopp.urls'
SECRET_KEY = get_env_variable("SECRET_KEY")

WSGI_APPLICATION = 'funnshopp.wsgi.application'
INTERNAL_IPS = ('127.0.0.1', 'localhost')

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '******'
EMAIL_HOST_PASSWORD = '********'
DEFAULT_FROM_EMAIL = '*****'

LOGIN_REDIRECT_URL = reverse_lazy('personnel:dashboard')
LOGIN_URL = reverse_lazy('personnel:login')
LOGOUT_URL = reverse_lazy('personnel:logout')

# django-admin-timeline
# ADMIN_TIMELINE_NUMBER_OF_ENTRIES_PER_PAGE = 50

GOOGLE_RECAPTCHA_SITE_KEY = get_env_variable("GOOGLE_RECAPTCHA_SITE_KEY")
GOOGLE_RECAPTCHA_SECRET_KEY = get_env_variable("GOOGLE_RECAPTCHA_SECRET_KEY")

# Application definition
PREREQ_APPS = [
    # these should come before contrib.admin
    'admin_tools',
    'admin_tools.theming',
    'admin_tools.menu',
    'admin_tools.dashboard',

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

PROJECT_APPS = [
    'account',
    'communication',
    'establishment',
    'personnel',
    'relation',
]

THIRD_PARTY_APPS = [
    'captcha',
    'guardian',
    'rules',
    'coverage',
    'admin_timeline',
    'django_extensions',
    'pure_pagination',
    'sorl.thumbnail',
    'django_addanother',
]

INSTALLED_APPS = PREREQ_APPS +  PROJECT_APPS + THIRD_PARTY_APPS

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'guardian.backends.ObjectPermissionBackend',
    'rules.permissions.ObjectPermissionBackend',
)

MIDDLEWARE = [
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': False,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders' : [
                'admin_tools.template_loaders.Loader',
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
                ]
        },
    },
]

# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Africa/Lagos'

USE_I18N = True

USE_L10N = True
USE_THOUSAND_SEPARATOR = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

AUTH_USER_MODEL = 'personnel.Person'

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

How does it not work?

I get this error

Got an error creating the test database: (1044, "Access denied for user 'funnshopp'@'%' to database 'test.db'")

That look like it's still trying to use MySQL. Check that the config you think you're running is the config that you're actually running. Also, check the Django docs for the version you're using to make sure that you have the config correct.

I appear to be having a similar problem. My tests run fine with sqlite but cannot get to work in mysql on pythonanywhere. In my settings :

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'nverse$nvlive1',
        'USER': 'nverse',
        'PASSWORD': '123456_not_really',
        'HOST': 'nverse.mysql.pythonanywhere-services.com',
        'TEST': {
            'NAME': 'livetest',
        },
    },
}

I have a database called nverse$livetest, but when I run my test I get:

Creating test database for alias 'default'...
Got an error creating the test database: (1044, "Access denied for user 'nverse'@'%' to database 'livetest'")

Fair enough - In database settings the database is called 'nverse$livetest', So I tried changing the database name in my settings thus:

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'nverse$nvlive1',
        'USER': 'nverse',
        'PASSWORD': '123456_not_really',
        'HOST': 'nverse.mysql.pythonanywhere-services.com',
        'TEST': {
            'NAME': 'nverse$livetest',
        },
    },
}

and I then get:

Creating test database for alias 'default'...
Got an error creating the test database: (1007, "Can't create database 'nverse$livetest'; database exists")

Thanks for the help.

When I tried that just now, I got an extra line after the "Got an error..." message:

Type 'yes' if you would like to try deleting the test database 'username$dbname', or 'no' to cancel:

I entered "yes", and the tests ran -- though it was kind of slow. Are you not seeing that prompt?

Thanks, I just ran it again - The prompt does come up, no idea why I didn't see it before. It takes a few minutes after typing yes for the test to run, but it appears not to use loads of CPU during this time so no worries. Thanks again.

No problem -- glad to help!