]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/tests/__init__.py
require at least one non-zero share on an expenditure
[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     meta.Session.add(bluechips.model.User(u'root', u'Charlie Root', True))
35     meta.Session.add(bluechips.model.User(u'ben', u'Ben Bitdiddle', True))
36     meta.Session.add(bluechips.model.User(u'gotta', u'Gotta Lisp', True))
37     meta.Session.add(bluechips.model.User(u'rich', u'Rich Scheme', True))
38
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(n=None):
52     if n is None:
53         n = random.randint(2, 5)
54     for i in xrange(n):
55         u = bluechips.model.User(sample_users[i].lower(), resident=True)
56         meta.Session.add(u)
57     meta.Session.commit()
58
59 def createExpenditures(n=None):
60     if n is None:
61         n = random.randint(5, 20)
62     users = meta.Session.query(bluechips.model.User).all()
63     for i in xrange(n):
64         e = bluechips.model.Expenditure(random.choice(users),
65                                         Currency(random.randint(1000, 100000)))
66         meta.Session.add(e)
67         e.even_split()
68     meta.Session.commit()
69
70 def deleteUsers():
71     map(meta.Session.delete, meta.Session.query(bluechips.model.User))
72
73 def deleteExpenditures():
74     map(meta.Session.delete, meta.Session.query(bluechips.model.Expenditure))