testConfig.py 3.64 KB
Newer Older
1 2
##############################################################################
#
3
# Copyright (c) 2003 Zope Corporation and Contributors.
4
# All Rights Reserved.
5
#
6 7 8 9 10 11
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
12
#
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
##############################################################################

import os
import errno
import shutil
import tempfile
import unittest

import ZODB.config
import ZODB.tests
from ZODB.POSException import ReadOnlyError
from ZEO.ClientStorage import ClientDisconnected

class ConfigTestBase(unittest.TestCase):
    def _opendb(self, s):
        return ZODB.config.databaseFromString(s)

    def _test(self, s):
        db = self._opendb(s)
        # Do something with the database to make sure it works
        cn = db.open()
        rt = cn.root()
        rt["test"] = 1
        get_transaction().commit()
        db.close()


class ZODBConfigTest(ConfigTestBase):
    def test_map_config1(self):
42 43 44 45 46 47
        self._test(
            """
            <zodb>
              <mappingstorage/>
            </zodb>
            """)
48 49 50

    def test_map_config2(self):
        self._test(
51 52 53 54 55
            """
            <zodb>
              <mappingstorage/>
              cache-size 1000
            </zodb>
56 57 58 59 60
            """)

    def test_file_config1(self):
        path = tempfile.mktemp()
        self._test(
61 62 63 64 65 66
            """
            <zodb>
              <filestorage>
                path %s
              </filestorage>
            </zodb>
67 68
            """ % path)
        os.unlink(path)
69

70 71 72
    def test_file_config2(self):
        path = tempfile.mktemp()
        cfg = """
73 74
        <zodb>
          <filestorage>
75 76
            path %s
            create false
77 78 79
            read-only true
          </filestorage>
        </zodb>
80 81 82 83 84
        """ % path
        self.assertRaises(ReadOnlyError, self._test, cfg)

    def test_zeo_config(self):
        cfg = """
85 86
        <zodb>
          <zeoclient>
87 88
            server /no/path/var/test/foo
            wait false
89 90
          </zeoclient>
        </zodb>
91 92 93
        """
        self.assertRaises(ClientDisconnected, self._test, cfg)

94 95
    def test_demo_config(self):
        cfg = """
96 97
        <zodb unused-name>
          <demostorage>
98 99
            name foo
            <mappingstorage/>
100 101
          </demostorage>
        </zodb>
102 103 104
        """
        self._test(cfg)

105 106 107 108 109 110 111 112 113 114 115 116 117 118
class BDBConfigTest(ConfigTestBase):
    def setUp(self):
        self._path = tempfile.mktemp()
        try:
            os.mkdir(self._path)
        except OSError, e:
            if e.errno <> errno.EEXIST:
                raise

    def tearDown(self):
        shutil.rmtree(self._path)

    def test_bdbfull_simple(self):
        cfg = """
119 120
        <zodb>
          <fullstorage>
121
            name %s
122 123
          </fullstorage>
        </zodb>
124 125 126 127 128
        """ % self._path
        self._test(cfg)

    def test_bdbminimal_simple(self):
        cfg = """
129 130
        <zodb>
          <minimalstorage>
131
            name %s
132 133
          </minimalstorage>
        </zodb>
134 135 136 137 138 139 140
        """ % self._path
        self._test(cfg)


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(ZODBConfigTest))
141 142 143
    # Only run the Berkeley tests if they are available
    import BDBStorage
    if BDBStorage.is_available:
144 145 146 147 148 149
        suite.addTest(unittest.makeSuite(BDBConfigTest))
    return suite


if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')