1
# Copyright 2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Testing helpers for buildmaster code."""
8
"BuilddManagerTestFixture",
11
from contextlib import contextmanager
12
from functools import wraps
17
from lp.services.database.transaction_policy import DatabaseTransactionPolicy
20
class BuilddManagerTestFixture(fixtures.Fixture):
21
"""Helps provide an environment more like `BuilddManager` provides.
23
This mimics the default transaction access policy of `BuilddManager`,
24
though it can be configured with a different policy by passing in `store`
25
and/or `read_only`. See `BuilddManager.enterReadOnlyDatabasePolicy`.
27
Because this will shift into a read-only database transaction access mode,
28
individual tests that need to do more setup can use the `extraSetUp()`
29
context manager to temporarily shift back to a read-write mode.
32
def __init__(self, store=None, read_only=True):
33
super(BuilddManagerTestFixture, self).__init__()
34
self.policy = DatabaseTransactionPolicy(
35
store=store, read_only=read_only)
38
# Commit everything done so far then shift into a read-only
39
# transaction access mode by default.
40
super(BuilddManagerTestFixture, self).setUp()
42
self.policy.__enter__()
43
self.addCleanup(self.policy.__exit__, None, None, None)
46
def extraSetUp(func=None):
47
"""Temporarily enter a read-write transaction to do extra setup.
51
with BuilddManagerTestFixture.extraSetUp():
52
removeSecurityProxy(self.build).date_finished = None
54
On exit it will commit the changes and restore the previous
55
transaction access mode.
57
Alternatively it can be used as a decorator:
59
@BuilddManagerTestFixture.extraSetUp
60
def makeSomethingOrOther(self):
63
Like with the context manager, on return it will commit the changes
64
and restore the previous transaction access mode.
69
with DatabaseTransactionPolicy(read_only=False):
75
def wrapper(*args, **kwargs):
76
with DatabaseTransactionPolicy(read_only=False):
77
result = func(*args, **kwargs)