~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/buildmaster/testing.py

  • Committer: Gavin Panella
  • Date: 2011-12-20 21:16:35 UTC
  • mto: This revision was merged to the branch mainline in revision 14641.
  • Revision ID: gavin.panella@canonical.com-20111220211635-7hqz729ujj9wdp0y
extraSetUp() can be a decorator or a context manager.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
    ]
10
10
 
11
11
from contextlib import contextmanager
 
12
from functools import wraps
12
13
 
13
14
import fixtures
14
15
import transaction
42
43
        self.addCleanup(self.policy.__exit__, None, None, None)
43
44
 
44
45
    @staticmethod
45
 
    @contextmanager
46
 
    def extraSetUp():
 
46
    def extraSetUp(func=None):
47
47
        """Temporarily enter a read-write transaction to do extra setup.
48
48
 
49
49
        For example:
50
50
 
51
 
          with self.extraSetUp():
 
51
          with BuilddManagerTestFixture.extraSetUp():
52
52
              removeSecurityProxy(self.build).date_finished = None
53
53
 
54
54
        On exit it will commit the changes and restore the previous
55
55
        transaction access mode.
 
56
 
 
57
        Alternatively it can be used as a decorator:
 
58
 
 
59
          @BuilddManagerTestFixture.extraSetUp
 
60
          def makeSomethingOrOther(self):
 
61
              return ...
 
62
 
 
63
        Like with the context manager, on return it will commit the changes
 
64
        and restore the previous transaction access mode.
56
65
        """
57
 
        with DatabaseTransactionPolicy(read_only=False):
58
 
            yield
59
 
            transaction.commit()
 
66
        if func is None:
 
67
            @contextmanager
 
68
            def context():
 
69
                with DatabaseTransactionPolicy(read_only=False):
 
70
                    yield
 
71
                    transaction.commit()
 
72
            return context()
 
73
        else:
 
74
            @wraps(func)
 
75
            def wrapper(*args, **kwargs):
 
76
                with DatabaseTransactionPolicy(read_only=False):
 
77
                    result = func(*args, **kwargs)
 
78
                    transaction.commit()
 
79
                    return result
 
80
            return wrapper