Example #1
0
    def test_populate_replicas_catches_misconfiguration(self):
        no_default = {
            'DATABASE_ROUTERS': ['pindb.StrictPinDbRouter'],
            'MASTER_DATABASES': dict([
                ('ham', {
                    'NAME': ':memory:',
                    'ENGINE': 'django.db.backends.sqlite3',
                }),
                ('egg', {
                    'NAME': ':memory:',
                    'ENGINE': 'django.db.backends.sqlite3',
                })
            ]),
            'DATABASE_SETS': {
                'ham': [],
                # This normally would have overrides, but lots of sqlites in
                # memory are happy together:
                'egg': [{}, {}]
            },
            'PINDB_DELEGATE_ROUTERS': ['test_project.router.HamAndEggRouter']
        }

        self.assertRaises(PinDbConfigError,
            pindb.populate_replicas,
            no_default['MASTER_DATABASES'],
            {}
        )

        default_ok = no_default.copy()  # Note: not deep, but doesn't matter
        default_ok['MASTER_DATABASES']['default'] = default_ok['MASTER_DATABASES']['ham']
        del default_ok['MASTER_DATABASES']['ham']
        try:
            pindb.populate_replicas(
                default_ok['MASTER_DATABASES'],
                 {'default': [], 'egg': []})
        except PinDbConfigError:
            self.fail("Expected default to be acceptable config.")
Example #2
0
def populate_databases(settings_dict):
    """Given a dict of various DB-related settings (DATABASES, MASTER_DATABASES, DATABASE_SETS), finalize all the settings into the DATABASES item of the dict.

    Also, turn the SQLite DBs we're using for testing into disk-based ones
    rather than memory-based ones, and give each a unique FS path, because
    Django buggily fails to come up with unique identifying tuples for multiple
    memory-based SQLites. Give each one a TEST_NAME, because otherwise,
    django.db.backends.sqlite3.creation reverts to ":memory:".

    """
    db_dir = tempfile.mkdtemp()
    if 'DATABASES' not in settings_dict:
        settings_dict['DATABASES'] = {}
    replicas =  pindb.populate_replicas(
        settings_dict['MASTER_DATABASES'],
        settings_dict['DATABASE_SETS']
    )

    settings_dict['DATABASES'].update(
        replicas
    )
    for alias, db in settings_dict['DATABASES'].items():
        db['NAME'] = db['TEST_NAME'] = os.path.join(db_dir, "test_%s" % alias)