~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/answers/tests/test_questiontarget.py

  • Committer: Abel Deuring
  • Date: 2009-03-24 12:43:49 UTC
  • mfrom: (8053 devel)
  • mto: This revision was merged to the branch mainline in revision 8328.
  • Revision ID: abel.deuring@canonical.com-20090324124349-xllu9kbw5ib27kno
RF merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2006 Canonical Ltd.  All rights reserved.
2
2
 
3
 
"""Test harness for running the questiontarget.txt interface test
4
 
 
5
 
This module will run the interface test against the Product, Distribution,
6
 
DistributionSourcePackage and SourcePackage implementations of that interface.
7
 
"""
 
3
"""Tests related to IQuestionTarget."""
8
4
 
9
5
__metaclass__ = type
10
6
 
13
9
import unittest
14
10
 
15
11
from zope.component import getUtility
 
12
from zope.security.proxy import removeSecurityProxy
16
13
 
17
 
from canonical.launchpad.interfaces import IDistributionSet, IProductSet
 
14
from canonical.testing import DatabaseFunctionalLayer
 
15
from canonical.launchpad.ftests import login_person
 
16
from canonical.launchpad.interfaces.distribution import IDistributionSet
 
17
from canonical.launchpad.interfaces.language import ILanguageSet
18
18
from canonical.launchpad.testing.systemdocs import (
19
19
    LayeredDocFileSuite, setUp, tearDown)
20
 
from canonical.testing import LaunchpadFunctionalLayer
21
 
 
22
 
 
23
 
def productSetUp(test):
24
 
    setUp(test)
25
 
    test.globs['target'] = getUtility(IProductSet).getByName('thunderbird')
26
 
 
27
 
 
28
 
def distributionSetUp(test):
29
 
    setUp(test)
30
 
    test.globs['target'] = getUtility(IDistributionSet).getByName('kubuntu')
31
 
 
32
 
 
33
 
def sourcepackageSetUp(test):
34
 
    setUp(test)
35
 
    ubuntu = getUtility(IDistributionSet).getByName('ubuntu')
36
 
    test.globs['target'] = ubuntu.currentseries.getSourcePackage('evolution')
37
 
 
38
 
 
39
 
def distributionsourcepackageSetUp(test):
40
 
    setUp(test)
41
 
    ubuntu = getUtility(IDistributionSet).getByName('ubuntu')
42
 
    test.globs['target'] = ubuntu.getSourcePackage('evolution')
 
20
from canonical.launchpad.testing import TestCaseWithFactory
 
21
 
 
22
 
 
23
class TestQuestionTarget_answer_contacts_with_languages(TestCaseWithFactory):
 
24
    """Tests for the 'answer_contacts_with_languages' property of question
 
25
    targets.
 
26
    """
 
27
    layer = DatabaseFunctionalLayer
 
28
 
 
29
    def setUp(self):
 
30
        super(TestQuestionTarget_answer_contacts_with_languages, self).setUp()
 
31
        self.answer_contact = self.factory.makePerson()
 
32
        login_person(self.answer_contact)
 
33
        lang_set = getUtility(ILanguageSet)
 
34
        self.answer_contact.addLanguage(lang_set['pt_BR'])
 
35
        self.answer_contact.addLanguage(lang_set['en'])
 
36
 
 
37
    def test_Product_implementation_should_prefill_cache(self):
 
38
        # Remove the answer contact's security proxy because we need to call
 
39
        # some non public methods to change its language cache.
 
40
        answer_contact = removeSecurityProxy(self.answer_contact)
 
41
        product = self.factory.makeProduct()
 
42
        product.addAnswerContact(answer_contact)
 
43
 
 
44
        # Must delete the cache because it's been filled in addAnswerContact.
 
45
        answer_contact.deleteLanguagesCache()
 
46
        self.assertRaises(AttributeError, answer_contact.getLanguagesCache)
 
47
 
 
48
        # Need to remove the product's security proxy because
 
49
        # answer_contacts_with_languages is not part of its public API.
 
50
        answer_contacts = removeSecurityProxy(
 
51
            product).answer_contacts_with_languages
 
52
        self.failUnlessEqual(answer_contacts, [answer_contact])
 
53
        langs = [
 
54
            lang.englishname for lang in answer_contact.getLanguagesCache()]
 
55
        # The languages cache has been filled in the correct order.
 
56
        self.failUnlessEqual(langs, [u'English', u'Portuguese (Brazil)'])
 
57
 
 
58
    def test_SourcePackage_implementation_should_prefill_cache(self):
 
59
        # Remove the answer contact's security proxy because we need to call
 
60
        # some non public methods to change its language cache.
 
61
        answer_contact = removeSecurityProxy(self.answer_contact)
 
62
        ubuntu = getUtility(IDistributionSet)['ubuntu']
 
63
        self.factory.makeSourcePackageName(name='test-pkg')
 
64
        source_package = ubuntu.getSourcePackage('test-pkg')
 
65
        source_package.addAnswerContact(answer_contact)
 
66
 
 
67
        # Must delete the cache because it's been filled in addAnswerContact.
 
68
        answer_contact.deleteLanguagesCache()
 
69
        self.assertRaises(AttributeError, answer_contact.getLanguagesCache)
 
70
 
 
71
        # Need to remove the sourcepackage's security proxy because
 
72
        # answer_contacts_with_languages is not part of its public API.
 
73
        answer_contacts = removeSecurityProxy(
 
74
            source_package).answer_contacts_with_languages
 
75
        self.failUnlessEqual(answer_contacts, [answer_contact])
 
76
        langs = [
 
77
            lang.englishname for lang in answer_contact.getLanguagesCache()]
 
78
        # The languages cache has been filled in the correct order.
 
79
        self.failUnlessEqual(langs, [u'English', u'Portuguese (Brazil)'])
43
80
 
44
81
 
45
82
def test_suite():
46
 
    suite = unittest.TestSuite()
47
 
 
48
 
    targets = [('product', productSetUp),
49
 
               ('distribution', distributionSetUp),
50
 
               ('sourcepackage', sourcepackageSetUp),
51
 
               ('distributionsourcepackage', distributionsourcepackageSetUp),
52
 
               ]
53
 
 
54
 
    for name, setUpMethod in targets:
55
 
        test = LayeredDocFileSuite('questiontarget.txt',
56
 
                    setUp=setUpMethod, tearDown=tearDown,
57
 
                    layer=LaunchpadFunctionalLayer)
58
 
        suite.addTest(test)
 
83
    suite = unittest.TestLoader().loadTestsFromName(__name__)
59
84
 
60
85
    test = LayeredDocFileSuite('questiontarget-sourcepackage.txt',
61
86
                setUp=setUp, tearDown=tearDown,
62
 
                layer=LaunchpadFunctionalLayer)
 
87
                layer=DatabaseFunctionalLayer)
63
88
    suite.addTest(test)
64
89
    return suite
65
90
 
66
 
 
67
 
if __name__ == '__main__':
68
 
    unittest.main()