本文整理汇总了Python中ming.odm.ODMSession类的典型用法代码示例。如果您正苦于以下问题:Python ODMSession类的具体用法?Python ODMSession怎么用?Python ODMSession使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ODMSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestPolymorphic
class TestPolymorphic(TestCase):
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 test_polymorphic(self):
self.Base(a=1)
self.Derived(a=2,b=2)
self.session.flush()
self.session.clear()
q = self.Base.query.find()
r = sorted(q.all())
assert r[0].__class__ is self.Base
assert r[1].__class__ is self.Derived
示例2: ObjectIdRelationship
class ObjectIdRelationship(TestCase):
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 tearDown(self):
self.session.clear()
self.datastore.conn.drop_all()
def test_empty_relationship(self):
child = self.Child()
self.session.flush()
self.assertIsNone(child.parent_id)
def test_empty_list_relationship(self):
parent = self.Parent()
self.session.flush()
self.assertEqual(parent.children, [])
def test_default_relationship(self):
parent = self.Parent(_id=bson.ObjectId('deadbeefdeadbeefdeadbeef'))
child = self.Child()
self.session.flush()
self.assertEqual(child.field_with_default, parent)
def test_default_list_relationship(self):
child = self.Child(_id=bson.ObjectId('deadbeefdeadbeefdeadbeef'))
parent = self.Parent()
self.session.flush()
self.assertEqual(parent.field_with_default, [child])
示例3: TestRelation
class TestRelation(TestCase):
def setUp(self):
self.datastore = DS.DataStore(
'mim:///', database='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 tearDown(self):
self.session.clear()
self.datastore.conn.drop_all()
def test_parent(self):
parent = self.Parent(_id=1)
children = [ self.Child(_id=i, parent_id=1) for i in range(5) ]
self.session.flush()
self.session.clear()
parent = self.Parent.query.get(_id=1)
self.assertEqual(len(parent.children), 5)
def test_readonly(self):
parent = self.Parent(_id=1)
children = [ self.Child(_id=i, parent_id=1) for i in range(5) ]
self.session.flush()
self.session.clear()
parent = self.Parent.query.get(_id=1)
def clearchildren():
parent.children = []
def setchild():
parent.children[0] = children[0]
self.assertRaises(TypeError, clearchildren)
self.assertRaises(TypeError, parent.children.append, children[0])
self.assertRaises(TypeError, setchild)
示例4: TestRelation
class TestRelation(TestCase):
def setUp(self):
self.datastore = DS.DataStore(
'mim:///', database='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)
parent_id = ForeignIdProperty('Parent')
parent = RelationProperty('Parent')
Mapper.compile_all()
self.Parent = Parent
self.Child = Child
def tearDown(self):
self.session.clear()
self.datastore.conn.drop_all()
def test_parent(self):
parent = self.Parent(_id=1)
children = [ self.Child(_id=i, parent_id=1) for i in range(5) ]
self.session.flush()
self.session.clear()
parent = self.Parent.query.get(_id=1)
self.assertEqual(len(parent.children), 5)
def test_readonly(self):
parent = self.Parent(_id=1)
children = [ self.Child(_id=i, parent_id=1) for i in range(5) ]
self.session.flush()
self.session.clear()
parent = self.Parent.query.get(_id=1)
def clearchildren():
parent.children = []
def setchild():
parent.children[0] = children[0]
self.assertRaises(TypeError, clearchildren)
self.assertRaises(TypeError, parent.children.append, children[0])
self.assertRaises(TypeError, setchild)
示例5: setUp
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
示例6: setUp
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
示例7: TestWithNoFields
class TestWithNoFields(TestCase):
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 tearDown(self):
self.session.clear()
self.datastore.conn.drop_all()
def test_query(self):
self.basic(dict(a=1)).m.insert()
doc = self.Basic.query.get(a=1)
示例8: TestPolymorphic
class TestPolymorphic(TestCase):
def setUp(self):
self.bind = DS.DataStore(master='mim:///', database='test_db')
self.doc_session = Session(self.bind)
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 test_polymorphic(self):
self.Base(a=1)
self.odm_session.flush()
self.Derived(a=2,b=2)
self.odm_session.flush()
self.odm_session.clear()
q = self.Base.query.find()
r = sorted(q.all())
assert r[0].__class__ is self.Base
assert r[1].__class__ is self.Derived
示例9: TestPolymorphic
class TestPolymorphic(TestCase):
def setUp(self):
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 test_polymorphic(self):
self.Base(a=1)
self.odm_session.flush()
self.Derived(a=2,b=2)
self.odm_session.flush()
self.odm_session.clear()
q = self.Base.query.find()
r = [x.__class__ for x in q]
self.assertEqual(2, len(r))
self.assertTrue(self.Base in r)
self.assertTrue(self.Derived in r)
示例10: TestManyToManyListReverseRelation
class TestManyToManyListReverseRelation(TestCase):
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(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 tearDown(self):
self.session.clear()
self.datastore.conn.drop_all()
def test_compiled_field(self):
self.assertEqual(self.Child._parents.field.type, [self.Parent._id.field.type])
def test_parent(self):
parent = self.Parent(_id=1)
other_parent = self.Parent(_id=2)
children = [ self.Child(_id=i, _parents=[parent._id]) for i in range(5) ]
for c in children[:2]:
c._parents.append(other_parent._id)
self.session.flush()
self.session.clear()
parent = self.Parent.query.get(_id=1)
self.assertEqual(len(parent.children), 5)
self.session.clear()
child = self.Child.query.get(_id=0)
self.assertEqual(len(child.parents), 2)
示例11: TestManyToManyListCyclic
class TestManyToManyListCyclic(TestCase):
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 tearDown(self):
self.session.clear()
self.datastore.conn.drop_all()
def test_compiled_field(self):
self.assertEqual(self.TestCollection._children.field.type, [self.TestCollection._id.field.type])
def test_cyclic(self):
children = [ self.TestCollection(_id=i) for i in range(10, 15) ]
parent = self.TestCollection(_id=1)
parent._children = [c._id for c in children]
other_parent = self.TestCollection(_id=2)
other_parent._children = [c._id for c in children[:2]]
self.session.flush()
self.session.clear()
parent = self.TestCollection.query.get(_id=1)
self.assertEqual(len(parent.children), 5)
self.session.clear()
child = self.TestCollection.query.get(_id=10)
self.assertEqual(len(child.parents), 2)
示例12: setUp
def setUp(self):
self.datastore = DS.DataStore(
'mim:///', database='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)
parent_id = ForeignIdProperty('Parent')
parent = RelationProperty('Parent')
Mapper.compile_all()
self.Parent = Parent
self.Child = Child
示例13: TestBasicMapperExtension
class TestBasicMapperExtension(TestCase):
def setUp(self):
self.datastore = DS.DataStore(
'mim:///', database='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 tearDown(self):
self.session.clear()
self.datastore.conn.drop_all()
def test_mapper_extension(self):
doc = self.Basic()
doc.a = 5
self.session.flush()
doc.a = 6
self.session.flush()
示例14: TestOrphanObjects
class TestOrphanObjects(TestCase):
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 tearDown(self):
self.session.clear()
self.datastore.conn.drop_all()
def test_orphan_object(self):
obj = self.Basic()
assert session(obj) is self.session
self.session.clear()
assert session(obj) is None
示例15: TestManyToManyListRelation
class TestManyToManyListRelation(TestCase):
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(int)
children = RelationProperty('Child')
_children = ForeignIdProperty('Child', uselist=True)
class Child(MappedClass):
class __mongometa__:
name='child'
session = self.session
_id = FieldProperty(int)
parents = RelationProperty('Parent')
Mapper.compile_all()
self.Parent = Parent
self.Child = Child
def tearDown(self):
self.session.clear()
self.datastore.conn.drop_all()
def test_compiled_field(self):
self.assertEqual(self.Parent._children.field.type, [self.Child._id.field.type])
def test_parent(self):
children = [ self.Child(_id=i) for i in range(5) ]
parent = self.Parent(_id=1)
parent._children = [c._id for c in children]
other_parent = self.Parent(_id=2)
other_parent._children = [c._id for c in children[:2]]
self.session.flush()
self.session.clear()
parent = self.Parent.query.get(_id=1)
self.assertEqual(len(parent.children), 5)
self.session.clear()
child = self.Child.query.get(_id=0)
self.assertEqual(len(child.parents), 2)
def test_instrumented_readonly(self):
children = [ self.Child(_id=i) for i in range(5) ]
parent = self.Parent(_id=1)
parent._children = [c._id for c in children]
self.session.flush()
self.session.clear()
parent = self.Parent.query.get(_id=1)
self.assertRaises(TypeError, parent.children.append, children[0])
def test_writable(self):
children = [ self.Child(_id=i) for i in range(5) ]
parent = self.Parent(_id=1)
parent._children = [c._id for c in children]
self.session.flush()
self.session.clear()
parent = self.Parent.query.get(_id=1)
parent.children = parent.children + [self.Child(_id=5)]
self.session.flush()
self.session.clear()
parent = self.Parent.query.get(_id=1)
self.assertEqual(len(parent.children), 6)
self.session.clear()
child = self.Child.query.get(_id=5)
self.assertEqual(len(child.parents), 1)
parent = self.Parent.query.get(_id=1)
parent.children = []
self.session.flush()
self.session.clear()
parent = self.Parent.query.get(_id=1)
self.assertEqual(len(parent.children), 0)
self.session.clear()
def test_writable_backref(self):
children = [ self.Child(_id=i) for i in range(5) ]
parent = self.Parent(_id=1)
parent._children = [c._id for c in children]
other_parent = self.Parent(_id=2)
other_parent._children = [c._id for c in children[:2]]
self.session.flush()
self.session.clear()
child = self.Child.query.get(_id=4)
self.assertEqual(len(child.parents), 1)
child.parents = child.parents + [other_parent]
self.session.flush()
self.session.clear()
child = self.Child.query.get(_id=4)
self.assertEqual(len(child.parents), 2)
#.........这里部分代码省略.........