~hardware-certification/zope3/certify-staging-2.5

« back to all changes in this revision

Viewing changes to src/ZODB/tests/.svn/text-base/blob_connection.txt.svn-base

  • Committer: Certification
  • Date: 2008-09-30 16:37:39 UTC
  • mfrom: (8.1.4 certify-zope3)
  • Revision ID: certification@macaroni-20080930163739-9badk5xhyh8nhy5k
Merged changes from trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2005 Zope Corporation and Contributors.
 
4
# All Rights Reserved.
 
5
#
 
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.
 
12
#
 
13
##############################################################################
 
14
 
 
15
Connection support for Blobs tests
 
16
==================================
 
17
 
 
18
Connections handle Blobs specially. To demonstrate that, we first need a Blob with some data:
 
19
 
 
20
    >>> from ZODB.interfaces import IBlob
 
21
    >>> from ZODB.blob import Blob
 
22
    >>> import transaction
 
23
    >>> blob = Blob()
 
24
    >>> data = blob.open("w")
 
25
    >>> data.write("I'm a happy Blob.")
 
26
    >>> data.close()
 
27
 
 
28
We also need a database with a blob supporting storage:
 
29
 
 
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)
 
38
 
 
39
Putting a Blob into a Connection works like every other object:
 
40
 
 
41
    >>> connection = database.open()
 
42
    >>> root = connection.root()
 
43
    >>> root['myblob'] = blob
 
44
    >>> transaction.commit()
 
45
 
 
46
We can also commit a transaction that seats a blob into place without
 
47
calling the blob's open method:
 
48
 
 
49
    >>> nothing = transaction.begin()
 
50
    >>> anotherblob = Blob()
 
51
    >>> root['anotherblob'] = anotherblob
 
52
    >>> nothing = transaction.commit()
 
53
 
 
54
Getting stuff out of there works similar:
 
55
 
 
56
    >>> connection2 = database.open()
 
57
    >>> root = connection2.root()
 
58
    >>> blob2 = root['myblob']
 
59
    >>> IBlob.providedBy(blob2)
 
60
    True
 
61
    >>> blob2.open("r").read()
 
62
    "I'm a happy Blob."
 
63
 
 
64
You can't put blobs into a database that has uses a Non-Blob-Storage, though:
 
65
 
 
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):
 
73
        ...
 
74
    Unsupported: Storing Blobs in <ZODB.MappingStorage.MappingStorage instance at ...> is not supported.
 
75
 
 
76
While we are testing this, we don't need the storage directory and
 
77
databases anymore:
 
78
 
 
79
    >>> transaction.abort()
 
80
    >>> database.close()
 
81
    >>> database2.close()