]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/tests/__init__.py
Move genereically useful functions out of specific modules
[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 from bluechips import 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 = model.User()
35     test_user.username = u'root'
36     test_user.name = u'Charlie Root'
37     test_user.resident = True
38     meta.Session.save(test_user)
39     meta.Session.commit()
40
41 def tearDownPackage():
42     meta.metadata.drop_all()
43
44 class TestController(TestCase):
45
46     def __init__(self, *args, **kwargs):
47         wsgiapp = loadapp('config:%s' % config['__file__'])
48         self.app = TestApp(wsgiapp)
49         TestCase.__init__(self, *args, **kwargs)
50
51 def createUsers():
52     for i in xrange(random.randint(2, 5)):
53         u = model.User()
54         u.username = sample_users[i].lower()
55         u.name = sample_users[i]
56         u.resident = 1
57         meta.Session.save(u)
58     meta.Session.commit()
59
60 def createExpenditures():
61     users = meta.Session.query(model.User).all()
62     for i in xrange(random.randint(5, 20)):
63         e = model.Expenditure()
64         e.spender = random.choice(users)
65         e.amount = Currency(random.randint(1000, 100000))
66         meta.Session.save(e)
67         e.even_split()
68     meta.Session.commit()
69
70 def deleteUsers():
71     map(meta.Session.delete, meta.Session.query(model.User))
72
73 def deleteExpenditures():
74     map(meta.Session.delete, meta.Session.query(model.Expenditure))