]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/tests/__init__.py
Merge remote branch 'storborg/master'
[bluechips.git] / bluechips / tests / __init__.py
1 """Pylons application test package
2
3 This package assumes the Pylons environment is already loaded, such as
4 when this script is imported from the `nosetests --with-pylons=test.ini`
5 command.
6
7 This module initializes the application via ``websetup`` (`paster
8 setup-app`) and provides the base testing objects.
9 """
10 from unittest import TestCase
11
12 from paste.deploy import loadapp
13 from paste.fixture import TestApp
14 from paste.script.appinstall import SetupCommand
15 from pylons import config
16 from routes import url_for
17
18 import bluechips.model
19 from bluechips.model import meta
20 from bluechips.model.types import Currency
21
22 import random
23
24 __all__ = ['url_for', 'TestController',
25            'createUsers', 'createExpenditures',
26            'deleteUsers', 'deleteExpenditures']
27
28 sample_users = [u'Alice', u'Bob', u'Charlie', u'Dave', u'Eve']
29
30 def setUpPackage():
31     # Invoke websetup with the current config file
32     SetupCommand('setup-app').run([config['__file__']])
33     
34     u1 = bluechips.model.User(u'root', u'Charlie Root', True)
35     u1.email = u'charlie@example.com'
36     u2 = bluechips.model.User(u'ben', u'Ben Bitdiddle', True)
37     u3 = bluechips.model.User(u'gotta', u'Gotta Lisp', True)
38     u4 = bluechips.model.User(u'rich', u'Rich Scheme', True)
39
40     for u in (u1, u2, u3, u4):
41         meta.Session.add(u)
42     meta.Session.commit()
43
44 def tearDownPackage():
45     meta.metadata.drop_all()
46
47 class TestController(TestCase):
48
49     def __init__(self, *args, **kwargs):
50         wsgiapp = loadapp('config:%s' % config['__file__'])
51         self.app = TestApp(wsgiapp)
52         TestCase.__init__(self, *args, **kwargs)
53
54 def createUsers(n=None):
55     if n is None:
56         n = random.randint(2, 5)
57     for i in xrange(n):
58         u = bluechips.model.User(sample_users[i].lower(), resident=True)
59         meta.Session.add(u)
60     meta.Session.commit()
61
62 def createExpenditures(n=None):
63     if n is None:
64         n = random.randint(5, 20)
65     users = meta.Session.query(bluechips.model.User).all()
66     for i in xrange(n):
67         e = bluechips.model.Expenditure(random.choice(users),
68                                         Currency(random.randint(1000, 100000)))
69         meta.Session.add(e)
70         e.even_split()
71     meta.Session.commit()
72
73 def deleteUsers():
74     map(meta.Session.delete, meta.Session.query(bluechips.model.User))
75
76 def deleteExpenditures():
77     map(meta.Session.delete, meta.Session.query(bluechips.model.Expenditure))