summaryrefslogtreecommitdiff
path: root/lib/testresources/tests/test_test_resource.py
blob: 02524e733dfa204856611ec11a4545c0bc52c854 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#  testresources: extensions to python unittest to allow declaritive use
#  of resources by test cases.
#
#  Copyright (c) 2005-2010 Testresources Contributors
#  
#  Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
#  license at the users choice. A copy of both licenses are available in the
#  project source as Apache-2.0 and BSD. You may not use this file except in
#  compliance with one of these two licences.
#  
#  Unless required by applicable law or agreed to in writing, software distributed
#  under these licenses is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
#  CONDITIONS OF ANY KIND, either express or implied.  See the license you chose
#  for the specific language governing permissions and limitations under that
#  license.
#

from fixtures.tests.helpers import LoggingFixture
import testtools

import testresources
from testresources.tests import (
    ResultWithResourceExtensions,
    ResultWithoutResourceExtensions,
    )


def test_suite():
    loader = testresources.tests.TestUtil.TestLoader()
    result = loader.loadTestsFromName(__name__)
    return result


class MockResourceInstance(object):

    def __init__(self, name):
        self._name = name

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

    def __cmp__(self, other):
        return cmp(self.__dict__, other.__dict__)

    def __repr__(self):
        return self._name


class MockResource(testresources.TestResource):
    """Mock resource that logs the number of make and clean calls."""

    def __init__(self):
        testresources.TestResource.__init__(self)
        self.makes = 0
        self.cleans = 0

    def clean(self, resource):
        self.cleans += 1

    def make(self, dependency_resources):
        self.makes += 1
        return MockResourceInstance("Boo!")


class MockResettableResource(MockResource):
    """Mock resource that logs the number of reset calls too."""

    def __init__(self):
        MockResource.__init__(self)
        self.resets = 0

    def reset(self, resource, result):
        self.resets += 1
        resource._name += "!"
        return resource


class TestTestResource(testtools.TestCase):

    def testUnimplementedGetResource(self):
        # By default, TestResource raises NotImplementedError on getResource
        # because make is not defined initially.
        resource_manager = testresources.TestResource()
        self.assertRaises(NotImplementedError, resource_manager.getResource)

    def testInitiallyNotDirty(self):
        resource_manager = testresources.TestResource()
        self.assertEqual(False, resource_manager._dirty)

    def testInitiallyUnused(self):
        resource_manager = testresources.TestResource()
        self.assertEqual(0, resource_manager._uses)

    def testInitiallyNoCurrentResource(self):
        resource_manager = testresources.TestResource()
        self.assertEqual(None, resource_manager._currentResource)

    def testneededResourcesDefault(self):
        # Calling neededResources on a default TestResource returns the
        # resource.
        resource = testresources.TestResource()
        self.assertEqual([resource], resource.neededResources())

    def testneededResourcesDependenciesFirst(self):
        # Calling neededResources on a TestResource with dependencies puts the
        # dependencies first.
        resource = testresources.TestResource()
        dep1 = testresources.TestResource()
        dep2 = testresources.TestResource()
        resource.resources.append(("dep1", dep1))
        resource.resources.append(("dep2", dep2))
        self.assertEqual([dep1, dep2, resource], resource.neededResources())

    def testneededResourcesClosure(self):
        # Calling neededResources on a TestResource with dependencies includes
        # the needed resources of the needed resources.
        resource = testresources.TestResource()
        dep1 = testresources.TestResource()
        dep2 = testresources.TestResource()
        resource.resources.append(("dep1", dep1))
        dep1.resources.append(("dep2", dep2))
        self.assertEqual([dep2, dep1, resource], resource.neededResources())

    def testDefaultCosts(self):
        # The base TestResource costs 1 to set up and to tear down.
        resource_manager = testresources.TestResource()
        self.assertEqual(resource_manager.setUpCost, 1)
        self.assertEqual(resource_manager.tearDownCost, 1)

    def testGetResourceReturnsMakeResource(self):
        resource_manager = MockResource()
        resource = resource_manager.getResource()
        self.assertEqual(resource_manager.make({}), resource)

    def testGetResourceIncrementsUses(self):
        resource_manager = MockResource()
        resource_manager.getResource()
        self.assertEqual(1, resource_manager._uses)
        resource_manager.getResource()
        self.assertEqual(2, resource_manager._uses)

    def testGetResourceDoesntDirty(self):
        resource_manager = MockResource()
        resource_manager.getResource()
        self.assertEqual(resource_manager._dirty, False)

    def testGetResourceSetsCurrentResource(self):
        resource_manager = MockResource()
        resource = resource_manager.getResource()
        self.assertIs(resource_manager._currentResource, resource)

    def testGetResourceTwiceReturnsIdenticalResource(self):
        resource_manager = MockResource()
        resource1 = resource_manager.getResource()
        resource2 = resource_manager.getResource()
        self.assertIs(resource1, resource2)

    def testGetResourceCallsMakeResource(self):
        resource_manager = MockResource()
        resource_manager.getResource()
        self.assertEqual(1, resource_manager.makes)

    def testIsDirty(self):
        resource_manager = MockResource()
        r = resource_manager.getResource()
        resource_manager.dirtied(r)
        self.assertTrue(resource_manager.isDirty())
        resource_manager.finishedWith(r)

    def testIsDirtyIsTrueIfDependenciesChanged(self):
        resource_manager = MockResource()
        dep1 = MockResource()
        dep2 = MockResource()
        dep3 = MockResource()
        resource_manager.resources.append(("dep1", dep1))
        resource_manager.resources.append(("dep2", dep2))
        resource_manager.resources.append(("dep3", dep3))
        r = resource_manager.getResource()
        dep2.dirtied(r.dep2)
        r2 =dep2.getResource()
        self.assertTrue(resource_manager.isDirty())
        resource_manager.finishedWith(r)
        dep2.finishedWith(r2)

    def testIsDirtyIsTrueIfDependenciesAreDirty(self):
        resource_manager = MockResource()
        dep1 = MockResource()
        dep2 = MockResource()
        dep3 = MockResource()
        resource_manager.resources.append(("dep1", dep1))
        resource_manager.resources.append(("dep2", dep2))
        resource_manager.resources.append(("dep3", dep3))
        r = resource_manager.getResource()
        dep2.dirtied(r.dep2)
        self.assertTrue(resource_manager.isDirty())
        resource_manager.finishedWith(r)

    def testRepeatedGetResourceCallsMakeResourceOnceOnly(self):
        resource_manager = MockResource()
        resource_manager.getResource()
        resource_manager.getResource()
        self.assertEqual(1, resource_manager.makes)

    def testGetResourceResetsUsedResource(self):
        resource_manager = MockResettableResource()
        resource_manager.getResource()
        resource = resource_manager.getResource()
        self.assertEqual(1, resource_manager.makes)
        resource_manager.dirtied(resource)
        resource_manager.getResource()
        self.assertEqual(1, resource_manager.makes)
        self.assertEqual(1, resource_manager.resets)
        resource_manager.finishedWith(resource)

    def testIsResetIfDependenciesAreDirty(self):
        resource_manager = MockResource()
        dep1 = MockResource()
        resource_manager.resources.append(("dep1", dep1))
        r = resource_manager.getResource()
        dep1.dirtied(r.dep1)
        # if we get the resource again, it should be clean
        r = resource_manager.getResource()
        self.assertFalse(resource_manager.isDirty())
        self.assertFalse(dep1.isDirty())

    def testUsedResourceResetBetweenUses(self):
        resource_manager = MockResettableResource()
        # take two refs; like happens with OptimisingTestSuite.
        resource_manager.getResource()
        resource = resource_manager.getResource()
        resource_manager.dirtied(resource)
        resource_manager.finishedWith(resource)
        # Get again, but its been dirtied.
        resource = resource_manager.getResource()
        resource_manager.finishedWith(resource)
        resource_manager.finishedWith(resource)
        # The resource is made once, reset once and cleaned once.
        self.assertEqual(1, resource_manager.makes)
        self.assertEqual(1, resource_manager.resets)
        self.assertEqual(1, resource_manager.cleans)

    def testFinishedWithDecrementsUses(self):
        resource_manager = MockResource()
        resource = resource_manager.getResource()
        resource = resource_manager.getResource()
        self.assertEqual(2, resource_manager._uses)
        resource_manager.finishedWith(resource)
        self.assertEqual(1, resource_manager._uses)
        resource_manager.finishedWith(resource)
        self.assertEqual(0, resource_manager._uses)

    def testFinishedWithResetsCurrentResource(self):
        resource_manager = MockResource()
        resource = resource_manager.getResource()
        resource_manager.finishedWith(resource)
        self.assertIs(None, resource_manager._currentResource)

    def testFinishedWithCallsCleanResource(self):
        resource_manager = MockResource()
        resource = resource_manager.getResource()
        resource_manager.finishedWith(resource)
        self.assertEqual(1, resource_manager.cleans)

    def testUsingTwiceMakesAndCleansTwice(self):
        resource_manager = MockResource()
        resource = resource_manager.getResource()
        resource_manager.finishedWith(resource)
        resource = resource_manager.getResource()
        resource_manager.finishedWith(resource)
        self.assertEqual(2, resource_manager.makes)
        self.assertEqual(2, resource_manager.cleans)

    def testFinishedWithCallsCleanResourceOnceOnly(self):
        resource_manager = MockResource()
        resource = resource_manager.getResource()
        resource = resource_manager.getResource()
        resource_manager.finishedWith(resource)
        self.assertEqual(0, resource_manager.cleans)
        resource_manager.finishedWith(resource)
        self.assertEqual(1, resource_manager.cleans)

    def testFinishedWithMarksNonDirty(self):
        resource_manager = MockResource()
        resource = resource_manager.getResource()
        resource_manager.dirtied(resource)
        resource_manager.finishedWith(resource)
        self.assertEqual(False, resource_manager._dirty)

    def testResourceAvailableBetweenFinishedWithCalls(self):
        resource_manager = MockResource()
        resource = resource_manager.getResource()
        resource = resource_manager.getResource()
        resource_manager.finishedWith(resource)
        self.assertIs(resource, resource_manager._currentResource)
        resource_manager.finishedWith(resource)

    # The default implementation of reset() performs a make/clean if
    # the dirty flag is set.
    def testDirtiedSetsDirty(self):
        resource_manager = MockResource()
        resource = resource_manager.getResource()
        self.assertEqual(False, resource_manager._dirty)
        resource_manager.dirtied(resource)
        self.assertEqual(True, resource_manager._dirty)

    def testDirtyingResourceTriggersCleanOnGet(self):
        resource_manager = MockResource()
        resource1 = resource_manager.getResource()
        resource2 = resource_manager.getResource()
        resource_manager.dirtied(resource2)
        resource_manager.finishedWith(resource2)
        self.assertEqual(0, resource_manager.cleans)
        resource3 = resource_manager.getResource()
        self.assertEqual(1, resource_manager.cleans)
        resource_manager.finishedWith(resource3)
        resource_manager.finishedWith(resource1)
        self.assertEqual(2, resource_manager.cleans)

    def testDefaultResetMethodPreservesCleanResource(self):
        resource_manager = MockResource()
        resource = resource_manager.getResource()
        self.assertEqual(1, resource_manager.makes)
        self.assertEqual(False, resource_manager._dirty)
        resource_manager.reset(resource)
        self.assertEqual(1, resource_manager.makes)
        self.assertEqual(0, resource_manager.cleans)

    def testDefaultResetMethodRecreatesDirtyResource(self):
        resource_manager = MockResource()
        resource = resource_manager.getResource()
        self.assertEqual(1, resource_manager.makes)
        resource_manager.dirtied(resource)
        resource_manager.reset(resource)
        self.assertEqual(2, resource_manager.makes)
        self.assertEqual(1, resource_manager.cleans)

    def testDirtyingWhenUnused(self):
        resource_manager = MockResource()
        resource = resource_manager.getResource()
        resource_manager.finishedWith(resource)
        resource_manager.dirtied(resource)
        self.assertEqual(1, resource_manager.makes)
        resource = resource_manager.getResource()
        self.assertEqual(2, resource_manager.makes)

    def testFinishedActivityForResourceWithoutExtensions(self):
        result = ResultWithoutResourceExtensions()
        resource_manager = MockResource()
        r = resource_manager.getResource()
        resource_manager.finishedWith(r, result)

    def testFinishedActivityForResourceWithExtensions(self):
        result = ResultWithResourceExtensions()
        resource_manager = MockResource()
        r = resource_manager.getResource()
        expected = [("clean", "start", resource_manager),
            ("clean", "stop", resource_manager)]
        resource_manager.finishedWith(r, result)
        self.assertEqual(expected, result._calls)

    def testGetActivityForResourceWithoutExtensions(self):
        result = ResultWithoutResourceExtensions()
        resource_manager = MockResource()
        r = resource_manager.getResource(result)
        resource_manager.finishedWith(r)

    def testGetActivityForResourceWithExtensions(self):
        result = ResultWithResourceExtensions()
        resource_manager = MockResource()
        r = resource_manager.getResource(result)
        expected = [("make", "start", resource_manager),
            ("make", "stop", resource_manager)]
        resource_manager.finishedWith(r)
        self.assertEqual(expected, result._calls)

    def testResetActivityForResourceWithoutExtensions(self):
        result = ResultWithoutResourceExtensions()
        resource_manager = MockResource()
        resource_manager.getResource()
        r = resource_manager.getResource()
        resource_manager.dirtied(r)
        resource_manager.finishedWith(r)
        r = resource_manager.getResource(result)
        resource_manager.dirtied(r)
        resource_manager.finishedWith(r)
        resource_manager.finishedWith(resource_manager._currentResource)

    def testResetActivityForResourceWithExtensions(self):
        result = ResultWithResourceExtensions()
        resource_manager = MockResource()
        expected = [("clean", "start", resource_manager),
            ("clean", "stop", resource_manager),
            ("make", "start", resource_manager),
            ("make", "stop", resource_manager)]
        resource_manager.getResource()
        r = resource_manager.getResource()
        resource_manager.dirtied(r)
        resource_manager.finishedWith(r)
        r = resource_manager.getResource(result)
        resource_manager.dirtied(r)
        resource_manager.finishedWith(r)
        resource_manager.finishedWith(resource_manager._currentResource)
        self.assertEqual(expected, result._calls)


class TestGenericResource(testtools.TestCase):

    def test_default_uses_setUp_tearDown(self):
        calls = []
        class Wrapped:
            def setUp(self):
                calls.append('setUp')
            def tearDown(self):
                calls.append('tearDown')
        mgr = testresources.GenericResource(Wrapped)
        resource = mgr.getResource()
        self.assertEqual(['setUp'], calls)
        mgr.finishedWith(resource)
        self.assertEqual(['setUp', 'tearDown'], calls)
        self.assertIsInstance(resource, Wrapped)

    def test_dependencies_passed_to_factory(self):
        calls = []
        class Wrapped:
            def __init__(self, **args):
                calls.append(args)
            def setUp(self):pass
            def tearDown(self):pass
        class Trivial(testresources.TestResource):
            def __init__(self, thing):
                testresources.TestResource.__init__(self)
                self.thing = thing
            def make(self, dependency_resources):return self.thing
            def clean(self, resource):pass
        mgr = testresources.GenericResource(Wrapped)
        mgr.resources = [('foo', Trivial('foo')), ('bar', Trivial('bar'))]
        resource = mgr.getResource()
        self.assertEqual([{'foo':'foo', 'bar':'bar'}], calls)
        mgr.finishedWith(resource)

    def test_setup_teardown_controllable(self):
        calls = []
        class Wrapped:
            def start(self):
                calls.append('setUp')
            def stop(self):
                calls.append('tearDown')
        mgr = testresources.GenericResource(Wrapped,
            setup_method_name='start', teardown_method_name='stop')
        resource = mgr.getResource()
        self.assertEqual(['setUp'], calls)
        mgr.finishedWith(resource)
        self.assertEqual(['setUp', 'tearDown'], calls)
        self.assertIsInstance(resource, Wrapped)

    def test_always_dirty(self):
        class Wrapped:
            def setUp(self):pass
            def tearDown(self):pass
        mgr = testresources.GenericResource(Wrapped)
        resource = mgr.getResource()
        self.assertTrue(mgr.isDirty())
        mgr.finishedWith(resource)


class TestFixtureResource(testtools.TestCase):

    def test_uses_setUp_cleanUp(self):
        fixture = LoggingFixture()
        mgr = testresources.FixtureResource(fixture)
        resource = mgr.getResource()
        self.assertEqual(fixture, resource)
        self.assertEqual(['setUp'], fixture.calls)
        mgr.finishedWith(resource)
        self.assertEqual(['setUp', 'cleanUp'], fixture.calls)

    def test_always_dirty(self):
        fixture = LoggingFixture()
        mgr = testresources.FixtureResource(fixture)
        resource = mgr.getResource()
        self.assertTrue(mgr.isDirty())
        mgr.finishedWith(resource)

    def test_reset_called(self):
        fixture = LoggingFixture()
        mgr = testresources.FixtureResource(fixture)
        resource = mgr.getResource()
        mgr.reset(resource)
        mgr.finishedWith(resource)
        self.assertEqual(
            ['setUp', 'cleanUp', 'setUp', 'cleanUp'], fixture.calls)