def db(local_config, request): timezone = request.param db = PostgresDb.from_config(local_config, application_name='test-run', validate_connection=False) # Drop and recreate tables so our tests have a clean db. with db.connect() as connection: _core.drop_db(connection._connection) remove_dynamic_indexes() # Disable informational messages since we're doing this on every test run. with _increase_logging(_core._LOG) as _: _core.ensure_db(db._engine) c = db._engine.connect() c.execute('alter database %s set timezone = %r' % (local_config.db_database, str(timezone))) c.close() # We don't need informational create/drop messages for every config change. _dynamic._LOG.setLevel(logging.WARN) yield db db.close()
def db(local_config): db = PostgresDb.from_config(local_config, application_name='test-run', validate_db=False) # Drop and recreate tables so our tests have a clean db. drop_db(db._connection) remove_dynamic_indexes() ensure_db(db._engine) return db
def db(local_config: LocalConfig): db = PostgresDb.from_config(local_config, application_name='dea-test-run', validate_connection=False) # Drop and recreate tables so our tests have a clean db. with db.connect() as connection: _core.drop_db(connection._connection) remove_dynamic_indexes() # Disable informational messages since we're doing this on every test run. with _increase_logging(_core._LOG) as _: _core.ensure_db(db._engine) # We don't need informational create/drop messages for every config change. _dynamic._LOG.setLevel(logging.WARN) yield db db.close()
def __init__(self, driver_manager, index=None, *args, **kargs): """Initialise the generic index. :param index: An index object behaving like :class:`datacube.index._api.Index` and used for testing purposes only. In the current implementation, only the `index._db` variable is used, and is passed to the index initialisation method, that should basically replace the existing DB connection with that variable. :param args: Optional positional arguments to be passed to the index on initialisation. Caution: In the current implementation all parameters get passed to all potential indexes. :param kargs: Optional keyword arguments to be passed to the index on initialisation. Caution: In the current implementation all parameters get passed to all potential indexes. """ self.logger = logging.getLogger(self.__class__.__name__) if index is None: local_config = kargs[ 'local_config'] if 'local_config' in kargs else None application_name = kargs[ 'application_name'] if 'application_name' in kargs else None validate_connection = kargs[ 'validate_connection'] if 'validate_connection' in kargs else True if local_config is None: local_config = LocalConfig.find() db = PostgresDb.from_config( local_config, application_name=application_name, validate_connection=validate_connection) else: db = index._db # pylint: disable=protected-access super(Index, self).__init__(driver_manager, db)
def test_index_dataset_in_transactions(index, db, local_config, default_metadata_type): """ :type index: datacube.index._api.Index :type db: datacube.index.postgres._api.PostgresDb """ assert not db.contains_dataset(_telemetry_uuid) with db.begin() as transaction: dataset_type = index.datasets.types.add_document(_pseudo_telemetry_dataset_type) was_inserted = db.insert_dataset( _telemetry_dataset, _telemetry_uuid, dataset_type.id ) assert was_inserted assert db.contains_dataset(_telemetry_uuid) # Insert again. It should be ignored. was_inserted = db.insert_dataset( _telemetry_dataset, _telemetry_uuid, dataset_type.id ) assert not was_inserted assert db.contains_dataset(_telemetry_uuid) transaction.rollback() # Rollback should leave a blank database: assert not db.contains_dataset(_telemetry_uuid) # Check with a new connection too: db = PostgresDb.from_config(local_config) assert not db.contains_dataset(_telemetry_uuid)