Skip to content

Test with two database where one is external and readonly #828

Closed
@layoaster

Description

@layoaster

I have a Django application A that takes the user's data from another Django application B with its own database. So basically I implemented a Django database router so the users are taken from a an external database with read-only permissions because the application A not creating users.

As a result, I have two databases in my settings.:

DATABASE_ROUTERS = ["config.routerdb.AuthRouter"]
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": environ.get("OPERATIONAL_DB_DATABASE", "moto"),
        "USER": environ.get("OPERATIONAL_DB_USER", "postgres"),
        "PASSWORD": environ.get("OPERATIONAL_DB_PASS", "invalid-pass"),
        "HOST": environ.get("OPERATIONAL_DB_HOST", "localhost"),
        "PORT": environ.get("OPERATIONAL_DB_PORT", "5432"),
    },
    "auth_db": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": environ.get("AUTH_DB_DATABASE", "cleverea"),
        "USER": environ.get("AUTH_DB_USER", "postgres_ro"),
        "PASSWORD": environ.get("AUTH_DB_PASS", "invalid-pass"),
        "HOST": environ.get("AUTH_DB_HOST", "localhost"),
        "PORT": environ.get("AUTH_DB_PORT", "5432"),
    },
}

Being auth_db the external read-only database. I've been trying to tell pytest-django with the help of a settings.test to simply create the users database on the local database with the following:

from .base import *  # noqa
DATABASE_ROUTERS = []

DATABASES["auth_db"]["NAME"]: environ.get("OPERATIONAL_DB_DATABASE", "moto")
DATABASES["auth_db"]["USER"]: environ.get("OPERATIONAL_DB_USER", "postgres")
DATABASES["auth_db"]["PASSWORD"]: environ.get("OPERATIONAL_DB_PASS", "invalid-pass")
DATABASES["auth_db"]["HOST"]: environ.get("OPERATIONAL_DB_HOST", "localhost")
DATABASES["auth_db"]["PORT"]: environ.get("OPERATIONAL_DB_PORT", "5432")

But for some reason I cannot explain I still get the same error:

Creating test database for alias 'default' ('test_moto')...
Got an error creating the test database: database "test_moto" already exists

Destroying old test database for alias 'default' ('test_moto')...
Creating test database for alias 'auth_db' ('test_cleverea')...
Got an error creating the test database: permission denied to create database

I honestly don't event create any users for tests since I tell factory boy to use a build strategy for the User factory and I use the factory so I can force Django Restf to make authenticated calls.

So I don't know what else I could do. Any Ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions