]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/tests/__init__.py
Merge branch 'master' into mobile_interface
[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     u1.password = u'charliepass'
37     u2 = bluechips.model.User(u'ben', u'Ben Bitdiddle', True)
38     u3 = bluechips.model.User(u'gotta', u'Gotta Lisp', True)
39     u4 = bluechips.model.User(u'rich', u'Rich Scheme', True)
40
41     for u in (u1, u2, u3, u4):
42         meta.Session.add(u)
43     meta.Session.commit()
44
45 def tearDownPackage():
46     meta.metadata.drop_all()
47
48 class TestController(TestCase):
49
50     def __init__(self, *args, **kwargs):
51         wsgiapp = loadapp('config:%s' % config['__file__'])
52         self.app = TestApp(wsgiapp)
53         TestCase.__init__(self, *args, **kwargs)
54
55 def createUsers(n=None):
56     if n is None:
57         n = random.randint(2, 5)
58     for i in xrange(n):
59         u = bluechips.model.User(sample_users[i].lower(), resident=True)
60         meta.Session.add(u)
61     meta.Session.commit()
62
63 def createExpenditures(n=None):
64     if n is None:
65         n = random.randint(5, 20)
66     users = meta.Session.query(bluechips.model.User).all()
67     for i in xrange(n):
68         e = bluechips.model.Expenditure(random.choice(users),
69                                         Currency(random.randint(1000, 100000)))
70         meta.Session.add(e)
71         e.even_split()
72     meta.Session.commit()
73
74 def deleteUsers():
75     map(meta.Session.delete, meta.Session.query(bluechips.model.User))
76
77 def deleteExpenditures():
78     map(meta.Session.delete, meta.Session.query(bluechips.model.Expenditure))