~launchpad-pqm/launchpad/devel

12126.2.2 by Curtis Hovey
Match pillar tokens as case insensative. Added extra tests.
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
"""Test the pillar vocabularies."""
5
6
__metaclass__ = type
7
8
from canonical.testing.layers import DatabaseFunctionalLayer
9
from lp.registry.vocabularies import (
10
    DistributionOrProductVocabulary,
11
    DistributionOrProductOrProjectGroupVocabulary,
12
    PillarVocabularyBase,
13
    )
14
from lp.testing import (
15
    celebrity_logged_in,
16
    TestCaseWithFactory,
17
    )
18
19
20
class TestPillarVocabularyBase(TestCaseWithFactory):
21
    """Test that the ProductVocabulary behaves as expected."""
22
    layer = DatabaseFunctionalLayer
23
24
    def setUp(self):
25
        super(TestPillarVocabularyBase, self).setUp()
26
        self.vocabulary = PillarVocabularyBase()
27
        self.product = self.factory.makeProduct(name='orchid-snark')
28
        self.distribution = self.factory.makeDistribution(name='zebra-snark')
29
        self.project_group = self.factory.makeProject(name='apple-snark')
30
31
    def test_toTerm(self):
32
        # Product terms are composed of title, name, and the object.
33
        term = self.vocabulary.toTerm(self.product)
34
        title = '%s (Product)' % self.product.title
35
        self.assertEqual(title, term.title)
36
        self.assertEqual(self.product.name, term.token)
37
        self.assertEqual(self.product, term.value)
38
39
    def test_getTermByToken(self):
40
        # Tokens are case insentive because the product name is lowercase.
41
        term = self.vocabulary.getTermByToken('ORCHID-SNARK')
42
        self.assertEqual(self.product, term.value)
43
44
    def test_getTermByToken_LookupError(self):
45
        # getTermByToken() raises a LookupError when no match is found.
46
        self.assertRaises(
47
            LookupError,
48
            self.vocabulary.getTermByToken, 'does-notexist')
49
50
    def test_order_by_name(self):
51
        # Results are ordered by name.
52
        terms = self.vocabulary.searchForTerms('snark')
53
        result = [term.value for term in terms]
54
        self.assertEqual(
55
            [self.project_group, self.product, self.distribution], result)
56
57
58
class TestDistributionOrProductVocabulary(TestCaseWithFactory):
59
    """Test that the ProductVocabulary behaves as expected."""
60
    layer = DatabaseFunctionalLayer
61
62
    def setUp(self):
63
        super(TestDistributionOrProductVocabulary, self).setUp()
64
        self.vocabulary = DistributionOrProductVocabulary()
65
        self.product = self.factory.makeProduct(name='orchid-snark')
66
        self.distribution = self.factory.makeDistribution(name='zebra-snark')
67
12126.2.6 by Curtis Hovey
Fixed spelling.
68
    def test_inactive_products_are_excluded(self):
12126.2.2 by Curtis Hovey
Match pillar tokens as case insensative. Added extra tests.
69
        # Inactive product are not in the vocabulary.
70
        with celebrity_logged_in('registry_experts'):
71
            self.product.active = False
72
        terms = self.vocabulary.searchForTerms('snark')
73
        result = [term.value for term in terms]
74
        self.assertEqual([self.distribution], result)
75
        self.assertFalse(self.product in self.vocabulary)
76
12126.2.6 by Curtis Hovey
Fixed spelling.
77
    def test_project_groups_are_excluded(self):
12126.2.2 by Curtis Hovey
Match pillar tokens as case insensative. Added extra tests.
78
        # Project groups are not in the vocabulary.
79
        project_group = self.factory.makeProject(name='apple-snark')
80
        terms = self.vocabulary.searchForTerms('snark')
81
        result = [term.value for term in terms]
82
        self.assertEqual([self.product, self.distribution], result)
83
        self.assertFalse(project_group in self.vocabulary)
84
85
86
class TestDistributionOrProductOrProjectGroupVocabulary(TestCaseWithFactory):
87
    """Test for DistributionOrProductOrProjectGroupVocabulary."""
88
    layer = DatabaseFunctionalLayer
89
90
    def setUp(self):
91
        super(TestDistributionOrProductOrProjectGroupVocabulary, self).setUp()
92
        self.vocabulary = DistributionOrProductOrProjectGroupVocabulary()
93
        self.product = self.factory.makeProduct(name='orchid-snark')
94
        self.distribution = self.factory.makeDistribution(name='zebra-snark')
95
        self.project_group = self.factory.makeProject(name='apple-snark')
96
97
    def test_contains_all_pillars_active(self):
98
        # All active products, project groups and distributions are included.
99
        self.assertTrue(self.product in self.vocabulary)
100
        self.assertTrue(self.distribution in self.vocabulary)
101
        self.assertTrue(self.project_group in self.vocabulary)
102
12126.2.6 by Curtis Hovey
Fixed spelling.
103
    def test_inactive_products_are_excluded(self):
104
        # Inactive product are not in the vocabulary.
12126.2.2 by Curtis Hovey
Match pillar tokens as case insensative. Added extra tests.
105
        with celebrity_logged_in('registry_experts'):
106
            self.product.active = False
107
        terms = self.vocabulary.searchForTerms('snark')
108
        result = [term.value for term in terms]
109
        self.assertEqual([self.project_group, self.distribution], result)
110
        self.assertFalse(self.product in self.vocabulary)
111
12126.2.6 by Curtis Hovey
Fixed spelling.
112
    def test_inactive_product_groups_are_excluded(self):
113
        # Inactive project groups are not in the vocabulary.
12126.2.2 by Curtis Hovey
Match pillar tokens as case insensative. Added extra tests.
114
        with celebrity_logged_in('registry_experts'):
115
            self.project_group.active = False
116
        terms = self.vocabulary.searchForTerms('snark')
117
        result = [term.value for term in terms]
118
        self.assertEqual([self.product, self.distribution], result)
119
        self.assertFalse(self.project_group in self.vocabulary)