def example(request, data): # This is a test fixture. You will want to write your own, and # delete this one. See the parallel # standard_yuixhr_test_template.js to see how to call fixtures # from your Javascript tests. # # A test fixture prepares the application for your test. You can # do whatever you need here, including creating objects with an # object factory and logging the browser in as a given user. # You'll see an example below. # # Test fixtures can also return information back to your test. # Simply stuff the information you want into the "data" dict. It # will be converted into JSON and sent back to the Javascript # caller. Even Launchpad objects are converted, using the # standard lazr.restful mechanism. This can be useful in several # ways. Here are three examples. # # First, you can communicate information about the objects you # have created in the setup so that the Javascript knows what URLs # to use. The code in this function has an example of this, # below. # # Second, you can return information about verifying some aspect # of the database state, so your Javascript test can easily assert # some fact that is not usually easily exposed to it. # # Finally, you can stash information that your teardown might # need. You shouldn't usually need to clean anything up, because # the database and librarian are reset after every test, but if # you do set something up that needs an explicit teardown, you can # stash JSON-serializable information in "data" that the teardown # can use to know what to clean up. # # You can compose these setups and teardowns as well, using .extend. # There is a small example of this as well, below. # # As a full example, we will create an administrator and another # person; we will have the administrator create an object; we will # log the browser in as the other person; and we will stash # information about the object and the two people in the data # object. # # Again, this is a semi-random example. Rip this whole fixture # out, and write the ones that you need. factory = LaunchpadObjectFactory() data['admin'] = factory.makeAdministrator() data['user'] = factory.makePerson() with person_logged_in(data['admin']): data['product'] = factory.makeProduct(owner=data['admin']) # This logs the browser in as a given person. You need to use # this function for that purpose--the standard lp.testing login # functions are insufficient. login_as_person(data['user']) # Now we've done everything we said we would. Let's imagine that # we had to also write some file to disk that would need to be # cleaned up at the end of the test. We might stash information # about that in "data" too. data['some random data we might need for cleaning up'] = 'rutebega'
def create_test_product(self): """Create a test product with `LaunchpadObjectFactory`. Returns the new object's name. """ factory = LaunchpadObjectFactory() product = factory.makeProduct(official_malone=True) transaction.commit() return product.name
def extended_example(request, data): # We have declared a new fixture named "extended_example", but # instead of using "setup" we used the "extend" method of the # "example" fixture. You can think of "example" wrapping # "extended_example". During test setup, the "example" setup will # be called first. Then this function, in "extended_example," # will be called. During test teardown, the "extended_example" # cleanups will be called first, followed by the "example" # cleanups. # # The "data" dict is the same one that was passed to the wrapping # fixture. You can look at it, mutate it, or do what you need. # You are also responsible for not overwriting or mangling the # dict so that the wrapping fixtures data and/or teardown is # compromised. # # For this example, we will log in as the user and make something. factory = LaunchpadObjectFactory() with person_logged_in(data['user']): data['another_product'] = factory.makeProduct(owner=data['user'])
class TestBranchPopupWidget(unittest.TestCase): """Tests for the branch popup widget.""" layer = LaunchpadFunctionalLayer def assertIs(self, first, second): """Assert `first` is `second`.""" self.assertTrue(first is second, "%r is not %r" % (first, second)) def installLaunchBag(self, user=None, product=None): bag = DummyLaunchBag(user, product) provideUtility(bag, ILaunchBag) return bag def makeBranchPopup(self, vocabulary=None): # Pick a random, semi-appropriate context. context = self.factory.makeProduct() if vocabulary is None: vocabulary = BranchVocabulary(context) request = self.makeRequest() return BranchPopupWidget(self.makeField(context, vocabulary), vocabulary, request) def makeField(self, context, vocabulary): field = Choice(title=_('Branch'), vocabulary=vocabulary, required=False, description=_("The Bazaar branch.")) field.context = context return field def makeRequest(self): return LaunchpadTestRequest() def setUp(self): login(ANONYMOUS) self._original_launch_bag = getUtility(ILaunchBag) self.factory = LaunchpadObjectFactory() self.launch_bag = self.installLaunchBag( user=self.factory.makePerson(), product=self.factory.makeProduct()) self.popup = self.makeBranchPopup() def tearDown(self): provideUtility(self._original_launch_bag, ILaunchBag) logout() def test_getProduct(self): """getProduct() returns the product in the LaunchBag.""" self.assertEqual(self.launch_bag.product, self.popup.getProduct()) def test_getPerson(self): """getPerson() returns the logged-in user.""" self.assertEqual(self.launch_bag.user, self.popup.getPerson()) def test_getBranchNameFromURL(self): """getBranchNameFromURL() gets a branch name from a url. In general, the name is the last path segment of the URL. """ url = self.factory.getUniqueURL() name = self.popup.getBranchNameFromURL(url) self.assertEqual(URI(url).path.split('/')[-1], name) def test_makeBranchFromURL(self): """makeBranchFromURL(url) creates a mirrored branch at `url`. The owner and registrant are the currently logged-in user, as given by getPerson(), and the product is the product in the LaunchBag. """ url = self.factory.getUniqueURL() expected_name = self.popup.getBranchNameFromURL(url) branch = self.popup.makeBranchFromURL(url) self.assertEqual(BranchType.MIRRORED, branch.branch_type) self.assertEqual(url, branch.url) self.assertEqual(self.popup.getPerson(), branch.owner) self.assertEqual(self.popup.getPerson(), branch.registrant) self.assertEqual(self.popup.getProduct(), branch.product) self.assertEqual(expected_name, branch.name) def test_makeBranch_used(self): # makeBranch makes up the branch name if the inferred one is already # used. url = self.factory.getUniqueURL() expected_name = self.popup.getBranchNameFromURL(url) self.factory.makeProductBranch(name=expected_name, product=self.popup.getProduct(), owner=self.popup.getPerson()) branch = self.popup.makeBranchFromURL(url) self.assertEqual(expected_name + '-1', branch.name) def test_makeBranchRequestsMirror(self): """makeBranch requests a mirror on the branch it creates.""" url = self.factory.getUniqueURL() branch = self.popup.makeBranchFromURL(url) self.assertNotEqual('None', str(branch.next_mirror_time)) def test_makeBranchNoProduct(self): """makeBranchFromURL(url) returns None if there's no product. Not all contexts for branch registration have products. In particular, a bug can be on a source package. When we link a branch to that bug, there's no clear product to choose, so we don't choose any. """ self.installLaunchBag(product=None, user=self.factory.makePerson()) url = self.factory.getUniqueURL() self.assertRaises(NoProductError, self.popup.makeBranchFromURL, url) def test_makeBranchTrailingSlash(self): """makeBranch creates a mirrored branch even if the URL ends with /. """ uri = URI(self.factory.getUniqueURL()) expected_name = self.popup.getBranchNameFromURL( str(uri.ensureNoSlash())) branch = self.popup.makeBranchFromURL(str(uri.ensureSlash())) self.assertEqual(str(uri.ensureNoSlash()), branch.url) self.assertEqual(expected_name, branch.name) def test_toFieldValueFallsBackToMakingBranch(self): """_toFieldValue falls back to making a branch if it's given a URL.""" url = self.factory.getUniqueURL() # Check that there's no branch with this URL. self.assertIs(None, getUtility(IBranchLookup).getByUrl(url)) branch = self.popup._toFieldValue(url) self.assertEqual(url, branch.url) def test_toFieldValueFetchesTheExistingBranch(self): """_toFieldValue returns the existing branch that has that URL.""" expected_branch = self.factory.makeAnyBranch( branch_type=BranchType.MIRRORED) branch = self.popup._toFieldValue(expected_branch.url) self.assertEqual(expected_branch, branch) def test_toFieldValueNonURL(self): """When the input isn't a URL, fall back to the original error.""" empty_search = 'doesntexist' self.assertRaises(ConversionError, self.popup._toFieldValue, empty_search) def test_toFieldValueNoProduct(self): """When there's no product, fall back to the original error.""" self.installLaunchBag(product=None, user=self.factory.makePerson()) self.assertRaises(ConversionError, self.popup._toFieldValue, self.factory.getUniqueURL()) def test_toFieldBadURL(self): """When the input is a bad URL, fall back to the original error. There are many valid URLs that are inappropriate for a mirrored branch. We don't want to register a mirrored branch when someone enters such a URL. """ bad_url = 'svn://svn.example.com/repo/trunk' self.assertRaises(ConversionError, self.popup._toFieldValue, bad_url) def test_branchInRestrictedProduct(self): # There are two reasons for a URL not being in the vocabulary. One # reason is that it's there's no registered branch with that URL. The # other is that the vocabulary on this form is restricted to one # product, and there *is* a branch with that URL, but it's registered # on a different product. # Make a popup restricted to a particular product. vocab = BranchRestrictedOnProductVocabulary(self.launch_bag.product) self.assertEqual(vocab.product, self.launch_bag.product) popup = self.makeBranchPopup(vocab) # Make a branch on a different product. branch = self.factory.makeProductBranch( branch_type=BranchType.MIRRORED) self.assertNotEqual(self.launch_bag.product, branch.product) # Trying to make a branch with that URL will fail. self.assertRaises(ConversionError, popup._toFieldValue, branch.url)
class TestBranchPopupWidget(unittest.TestCase): """Tests for the branch popup widget.""" layer = LaunchpadFunctionalLayer def assertIs(self, first, second): """Assert `first` is `second`.""" self.assertTrue(first is second, "%r is not %r" % (first, second)) def installLaunchBag(self, user=None, product=None): bag = DummyLaunchBag(user, product) provideUtility(bag, ILaunchBag) return bag def makeBranchPopup(self, vocabulary=None): # Pick a random, semi-appropriate context. context = self.factory.makeProduct() if vocabulary is None: vocabulary = BranchVocabulary(context) request = self.makeRequest() return BranchPopupWidget( self.makeField(context, vocabulary), vocabulary, request) def makeField(self, context, vocabulary): field = Choice( title=_('Branch'), vocabulary=vocabulary, required=False, description=_("The Bazaar branch.")) field.context = context return field def makeRequest(self): return LaunchpadTestRequest() def setUp(self): login(ANONYMOUS) self._original_launch_bag = getUtility(ILaunchBag) self.factory = LaunchpadObjectFactory() self.launch_bag = self.installLaunchBag( user=self.factory.makePerson(), product=self.factory.makeProduct()) self.popup = self.makeBranchPopup() def tearDown(self): provideUtility(self._original_launch_bag, ILaunchBag) logout() def test_getProduct(self): """getProduct() returns the product in the LaunchBag.""" self.assertEqual(self.launch_bag.product, self.popup.getProduct()) def test_getPerson(self): """getPerson() returns the logged-in user.""" self.assertEqual(self.launch_bag.user, self.popup.getPerson()) def test_getBranchNameFromURL(self): """getBranchNameFromURL() gets a branch name from a url. In general, the name is the last path segment of the URL. """ url = self.factory.getUniqueURL() name = self.popup.getBranchNameFromURL(url) self.assertEqual(URI(url).path.split('/')[-1], name) def test_makeBranchFromURL(self): """makeBranchFromURL(url) creates a mirrored branch at `url`. The owner and registrant are the currently logged-in user, as given by getPerson(), and the product is the product in the LaunchBag. """ url = self.factory.getUniqueURL() expected_name = self.popup.getBranchNameFromURL(url) branch = self.popup.makeBranchFromURL(url) self.assertEqual(BranchType.MIRRORED, branch.branch_type) self.assertEqual(url, branch.url) self.assertEqual(self.popup.getPerson(), branch.owner) self.assertEqual(self.popup.getPerson(), branch.registrant) self.assertEqual(self.popup.getProduct(), branch.product) self.assertEqual(expected_name, branch.name) def test_makeBranch_used(self): # makeBranch makes up the branch name if the inferred one is already # used. url = self.factory.getUniqueURL() expected_name = self.popup.getBranchNameFromURL(url) self.factory.makeProductBranch( name=expected_name, product=self.popup.getProduct(), owner=self.popup.getPerson()) branch = self.popup.makeBranchFromURL(url) self.assertEqual(expected_name + '-1', branch.name) def test_makeBranchRequestsMirror(self): """makeBranch requests a mirror on the branch it creates.""" url = self.factory.getUniqueURL() branch = self.popup.makeBranchFromURL(url) self.assertNotEqual('None', str(branch.next_mirror_time)) def test_makeBranchNoProduct(self): """makeBranchFromURL(url) returns None if there's no product. Not all contexts for branch registration have products. In particular, a bug can be on a source package. When we link a branch to that bug, there's no clear product to choose, so we don't choose any. """ self.installLaunchBag(product=None, user=self.factory.makePerson()) url = self.factory.getUniqueURL() self.assertRaises(NoProductError, self.popup.makeBranchFromURL, url) def test_makeBranchTrailingSlash(self): """makeBranch creates a mirrored branch even if the URL ends with /. """ uri = URI(self.factory.getUniqueURL()) expected_name = self.popup.getBranchNameFromURL( str(uri.ensureNoSlash())) branch = self.popup.makeBranchFromURL(str(uri.ensureSlash())) self.assertEqual(str(uri.ensureNoSlash()), branch.url) self.assertEqual(expected_name, branch.name) def test_toFieldValueFallsBackToMakingBranch(self): """_toFieldValue falls back to making a branch if it's given a URL.""" url = self.factory.getUniqueURL() # Check that there's no branch with this URL. self.assertIs(None, getUtility(IBranchLookup).getByUrl(url)) branch = self.popup._toFieldValue(url) self.assertEqual(url, branch.url) def test_toFieldValueFetchesTheExistingBranch(self): """_toFieldValue returns the existing branch that has that URL.""" expected_branch = self.factory.makeAnyBranch( branch_type=BranchType.MIRRORED) branch = self.popup._toFieldValue(expected_branch.url) self.assertEqual(expected_branch, branch) def test_toFieldValueNonURL(self): """When the input isn't a URL, fall back to the original error.""" empty_search = 'doesntexist' self.assertRaises( ConversionError, self.popup._toFieldValue, empty_search) def test_toFieldValueNoProduct(self): """When there's no product, fall back to the original error.""" self.installLaunchBag(product=None, user=self.factory.makePerson()) self.assertRaises( ConversionError, self.popup._toFieldValue, self.factory.getUniqueURL()) def test_toFieldBadURL(self): """When the input is a bad URL, fall back to the original error. There are many valid URLs that are inappropriate for a mirrored branch. We don't want to register a mirrored branch when someone enters such a URL. """ bad_url = 'svn://svn.example.com/repo/trunk' self.assertRaises(ConversionError, self.popup._toFieldValue, bad_url) def test_branchInRestrictedProduct(self): # There are two reasons for a URL not being in the vocabulary. One # reason is that it's there's no registered branch with that URL. The # other is that the vocabulary on this form is restricted to one # product, and there *is* a branch with that URL, but it's registered # on a different product. # Make a popup restricted to a particular product. vocab = BranchRestrictedOnProductVocabulary(self.launch_bag.product) self.assertEqual(vocab.product, self.launch_bag.product) popup = self.makeBranchPopup(vocab) # Make a branch on a different product. branch = self.factory.makeProductBranch( branch_type=BranchType.MIRRORED) self.assertNotEqual(self.launch_bag.product, branch.product) # Trying to make a branch with that URL will fail. self.assertRaises(ConversionError, popup._toFieldValue, branch.url)