1
# Copyright 2010 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
6
from itertools import count
7
from random import randint
8
from threading import local
10
from storm.django.stores import get_store
12
from zope.component import getUtility
14
from launchpadresults.interfaces import IPersonSet
23
class ObjectFactory(object):
26
# Initialise the unique identifier.
29
def getUniqueEmailAddress(self):
30
return "%s@example.com" % self.getUniqueString("email")
32
def getUniqueInteger(self):
33
"""Return an integer unique to this factory instance.
35
For each thread, this will be a series of increasing numbers, but the
36
starting point will be unique per thread.
38
counter = getattr(self._local, "integer", None)
40
counter = count(randint(0, 1000000))
41
self._local.integer = counter
44
def getUniqueString(self, prefix=None):
45
"""Return a string unique to this factory instance.
47
:param prefix: Used as a prefix for the unique string. If unspecified,
48
defaults to "generic-string".
51
prefix = "generic-string"
52
string = "%s%s" % (prefix, self.getUniqueInteger())
53
return string.replace("_", "-").lower()
56
class DatabaseFactory(ObjectFactory):
64
def getMainStore(self):
65
return get_store("main")
67
def getProjectStore(self):
68
return get_store("project-1")
70
def getAltProjectStore(self):
71
return get_store("project-2")
73
def getSessionStore(self):
74
return get_store("session")
76
def getSessionAutocommitStore(self):
77
return get_store("session-autocommit")
80
class TestFactory(DatabaseFactory):
82
def makePerson(self, name=None, identity=None):
84
name = self.getUniqueString("person-name")
86
name = self.getUniqueString("https://login.launchpad.net/+id/")
88
return getUtility(IPersonSet).create(name, identity)