class TestThreadLocalSession(TestSession): def setUp(self): self.bind = mock_datastore() self.session = ThreadLocalProxy(Session, self.bind) class TestDoc(Document): class __mongometa__: name = "test_doc" session = self.session indexes = [("b", "c")] unique_indexes = [("cc")] _id = Field(S.ObjectId, if_missing=None) a = Field(S.Int, if_missing=None) b = Field(S.Object(dict(a=S.Int(if_missing=None)))) cc = Field(dict(dd=int, ee=int)) class TestDocNoSchema(Document): class __mongometa__: name = "test_doc" session = self.session self.TestDoc = TestDoc self.TestDocNoSchema = TestDocNoSchema def tearDown(self): self.session.close() def test_basic_tl_session(self): pass
def setUp(self): self.bind = mock_datastore() self.session = ThreadLocalProxy(Session, self.bind) class TestDoc(Document): class __mongometa__: name = 'test_doc' session = self.session indexes = [('b', 'c')] unique_indexes = [ ('cc'), ] _id = Field(S.ObjectId, if_missing=None) a = Field(S.Int, if_missing=None) b = Field(S.Object(dict(a=S.Int(if_missing=None)))) cc = Field(dict(dd=int, ee=int)) class TestDocNoSchema(Document): class __mongometa__: name = 'test_doc' session = self.session self.TestDoc = TestDoc self.TestDocNoSchema = TestDocNoSchema
class ThreadLocalODMSession(ThreadLocalProxy): _session_registry = ThreadLocalProxy(dict) def __init__(self, *args, **kwargs): kwargs.setdefault('extensions', []) ThreadLocalProxy.__init__(self, ODMSession, *args, **kwargs) def _get(self): result = super(ThreadLocalODMSession, self)._get() self._session_registry.__setitem__(id(self), self) return result def register_extension(self, extension): self._kwargs['extensions'].append(extension) def close(self): self._get().close() super(ThreadLocalODMSession, self).close() def mapper(self, cls, collection, **kwargs): return mapper( cls, collection=collection, session=self, **kwargs) @classmethod def flush_all(cls): for sess in cls._session_registry.values(): sess.flush() @classmethod def close_all(cls): for sess in cls._session_registry.values(): sess.close()
def setUp(self): self.bind = mock_datastore() self.session = ThreadLocalProxy(Session, self.bind) class TestDoc(Document): class __mongometa__: name='test_doc' session = self.session indexes = [ ('b','c') ] unique_indexes = [ ('cc'), ] _id=Field(S.ObjectId, if_missing=None) a=Field(S.Int, if_missing=None) b=Field(S.Object, dict(a=S.Int(if_missing=None))) cc=Field(dict(dd=int, ee=int)) class TestDocNoSchema(Document): class __mongometa__: name='test_doc' session = self.session self.TestDoc = TestDoc self.TestDocNoSchema = TestDocNoSchema
def __init__(self, *args, **kwargs): kwargs.setdefault('extensions', []) ThreadLocalProxy.__init__(self, ODMSession, *args, **kwargs)
class ThreadLocalODMSession(ThreadLocalProxy): """ThreadLocalODMSession is a thread-safe proxy to :class:`ODMSession`. This routes properties and methods to the session in charge of the current thread. For a reference of available *methods* and *properties* refer to the :class:`ODMSession`. """ _session_registry = ThreadLocalProxy(dict) def __init__(self, *args, **kwargs): kwargs.setdefault('extensions', []) ThreadLocalProxy.__init__(self, ODMSession, *args, **kwargs) def _get(self): result = super(ThreadLocalODMSession, self)._get() self._session_registry.__setitem__(id(self), self) return result def register_extension(self, extension): self._kwargs['extensions'].append(extension) def close(self): self._get().close() super(ThreadLocalODMSession, self).close() def mapper(self, cls, collection, **kwargs): return mapper(cls, collection=collection, session=self, **kwargs) @classmethod def by_name(cls, name): """Retrieve or create a new ThreadLocalODMSession with the given ``name``. This is useful to keep around multiple sessions and identify them by name. The session registry is global so they are available everywhere as far as the ``ming`` module is the same. """ datastore = Session._datastores.get(name) if datastore is None: return None for odmsession in cls._session_registry.values(): if odmsession.bind is datastore: return odmsession else: return ThreadLocalODMSession(bind=datastore) @classmethod def flush_all(cls): """Flush all the ODMSessions registered in current thread Usually is not necessary as only one session is registered per-thread. """ for sess in cls._session_registry.values(): sess.flush() @classmethod def close_all(cls): """Closes all the ODMSessions registered in current thread. Usually is not necessary as only one session is registered per-thread. """ for sess in cls._session_registry.values(): sess.close()
def __init__(self, *args, **kwargs): ThreadLocalProxy.__init__(self, ORMSession, *args, **kwargs)