> In def test_codehosting_usage(self): why do you make a new distribution instead of using self.distribution? > Same question for def test_bug_tracking_usage(self): A bad job of refactoring early on? I must have just missed it when I moved the call to makeDistribution up into setUp. > > We discussed on IRC refactoring the tests since they are mostly the same. There is now one BaseTestCase that does most of the work, with subclass that define the target to product or distribution. Thanks for the pointers, Brad. === modified file 'lib/lp/registry/tests/test_distribution.py' --- lib/lp/registry/tests/test_distribution.py 2010-08-20 18:37:10 +0000 +++ lib/lp/registry/tests/test_distribution.py 2010-08-20 21:48:02 +0000 @@ -10,155 +10,17 @@ from lazr.lifecycle.snapshot import Snapshot from lp.registry.tests.test_distroseries import ( TestDistroSeriesCurrentSourceReleases) -from lp.app.enums import ServiceUsage from lp.registry.interfaces.distroseries import NoSuchDistroSeries from lp.registry.interfaces.series import SeriesStatus from lp.registry.interfaces.distribution import IDistribution from lp.soyuz.interfaces.distributionsourcepackagerelease import ( IDistributionSourcePackageRelease) -from lp.testing import ( - login_person, - TestCaseWithFactory, - ) +from lp.testing import TestCaseWithFactory from canonical.testing.layers import ( DatabaseFunctionalLayer, LaunchpadFunctionalLayer) -class TestDistributionUsageEnums(TestCaseWithFactory): - """Tests the usage enums for the distribution.""" - - layer = DatabaseFunctionalLayer - - def setUp(self): - super(TestDistributionUsageEnums, self).setUp() - self.distribution = self.factory.makeDistribution() - - def test_answers_usage_no_data(self): - # By default, we don't know anything about a distribution - self.assertEqual( - ServiceUsage.UNKNOWN, - self.distribution.answers_usage) - - def test_answers_usage_using_bool(self): - # If the old bool says they use Launchpad, return LAUNCHPAD - # if the ServiceUsage is unknown. - login_person(self.distribution.owner) - self.distribution.official_answers = True - self.assertEqual( - ServiceUsage.LAUNCHPAD, - self.distribution.answers_usage) - - def test_answers_usage_with_enum_data(self): - # If the enum has something other than UNKNOWN as its status, - # use that. - login_person(self.distribution.owner) - self.distribution.answers_usage = ServiceUsage.EXTERNAL - self.assertEqual( - ServiceUsage.EXTERNAL, - self.distribution.answers_usage) - - def test_answers_setter(self): - login_person(self.distribution.owner) - self.distribution.official_answers = True - self.distribution.answers_usage = ServiceUsage.EXTERNAL - self.assertEqual( - False, - self.distribution.official_answers) - self.distribution.answers_usage = ServiceUsage.LAUNCHPAD - self.assertEqual( - True, - self.distribution.official_answers) - - def test_codehosting_usage(self): - # Only test get for codehosting; this has no setter because the - # state is derived from other data. - distribution = self.factory.makeDistribution() - self.assertEqual( - ServiceUsage.NOT_APPLICABLE, - distribution.codehosting_usage) - - def test_translations_usage_no_data(self): - # By default, we don't know anything about a distribution - self.assertEqual( - ServiceUsage.UNKNOWN, - self.distribution.translations_usage) - - def test_translations_usage_using_bool(self): - # If the old bool says they use Launchpad, return LAUNCHPAD - # if the ServiceUsage is unknown. - login_person(self.distribution.owner) - self.distribution.official_rosetta = True - self.assertEqual( - ServiceUsage.LAUNCHPAD, - self.distribution.translations_usage) - - def test_translations_usage_with_enum_data(self): - # If the enum has something other than UNKNOWN as its status, - # use that. - login_person(self.distribution.owner) - self.distribution.translations_usage = ServiceUsage.EXTERNAL - self.assertEqual( - ServiceUsage.EXTERNAL, - self.distribution.translations_usage) - - def test_translations_setter(self): - login_person(self.distribution.owner) - self.distribution.official_rosetta = True - self.distribution.translations_usage = ServiceUsage.EXTERNAL - self.assertEqual( - False, - self.distribution.official_rosetta) - self.distribution.translations_usage = ServiceUsage.LAUNCHPAD - self.assertEqual( - True, - self.distribution.official_rosetta) - - def test_bug_tracking_usage(self): - # Only test get for bug_tracking; this has no setter because the - # state is derived from other data. - distribution = self.factory.makeDistribution() - self.assertEqual( - ServiceUsage.UNKNOWN, - distribution.bug_tracking_usage) - - def test_blueprints_usage_no_data(self): - # By default, we don't know anything about a distribution - self.assertEqual( - ServiceUsage.UNKNOWN, - self.distribution.blueprints_usage) - - def test_blueprints_usage_using_bool(self): - # If the old bool says they use Launchpad, return LAUNCHPAD - # if the ServiceUsage is unknown. - login_person(self.distribution.owner) - self.distribution.official_blueprints = True - self.assertEqual( - ServiceUsage.LAUNCHPAD, - self.distribution.blueprints_usage) - - def test_blueprints_usage_with_enum_data(self): - # If the enum has something other than UNKNOWN as its status, - # use that. - login_person(self.distribution.owner) - self.distribution.blueprints_usage = ServiceUsage.EXTERNAL - self.assertEqual( - ServiceUsage.EXTERNAL, - self.distribution.blueprints_usage) - - def test_blueprints_setter(self): - login_person(self.distribution.owner) - self.distribution.official_blueprints = True - self.distribution.blueprints_usage = ServiceUsage.EXTERNAL - self.assertEqual( - False, - self.distribution.official_blueprints) - self.distribution.blueprints_usage = ServiceUsage.LAUNCHPAD - self.assertEqual( - True, - self.distribution.official_blueprints) - - class TestDistribution(TestCaseWithFactory): layer = DatabaseFunctionalLayer === modified file 'lib/lp/registry/tests/test_product.py' --- lib/lp/registry/tests/test_product.py 2010-08-20 18:37:10 +0000 +++ lib/lp/registry/tests/test_product.py 2010-08-20 21:48:46 +0000 @@ -14,145 +14,18 @@ from canonical.launchpad.ftests import login from canonical.launchpad.testing.pages import ( find_main_content, get_feedback_messages, setupBrowser) -from canonical.testing import ( - DatabaseFunctionalLayer, - LaunchpadFunctionalLayer, - ) - +from canonical.testing import LaunchpadFunctionalLayer from canonical.launchpad.ftests import syncUpdate from lazr.lifecycle.snapshot import Snapshot -from lp.app.enums import ServiceUsage + from lp.registry.interfaces.person import IPersonSet from lp.registry.interfaces.product import IProduct, License from lp.registry.model.product import Product from lp.registry.model.productlicense import ProductLicense from lp.registry.model.commercialsubscription import ( CommercialSubscription) -from lp.testing import ( - login_person, - TestCaseWithFactory, - ) - - -class TestProductUsageEnums(TestCaseWithFactory): - """Tests the usage enums for the product.""" - - layer = DatabaseFunctionalLayer - - def setUp(self): - super(TestProductUsageEnums, self).setUp() - self.product = self.factory.makeProduct() - - def test_answers_usage_no_data(self): - # By default, we don't know anything about a product - self.assertEqual(ServiceUsage.UNKNOWN, self.product.answers_usage) - - def test_answers_usage_using_bool(self): - # If the old bool says they use Launchpad, return LAUNCHPAD - # if the ServiceUsage is unknown. - login_person(self.product.owner) - self.product.official_answers = True - self.assertEqual(ServiceUsage.LAUNCHPAD, self.product.answers_usage) - - def test_answers_usage_with_enum_data(self): - # If the enum has something other than UNKNOWN as its status, - # use that. - login_person(self.product.owner) - self.product.answers_usage = ServiceUsage.EXTERNAL - self.assertEqual(ServiceUsage.EXTERNAL, self.product.answers_usage) - - def test_answers_setter(self): - login_person(self.product.owner) - self.product.official_answers = True - self.product.answers_usage = ServiceUsage.EXTERNAL - self.assertEqual( - False, - self.product.official_answers) - self.product.answers_usage = ServiceUsage.LAUNCHPAD - self.assertEqual( - True, - self.product.official_answers) - - def test_codehosting_usage(self): - # Only test get for codehosting; this has no setter because the - # state is derived from other data. - product = self.factory.makeProduct() - self.assertEqual(ServiceUsage.UNKNOWN, product.codehosting_usage) - - def test_translations_usage_no_data(self): - # By default, we don't know anything about a product - self.assertEqual( - ServiceUsage.UNKNOWN, - self.product.translations_usage) - - def test_translations_usage_using_bool(self): - # If the old bool says they use Launchpad, return LAUNCHPAD - # if the ServiceUsage is unknown. - login_person(self.product.owner) - self.product.official_rosetta = True - self.assertEqual( - ServiceUsage.LAUNCHPAD, - self.product.translations_usage) - - def test_translations_usage_with_enum_data(self): - # If the enum has something other than UNKNOWN as its status, - # use that. - login_person(self.product.owner) - self.product.translations_usage = ServiceUsage.EXTERNAL - self.assertEqual( - ServiceUsage.EXTERNAL, - self.product.translations_usage) - - def test_translations_setter(self): - login_person(self.product.owner) - self.product.official_rosetta = True - self.product.translations_usage = ServiceUsage.EXTERNAL - self.assertEqual( - False, - self.product.official_rosetta) - self.product.translations_usage = ServiceUsage.LAUNCHPAD - self.assertEqual( - True, - self.product.official_rosetta) - - def test_bug_tracking_usage(self): - # Only test get for bug_tracking; this has no setter because the - # state is derived from other data. - product = self.factory.makeProduct() - self.assertEqual(ServiceUsage.UNKNOWN, product.bug_tracking_usage) - - def test_blueprints_usage_no_data(self): - # By default, we don't know anything about a product - self.assertEqual(ServiceUsage.UNKNOWN, self.product.blueprints_usage) - - def test_blueprints_usage_using_bool(self): - # If the old bool says they use Launchpad, return LAUNCHPAD - # if the ServiceUsage is unknown. - login_person(self.product.owner) - self.product.official_blueprints = True - self.assertEqual( - ServiceUsage.LAUNCHPAD, - self.product.blueprints_usage) - - def test_blueprints_usage_with_enum_data(self): - # If the enum has something other than UNKNOWN as its status, - # use that. - login_person(self.product.owner) - self.product.blueprints_usage = ServiceUsage.EXTERNAL - self.assertEqual(ServiceUsage.EXTERNAL, self.product.blueprints_usage) - - def test_blueprints_setter(self): - login_person(self.product.owner) - self.product.official_blueprints = True - self.product.blueprints_usage = ServiceUsage.EXTERNAL - self.assertEqual( - False, - self.product.official_blueprints) - self.product.blueprints_usage = ServiceUsage.LAUNCHPAD - self.assertEqual( - True, - self.product.official_blueprints) +from lp.testing import TestCaseWithFactory class TestProduct(TestCaseWithFactory): === added file 'lib/lp/registry/tests/test_service_usage.py' --- lib/lp/registry/tests/test_service_usage.py 1970-01-01 00:00:00 +0000 +++ lib/lp/registry/tests/test_service_usage.py 2010-08-20 21:49:50 +0000 @@ -0,0 +1,180 @@ +# Copyright 2010 Canonical Ltd. This software is licensed under the +# GNU Affero General Public License version 3 (see the file LICENSE). + +__metaclass__ = type + +import unittest + +from canonical.testing import DatabaseFunctionalLayer + +from lp.app.enums import ServiceUsage +from lp.testing import ( + login_person, + TestCaseWithFactory, + ) + + +class UsageEnumsBaseTestCase(TestCaseWithFactory): + """Base class for testing the UsageEnums on their pillars.""" + + layer = DatabaseFunctionalLayer + + def setUp(self): + super(UsageEnumsBaseTestCase, self).setUp() + self.target = None + + def test_answers_usage_no_data(self): + # By default, we don't know anything about a target + self.assertEqual( + ServiceUsage.UNKNOWN, + self.target.answers_usage) + + def test_answers_usage_using_bool(self): + # If the old bool says they use Launchpad, return LAUNCHPAD + # if the ServiceUsage is unknown. + login_person(self.target.owner) + self.target.official_answers = True + self.assertEqual( + ServiceUsage.LAUNCHPAD, + self.target.answers_usage) + + def test_answers_usage_with_enum_data(self): + # If the enum has something other than UNKNOWN as its status, + # use that. + login_person(self.target.owner) + self.target.answers_usage = ServiceUsage.EXTERNAL + self.assertEqual( + ServiceUsage.EXTERNAL, + self.target.answers_usage) + + def test_answers_setter(self): + login_person(self.target.owner) + self.target.official_answers = True + self.target.answers_usage = ServiceUsage.EXTERNAL + self.assertEqual( + False, + self.target.official_answers) + self.target.answers_usage = ServiceUsage.LAUNCHPAD + self.assertEqual( + True, + self.target.official_answers) + + def test_codehosting_usage(self): + # Only test get for codehosting; this has no setter because the + # state is derived from other data. + self.assertEqual( + ServiceUsage.UNKNOWN, + self.target.codehosting_usage) + + def test_translations_usage_no_data(self): + # By default, we don't know anything about a target + self.assertEqual( + ServiceUsage.UNKNOWN, + self.target.translations_usage) + + def test_translations_usage_using_bool(self): + # If the old bool says they use Launchpad, return LAUNCHPAD + # if the ServiceUsage is unknown. + login_person(self.target.owner) + self.target.official_rosetta = True + self.assertEqual( + ServiceUsage.LAUNCHPAD, + self.target.translations_usage) + + def test_translations_usage_with_enum_data(self): + # If the enum has something other than UNKNOWN as its status, + # use that. + login_person(self.target.owner) + self.target.translations_usage = ServiceUsage.EXTERNAL + self.assertEqual( + ServiceUsage.EXTERNAL, + self.target.translations_usage) + + def test_translations_setter(self): + login_person(self.target.owner) + self.target.official_rosetta = True + self.target.translations_usage = ServiceUsage.EXTERNAL + self.assertEqual( + False, + self.target.official_rosetta) + self.target.translations_usage = ServiceUsage.LAUNCHPAD + self.assertEqual( + True, + self.target.official_rosetta) + + def test_bug_tracking_usage(self): + # Only test get for bug_tracking; this has no setter because the + # state is derived from other data. + self.assertEqual( + ServiceUsage.UNKNOWN, + self.target.bug_tracking_usage) + + def test_blueprints_usage_no_data(self): + # By default, we don't know anything about a target + self.assertEqual( + ServiceUsage.UNKNOWN, + self.target.blueprints_usage) + + def test_blueprints_usage_using_bool(self): + # If the old bool says they use Launchpad, return LAUNCHPAD + # if the ServiceUsage is unknown. + login_person(self.target.owner) + self.target.official_blueprints = True + self.assertEqual( + ServiceUsage.LAUNCHPAD, + self.target.blueprints_usage) + + def test_blueprints_usage_with_enum_data(self): + # If the enum has something other than UNKNOWN as its status, + # use that. + login_person(self.target.owner) + self.target.blueprints_usage = ServiceUsage.EXTERNAL + self.assertEqual( + ServiceUsage.EXTERNAL, + self.target.blueprints_usage) + + def test_blueprints_setter(self): + login_person(self.target.owner) + self.target.official_blueprints = True + self.target.blueprints_usage = ServiceUsage.EXTERNAL + self.assertEqual( + False, + self.target.official_blueprints) + self.target.blueprints_usage = ServiceUsage.LAUNCHPAD + self.assertEqual( + True, + self.target.official_blueprints) + + +class TestDistributionUsageEnums(UsageEnumsBaseTestCase): + """Tests the usage enums for the distribution.""" + + def setUp(self): + super(TestDistributionUsageEnums, self).setUp() + self.target = self.factory.makeDistribution() + + def test_codehosting_usage(self): + # This method must be changed for Distribution, because its + # enum defaults to different data. + self.assertEqual( + ServiceUsage.NOT_APPLICABLE, + self.target.codehosting_usage) + + +class TestProductUsageEnums(UsageEnumsBaseTestCase): + """Tests the usage enums for the product.""" + + def setUp(self): + super(TestProductUsageEnums, self).setUp() + self.target = self.factory.makeProduct() + +# Manually create the TestLoader list, because the UsageEnumsBaseTestCase +# shouldn't run. +test_list = [ + __name__ + ".TestDistributionUsageEnums", + __name__ + ".TestProductUsageEnums", + ] + + +def test_suite(): + return unittest.TestLoader().loadTestsFromNames(test_list)