def failing_mongo_observer(): db = FailingMongoClient(max_calls_before_failure=2, # exception_to_raise=pymongo.errors.AutoReconnect exception_to_raise=pymongo.errors.ServerSelectionTimeoutError).db runs = db.runs metrics = db.metrics fs = mock.MagicMock() return MongoObserver(runs, fs, metrics_collection=metrics)
def test_mongo_observer_equality(mongo_obs): runs = mongo_obs.runs fs = mock.MagicMock() m = MongoObserver(runs, fs) assert mongo_obs == m assert not mongo_obs != m assert not mongo_obs == 'foo' assert mongo_obs != 'foo'
def test_create_should_raise_error_on_both_prefix_and_runs(): real_client = pymongo.MongoClient() with pytest.raises( ValueError, match="Cannot pass both collection and a collection prefix." ): MongoObserver( client=real_client, collection_prefix="myprefix", collection="some_collection", )
def test_mongo_observer_equality(mongo_obs): runs = mongo_obs.runs mongo_obs.join() fs = mock.MagicMock() m = MongoObserver(runs, fs) assert mongo_obs == m assert not mongo_obs != m assert not mongo_obs == "foo" assert mongo_obs != "foo"
def run_from_queue(self, wait_time_in_s=10 * 60, sleep_time=5): run = self._dequeue_run(wait_time_in_s, sleep_time) if run is None: self.logger.warn( "No run found in queue for {} s -> terminating".format( wait_time_in_s)) return None else: # remove MongoObserver if we have one for that experiment had_matching_observer = False if self.ex in self.observer_mapping: had_matching_observer = True matching = None for i, observer in enumerate(self.ex.observers): if observer == self.observer_mapping[self.ex]: matching = i if matching is None: self.logger.warn( "Could not remove observer in run_from_queue") pass else: del self.ex.observers[matching] del self.observer_mapping[self.ex] # add a matching MongoObserver to the experiment and tell it to # overwrite the run fs = gridfs.GridFS(self.db, collection=self.prefix) self.ex.observers.append( MongoObserver(self.runs, fs, overwrite=run)) # run the experiment res = self.ex.run_command(run['command'], config_updates=run['config']) # remove the extra observer self.ex.observers.pop() # and inject the default one if had_matching_observer: self._inject_observer() return res
def mongo_obs(): db = mongomock.MongoClient().db runs = db.runs metrics = db.metrics fs = mock.MagicMock() return MongoObserver(runs, fs, metrics_collection=metrics)
def mongo_obs(): db = mongomock.MongoClient().db runs = db.runs fs = mock.MagicMock() return MongoObserver(runs, fs)
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(client=real_client, url="mymongourl")
def test_create_should_raise_error_on_non_pymongo_client(): client = mongomock.MongoClient() with pytest.raises(ValueError): MongoObserver(client=client)
def mongo_obs_with_collection(): # old, deprecated way of creating a mongo observer # should use 'my_collection' for runs and 'metrics' for metrics real_client = pymongo.MongoClient() return MongoObserver(client=real_client, collection="my_collection")
def mongo_obs_without_prefix(): # create a mongo observer without a collection prefix # i.e. should default to collections 'runs' and 'metrics' real_client = pymongo.MongoClient() return MongoObserver(client=real_client)
def mongo_obs_with_prefix(): # create a mongo observer with a collection prefix real_client = pymongo.MongoClient() return MongoObserver(collection_prefix="testing", client=real_client)