~jkakar/storm/documentation

« back to all changes in this revision

Viewing changes to tests/zope/testing.py

  • Committer: Jamu Kakar
  • Date: 2011-06-11 22:23:33 UTC
  • mfrom: (370.1.23 trunk)
  • Revision ID: jkakar@kakar.ca-20110611222333-ywb4uqqi581oiylq
- Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
        create = ["CREATE TABLE test (foo TEXT UNIQUE, bar INT)"]
57
57
        drop = ["DROP TABLE test"]
58
58
        delete = ["DELETE FROM test"]
 
59
        uri = "sqlite:///%s" % self.makeFile()
59
60
        schema = ZSchema(create, drop, delete, patch_package)
60
 
        uri = "sqlite:///%s" % self.makeFile()
61
 
        self.resource = ZStormResourceManager({"test": (uri, schema)})
 
61
        self.databases = [{"name": "test", "uri": uri, "schema": schema}]
 
62
        self.resource = ZStormResourceManager(self.databases)
62
63
        self.store = Store(create_database(uri))
63
64
 
64
65
    def tearDown(self):
108
109
        L{ZStormResourceManager.clean} tries to flush the stores to make sure
109
110
        that they are all in a consistent state.
110
111
        """
111
 
 
112
112
        class Test(object):
113
113
            __storm_table__ = "test"
114
114
            foo = Unicode()
135
135
        store.commit()
136
136
        self.resource.clean(zstorm)
137
137
        self.assertEqual([], list(self.store.execute("SELECT * FROM test")))
 
138
 
 
139
    def test_clean_with_force_delete(self):
 
140
        """
 
141
        If L{ZStormResourceManager.force_delete} is C{True}, L{Schema.delete}
 
142
        is always invoked upon test cleanup.
 
143
        """
 
144
        zstorm = self.resource.make([])
 
145
        self.store.execute("INSERT INTO test (foo, bar) VALUES ('data', 123)")
 
146
        self.store.commit()
 
147
        self.resource.force_delete = True
 
148
        self.resource.clean(zstorm)
 
149
        self.assertEqual([], list(self.store.execute("SELECT * FROM test")))
 
150
 
 
151
    def test_wb_clean_clears_alive_cache_before_abort(self):
 
152
        """
 
153
        L{ZStormResourceManager.clean} clears the alive cache before
 
154
        aborting the transaction.
 
155
        """
 
156
        class Test(object):
 
157
            __storm_table__ = "test"
 
158
            bar = Int(primary=True)
 
159
 
 
160
            def __init__(self, bar):
 
161
                self.bar = bar
 
162
 
 
163
        zstorm = self.resource.make([])
 
164
        store = zstorm.get("test")
 
165
        store.add(Test(1))
 
166
        store.add(Test(2))
 
167
        real_invalidate = store.invalidate
 
168
 
 
169
        def invalidate_proxy():
 
170
            self.assertEqual(0, len(store._alive.values()))
 
171
            real_invalidate()
 
172
        store.invalidate = invalidate_proxy
 
173
 
 
174
        self.resource.clean(zstorm)
 
175
 
 
176
    def test_schema_uri(self):
 
177
        """
 
178
        It's possible to specify an alternate URI for applying the schema
 
179
        and cleaning up tables after a test.
 
180
        """
 
181
        schema_uri = "sqlite:///%s" % self.makeFile()
 
182
        self.databases[0]["schema-uri"] = schema_uri
 
183
        zstorm = self.resource.make([])
 
184
        store = zstorm.get("test")
 
185
        schema_store = Store(create_database(schema_uri))
 
186
 
 
187
        # The schema was applied using the alternate schema URI
 
188
        statement = "SELECT name FROM sqlite_master WHERE name='patch'"
 
189
        self.assertEqual([], list(store.execute(statement)))
 
190
        self.assertEqual([("patch",)], list(schema_store.execute(statement)))
 
191
 
 
192
        # The cleanup is performed with the alternate schema URI
 
193
        store.commit()
 
194
        schema_store.execute("INSERT INTO test (foo) VALUES ('data')")
 
195
        schema_store.commit()
 
196
        self.resource.clean(zstorm)
 
197
        self.assertEqual([], list(schema_store.execute("SELECT * FROM test")))
 
198
 
 
199
    def test_deprecated_database_format(self):
 
200
        """
 
201
        The old deprecated format of the 'database' constructor parameter is
 
202
        still supported.
 
203
        """
 
204
        import patch_package
 
205
        uri = "sqlite:///%s" % self.makeFile()
 
206
        schema = ZSchema([], [], [], patch_package)
 
207
        resource = ZStormResourceManager({"test": (uri, schema)})
 
208
        zstorm = resource.make([])
 
209
        store = zstorm.get("test")
 
210
        self.assertIsNot(None, store)