Merge lp:~bjornt/launchpad/more-unique-factory-strings into lp:launchpad

Proposed by Björn Tillenius
Status: Merged
Approved by: Graham Binns
Approved revision: no longer in the source branch.
Merged at revision: 10887
Proposed branch: lp:~bjornt/launchpad/more-unique-factory-strings
Merge into: lp:launchpad
Diff against target: 20 lines (+2/-1)
1 file modified
lib/lp/testing/factory.py (+2/-1)
To merge this branch: bzr merge lp:~bjornt/launchpad/more-unique-factory-strings
Reviewer Review Type Date Requested Status
Graham Binns (community) code Approve
Review via email: mp+25438@code.launchpad.net

Description of the change

Make LaunchpadObjectFactory.getUniqueString() a bit more unique.

Currently LaunchpadObjectFactory aims to generate unique strings within
an instance. This is fine for tests, but not so good when using it to
generate test data for manual testing. I've often run into the situation
where I use the object factory to generate a number of objects in a
script. After a while I want to generate more objects, but it fails,
since the it tries to use the same names as before. With this change
it's quite unlikely that it will use the same names twice, even though
there's still a small risk. If we see a test failing due to this, we can
quite easily make sure that it's really unique within an instance, by
keeping track of generated uuids.

One benefit of this change is that it makes sure that people don't
hardcode automatically generated names in the tests. I might have to fix
some existing tests; I've not finished running the test suite yet.

To post a comment you must log in.
Revision history for this message
Jonathan Lange (jml) wrote :

2010/5/17 Björn Tillenius <email address hidden>:
> If we see a test failing due to this, we can
> quite easily make sure that it's really unique within an instance, by
> keeping track of generated uuids.
>

Or you could drop the uuid approach and simply put id(self) into the
string before the getUniqueInteger()-generated segment.

jml

Revision history for this message
Björn Tillenius (bjornt) wrote :

On Mon, May 17, 2010 at 02:28:30PM -0000, Jonathan Lange wrote:
> 2010/5/17 Björn Tillenius <email address hidden>:
> > If we see a test failing due to this, we can
> > quite easily make sure that it's really unique within an instance, by
> > keeping track of generated uuids.
> >
>
> Or you could drop the uuid approach and simply put id(self) into the
> string before the getUniqueInteger()-generated segment.

I'd rather stick with uuid, since id() seems to be to prone to generated
duplicates:

    >>> def foo():
    ... foo_factory = LaunchpadObjectFactory()
    ... print id(foo_factory)
    ...
    >>> foo()
    124609680
    >>> foo()
    124609680

If I would have used id(self) and used the factory to create objects in
foo(), it would have failed the second time.

--
Björn Tillenius | https://launchpad.net/~bjornt

Revision history for this message
Graham Binns (gmb) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/testing/factory.py'
2--- lib/lp/testing/factory.py 2010-05-17 19:02:37 +0000
3+++ lib/lp/testing/factory.py 2010-05-19 07:42:36 +0000
4@@ -56,6 +56,7 @@
5 from canonical.launchpad.webapp.dbpolicy import MasterDatabasePolicy
6 from canonical.launchpad.webapp.interfaces import (
7 IStoreSelector, MAIN_STORE, DEFAULT_FLAVOR)
8+from canonical.uuid import generate_uuid
9
10 from lp.blueprints.interfaces.specification import (
11 ISpecificationSet, SpecificationDefinitionStatus)
12@@ -244,7 +245,7 @@
13 """
14 if prefix is None:
15 prefix = "generic-string"
16- string = "%s%s" % (prefix, self.getUniqueInteger())
17+ string = "%s%s" % (prefix, generate_uuid())
18 return string.replace('_', '-').lower()
19
20 def getUniqueUnicode(self):