def _databroker(): mongo_box = MongoBox() try: mongo_box.start() mongo_client = mongo_box.client() mongo_host, mongo_port = mongo_client.address mongo_uri = f"mongodb://{mongo_host}:{mongo_port}" catalog_descriptor_path = tmp_path / Path("mad.yml") with open(catalog_descriptor_path, "w") as f: f.write(f"""\ sources: mad: description: Made up beamline driver: "bluesky-mongo-normalized-catalog" container: catalog args: metadatastore_db: {mongo_uri} asset_registry_db: {mongo_uri} handler_registry: NPY_SEQ: ophyd.sim.NumpySeqHandler metadata: beamline: "00-ID" """) yield intake.open_catalog(catalog_descriptor_path) finally: mongo_box.stop()
def test_keep_db_path(self): db_path = tempfile.mkdtemp() box = MongoBox(db_path=db_path) box.start() box.stop() self.assertTrue(os.path.exists(db_path)) shutil.rmtree(db_path)
def setUp(self): self.box = MongoBox() self.box.start() db = self.box.client().matomat db.users.insert(self.dummy_user) self.sut = Authorization(db, MicroMock.get_log_mock())
def mongo_db(): db_path = tempfile.mkdtemp(dir="/dev/shm") mongobox = MongoBox(db_path=db_path) port_envvar = DEFAULT_PORT_ENVVAR mongobox.start() os.environ[port_envvar] = str(mongobox.port) yield mongobox mongobox.stop() del os.environ[port_envvar] shutil.rmtree(db_path)
def test_can_run_mongo(self): box = MongoBox() box.start() db_path = box.db_path self.assertTrue(box.running()) self.assertIsNotNone(box.port) client = box.client() self.assertTrue(client.alive()) box.stop() self.assertFalse(box.running()) self.assertFalse(client.alive()) self.assertFalse(os.path.exists(db_path))
def test_replset(self): box = MongoBox(replset="repl0") box.start() client = box.client() # initiate the replSet config = { '_id': 'repl0', 'members': [{ '_id': 0, 'host': '%s:%s' % ('127.0.0.1', box.port) }] } client.admin.command("replSetInitiate", config) setconfig = client.admin.command("replSetGetConfig") self.assertEqual(setconfig['config']['_id'], 'repl0')
def test_auth(self): box = MongoBox(auth=True) box.start() client = box.client() client['admin'].add_user('foo', 'bar') self.assertRaises(OperationFailure, client['test'].add_user, 'test', 'test') client['admin'].authenticate('foo', 'bar') try: client['test'].add_user('test', 'test') except OperationFailure: self.fail("add_user() operation unexpectedly failed") client = box.client() self.assertRaises(OperationFailure, client['test'].collection_names) client['admin'].authenticate('foo', 'bar') try: client['test'].collection_names() except OperationFailure: self.fail("collection_names() operation unexpectedly failed")
def test_can_run_mongo(self): box = MongoBox() box.start() db_path = box.db_path self.assertTrue(box.running()) self.assertIsNotNone(box.port) client = box.client() try: client.list_databases() except: self.fail('Cannot list databases') box.stop() self.assertFalse(box.running()) self.assertFalse(os.path.exists(db_path)) with self.assertRaises(AutoReconnect): client.list_databases()
def test_auth(self): box = MongoBox(auth=True) box.start() client = box.client() client['admin'].command('createUser', 'foo', pwd='bar', roles=['root']) with self.assertRaises(OperationFailure): client['test'].command('createUser', 'test', pwd='test', roles=[]) client['admin'].authenticate('foo', 'bar') try: client['test'].command('createUser', 'test', pwd='test', roles=[]) except OperationFailure: self.fail("createUser() operation unexpectedly failed") client = box.client() self.assertRaises(OperationFailure, client['test'].collection_names) client['admin'].authenticate('foo', 'bar') try: client['test'].collection_names() except OperationFailure: self.fail("collection_names() operation unexpectedly failed") box.stop()
def setUp(self): self.box = MongoBox() self.box.start() self.req = self.box.client().db1.req self.sum = self.box.client().db1.sum
def __init__(self): self._box = MongoBox() self._box.start()
from mongobox import MongoBox import time # Start up MongoDB with user permissions. # In a real deployment we would simply connect to an existing, # separately managed MongoDB deployment. box = MongoBox(port=27017) box.start() client = box.client() while True: time.sleep(1)
def setUp(self): self.box = MongoBox() self.box.start() self.coll = get_collection('db1', 'coll1', ('localhost', self.box.port))
import os import sys import weakref import pymongo from mongobox import MongoBox box = MongoBox() box.start() db_client = box.client() # pymongo client db = db_client['test'] classes = {} class ActiveRecordMeta(type): def __new__(meta_cls, cls_name, cls_bases, cls_dict): if cls_name in classes: raise Exception('`%s` is existd.' % cls_name) cls = type.__new__(meta_cls, cls_name, cls_bases, cls_dict) classes[cls_name] = cls return cls class ActiveRecord(object): __metaclass__ = ActiveRecordMeta __table__ = None @classmethod def create(cls, attrs): """Create new one and save in db immediate. """