def _init_db(self): if self.db_name is None: if self.mongo_observer is None: mongo_observers = sorted([mo for mo in self.ex.observers if isinstance(mo, MongoObserver)], key=lambda x: x.priority) if not mongo_observers: raise RuntimeError('No mongo observer found!') self.mongo_observer = mongo_observers[-1] else: self.mongo_observer = MongoObserver.create(db_name=self.db_name, collection=self.prefix, url=self.url) self._inject_observer() self.runs = self.mongo_observer.runs self.db = self.runs.database self.db_search_space = self.db.search_space for manipulator in SON_MANIPULATORS: self.db.add_son_manipulator(manipulator)
def test_create_should_raise_error_on_both_client_and_url(): real_client = pymongo.MongoClient() with pytest.raises(ValueError, match="Cannot pass both a client and a url."): MongoObserver.create(client=real_client, url="mymongourl")
def test_create_should_raise_error_on_non_pymongo_client(): client = mongomock.MongoClient() with pytest.raises(ValueError): MongoObserver.create(client=client)