• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python odm.ODMSession类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中ming.odm.ODMSession的典型用法代码示例。如果您正苦于以下问题:Python ODMSession类的具体用法?Python ODMSession怎么用?Python ODMSession使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了ODMSession类的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:duilio,项目名称:Ming,代码行数:34,代码来源:test_mapper.py


示例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])
开发者ID:vzhz,项目名称:function_generator,代码行数:54,代码来源:test_declarative.py


示例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)
开发者ID:apendleton,项目名称:Ming,代码行数:48,代码来源:test_mapper.py


示例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)
开发者ID:apendleton,项目名称:Ming,代码行数:48,代码来源:test_declarative.py


示例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
开发者ID:vzhz,项目名称:function_generator,代码行数:27,代码来源:test_declarative.py


示例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
开发者ID:duilio,项目名称:Ming,代码行数:10,代码来源:test_mapper.py


示例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)
开发者ID:duilio,项目名称:Ming,代码行数:20,代码来源:test_mapper.py


示例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
开发者ID:apendleton,项目名称:Ming,代码行数:34,代码来源:test_declarative.py


示例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)
开发者ID:vzhz,项目名称:function_generator,代码行数:35,代码来源:test_declarative.py


示例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)
开发者ID:vzhz,项目名称:function_generator,代码行数:45,代码来源:test_declarative.py


示例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)
开发者ID:vzhz,项目名称:function_generator,代码行数:41,代码来源:test_declarative.py


示例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
开发者ID:apendleton,项目名称:Ming,代码行数:20,代码来源:test_declarative.py


示例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()
开发者ID:apendleton,项目名称:Ming,代码行数:38,代码来源:test_declarative.py


示例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
开发者ID:duilio,项目名称:Ming,代码行数:22,代码来源:test_mapper.py


示例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)
#.........这里部分代码省略.........
开发者ID:vzhz,项目名称:function_generator,代码行数:101,代码来源:test_declarative.py


示例16: TestBasicMapping

class TestBasicMapping(TestCase):
    
    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 tearDown(self):
        self.session.clear()
        self.datastore.conn.drop_all()

    def test_set_to_same(self):
        obj = self.Basic(a=1)
        assert state(obj).status == 'new'
        self.session.flush()
        assert state(obj).status == 'clean'
        obj.a = 1
        assert state(obj).status == 'clean'

    def test_disable_instrument(self):
        # Put a doc in the DB
        self.Basic(a=1, b=[2,3], c=dict(d=4, e=5))
        self.session.flush()
        # Load back with instrumentation
        self.session.clear()
        obj = self.Basic.query.find().options(instrument=True).first()
        self.assertEqual(type(obj.b), InstrumentedList)
        self.assertEqual(type(obj.c), InstrumentedObj)
        # Load back without instrumentation
        self.session.clear()
        obj = self.Basic.query.find().options(instrument=False).first()
        self.assertEqual(type(obj.b), list)
        self.assertEqual(type(obj.c), Object)

    def test_enable_instrument(self):
        session = Session(bind=self.datastore)
        basic1 = collection(
            'basic1', session,
            Field('_id', S.ObjectId),
            Field('a', int),
            Field('b', [int]),
            Field('c', dict(
                    d=int, e=int)))
        class Basic1(object):
            pass                    
        self.session.mapper(Basic1, basic1, options=dict(instrument=False))
        # Put a doc in the DB
        Basic1(a=1, b=[2,3], c=dict(d=4, e=5))
        self.session.flush()
        # Load back with instrumentation
        self.session.clear()
        obj = Basic1.query.find().options(instrument=True).first()
        self.assertEqual(type(obj.b), InstrumentedList)
        self.assertEqual(type(obj.c), InstrumentedObj)
        # Load back without instrumentation
        self.session.clear()
        obj = Basic1.query.find().options(instrument=False).first()
        self.assertEqual(type(obj.b), list)
        self.assertEqual(type(obj.c), Object)

    def test_repr(self):
        doc = self.Basic(a=1, b=[2,3], c=dict(d=4,e=5))
        sdoc = repr(doc)
        assert 'a=1' in sdoc, sdoc
        assert 'b=I[2, 3]' in sdoc, sdoc
        assert "c=I{'e': 5, 'd': 4}" in sdoc, sdoc

    def test_create(self):
        doc = self.Basic()
        assert state(doc).status == 'new'
        self.session.flush()
        assert state(doc).status == 'clean'
        doc.a = 5
        assert state(doc).status == 'dirty'
        self.session.flush()
        assert state(doc).status == 'clean'
        c = doc.c
        c.e = 5
        assert state(doc).status == 'dirty', state(doc).status
        assert repr(state(doc)).startswith('<ObjectState')

    def test_mapped_object(self):
        doc = self.Basic(a=1, b=[2,3], c=dict(d=4, e=5))
        self.assertEqual(doc.a, doc['a'])
        self.assertRaises(AttributeError, getattr, doc, 'foo')
        self.assertRaises(KeyError, doc.__getitem__, 'foo')
        doc['a'] = 5
        self.assertEqual(doc.a, doc['a'])
#.........这里部分代码省略.........
开发者ID:duilio,项目名称:Ming,代码行数:101,代码来源:test_mapper.py


示例17: TestRelation

class TestRelation(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)
            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_instrumented_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)
        self.assertRaises(TypeError, parent.children.append, children[0])

    def test_writable(self):
        parent = self.Parent(_id=1)
        children = [ self.Child(_id=i, parent_id=1) for i in range(5) ]
        other_parent = self.Parent(_id=2)
        self.session.flush()
        self.session.clear()

        child = self.Child.query.get(_id=4)
        child.parent = other_parent
        self.session.flush()
        self.session.clear()

        parent1 = self.Parent.query.get(_id=1)
        self.assertEqual(len(parent1.children), 4)

        parent2 = self.Parent.query.get(_id=2)
        self.assertEqual(len(parent2.children), 1)

    def test_writable_backref(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)
        parent.children = parent.children[:4]
        self.session.flush()
        self.session.clear()

        parent = self.Parent.query.get(_id=1)
        self.assertEqual(len(parent.children), 4)

    def test_nullable_relationship(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()

        child = self.Child.query.get(_id=0)
        child.parent = None
        self.session.flush()
        self.session.clear()

        parent = self.Parent.query.get(_id=1)
        self.assertEqual(len(parent.children), 4)

        child = self.Child.query.get(_id=0)
        self.assertEqual(child.parent, None)
        self.assertEqual(child.parent_id, None)

    def test_nullable_foreignid(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()

        child = self.Child.query.get(_id=0)
        child.parent_id = None
#.........这里部分代码省略.........
开发者ID:vzhz,项目名称:function_generator,代码行数:101,代码来源:test_declarative.py


示例18: TestRelationWithNone

class TestRelationWithNone(TestCase):

    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 tearDown(self):
        self.session.clear()
        self.datastore.conn.drop_all()

    def test_none_allowed(self):
        parent = self.Parent(_id=1)
        child = self.Child(_id=1, parent_id=parent._id)
        none_parent = self.Parent(_id=None)
        none_child = self.Child(_id=2, parent_id=None)
        self.session.flush()
        self.session.clear()

        child = self.Child.query.get(_id=1)
        parent = child.parent
        self.assertEqual(parent._id, 1)

        none_child = self.Child.query.get(_id=2)
        none_parent = none_child.parent
        self.assertNotEqual(none_parent, None)
        self.assertEqual(none_parent._id, None)

    def test_none_not_allowed(self):
        grandparent = self.GrandParent(_id=1)
        parent = self.Parent(_id=1, grandparent_id=grandparent._id)
        none_grandparent = self.GrandParent(_id=None)
        none_parent = self.Parent(_id=2, grandparent_id=None)
        self.session.flush()
        self.session.clear()

        parent = self.Parent.query.get(_id=1)
        grandparent = parent.grandparent
        self.assertEqual(grandparent._id, 1)

        none_parent = self.Parent.query.get(_id=2)
        none_grandparent = none_parent.grandparent
        self.assertEqual(none_grandparent, None)
开发者ID:vzhz,项目名称:function_generator,代码行数:66,代码来源:test_declarative.py


示例19: TestBasicMapping

class TestBasicMapping(TestCase):

    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 tearDown(self):
        self.session.clear()
        self.datastore.conn.drop_all()

    def test_repr(self):
        doc = self.Basic(a=1, b=[2,3], c=dict(d=4, e=5))
        repr(self.session)

    def test_create(self):
        doc = self.Basic()
        assert state(doc).status == 'new'
        self.session.flush()
        assert state(doc).status == 'clean'
        doc.a = 5
        assert state(doc).status == 'dirty'
        self.session.flush()
        assert state(doc).status == 'clean'
        c = doc.c
        c.e = 5
        assert state(doc).status == 'dirty'
        assert repr(state(doc)).startswith('<ObjectState')

    def test_mapped_object(self):
        doc = self.Basic(a=1, b=[2,3], c=dict(d=4, e=5))
        self.assertEqual(doc.a, doc['a'])
        self.assertEqual(doc.d, None)
        self.assertRaises(AttributeError, getattr, doc, 'e')
        self.assertRaises(AttributeError, getattr, doc, 'foo')
        self.assertRaises(KeyError, doc.__getitem__, 'foo')

        doc['d'] = 'test'
        self.assertEqual(doc.d, doc['d'])
        doc['e'] = 'test'
        self.assertEqual(doc.e, doc['e'])
        del doc.d
        self.assertEqual(doc.d, None)
        del doc.e
        self.assertRaises(AttributeError, getattr, doc, 'e')

        doc['a'] = 5
        self.assertEqual(doc.a, doc['a'])
        self.assertEqual(doc.a, 5)
        self.assert_('a' in doc)
        doc.delete()

    def test_mapper(self):
        m = mapper(self.Basic)
        self.assertEqual(repr(m), '<Mapper Basic:basic>')
        doc = self.Basic(a=1, b=[2,3], c=dict(d=4, e=5))
        self.session.flush()
        q = self.Basic.query.find()
        self.assertEqual(q.count(), 1)
        self.session.remove(self.Basic, {})
        q = self.Basic.query.find()
        self.assertEqual(q.count(), 0)

    def test_query(self):
        doc = self.Basic(a=1, b=[2,3], c=dict(d=4, e=5))
        self.session.flush()
        q = self.Basic.query.find(dict(a=1))
        self.assertEqual(q.count(), 1)
        doc.a = 5
        self.session.flush()
        q = self.Basic.query.find(dict(a=1))
        self.assertEqual(q.count(), 0)
        self.assertEqual(doc.query.find(dict(a=1)).count(), 0)
        doc = self.Basic.query.get(a=5)
        self.assert_(doc is not None)
        self.Basic.query.remove({})
        self.assertEqual(self.Basic.query.find().count(), 0)

    def test_delete(self):
        doc = self.Basic(a=1, b=[2,3], c=dict(d=4, e=5))
        self.session.flush()
        q = self.Basic.query.find()
        self.assertEqual(q.count(), 1)
        doc.delete()
        q = self.Basic.query.find()
        self.assertEqual(q.count(), 1)
        self.session.flush()
#.........这里部分代码省略.........
开发者ID:vzhz,项目名称:function_generator,代码行数:101,代码来源:test_declarative.py



注:本文中的ming.odm.ODMSession类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python odm.ThreadLocalORMSession类代码示例发布时间:2022-05-27
下一篇:
Python odm.session函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap