~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-07-02 07:06:39 UTC
  • mfrom: (2553.2.14 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20070702070639-um9oyfoc2i6g8umv
(robertc) Reduce duplication in interface based testing by extracting a new class TestScenarioApplier.

Show diffs side-by-side

added added

removed removed

Lines of Context:
114
114
            def get_test_permutations(self):
115
115
                return sample_permutation
116
116
        sample_permutation = [(1,2), (3,4)]
117
 
        from bzrlib.transport import TransportTestProviderAdapter
 
117
        from bzrlib.tests.test_transport_implementations \
 
118
            import TransportTestProviderAdapter
118
119
        adapter = TransportTestProviderAdapter()
119
120
        self.assertEqual(sample_permutation,
120
121
                         adapter.get_transport_test_permutations(MockModule()))
125
126
        # - we assume if this matches its probably doing the right thing
126
127
        # especially in combination with the tests for setting the right
127
128
        # classes below.
128
 
        from bzrlib.transport import (TransportTestProviderAdapter,
129
 
                                      _get_transport_modules
130
 
                                      )
 
129
        from bzrlib.tests.test_transport_implementations \
 
130
            import TransportTestProviderAdapter
 
131
        from bzrlib.transport import _get_transport_modules
131
132
        modules = _get_transport_modules()
132
133
        permutation_count = 0
133
134
        for module in modules:
150
151
        # This test used to know about all the possible transports and the
151
152
        # order they were returned but that seems overly brittle (mbp
152
153
        # 20060307)
153
 
        input_test = TestTransportProviderAdapter(
154
 
            "test_adapter_sets_transport_class")
155
 
        from bzrlib.transport import TransportTestProviderAdapter
156
 
        suite = TransportTestProviderAdapter().adapt(input_test)
157
 
        tests = list(iter(suite))
158
 
        self.assertTrue(len(tests) > 6)
 
154
        from bzrlib.tests.test_transport_implementations \
 
155
            import TransportTestProviderAdapter
 
156
        scenarios = TransportTestProviderAdapter().scenarios
159
157
        # there are at least that many builtin transports
160
 
        one_test = tests[0]
161
 
        self.assertTrue(issubclass(one_test.transport_class, 
 
158
        self.assertTrue(len(scenarios) > 6)
 
159
        one_scenario = scenarios[0]
 
160
        self.assertIsInstance(one_scenario[0], str)
 
161
        self.assertTrue(issubclass(one_scenario[1]["transport_class"],
162
162
                                   bzrlib.transport.Transport))
163
 
        self.assertTrue(issubclass(one_test.transport_server, 
 
163
        self.assertTrue(issubclass(one_scenario[1]["transport_server"],
164
164
                                   bzrlib.transport.Server))
165
165
 
166
166
 
167
167
class TestBranchProviderAdapter(TestCase):
168
168
    """A group of tests that test the branch implementation test adapter."""
169
169
 
170
 
    def test_adapted_tests(self):
 
170
    def test_constructor(self):
171
171
        # check that constructor parameters are passed through to the adapted
172
172
        # test.
173
 
        from bzrlib.branch import BranchTestProviderAdapter
174
 
        input_test = TestBranchProviderAdapter(
175
 
            "test_adapted_tests")
 
173
        from bzrlib.tests.branch_implementations import BranchTestProviderAdapter
176
174
        server1 = "a"
177
175
        server2 = "b"
178
176
        formats = [("c", "C"), ("d", "D")]
179
177
        adapter = BranchTestProviderAdapter(server1, server2, formats)
180
 
        suite = adapter.adapt(input_test)
181
 
        tests = list(iter(suite))
182
 
        self.assertEqual(2, len(tests))
183
 
        self.assertEqual(tests[0].branch_format, formats[0][0])
184
 
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
185
 
        self.assertEqual(tests[0].transport_server, server1)
186
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
187
 
        self.assertEqual(tests[1].branch_format, formats[1][0])
188
 
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
189
 
        self.assertEqual(tests[1].transport_server, server1)
190
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
178
        self.assertEqual(2, len(adapter.scenarios))
 
179
        self.assertEqual([
 
180
            ('str',
 
181
             {'branch_format': 'c',
 
182
              'bzrdir_format': 'C',
 
183
              'transport_readonly_server': 'b',
 
184
              'transport_server': 'a'}),
 
185
            ('str',
 
186
             {'branch_format': 'd',
 
187
              'bzrdir_format': 'D',
 
188
              'transport_readonly_server': 'b',
 
189
              'transport_server': 'a'})],
 
190
            adapter.scenarios)
191
191
 
192
192
 
193
193
class TestBzrDirProviderAdapter(TestCase):
196
196
    def test_adapted_tests(self):
197
197
        # check that constructor parameters are passed through to the adapted
198
198
        # test.
199
 
        from bzrlib.bzrdir import BzrDirTestProviderAdapter
200
 
        input_test = TestBzrDirProviderAdapter(
201
 
            "test_adapted_tests")
 
199
        from bzrlib.tests.bzrdir_implementations import BzrDirTestProviderAdapter
202
200
        vfs_factory = "v"
203
201
        server1 = "a"
204
202
        server2 = "b"
205
203
        formats = ["c", "d"]
206
204
        adapter = BzrDirTestProviderAdapter(vfs_factory,
207
205
            server1, server2, formats)
208
 
        suite = adapter.adapt(input_test)
209
 
        tests = list(iter(suite))
210
 
        self.assertEqual(2, len(tests))
211
 
        self.assertEqual(tests[0].bzrdir_format, formats[0])
212
 
        self.assertEqual(tests[0].vfs_transport_factory, vfs_factory)
213
 
        self.assertEqual(tests[0].transport_server, server1)
214
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
215
 
        self.assertEqual(tests[1].bzrdir_format, formats[1])
216
 
        self.assertEqual(tests[1].vfs_transport_factory, vfs_factory)
217
 
        self.assertEqual(tests[1].transport_server, server1)
218
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
206
        self.assertEqual([
 
207
            ('str',
 
208
             {'bzrdir_format': 'c',
 
209
              'transport_readonly_server': 'b',
 
210
              'transport_server': 'a',
 
211
              'vfs_transport_factory': 'v'}),
 
212
            ('str',
 
213
             {'bzrdir_format': 'd',
 
214
              'transport_readonly_server': 'b',
 
215
              'transport_server': 'a',
 
216
              'vfs_transport_factory': 'v'})],
 
217
            adapter.scenarios)
219
218
 
220
219
 
221
220
class TestRepositoryProviderAdapter(TestCase):
222
221
    """A group of tests that test the repository implementation test adapter."""
223
222
 
224
 
    def test_adapted_tests(self):
225
 
        # check that constructor parameters are passed through to the adapted
226
 
        # test.
227
 
        from bzrlib.repository import RepositoryTestProviderAdapter
228
 
        input_test = TestRepositoryProviderAdapter(
229
 
            "test_adapted_tests")
 
223
    def test_constructor(self):
 
224
        # check that constructor parameters are passed through to the
 
225
        # scenarios.
 
226
        from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
230
227
        server1 = "a"
231
228
        server2 = "b"
232
229
        formats = [("c", "C"), ("d", "D")]
233
230
        adapter = RepositoryTestProviderAdapter(server1, server2, formats)
234
 
        suite = adapter.adapt(input_test)
235
 
        tests = list(iter(suite))
236
 
        self.assertEqual(2, len(tests))
237
 
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
238
 
        self.assertEqual(tests[0].repository_format, formats[0][0])
239
 
        self.assertEqual(tests[0].transport_server, server1)
240
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
241
 
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
242
 
        self.assertEqual(tests[1].repository_format, formats[1][0])
243
 
        self.assertEqual(tests[1].transport_server, server1)
244
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
231
        self.assertEqual([
 
232
            ('str',
 
233
             {'bzrdir_format': 'C',
 
234
              'repository_format': 'c',
 
235
              'transport_readonly_server': 'b',
 
236
              'transport_server': 'a'}),
 
237
            ('str',
 
238
             {'bzrdir_format': 'D',
 
239
              'repository_format': 'd',
 
240
              'transport_readonly_server': 'b',
 
241
              'transport_server': 'a'})],
 
242
            adapter.scenarios)
245
243
 
246
244
    def test_setting_vfs_transport(self):
247
245
        """The vfs_transport_factory can be set optionally."""
248
 
        from bzrlib.repository import RepositoryTestProviderAdapter
249
 
        input_test = TestRepositoryProviderAdapter(
250
 
            "test_adapted_tests")
251
 
        formats = [("c", "C")]
 
246
        from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
 
247
        formats = [("a", "b"), ("c", "d")]
252
248
        adapter = RepositoryTestProviderAdapter(None, None, formats,
253
249
            vfs_transport_factory="vfs")
254
 
        suite = adapter.adapt(input_test)
255
 
        tests = list(iter(suite))
256
 
        self.assertEqual(1, len(tests))
257
 
        self.assertEqual(tests[0].vfs_transport_factory, "vfs")
 
250
        self.assertEqual([
 
251
            ('str',
 
252
             {'bzrdir_format': 'b',
 
253
              'repository_format': 'a',
 
254
              'transport_readonly_server': None,
 
255
              'transport_server': None,
 
256
              'vfs_transport_factory': 'vfs'}),
 
257
            ('str',
 
258
             {'bzrdir_format': 'd',
 
259
              'repository_format': 'c',
 
260
              'transport_readonly_server': None,
 
261
              'transport_server': None,
 
262
              'vfs_transport_factory': 'vfs'})],
 
263
            adapter.scenarios)
 
264
 
 
265
    def test_formats_to_scenarios(self):
 
266
        """The adapter can generate all the scenarios needed."""
 
267
        from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
 
268
        no_vfs_adapter = RepositoryTestProviderAdapter("server", "readonly",
 
269
            [], None)
 
270
        vfs_adapter = RepositoryTestProviderAdapter("server", "readonly",
 
271
            [], vfs_transport_factory="vfs")
 
272
        # no_vfs generate scenarios without vfs_transport_factor
 
273
        formats = [("c", "C"), (1, "D")]
 
274
        self.assertEqual([
 
275
            ('str',
 
276
             {'bzrdir_format': 'C',
 
277
              'repository_format': 'c',
 
278
              'transport_readonly_server': 'readonly',
 
279
              'transport_server': 'server'}),
 
280
            ('int',
 
281
             {'bzrdir_format': 'D',
 
282
              'repository_format': 1,
 
283
              'transport_readonly_server': 'readonly',
 
284
              'transport_server': 'server'})],
 
285
            no_vfs_adapter.formats_to_scenarios(formats))
 
286
        self.assertEqual([
 
287
            ('str',
 
288
             {'bzrdir_format': 'C',
 
289
              'repository_format': 'c',
 
290
              'transport_readonly_server': 'readonly',
 
291
              'transport_server': 'server',
 
292
              'vfs_transport_factory': 'vfs'}),
 
293
            ('int',
 
294
             {'bzrdir_format': 'D',
 
295
              'repository_format': 1,
 
296
              'transport_readonly_server': 'readonly',
 
297
              'transport_server': 'server',
 
298
              'vfs_transport_factory': 'vfs'})],
 
299
            vfs_adapter.formats_to_scenarios(formats))
 
300
 
 
301
 
 
302
class TestTestScenarioApplier(TestCase):
 
303
    """Tests for the test adaption facilities."""
 
304
 
 
305
    def test_adapt_applies_scenarios(self):
 
306
        from bzrlib.tests.repository_implementations import TestScenarioApplier
 
307
        input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
 
308
        adapter = TestScenarioApplier()
 
309
        adapter.scenarios = [("1", "dict"), ("2", "settings")]
 
310
        calls = []
 
311
        def capture_call(test, scenario):
 
312
            calls.append((test, scenario))
 
313
            return test
 
314
        adapter.adapt_test_to_scenario = capture_call
 
315
        adapter.adapt(input_test)
 
316
        self.assertEqual([(input_test, ("1", "dict")),
 
317
            (input_test, ("2", "settings"))], calls)
 
318
 
 
319
    def test_adapt_test_to_scenario(self):
 
320
        from bzrlib.tests.repository_implementations import TestScenarioApplier
 
321
        input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
 
322
        adapter = TestScenarioApplier()
 
323
        # setup two adapted tests
 
324
        adapted_test1 = adapter.adapt_test_to_scenario(input_test,
 
325
            ("new id",
 
326
            {"bzrdir_format":"bzr_format",
 
327
             "repository_format":"repo_fmt",
 
328
             "transport_server":"transport_server",
 
329
             "transport_readonly_server":"readonly-server"}))
 
330
        adapted_test2 = adapter.adapt_test_to_scenario(input_test,
 
331
            ("new id 2", {"bzrdir_format":None}))
 
332
        # input_test should have been altered.
 
333
        self.assertRaises(AttributeError, getattr, input_test, "bzrdir_format")
 
334
        # the new tests are mutually incompatible, ensuring it has 
 
335
        # made new ones, and unspecified elements in the scenario
 
336
        # should not have been altered.
 
337
        self.assertEqual("bzr_format", adapted_test1.bzrdir_format)
 
338
        self.assertEqual("repo_fmt", adapted_test1.repository_format)
 
339
        self.assertEqual("transport_server", adapted_test1.transport_server)
 
340
        self.assertEqual("readonly-server",
 
341
            adapted_test1.transport_readonly_server)
 
342
        self.assertEqual(
 
343
            "bzrlib.tests.test_selftest.TestTestScenarioApplier."
 
344
            "test_adapt_test_to_scenario(new id)",
 
345
            adapted_test1.id())
 
346
        self.assertEqual(None, adapted_test2.bzrdir_format)
 
347
        self.assertEqual(
 
348
            "bzrlib.tests.test_selftest.TestTestScenarioApplier."
 
349
            "test_adapt_test_to_scenario(new id 2)",
 
350
            adapted_test2.id())
258
351
 
259
352
 
260
353
class TestInterRepositoryProviderAdapter(TestCase):
263
356
    def test_adapted_tests(self):
264
357
        # check that constructor parameters are passed through to the adapted
265
358
        # test.
266
 
        from bzrlib.repository import InterRepositoryTestProviderAdapter
267
 
        input_test = TestInterRepositoryProviderAdapter(
268
 
            "test_adapted_tests")
 
359
        from bzrlib.tests.interrepository_implementations import \
 
360
            InterRepositoryTestProviderAdapter
269
361
        server1 = "a"
270
362
        server2 = "b"
271
363
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
272
364
        adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
273
 
        suite = adapter.adapt(input_test)
274
 
        tests = list(iter(suite))
275
 
        self.assertEqual(2, len(tests))
276
 
        self.assertEqual(tests[0].interrepo_class, formats[0][0])
277
 
        self.assertEqual(tests[0].repository_format, formats[0][1])
278
 
        self.assertEqual(tests[0].repository_format_to, formats[0][2])
279
 
        self.assertEqual(tests[0].transport_server, server1)
280
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
281
 
        self.assertEqual(tests[1].interrepo_class, formats[1][0])
282
 
        self.assertEqual(tests[1].repository_format, formats[1][1])
283
 
        self.assertEqual(tests[1].repository_format_to, formats[1][2])
284
 
        self.assertEqual(tests[1].transport_server, server1)
285
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
365
        self.assertEqual([
 
366
            ('str',
 
367
             {'interrepo_class': str,
 
368
              'repository_format': 'C1',
 
369
              'repository_format_to': 'C2',
 
370
              'transport_readonly_server': 'b',
 
371
              'transport_server': 'a'}),
 
372
            ('int',
 
373
             {'interrepo_class': int,
 
374
              'repository_format': 'D1',
 
375
              'repository_format_to': 'D2',
 
376
              'transport_readonly_server': 'b',
 
377
              'transport_server': 'a'})],
 
378
            adapter.formats_to_scenarios(formats))
286
379
 
287
380
 
288
381
class TestInterVersionedFileProviderAdapter(TestCase):
289
382
    """A group of tests that test the InterVersionedFile test adapter."""
290
383
 
291
 
    def test_adapted_tests(self):
 
384
    def test_scenarios(self):
292
385
        # check that constructor parameters are passed through to the adapted
293
386
        # test.
294
 
        from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
295
 
        input_test = TestInterRepositoryProviderAdapter(
296
 
            "test_adapted_tests")
 
387
        from bzrlib.tests.interversionedfile_implementations \
 
388
            import InterVersionedFileTestProviderAdapter
297
389
        server1 = "a"
298
390
        server2 = "b"
299
391
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
300
392
        adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
301
 
        suite = adapter.adapt(input_test)
302
 
        tests = list(iter(suite))
303
 
        self.assertEqual(2, len(tests))
304
 
        self.assertEqual(tests[0].interversionedfile_class, formats[0][0])
305
 
        self.assertEqual(tests[0].versionedfile_factory, formats[0][1])
306
 
        self.assertEqual(tests[0].versionedfile_factory_to, formats[0][2])
307
 
        self.assertEqual(tests[0].transport_server, server1)
308
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
309
 
        self.assertEqual(tests[1].interversionedfile_class, formats[1][0])
310
 
        self.assertEqual(tests[1].versionedfile_factory, formats[1][1])
311
 
        self.assertEqual(tests[1].versionedfile_factory_to, formats[1][2])
312
 
        self.assertEqual(tests[1].transport_server, server1)
313
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
393
        self.assertEqual([
 
394
            ('str',
 
395
             {'interversionedfile_class':str,
 
396
              'transport_readonly_server': 'b',
 
397
              'transport_server': 'a',
 
398
              'versionedfile_factory': 'C1',
 
399
              'versionedfile_factory_to': 'C2'}),
 
400
            ('int',
 
401
             {'interversionedfile_class': int,
 
402
              'transport_readonly_server': 'b',
 
403
              'transport_server': 'a',
 
404
              'versionedfile_factory': 'D1',
 
405
              'versionedfile_factory_to': 'D2'})],
 
406
            adapter.scenarios)
314
407
 
315
408
 
316
409
class TestRevisionStoreProviderAdapter(TestCase):
317
410
    """A group of tests that test the RevisionStore test adapter."""
318
411
 
319
 
    def test_adapted_tests(self):
 
412
    def test_scenarios(self):
320
413
        # check that constructor parameters are passed through to the adapted
321
414
        # test.
322
 
        from bzrlib.store.revision import RevisionStoreTestProviderAdapter
323
 
        input_test = TestRevisionStoreProviderAdapter(
324
 
            "test_adapted_tests")
 
415
        from bzrlib.tests.revisionstore_implementations \
 
416
            import RevisionStoreTestProviderAdapter
325
417
        # revision stores need a store factory - i.e. RevisionKnit
326
418
        #, a readonly and rw transport 
327
419
        # transport servers:
329
421
        server2 = "b"
330
422
        store_factories = ["c", "d"]
331
423
        adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
332
 
        suite = adapter.adapt(input_test)
333
 
        tests = list(iter(suite))
334
 
        self.assertEqual(2, len(tests))
335
 
        self.assertEqual(tests[0].store_factory, store_factories[0][0])
336
 
        self.assertEqual(tests[0].transport_server, server1)
337
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
338
 
        self.assertEqual(tests[1].store_factory, store_factories[1][0])
339
 
        self.assertEqual(tests[1].transport_server, server1)
340
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
424
        self.assertEqual([
 
425
            ('c',
 
426
             {'store_factory': 'c',
 
427
              'transport_readonly_server': 'b',
 
428
              'transport_server': 'a'}),
 
429
            ('d',
 
430
             {'store_factory': 'd',
 
431
              'transport_readonly_server': 'b',
 
432
              'transport_server': 'a'})],
 
433
            adapter.scenarios)
341
434
 
342
435
 
343
436
class TestWorkingTreeProviderAdapter(TestCase):
344
437
    """A group of tests that test the workingtree implementation test adapter."""
345
438
 
346
 
    def test_adapted_tests(self):
 
439
    def test_scenarios(self):
347
440
        # check that constructor parameters are passed through to the adapted
348
441
        # test.
349
 
        from bzrlib.workingtree import WorkingTreeTestProviderAdapter
350
 
        input_test = TestWorkingTreeProviderAdapter(
351
 
            "test_adapted_tests")
 
442
        from bzrlib.tests.workingtree_implementations \
 
443
            import WorkingTreeTestProviderAdapter
352
444
        server1 = "a"
353
445
        server2 = "b"
354
446
        formats = [("c", "C"), ("d", "D")]
355
447
        adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
356
 
        suite = adapter.adapt(input_test)
357
 
        tests = list(iter(suite))
358
 
        self.assertEqual(2, len(tests))
359
 
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
360
 
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
361
 
        self.assertEqual(tests[0].transport_server, server1)
362
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
363
 
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
364
 
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
365
 
        self.assertEqual(tests[1].transport_server, server1)
366
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
 
448
        self.assertEqual([
 
449
            ('str',
 
450
             {'bzrdir_format': 'C',
 
451
              'transport_readonly_server': 'b',
 
452
              'transport_server': 'a',
 
453
              'workingtree_format': 'c'}),
 
454
            ('str',
 
455
             {'bzrdir_format': 'D',
 
456
              'transport_readonly_server': 'b',
 
457
              'transport_server': 'a',
 
458
              'workingtree_format': 'd'})],
 
459
            adapter.scenarios)
367
460
 
368
461
 
369
462
class TestTreeProviderAdapter(TestCase):