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

"""
Run the doctests and pagetests.
"""

import os
import unittest

from zope.component import getUtility

from lp.testing import (
    ANONYMOUS,
    login,
    )
from lp.testing.layers import DatabaseFunctionalLayer
from lp.registry.interfaces.distribution import IDistributionSet
from lp.registry.interfaces.product import IProductSet
from lp.registry.interfaces.projectgroup import IProjectGroupSet
from lp.services.mail.tests.test_doc import ProcessMailLayer
from lp.services.testing import build_test_suite
from lp.testing.systemdocs import (
    LayeredDocFileSuite,
    setUp,
    tearDown,
    )


here = os.path.dirname(os.path.realpath(__file__))


def productSetUp(test):
    """Test environment for product."""
    setUp(test)
    thunderbird = getUtility(IProductSet).getByName('thunderbird')
    test.globs['target'] = thunderbird
    test.globs['collection'] = thunderbird
    login('foo.bar@canonical.com')
    test.globs['newFAQ'] = thunderbird.newFAQ
    login(ANONYMOUS)


def distributionSetUp(test):
    """Test environment for distribution."""
    setUp(test)
    kubuntu = getUtility(IDistributionSet).getByName('kubuntu')
    test.globs['target'] = kubuntu
    test.globs['collection'] = kubuntu
    login('foo.bar@canonical.com')
    test.globs['newFAQ'] = kubuntu.newFAQ
    login(ANONYMOUS)


def projectSetUp(test):
    """Test environment for project."""
    setUp(test)
    gnome_project = getUtility(IProjectGroupSet).getByName('gnome')
    products_queue = list(gnome_project.products)

    def newFAQ(owner, title, content, keywords=None, date_created=None):
        """Create a new FAQ on each project's product in turn."""
        product = products_queue.pop(0)
        products_queue.append(product)
        return product.newFAQ(
            owner, title, content, keywords=keywords,
            date_created=date_created)

    test.globs['collection'] = gnome_project
    test.globs['newFAQ'] = newFAQ


def sourcepackageSetUp(test):
    setUp(test)
    ubuntu = getUtility(IDistributionSet).getByName('ubuntu')
    test.globs['target'] = ubuntu.currentseries.getSourcePackage('evolution')


def distributionsourcepackageSetUp(test):
    setUp(test)
    ubuntu = getUtility(IDistributionSet).getByName('ubuntu')
    test.globs['target'] = ubuntu.getSourcePackage('evolution')


def create_interface_test_suite(test_file, targets):
    """Create a test suite for an interface test using several fixtures."""

    suite = unittest.TestSuite()
    for name, setup_func in targets:
        test = LayeredDocFileSuite(
            os.path.join(os.path.pardir, 'doc', test_file),
                    setUp=setup_func, tearDown=tearDown,
                    layer=DatabaseFunctionalLayer)
        suite.addTest(test)
    return suite


special = {
    'questiontarget.txt': create_interface_test_suite(
        'questiontarget.txt',
        [('product', productSetUp),
         ('distribution', distributionSetUp),
         ('distributionsourcepackage', distributionsourcepackageSetUp),
         ]),

    'faqtarget.txt': create_interface_test_suite(
        'faqtarget.txt',
        [('product', productSetUp),
         ('distribution', distributionSetUp),
         ]),

    'faqcollection.txt': create_interface_test_suite(
        'faqcollection.txt',
        [('product', productSetUp),
         ('distribution', distributionSetUp),
         ('project', projectSetUp),
         ]),
    'emailinterface.txt': LayeredDocFileSuite(
        'emailinterface.txt',
        setUp=setUp, tearDown=tearDown,
        layer=ProcessMailLayer,
        stdout_logging=False)
    }


def test_suite():
    return build_test_suite(here, special)