~launchpad-pqm/launchpad/devel

14022.1.2 by Curtis Hovey
Add UsesAnswersDistributionVocabulary.
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
"""Test the answers domain vocabularies."""
5
6
__metaclass__ = type
7
14604.1.1 by Curtis Hovey
Separate test-authoring classes from test-running classes.
8
from lp.testing.layers import DatabaseFunctionalLayer
14022.1.2 by Curtis Hovey
Add UsesAnswersDistributionVocabulary.
9
from lp.answers.vocabulary import UsesAnswersDistributionVocabulary
10
from lp.testing import (
11
    person_logged_in,
12
    TestCaseWithFactory,
13
    )
14
15
16
class UsesAnswersDistributionVocabularyTestCase(TestCaseWithFactory):
17
    """Test that the vocabulary behaves as expected."""
18
    layer = DatabaseFunctionalLayer
19
20
    def test_init_with_distribution(self):
21
        # When the context is adaptable to IDistribution, the distribution
22
        # property is the distribution.
23
        distribution = self.factory.makeDistribution()
24
        vocabulary = UsesAnswersDistributionVocabulary(distribution)
25
        self.assertEqual(distribution, vocabulary.context)
26
        self.assertEqual(distribution, vocabulary.distribution)
27
28
    def test_init_without_distribution(self):
29
        # When the context is not adaptable to IDistribution, the
30
        # distribution property is None
31
        thing = self.factory.makeProduct()
32
        vocabulary = UsesAnswersDistributionVocabulary(thing)
33
        self.assertEqual(thing, vocabulary.context)
34
        self.assertEqual(None, vocabulary.distribution)
35
36
    def test_contains_distros_that_use_answers(self):
37
        # The vocabulary contains distributions that also use
38
        # Launchpad to track answers.
39
        distro_less_answers = self.factory.makeDistribution()
40
        distro_uses_answers = self.factory.makeDistribution()
41
        with person_logged_in(distro_uses_answers.owner):
42
            distro_uses_answers.official_answers = True
43
        vocabulary = UsesAnswersDistributionVocabulary()
44
        self.assertFalse(
45
            distro_less_answers in vocabulary,
46
            "Vocabulary contains distros that do not use Launchpad Answers.")
47
        self.assertTrue(
48
            distro_uses_answers in vocabulary,
49
            "Vocabulary missing distros that use Launchpad Answers.")
50
51
    def test_contains_context_distro(self):
52
        # The vocabulary contains the context distro even it it does not
53
        # use Launchpad to track answers. The distro may have tracked answers
54
        # in the past so it is a legitimate choise for historic data.
55
        distro_less_answers = self.factory.makeDistribution()
56
        vocabulary = UsesAnswersDistributionVocabulary(distro_less_answers)
57
        self.assertFalse(distro_less_answers.official_answers)
58
        self.assertTrue(
59
            distro_less_answers in vocabulary,
60
            "Vocabulary missing context distro.")
61
62
    def test_contains_missing_context(self):
14022.1.7 by Curtis Hovey
Make the comments intelligible.
63
        # The vocabulary does not contain the context if the
14022.1.2 by Curtis Hovey
Add UsesAnswersDistributionVocabulary.
64
        # context is not adaptable to a distribution.
65
        thing = self.factory.makeProduct()
66
        vocabulary = UsesAnswersDistributionVocabulary(thing)
67
        self.assertFalse(
68
            thing in vocabulary,
69
            "Vocabulary contains a non-distribution.")