def setUp(self): self.datastore = create_datastore('mim:///test_db') self.session = ODMSession(bind=self.datastore) class GrandParent(MappedClass): class __mongometa__: name='grand_parent' session=self.session _id = FieldProperty(int) class Parent(MappedClass): class __mongometa__: name='parent' session = self.session _id = FieldProperty(int) grandparent_id = ForeignIdProperty('GrandParent') grandparent = RelationProperty('GrandParent') children = RelationProperty('Child') class Child(MappedClass): class __mongometa__: name='child' session = self.session _id = FieldProperty(int) parent_id = ForeignIdProperty('Parent', allow_none=True) parent = RelationProperty('Parent') Mapper.compile_all() self.GrandParent = GrandParent self.Parent = Parent self.Child = Child
def setUp(self): Mapper._mapper_by_classname.clear() self.datastore = create_datastore('mim:///test_db') self.doc_session = Session(self.datastore) self.odm_session = ODMSession(self.doc_session) class Base(MappedClass): class __mongometa__: name = 'test_doc' session = self.odm_session polymorphic_on = 'type' polymorphic_identity = 'base' _id = FieldProperty(S.ObjectId) type = FieldProperty(str, if_missing='base') a = FieldProperty(int) class Derived(Base): class __mongometa__: polymorphic_identity = 'derived' type = FieldProperty(str, if_missing='derived') b = FieldProperty(int) Mapper.compile_all() self.Base = Base self.Derived = Derived
def setUp(self): self.datastore = create_datastore('mim:///test_db') self.session = ODMSession(bind=self.datastore) class Parent(MappedClass): class __mongometa__: name='parent' session = self.session _id = FieldProperty(S.ObjectId) children = ForeignIdProperty('Child', uselist=True) field_with_default_id = ForeignIdProperty( 'Child', uselist=True, if_missing=lambda:[bson.ObjectId('deadbeefdeadbeefdeadbeef')]) field_with_default = RelationProperty('Child', 'field_with_default_id') class Child(MappedClass): class __mongometa__: name='child' session = self.session _id = FieldProperty(S.ObjectId) parent_id = ForeignIdProperty(Parent) field_with_default_id = ForeignIdProperty( Parent, if_missing=lambda:bson.ObjectId('deadbeefdeadbeefdeadbeef')) field_with_default = RelationProperty('Parent', 'field_with_default_id') Mapper.compile_all() self.Parent = Parent self.Child = Child
def setUp(self): self.datastore = create_datastore('mim:///test_db') self.session = ODMSession(bind=self.datastore) class BasicMapperExtension(MapperExtension): def after_insert(self, instance, state, session): assert 'clean'==state.status def before_insert(self, instance, state, session): assert 'new'==state.status def before_update(self, instance, state, session): assert 'dirty'==state.status def after_update(self, instance, state, session): assert 'clean'==state.status class Basic(MappedClass): class __mongometa__: name='basic' session = self.session extensions = [BasicMapperExtension, MapperExtension] _id = FieldProperty(S.ObjectId) a = FieldProperty(int) b = FieldProperty([int]) c = FieldProperty(dict( d=int, e=int)) Mapper.compile_all() self.Basic = Basic self.session.remove(self.Basic)
def setUp(self): Mapper._mapper_by_classname.clear() self.datastore = create_datastore('mim:///test_db') self.session = ODMSession(bind=self.datastore) class Parent(MappedClass): class __mongometa__: name = 'parent' session = self.session _id = FieldProperty(int) children = RelationProperty('Child') class Child(MappedClass): class __mongometa__: name = 'child' session = self.session _id = FieldProperty(int) parents = RelationProperty('Parent') _parents = ForeignIdProperty('Parent', uselist=True) Mapper.compile_all() self.Parent = Parent self.Child = Child
def setUp(self): self.datastore = create_datastore('mim:///test_db') session = Session(bind=self.datastore) self.session = ODMSession(session) base = collection('test_doc', session, Field('_id', S.ObjectId), Field('type', str, if_missing='base'), Field('a', int), polymorphic_on='type', polymorphic_identity='base') derived = collection(base, Field('type', str, if_missing='derived'), Field('b', int), polymorphic_identity='derived') class Base(object): pass class Derived(Base): pass mapper(Base, base, self.session) mapper(Derived, derived, self.session) self.Base = Base self.Derived = Derived
def setUp(self): self.datastore = create_datastore('mim:///test_db') session = Session(bind=self.datastore) self.session = ODMSession(session) class Parent(object): pass class Child(object): pass parent = collection('parent', session, Field('_id', int)) child = collection('child', session, Field('_id', int), Field('parent_id', int)) mapper(Parent, parent, self.session, properties=dict(children=RelationProperty(Child))) mapper(Child, child, self.session, properties=dict(parent_id=ForeignIdProperty(Parent), parent=RelationProperty(Parent))) self.Parent = Parent self.Child = Child
def test_hook_base(self): assert id(self.Basic.query.session) == id(self.session) session2 = MagicMock() new_session = ODMSession(bind=session2) Mapper.replace_session(new_session) assert id(self.Basic.query.session) == id(new_session) assert id(self.session) != id(new_session)
def setUp(self): self.datastore = create_datastore('mim:///test_db') session = Session(bind=self.datastore) self.session = ODMSession(session) basic = collection('basic', session) class Basic(object): pass self.session.mapper(Basic, basic) self.basic = basic self.Basic = Basic
def setUp(self): self.datastore = create_datastore('mim:///test_db') session = Session(bind=self.datastore) self.session = ODMSession(session) basic = collection('basic', session, Field('_id', S.ObjectId), Field('a', int), Field('b', [int]), Field('c', dict(d=int, e=int))) class Basic(object): pass self.session.mapper(Basic, basic) self.basic = basic self.Basic = Basic
def setUp(self): Mapper._mapper_by_classname.clear() self.datastore = create_datastore('mim:///test_db') self.session = ODMSession(bind=self.datastore) class Basic(MappedClass): class __mongometa__: name = 'hook' session = self.session _id = FieldProperty(S.ObjectId) a = FieldProperty(int) Mapper.compile_all() self.Basic = Basic self.session.remove(self.Basic)
def setUp(self): self.datastore = create_datastore('mim:///test_db') self.session = ODMSession(bind=self.datastore) class TestCollection(MappedClass): class __mongometa__: name='test_collection' session = self.session _id = FieldProperty(int) children = RelationProperty('TestCollection') _children = ForeignIdProperty('TestCollection', uselist=True) parents = RelationProperty('TestCollection', via=('_children', False)) Mapper.compile_all() self.TestCollection = TestCollection
def setUp(self): self.datastore = create_datastore('mim:///test_db') self.session = ODMSession(bind=self.datastore) class Basic(MappedClass): class __mongometa__: name='basic' session = self.session _id = FieldProperty(S.ObjectId) a = FieldProperty(int) b = FieldProperty([int]) c = FieldProperty(dict( d=int, e=int)) d = FieldPropertyWithMissingNone(str, if_missing=S.Missing) e = FieldProperty(str, if_missing=S.Missing) Mapper.compile_all() self.Basic = Basic self.session.remove(self.Basic)
def setUp(self): Mapper._mapper_by_classname.clear() self.datastore = create_datastore('mim:///test_db') self.session = ODMSession(bind=self.datastore) self.hooks_called = defaultdict(list) tc = self class Basic(MappedClass): class __mongometa__: name = 'hook' session = self.session def before_save(instance): tc.hooks_called['before_save'].append(instance) _id = FieldProperty(S.ObjectId) a = FieldProperty(int) Mapper.compile_all() self.Basic = Basic self.session.remove(self.Basic)
def setupClass(cls): if ming is None: raise SkipTest('Ming not available...') cls.basic_session = Session(create_datastore('mim:///')) cls.s = ODMSession(cls.basic_session) class Author(MappedClass): class __mongometa__: session = cls.s name = 'wiki_author' _id = FieldProperty(schema.ObjectId) name = FieldProperty(str) pages = RelationProperty('WikiPage') class WikiPage(MappedClass): class __mongometa__: session = cls.s name = 'wiki_page' _id = FieldProperty(schema.ObjectId) title = FieldProperty(str) text = FieldProperty(str) order = FieldProperty(int) author_id = ForeignIdProperty(Author) author = RelationProperty(Author) cls.Author = Author cls.WikiPage = WikiPage Mapper.compile_all() cls.author = Author(name='author1') author2 = Author(name='author2') WikiPage(title='Hello', text='Text', order=1, author=cls.author) WikiPage(title='Another', text='Text', order=2, author=cls.author) WikiPage(title='ThirdOne', text='Text', order=3, author=author2) cls.s.flush() cls.s.clear()
def run_migration(datastore, target_versions, dry_run): '''Attempt to migrate the database to a specific set of required modules & versions.''' # Get the migration status of the db session = MigrationInfo.__mongometa__.session session.bind = datastore odmsession = ODMSession(session) info = MigrationInfo.m.get() if info is None: info = MigrationInfo.make({}) latest_versions = Migration.latest_versions() for k, v in target_versions.iteritems(): cur = info.versions.get(k, -1) islatest = ' (LATEST)' if v == latest_versions[k] else '' log.info('Target %s=%s%s (current=%s)', k, v, islatest, cur) # Create a migration plan plan = list(plan_migration(session, odmsession, info, target_versions)) # Execute (or print) the plan for step in plan: log.info('Migrate %r', step) if dry_run: continue step.apply(info.versions) info.m.save()
def setUp(self): Mapper._mapper_by_classname.clear() self.datastore = create_datastore(self.DATASTORE) self.session = ODMSession(bind=self.datastore)
from pymongo import MongoClient def init_logging(): logger = logging.getLogger('lenin') logger.setLevel(logging.INFO) # create file handler which logs even debug messages fh = logging.FileHandler('lenin.log') fh.setLevel(logging.INFO) # create console handler with a higher log level ch = logging.StreamHandler() ch.setLevel(logging.INFO) # create formatter and add it to the handlers formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) # add the handlers to the logger logger.addHandler(fh) logger.addHandler(ch) init_logging() client = MongoClient() db = client[config.DATABASE_NAME] session = Session.by_name('document_store') odm_session = ODMSession(doc_session=session)
from typing import Any, Dict import bson from dotenv import load_dotenv from ming import create_datastore, schema from ming.odm import FieldProperty, MappedClass, Mapper, ODMSession load_dotenv() DBHOST = environ[ 'DBHOST'] if 'DBHOST' in environ else 'mongodb://localhost:27017' DATABASE_NAME = environ[ 'DATABASE_NAME'] if 'DATABASE_NAME' in environ else 'classifier_dev' datastore = create_datastore(DBHOST + '/' + DATABASE_NAME) session = ODMSession(bind=datastore, autoflush=False) sessionLock = threading.RLock() def hasher(seq: str) -> str: """MongoDb can't index sequences > 1024b, so we index hashes instead.""" return hashlib.md5(str.encode(seq)).hexdigest() def ValueToJson(value: Any) -> Any: if isinstance(value, bson.objectid.ObjectId): return str(value) elif isinstance(value, datetime): return value.isoformat() elif isinstance(value, list): return [ValueToJson(member) for member in value]