Python google.appengine.ext.db 模块,Expando() 实例源码

我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用google.appengine.ext.db.Expando()

项目:Tinychat-Bot--Discontinued    作者:Tinychat    | 项目源码 | 文件源码
def test_get(self):
        x = self.klass()

        # not a class type
        with self.assertRaises(TypeError):
            x.get(chr, '')

        # not a subclass of db.Model/db.Expando
        with self.assertRaises(TypeError):
            x.get(Spam, '')

        x = self.klass()

        with self.assertRaises(KeyError):
            x.get(models.PetModel, 'foo')

        self.assertEqual(x, {models.PetModel: {}})

        obj = object()

        x[models.PetModel]['foo'] = obj

        obj2 = x.get(models.PetModel, 'foo')

        self.assertEqual(id(obj), id(obj2))
        self.assertEqual(x, {models.PetModel: {'foo': obj}})
项目:Tinychat-Bot--Discontinued    作者:Tinychat    | 项目源码 | 文件源码
def test_add(self):
        x = self.klass()

        # not a class type
        with self.assertRaises(TypeError):
            x.set(chr, '')

        # not a subclass of db.Model/db.Expando
        with self.assertRaises(TypeError):
            x.set(Spam, '')

        # wrong type for key
        with self.assertRaises(TypeError):
            x.set(models.PetModel, 3)

        x = self.klass()
        pm1 = models.PetModel(type='cat', name='Jessica')
        pm2 = models.PetModel(type='dog', name='Sam')
        pe1 = models.PetExpando(type='cat', name='Toby')

        self.assertEqual(x, {})

        x.set(models.PetModel, 'foo', pm1)
        self.assertEqual(x, {models.PetModel: {'foo': pm1}})
        x.set(models.PetModel, 'bar', pm2)
        self.assertEqual(x, {models.PetModel: {'foo': pm1, 'bar': pm2}})
        x.set(models.PetExpando, 'baz', pe1)
        self.assertEqual(x, {
            models.PetModel: {'foo': pm1, 'bar': pm2},
            models.PetExpando: {'baz': pe1}
        })