2
# Copyright (c) 2008 Canonical
4
# Written by James Henstridge <jamesh@canonical.com>
6
# This file is part of Storm Object Relational Mapper.
8
# Storm is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU Lesser General Public License as
10
# published by the Free Software Foundation; either version 2.1 of
11
# the License, or (at your option) any later version.
13
# Storm is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU Lesser General Public License for more details.
18
# You should have received a copy of the GNU Lesser General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
28
have_django_and_transaction = False
30
have_django_and_transaction = True
31
from django import conf
32
from storm.django import stores
33
from storm.zope.zstorm import global_zstorm
35
from storm.store import Store
36
from tests.helper import TestHelper
39
class DjangoStoreTests(TestHelper):
41
def is_supported(self):
42
return have_django_and_transaction
45
super(DjangoStoreTests, self).setUp()
46
conf.settings.configure(STORM_STORES={})
49
if django.VERSION >= (1, 1):
50
if django.VERSION >= (1, 6):
51
from django.utils.functional import empty
54
conf.settings._wrapped = empty
56
conf.settings._target = None
57
# Reset the utility to cleanup the StoreSynchronizer's from the
59
global_zstorm._reset()
60
stores.have_configured_stores = False
61
# Free the transaction to avoid having errors that cross
63
transaction.manager.free(transaction.get())
64
super(DjangoStoreTests, self).tearDown()
66
def test_configure_stores_configures_store_uris(self):
67
conf.settings.MIDDLEWARE_CLASSES += (
68
"storm.django.middleware.ZopeTransactionMiddleware",)
69
conf.settings.STORM_STORES = {"name1": "sqlite:1",
72
stores.configure_stores(conf.settings)
73
default_uris = global_zstorm.get_default_uris()
74
self.assertEquals(default_uris, {"name1": "sqlite:1",
78
def test_get_store(self):
79
conf.settings.MIDDLEWARE_CLASSES += (
80
"storm.django.middleware.ZopeTransactionMiddleware",)
81
conf.settings.STORM_STORES = {"name": "sqlite:"}
82
store = stores.get_store("name")
83
self.assertTrue(isinstance(store, Store))
84
# Calling get_store() twice returns the same store.
85
store2 = stores.get_store("name")
86
self.assertTrue(store is store2)
88
def test_get_store_returns_per_thread_stores(self):
89
conf.settings.MIDDLEWARE_CLASSES += (
90
"storm.django.middleware.ZopeTransactionMiddleware",)
91
conf.settings.STORM_STORES = {"name": "sqlite:"}
93
store = stores.get_store("name")
96
other_stores.append(stores.get_store("name"))
98
thread = threading.Thread(target=f)
101
self.assertEqual(len(other_stores), 1)
102
self.assertNotEqual(other_stores[0], store)
104
def test_get_store_uri(self):
105
conf.settings.MIDDLEWARE_CLASSES += (
106
"storm.django.middleware.ZopeTransactionMiddleware",)
107
conf.settings.STORM_STORES = {"name": "sqlite:"}
109
self.assertEqual(stores.get_store_uri("name"), "sqlite:")