]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/tests/__init__.py
brought up to date with latest sqlalchemy conventions
[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     test_user = bluechips.model.User(u'root', u'Charlie Root', False)
35     meta.Session.add(test_user)
36     meta.Session.commit()
37
38 def tearDownPackage():
39     meta.metadata.drop_all()
40
41 class TestController(TestCase):
42
43     def __init__(self, *args, **kwargs):
44         wsgiapp = loadapp('config:%s' % config['__file__'])
45         self.app = TestApp(wsgiapp)
46         TestCase.__init__(self, *args, **kwargs)
47
48 def createUsers(n=None):
49     if n is None:
50         n = random.randint(2, 5)
51     for i in xrange(n):
52         u = bluechips.model.User(sample_users[i].lower(), resident=True)
53         meta.Session.add(u)
54     meta.Session.commit()
55
56 def createExpenditures(n=None):
57     if n is None:
58         n = random.randint(5, 20)
59     users = meta.Session.query(bluechips.model.User).all()
60     for i in xrange(n):
61         e = bluechips.model.Expenditure(random.choice(users),
62                                         Currency(random.randint(1000, 100000)))
63         meta.Session.add(e)
64         e.even_split()
65     meta.Session.commit()
66
67 def deleteUsers():
68     map(meta.Session.delete, meta.Session.query(bluechips.model.User))
69
70 def deleteExpenditures():
71     map(meta.Session.delete, meta.Session.query(bluechips.model.Expenditure))