def create_db_instance(null_db_instances, clean_db): """Create and save a singleton database instance.""" try: database = { "type": "MongoDB", "name": "orion_test", "username": "******", "password": "******", } db = Storage(of_type="legacy", database=database) except ValueError: db = Storage() db = db._db return db
def create_db_instance(null_db_instances, clean_db): """Create and save a singleton database instance.""" try: database = { 'type': 'MongoDB', 'name': 'orion_test', 'username': '******', 'password': '******' } db = Storage(of_type='legacy', database=database) except ValueError: db = Storage() db = db._db return db
def test_setup_storage_default(): """Test that storage is setup using default config""" update_singletons() setup_storage() storage = Storage() assert isinstance(storage, Legacy) assert isinstance(storage._db, PickledDB)
def test_setup_storage_custom_legacy_emtpy(): """Test setup with local configuration with legacy but no config""" update_singletons() setup_storage({"type": "legacy"}) storage = Storage() assert isinstance(storage, Legacy) assert isinstance(storage._db, PickledDB) assert storage._db.host == orion.core.config.storage.database.host
def test_setup_storage_custom_type_missing(): """Test setup with local configuration with type missing""" update_singletons() setup_storage({"database": {"type": "pickleddb", "host": "test.pkl"}}) storage = Storage() assert isinstance(storage, Legacy) assert isinstance(storage._db, PickledDB) assert storage._db.host == "test.pkl"
def test_setup_storage_custom_type_missing(): """Test setup with local configuration with type missing""" update_singletons() setup_storage({'database': {'type': 'pickleddb', 'host': 'test.pkl'}}) storage = Storage() assert isinstance(storage, Legacy) assert isinstance(storage._db, PickledDB) assert storage._db.host == 'test.pkl'
def test_setup_storage_bad_config_override(): """Test setup with different config than existing singleton""" update_singletons() setup_storage({"database": {"type": "pickleddb", "host": "test.pkl"}}) storage = Storage() assert isinstance(storage, Legacy) assert isinstance(storage._db, PickledDB) with pytest.raises(SingletonAlreadyInstantiatedError): setup_storage({"database": {"type": "mongodb"}})
def test_setup_storage_bad_config_override(): """Test setup with different config than existing singleton""" update_singletons() setup_storage({'database': {'type': 'pickleddb', 'host': 'test.pkl'}}) storage = Storage() assert isinstance(storage, Legacy) assert isinstance(storage._db, PickledDB) with pytest.raises(SingletonAlreadyInstantiatedError): setup_storage({'database': {'type': 'mongodb'}})
def setup_storage(self, config): """Create the storage instance from a configuration. Parameters ---------- config: dict Configuration for the database. """ # TODO: Fix this in config refactoring storage_opts = config.get('protocol', {'type': 'legacy'}) storage_type = storage_opts.pop('type') log.debug("Creating %s storage client with args: %s", storage_type, storage_opts) try: Storage(of_type=storage_type, config=config, **storage_opts) except ValueError: if Storage().__class__.__name__.lower() != storage_type.lower(): raise
def test_setup_storage_bad_override(): """Test setup with different type than existing singleton""" update_singletons() setup_storage( {"type": "legacy", "database": {"type": "pickleddb", "host": "test.pkl"}} ) storage = Storage() assert isinstance(storage, Legacy) assert isinstance(storage._db, PickledDB) with pytest.raises(SingletonAlreadyInstantiatedError) as exc: setup_storage({"type": "track"}) assert exc.match("A singleton instance of \(type: Storage\)")
def storage(self): """Return test storage""" try: storage_type = self.database_config.pop('storage_type', 'legacy') config = {'database': self.database_config} db = Storage(of_type=storage_type, config=config) self.database_config['storage_type'] = storage_type except SingletonAlreadyInstantiatedError: db = get_storage() except KeyError: print(self.database_config) raise return db
def storage(self, config=None): """Return test storage""" if config is None: return get_storage() try: config['of_type'] = config.pop('type') db = Storage(**config) self.storage_config = config except SingletonAlreadyInstantiatedError: db = get_storage() except KeyError: print(self.storage_config) raise return db
def test_setup_storage_bad_override(): """Test setup with different type than existing singleton""" update_singletons() setup_storage({ 'type': 'legacy', 'database': { 'type': 'pickleddb', 'host': 'test.pkl' } }) storage = Storage() assert isinstance(storage, Legacy) assert isinstance(storage._db, PickledDB) with pytest.raises(SingletonAlreadyInstantiatedError) as exc: setup_storage({'type': 'track'}) assert exc.match('A singleton instance of \(type: Storage\)')