1 | #! /usr/bin/env python
|
---|
2 |
|
---|
3 | __ident__ = "$Id: test_models.py 48 2006-07-04 17:07:37Z erob $"
|
---|
4 |
|
---|
5 | # A really nice blog from Ian Maurer on unit testing django:
|
---|
6 | # http://itmaurer.com/blog/?feed=atom
|
---|
7 |
|
---|
8 | from test.test_support import run_unittest, verbose
|
---|
9 |
|
---|
10 | import os, os.path
|
---|
11 | import unittest
|
---|
12 |
|
---|
13 | class TestCase(unittest.TestCase):
|
---|
14 | def setUp(self):
|
---|
15 | # Test fixture method
|
---|
16 | try:
|
---|
17 | # Must configure django settings first
|
---|
18 | #settings.configure(default_settings=_settings, USE_I18N=True)
|
---|
19 | os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
|
---|
20 | except Exception, e:
|
---|
21 | from traceback import print_exc
|
---|
22 | tb = print_exc()
|
---|
23 | print str(tb)
|
---|
24 | raise e
|
---|
25 | else:
|
---|
26 | from django.conf import settings
|
---|
27 | self.settings = settings or None
|
---|
28 |
|
---|
29 | # Load the App instance object.
|
---|
30 | try:
|
---|
31 | from notmm.models import Poll, Choice
|
---|
32 | except Exception, e:
|
---|
33 | from traceback import print_exc
|
---|
34 | tb = print_exc()
|
---|
35 | if verbose: print str(tb)
|
---|
36 | raise e
|
---|
37 | else:
|
---|
38 | # this technique must be upgraded..
|
---|
39 | self.app = Poll()
|
---|
40 |
|
---|
41 | def tearDown(self):
|
---|
42 | # test closure method
|
---|
43 | self.settings = None
|
---|
44 | del self.app
|
---|
45 |
|
---|
46 | def test_app_settings(self):
|
---|
47 | # put some code here
|
---|
48 | settings = self.settings
|
---|
49 | self.failIf(settings.DATABASE_ENGINE != "postgresql_psycopg2",
|
---|
50 | self.settings.DATABASE_ENGINE)
|
---|
51 |
|
---|
52 | def test_app_inheritance(self):
|
---|
53 | from notmm.models import Poll
|
---|
54 | from django.db import models
|
---|
55 | self.failUnless(issubclass(Poll, models.Model))
|
---|
56 |
|
---|
57 | def test_app_label(self):
|
---|
58 | from notmm.models import Poll
|
---|
59 | self.assertEqual(Poll._meta.app_label, 'notmm')
|
---|
60 |
|
---|
61 | def test_app_save(self):
|
---|
62 | # try the app.save() method (it must work as expected)
|
---|
63 | from notmm.models import Poll
|
---|
64 | new_poll = Poll()
|
---|
65 | new_poll.question = "What is Rhizobium?"
|
---|
66 | self.failUnless(new_poll.save(), "Expecting save() to work")
|
---|
67 |
|
---|
68 | def test_app_install(self):
|
---|
69 | try:
|
---|
70 | from django.core import management
|
---|
71 | from django.db.models import loading
|
---|
72 | from notmm.models import Poll
|
---|
73 | pyobj = loading.get_app(Poll._meta.app_label)
|
---|
74 | management.install(pyobj)
|
---|
75 | except Exception, e:
|
---|
76 | from traceback import print_exc
|
---|
77 | tb = print_exc()
|
---|
78 | print str(tb)
|
---|
79 | raise e
|
---|
80 |
|
---|
81 | def test_get_apps(self):
|
---|
82 | from django.db.models import loading
|
---|
83 | output = loading.get_apps()
|
---|
84 | if verbose: print output
|
---|
85 |
|
---|
86 | def test_get_models(self):
|
---|
87 | from django.db.models import get_models
|
---|
88 | app = self.app
|
---|
89 | models = get_models()
|
---|
90 | self.failIf(models == [], 'Expecting a list of valid models, got %s'
|
---|
91 | % models)
|
---|
92 |
|
---|
93 | def test_get_sql_create(self):
|
---|
94 | from django.core.management import get_sql_create
|
---|
95 | app = self.app
|
---|
96 | output = get_sql_create(app)
|
---|
97 | if verbose: print output
|
---|
98 |
|
---|
99 |
|
---|
100 | def test_main():
|
---|
101 | tests = [ TestCase ]
|
---|
102 | run_unittest(*tests)
|
---|
103 |
|
---|
104 | if __name__ == '__main__':
|
---|
105 | test_main()
|
---|
106 |
|
---|