~launchpad-pqm/launchpad/devel

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
# Copyright 2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Provides a context manager to run parts of a test as a different dbuser."""

__metaclass__ = type
__all__ = [
    'dbuser',
    'lp_dbuser',
    ]

from contextlib import contextmanager

from canonical.testing.layers import LaunchpadZopelessLayer

@contextmanager
def dbuser(temporary_name):
    """A context manager that temporarily changes the dbuser.
    
    Use with the LaunchpadZopelessLayer layer and subclasses.
    
    temporary_name is the name of the dbuser that should be in place for the
    code in the "with" block.
    """
    restore_name = LaunchpadZopelessLayer.txn._dbuser
    LaunchpadZopelessLayer.txn.commit()
    # Note that this will raise an assertion error if the
    # LaunchpadZopelessLayer is not already set up.
    LaunchpadZopelessLayer.switchDbUser(temporary_name)
    yield
    LaunchpadZopelessLayer.txn.commit()
    LaunchpadZopelessLayer.switchDbUser(restore_name)

def lp_dbuser():
    """A context manager that temporarily changes to the launchpad dbuser.
    
    Use with the LaunchpadZopelessLayer layer and subclasses.
    """
    return dbuser('launchpad')