本文整理匯總了Python中ming.odm.ODMSession.mapper方法的典型用法代碼示例。如果您正苦於以下問題:Python ODMSession.mapper方法的具體用法?Python ODMSession.mapper怎麽用?Python ODMSession.mapper使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ming.odm.ODMSession
的用法示例。
在下文中一共展示了ODMSession.mapper方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: TestWithNoFields
# 需要導入模塊: from ming.odm import ODMSession [as 別名]
# 或者: from ming.odm.ODMSession import mapper [as 別名]
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)
示例2: TestOrphanObjects
# 需要導入模塊: from ming.odm import ODMSession [as 別名]
# 或者: from ming.odm.ODMSession import mapper [as 別名]
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
示例3: TestBasicMapping
# 需要導入模塊: from ming.odm import ODMSession [as 別名]
# 或者: from ming.odm.ODMSession import mapper [as 別名]
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'])
#.........這裏部分代碼省略.........