~cr3/launchpad-results/trunk

« back to all changes in this revision

Viewing changes to launchpadresults/testing/factories.py

  • Committer: Marc Tardif
  • Date: 2010-09-23 23:40:07 UTC
  • Revision ID: marc.tardif@canonical.com-20100923234007-kxouzhvgv613957k
Added testing layers and factories.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
import transaction
 
5
 
 
6
from itertools import count
 
7
from random import randint
 
8
from threading import local
 
9
 
 
10
from storm.django.stores import get_store
 
11
 
 
12
from zope.component import getUtility
 
13
 
 
14
from launchpadresults.interfaces import IPersonSet
 
15
 
 
16
 
 
17
 
 
18
__all__ = [
 
19
    "TestFactory",
 
20
    ]
 
21
 
 
22
 
 
23
class ObjectFactory(object):
 
24
 
 
25
    def __init__(self):
 
26
        # Initialise the unique identifier.
 
27
        self._local = local()
 
28
 
 
29
    def getUniqueEmailAddress(self):
 
30
        return "%s@example.com" % self.getUniqueString("email")
 
31
 
 
32
    def getUniqueInteger(self):
 
33
        """Return an integer unique to this factory instance.
 
34
 
 
35
        For each thread, this will be a series of increasing numbers, but the
 
36
        starting point will be unique per thread.
 
37
        """
 
38
        counter = getattr(self._local, "integer", None)
 
39
        if counter is None:
 
40
            counter = count(randint(0, 1000000))
 
41
            self._local.integer = counter
 
42
        return counter.next()
 
43
 
 
44
    def getUniqueString(self, prefix=None):
 
45
        """Return a string unique to this factory instance.
 
46
 
 
47
        :param prefix: Used as a prefix for the unique string. If unspecified,
 
48
            defaults to "generic-string".
 
49
        """
 
50
        if prefix is None:
 
51
            prefix = "generic-string"
 
52
        string = "%s%s" % (prefix, self.getUniqueInteger())
 
53
        return string.replace("_", "-").lower()
 
54
 
 
55
 
 
56
class DatabaseFactory(ObjectFactory):
 
57
 
 
58
    def abort(self):
 
59
        transaction.abort()
 
60
 
 
61
    def commit(self):
 
62
        transaction.commit()
 
63
 
 
64
    def getMainStore(self):
 
65
        return get_store("main")
 
66
 
 
67
    def getProjectStore(self):
 
68
        return get_store("project-1")
 
69
 
 
70
    def getAltProjectStore(self):
 
71
        return get_store("project-2")
 
72
 
 
73
    def getSessionStore(self):
 
74
        return get_store("session")
 
75
 
 
76
    def getSessionAutocommitStore(self):
 
77
        return get_store("session-autocommit")
 
78
 
 
79
 
 
80
class TestFactory(DatabaseFactory):
 
81
 
 
82
    def makePerson(self, name=None, identity=None):
 
83
        if name is None:
 
84
            name = self.getUniqueString("person-name")
 
85
        if identity is None:
 
86
            name = self.getUniqueString("https://login.launchpad.net/+id/")
 
87
 
 
88
        return getUtility(IPersonSet).create(name, identity)