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

Python python_support.b函数代码示例

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

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



在下文中一共展示了b函数的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_file_fields_set

    def test_file_fields_set(self):

        class SetFile(Document):
            the_file = FileField()

        text = b('Hello, World!')
        more_text = b('Foo Bar')

        SetFile.drop_collection()

        setfile = SetFile()
        setfile.the_file = text
        setfile.save()

        result = SetFile.objects.first()
        self.assertTrue(setfile == result)
        self.assertEqual(result.the_file.read(), text)

        # Try replacing file with new one
        result.the_file.replace(more_text)
        result.save()

        result = SetFile.objects.first()
        self.assertTrue(setfile == result)
        self.assertEqual(result.the_file.read(), more_text)
        result.the_file.delete()
开发者ID:Demoncode,项目名称:mongoengine,代码行数:26,代码来源:file_tests.py


示例2: test_file_fields_stream

    def test_file_fields_stream(self):
        """Ensure that file fields can be written to and their data retrieved
        """
        class StreamFile(Document):
            the_file = FileField()

        StreamFile.drop_collection()

        text = b('Hello, World!')
        more_text = b('Foo Bar')
        content_type = 'text/plain'

        streamfile = StreamFile()
        streamfile.the_file.new_file(content_type=content_type)
        streamfile.the_file.write(text)
        streamfile.the_file.write(more_text)
        streamfile.the_file.close()
        streamfile.save()

        result = StreamFile.objects.first()
        self.assertTrue(streamfile == result)
        self.assertEqual(result.the_file.read(), text + more_text)
        self.assertEqual(result.the_file.content_type, content_type)
        result.the_file.seek(0)
        self.assertEqual(result.the_file.tell(), 0)
        self.assertEqual(result.the_file.read(len(text)), text)
        self.assertEqual(result.the_file.tell(), len(text))
        self.assertEqual(result.the_file.read(len(more_text)), more_text)
        self.assertEqual(result.the_file.tell(), len(text + more_text))
        result.the_file.delete()

        # Ensure deleted file returns None
        self.assertTrue(result.the_file.read() == None)
开发者ID:Demoncode,项目名称:mongoengine,代码行数:33,代码来源:file_tests.py


示例3: test_file_multidb

    def test_file_multidb(self):
        register_connection('test_files', 'test_files')

        class TestFile(Document):
            name = StringField()
            the_file = FileField(db_alias="test_files",
                                 collection_name="macumba")

        TestFile.drop_collection()

        # delete old filesystem
        get_db("test_files").macumba.files.drop()
        get_db("test_files").macumba.chunks.drop()

        # First instance
        test_file = TestFile()
        test_file.name = "Hello, World!"
        test_file.the_file.put(b('Hello, World!'),
                          name="hello.txt")
        test_file.save()

        data = get_db("test_files").macumba.files.find_one()
        self.assertEqual(data.get('name'), 'hello.txt')

        test_file = TestFile.objects.first()
        self.assertEqual(test_file.the_file.read(),
                          b('Hello, World!'))
开发者ID:Demoncode,项目名称:mongoengine,代码行数:27,代码来源:file_tests.py


示例4: test_image_field

    def test_image_field(self):
        if not HAS_PIL:
            raise SkipTest('PIL not installed')

        class TestImage(Document):
            image = ImageField()

        TestImage.drop_collection()

        with tempfile.TemporaryFile() as f:
            f.write(b("Hello World!"))
            f.flush()

            t = TestImage()
            try:
                t.image.put(f)
                self.fail("Should have raised an invalidation error")
            except ValidationError as e:
                self.assertEqual("%s" % e, "Invalid image: cannot identify image file %s" % f)

        t = TestImage()
        t.image.put(open(TEST_IMAGE_PATH, 'rb'))
        t.save()

        t = TestImage.objects.first()

        self.assertEqual(t.image.format, 'PNG')

        w, h = t.image.size
        self.assertEqual(w, 371)
        self.assertEqual(h, 76)

        t.image.delete()
开发者ID:local-projects,项目名称:mongoengine,代码行数:33,代码来源:file_tests.py


示例5: test_file_field_no_default

    def test_file_field_no_default(self):

        class GridDocument(Document):
            the_file = FileField()

        GridDocument.drop_collection()

        with tempfile.TemporaryFile() as f:
            f.write(b("Hello World!"))
            f.flush()

            # Test without default
            doc_a = GridDocument()
            doc_a.save()

            doc_b = GridDocument.objects.with_id(doc_a.id)
            doc_b.the_file.replace(f, filename='doc_b')
            doc_b.save()
            self.assertNotEqual(doc_b.the_file.grid_id, None)

            # Test it matches
            doc_c = GridDocument.objects.with_id(doc_b.id)
            self.assertEqual(doc_b.the_file.grid_id, doc_c.the_file.grid_id)

            # Test with default
            doc_d = GridDocument(the_file=b(''))
            doc_d.save()

            doc_e = GridDocument.objects.with_id(doc_d.id)
            self.assertEqual(doc_d.the_file.grid_id, doc_e.the_file.grid_id)

            doc_e.the_file.replace(f, filename='doc_e')
            doc_e.save()

            doc_f = GridDocument.objects.with_id(doc_e.id)
            self.assertEqual(doc_e.the_file.grid_id, doc_f.the_file.grid_id)

        db = GridDocument._get_db()
        grid_fs = gridfs.GridFS(db)
        self.assertEqual(['doc_b', 'doc_e'], grid_fs.list())
开发者ID:Demoncode,项目名称:mongoengine,代码行数:40,代码来源:file_tests.py


示例6: test_file_boolean

    def test_file_boolean(self):
        """Ensure that a boolean test of a FileField indicates its presence
        """
        class TestFile(Document):
            the_file = FileField()
        TestFile.drop_collection()

        test_file = TestFile()
        self.assertFalse(bool(test_file.the_file))
        test_file.the_file.put(b('Hello, World!'), content_type='text/plain')
        test_file.save()
        self.assertTrue(bool(test_file.the_file))

        test_file = TestFile.objects.first()
        self.assertEqual(test_file.the_file.content_type, "text/plain")
开发者ID:Demoncode,项目名称:mongoengine,代码行数:15,代码来源:file_tests.py


示例7: test_copyable

    def test_copyable(self):
        class PutFile(Document):
            the_file = FileField()

        PutFile.drop_collection()

        text = b('Hello, World!')
        content_type = 'text/plain'

        putfile = PutFile()
        putfile.the_file.put(text, content_type=content_type)
        putfile.save()

        class TestFile(Document):
            name = StringField()

        self.assertEqual(putfile, copy.copy(putfile))
        self.assertEqual(putfile, copy.deepcopy(putfile))
开发者ID:Demoncode,项目名称:mongoengine,代码行数:18,代码来源:file_tests.py


示例8: test_image_field

    def test_image_field(self):
        if not HAS_PIL:
            raise SkipTest('PIL not installed')

        class TestImage(Document):
            image = ImageField()

        TestImage.drop_collection()

        with tempfile.TemporaryFile() as f:
            f.write(b("Hello World!"))
            f.flush()

            t = TestImage()
            try:
                t.image.put(f)
                self.fail("Should have raised an invalidation error")
            except ValidationError, e:
                self.assertEqual("%s" % e, "Invalid image: cannot identify image file %s" % f)
开发者ID:MasterMind2k,项目名称:mongoengine,代码行数:19,代码来源:file_tests.py


示例9: test_file_uniqueness

    def test_file_uniqueness(self):
        """Ensure that each instance of a FileField is unique
        """
        class TestFile(Document):
            name = StringField()
            the_file = FileField()

        # First instance
        test_file = TestFile()
        test_file.name = "Hello, World!"
        test_file.the_file.put(b('Hello, World!'))
        test_file.save()

        # Second instance
        test_file_dupe = TestFile()
        data = test_file_dupe.the_file.read()  # Should be None

        self.assertTrue(test_file.name != test_file_dupe.name)
        self.assertTrue(test_file.the_file.read() != data)

        TestFile.drop_collection()
开发者ID:Demoncode,项目名称:mongoengine,代码行数:21,代码来源:file_tests.py


示例10: test_file_fields

    def test_file_fields(self):
        """Ensure that file fields can be written to and their data retrieved
        """

        class PutFile(Document):
            the_file = FileField()

        PutFile.drop_collection()

        text = b('Hello, World!')
        content_type = 'text/plain'

        putfile = PutFile()
        putfile.the_file.put(text, content_type=content_type, filename="hello")
        putfile.save()

        result = PutFile.objects.first()
        self.assertTrue(putfile == result)
        self.assertEqual("%s" % result.the_file, "<GridFSProxy: hello>")
        self.assertEqual(result.the_file.read(), text)
        self.assertEqual(result.the_file.content_type, content_type)
        result.the_file.delete()  # Remove file from GridFS
        PutFile.objects.delete()

        # Ensure file-like objects are stored
        PutFile.drop_collection()

        putfile = PutFile()
        putstring = StringIO()
        putstring.write(text)
        putstring.seek(0)
        putfile.the_file.put(putstring, content_type=content_type)
        putfile.save()

        result = PutFile.objects.first()
        self.assertTrue(putfile == result)
        self.assertEqual(result.the_file.read(), text)
        self.assertEqual(result.the_file.content_type, content_type)
        result.the_file.delete()
开发者ID:MasterMind2k,项目名称:mongoengine,代码行数:39,代码来源:file_tests.py


示例11: test_file_disk_space

    def test_file_disk_space(self): 
        """ Test disk space usage when we delete/replace a file """ 
        class TestFile(Document):
            the_file = FileField()
            
        text = b('Hello, World!')
        content_type = 'text/plain'

        testfile = TestFile()
        testfile.the_file.put(text, content_type=content_type, filename="hello")
        testfile.save()
        
        # Now check fs.files and fs.chunks 
        db = TestFile._get_db()
        
        files = db.fs.files.find()
        chunks = db.fs.chunks.find()
        self.assertEquals(len(list(files)), 1)
        self.assertEquals(len(list(chunks)), 1)

        # Deleting the docoument should delete the files 
        testfile.delete()
        
        files = db.fs.files.find()
        chunks = db.fs.chunks.find()
        self.assertEquals(len(list(files)), 0)
        self.assertEquals(len(list(chunks)), 0)
        
        # Test case where we don't store a file in the first place 
        testfile = TestFile()
        testfile.save()
        
        files = db.fs.files.find()
        chunks = db.fs.chunks.find()
        self.assertEquals(len(list(files)), 0)
        self.assertEquals(len(list(chunks)), 0)
        
        testfile.delete()
        
        files = db.fs.files.find()
        chunks = db.fs.chunks.find()
        self.assertEquals(len(list(files)), 0)
        self.assertEquals(len(list(chunks)), 0)
        
        # Test case where we overwrite the file 
        testfile = TestFile()
        testfile.the_file.put(text, content_type=content_type, filename="hello")
        testfile.save()
        
        text = b('Bonjour, World!')
        testfile.the_file.replace(text, content_type=content_type, filename="hello")
        testfile.save()
        
        files = db.fs.files.find()
        chunks = db.fs.chunks.find()
        self.assertEquals(len(list(files)), 1)
        self.assertEquals(len(list(chunks)), 1)
        
        testfile.delete()
        
        files = db.fs.files.find()
        chunks = db.fs.chunks.find()
        self.assertEquals(len(list(files)), 0)
        self.assertEquals(len(list(chunks)), 0)
开发者ID:3lnc,项目名称:mongoengine,代码行数:64,代码来源:file_tests.py



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Python python_support.txt_type函数代码示例发布时间:2022-05-27
下一篇:
Python shortcuts.get_document_or_404函数代码示例发布时间: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