1
##############################################################################
3
# Copyright (c) 2005 Zope Corporation and Contributors.
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
13
##############################################################################
15
Connection support for Blobs tests
16
==================================
18
Connections handle Blobs specially. To demonstrate that, we first need a Blob with some data:
20
>>> from ZODB.interfaces import IBlob
21
>>> from ZODB.blob import Blob
22
>>> import transaction
24
>>> data = blob.open("w")
25
>>> data.write("I'm a happy Blob.")
28
We also need a database with a blob supporting storage:
30
>>> from ZODB.MappingStorage import MappingStorage
31
>>> from ZODB.blob import BlobStorage
32
>>> from ZODB.DB import DB
33
>>> from tempfile import mkdtemp
34
>>> base_storage = MappingStorage("test")
35
>>> blob_dir = mkdtemp()
36
>>> blob_storage = BlobStorage(blob_dir, base_storage)
37
>>> database = DB(blob_storage)
39
Putting a Blob into a Connection works like every other object:
41
>>> connection = database.open()
42
>>> root = connection.root()
43
>>> root['myblob'] = blob
44
>>> transaction.commit()
46
We can also commit a transaction that seats a blob into place without
47
calling the blob's open method:
49
>>> nothing = transaction.begin()
50
>>> anotherblob = Blob()
51
>>> root['anotherblob'] = anotherblob
52
>>> nothing = transaction.commit()
54
Getting stuff out of there works similar:
56
>>> connection2 = database.open()
57
>>> root = connection2.root()
58
>>> blob2 = root['myblob']
59
>>> IBlob.providedBy(blob2)
61
>>> blob2.open("r").read()
64
You can't put blobs into a database that has uses a Non-Blob-Storage, though:
66
>>> no_blob_storage = MappingStorage()
67
>>> database2 = DB(no_blob_storage)
68
>>> connection3 = database2.open()
69
>>> root = connection3.root()
70
>>> root['myblob'] = Blob()
71
>>> transaction.commit() # doctest: +ELLIPSIS
72
Traceback (most recent call last):
74
Unsupported: Storing Blobs in <ZODB.MappingStorage.MappingStorage instance at ...> is not supported.
76
While we are testing this, we don't need the storage directory and
79
>>> transaction.abort()