~free.ekanayaka/storm/any-expr

« back to all changes in this revision

Viewing changes to tests/django/backend.py

  • Committer: Thomas Hervé
  • Date: 2008-09-02 11:45:25 UTC
  • mfrom: (262.1.3 262607-join-none)
  • Revision ID: thomas@canonical.com-20080902114525-82j6grtlc71vwb8b
Merge 262607-join-none [f=262607] [r=niemeyer,jamesh]

Make LEFT JOIN works with empty objects in the joined tables and constraints
on the keys (like allow_none=False).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# Copyright (c) 2008 Canonical
3
 
#
4
 
# Written by James Henstridge <jamesh@canonical.com>
5
 
#
6
 
# This file is part of Storm Object Relational Mapper.
7
 
#
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.
12
 
#
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.
17
 
#
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/>.
20
 
#
21
 
 
22
 
import os
23
 
 
24
 
try:
25
 
    from django.conf import settings
26
 
    import transaction
27
 
except ImportError:
28
 
    have_django_and_transaction = False
29
 
else:
30
 
    have_django_and_transaction = True
31
 
    from storm.django import stores
32
 
    from storm.zope.zstorm import global_zstorm, StoreDataManager
33
 
 
34
 
from tests.helper import TestHelper
35
 
 
36
 
 
37
 
class DjangoBackendTests(object):
38
 
 
39
 
    def is_supported(self):
40
 
        return have_django_and_transaction and self.get_store_uri() is not None
41
 
 
42
 
    def setUp(self):
43
 
        super(DjangoBackendTests, self).setUp()
44
 
        settings.configure(STORM_STORES={})
45
 
        settings.MIDDLEWARE_CLASSES += (
46
 
            "storm.django.middleware.ZopeTransactionMiddleware",)
47
 
 
48
 
        settings.DATABASE_ENGINE = "storm.django.backend"
49
 
        settings.DATABASE_NAME = "django"
50
 
        settings.STORM_STORES["django"] = self.get_store_uri()
51
 
        stores.have_configured_stores = False
52
 
        self.create_tables()
53
 
 
54
 
    def tearDown(self):
55
 
        transaction.abort()
56
 
        self.drop_tables()
57
 
        settings._target = None
58
 
        global_zstorm._reset()
59
 
        stores.have_configured_stores = False
60
 
        transaction.manager.free(transaction.get())
61
 
        super(DjangoBackendTests, self).tearDown()
62
 
 
63
 
    def get_store_uri(self):
64
 
        raise NotImplementedError
65
 
 
66
 
    def get_wrapper_class(self):
67
 
        raise NotImplementedError
68
 
 
69
 
    def create_tables(self):
70
 
        raise NotImplementedError
71
 
 
72
 
    def drop_tables(self):
73
 
        raise NotImplementedError
74
 
 
75
 
    def test_create_wrapper(self):
76
 
        from storm.django.backend import base
77
 
        wrapper = base.DatabaseWrapper(**settings.DATABASE_OPTIONS)
78
 
        self.assertTrue(isinstance(wrapper, self.get_wrapper_class()))
79
 
 
80
 
        # The wrapper uses the same database connection as the store.
81
 
        store = stores.get_store("django")
82
 
        self.assertEqual(store._connection._raw_connection, wrapper.connection)
83
 
 
84
 
    def _isInTransaction(self, store):
85
 
        """Check if a Store is part of the current transaction."""
86
 
        for dm in transaction.get()._resources:
87
 
            if isinstance(dm, StoreDataManager) and dm._store is store:
88
 
                return True
89
 
        return False
90
 
 
91
 
    def assertInTransaction(self, store):
92
 
        """Check that the given store is joined to the transaction."""
93
 
        self.assertTrue(self._isInTransaction(store),
94
 
                        "%r should be joined to the transaction" % store)
95
 
 
96
 
    def test_using_wrapper_joins_transaction(self):
97
 
        from storm.django.backend import base
98
 
        wrapper = base.DatabaseWrapper(**settings.DATABASE_OPTIONS)
99
 
        cursor = wrapper.cursor()
100
 
        cursor.execute("SELECT 1")
101
 
        self.assertInTransaction(stores.get_store("django"))
102
 
 
103
 
    def test_commit(self):
104
 
        from storm.django.backend import base
105
 
        wrapper = base.DatabaseWrapper(**settings.DATABASE_OPTIONS)
106
 
        cursor = wrapper.cursor()
107
 
        cursor.execute("INSERT INTO django_test (title) VALUES ('foo')")
108
 
        wrapper._commit()
109
 
 
110
 
        cursor = wrapper.cursor()
111
 
        cursor.execute("SELECT title FROM django_test")
112
 
        result = cursor.fetchall()
113
 
        self.assertEqual(len(result), 1)
114
 
        self.assertEqual(result[0][0], "foo")
115
 
 
116
 
    def test_rollback(self):
117
 
        from storm.django.backend import base
118
 
        wrapper = base.DatabaseWrapper(**settings.DATABASE_OPTIONS)
119
 
        cursor = wrapper.cursor()
120
 
        cursor.execute("INSERT INTO django_test (title) VALUES ('foo')")
121
 
        wrapper._rollback()
122
 
 
123
 
        cursor = wrapper.cursor()
124
 
        cursor.execute("SELECT title FROM django_test")
125
 
        result = cursor.fetchall()
126
 
        self.assertEqual(len(result), 0)
127
 
 
128
 
 
129
 
class PostgresDjangoBackendTests(DjangoBackendTests, TestHelper):
130
 
 
131
 
    def get_store_uri(self):
132
 
        return os.environ.get("STORM_POSTGRES_URI")
133
 
 
134
 
    def get_wrapper_class(self):
135
 
        from storm.django.backend import base
136
 
        return base.PostgresStormDatabaseWrapper
137
 
 
138
 
    def create_tables(self):
139
 
        store = stores.get_store("django")
140
 
        store.execute("CREATE TABLE django_test ("
141
 
                      "  id SERIAL PRIMARY KEY,"
142
 
                      "  title TEXT)")
143
 
        transaction.commit()
144
 
 
145
 
    def drop_tables(self):
146
 
        store = stores.get_store("django")
147
 
        store.execute("DROP TABLE django_test")
148
 
        transaction.commit()
149
 
 
150
 
 
151
 
class MySQLDjangoBackendTests(DjangoBackendTests, TestHelper):
152
 
 
153
 
    def get_store_uri(self):
154
 
        return os.environ.get("STORM_MYSQL_URI")
155
 
 
156
 
    def get_wrapper_class(self):
157
 
        from storm.django.backend import base
158
 
        return base.MySQLStormDatabaseWrapper
159
 
 
160
 
    def create_tables(self):
161
 
        store = stores.get_store("django")
162
 
        store.execute("CREATE TABLE django_test ("
163
 
                      "  id INT AUTO_INCREMENT PRIMARY KEY,"
164
 
                      "  title TEXT) ENGINE=InnoDB")
165
 
        transaction.commit()
166
 
 
167
 
    def drop_tables(self):
168
 
        store = stores.get_store("django")
169
 
        store.execute("DROP TABLE django_test")
170
 
        transaction.commit()