Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Move testrunner.py and allow tests to be run with standard test command.

  • Loading branch information...
commit e7613ca7655f3002d606433580941b19f6dd33d2 1 parent f66a953
Jacek Tomaszewski authored July 07, 2012
337  dbsettings/tests/__init__.py
... ...
@@ -0,0 +1,337 @@
  1
+import datetime
  2
+
  3
+from django.db import models
  4
+from django import test
  5
+from django.utils.functional import curry
  6
+
  7
+import dbsettings
  8
+from dbsettings import loading, views
  9
+
  10
+# Set up some settings to test
  11
+class TestSettings(dbsettings.Group):
  12
+    boolean = dbsettings.BooleanValue()
  13
+    integer = dbsettings.IntegerValue()
  14
+    string = dbsettings.StringValue()
  15
+    list_semi_colon = dbsettings.MultiSeparatorValue()
  16
+    list_comma = dbsettings.MultiSeparatorValue(separator=',')
  17
+    date = dbsettings.DateValue()
  18
+    time = dbsettings.TimeValue()
  19
+    datetime = dbsettings.DateTimeValue()
  20
+
  21
+# This is assigned to module, rather than a model
  22
+module_settings = TestSettings()
  23
+
  24
+class Defaults(models.Model):
  25
+    class settings(dbsettings.Group):
  26
+        boolean = dbsettings.BooleanValue(default=True)
  27
+        boolean_false = dbsettings.BooleanValue(default=False)
  28
+        integer = dbsettings.IntegerValue(default=1)
  29
+        string = dbsettings.StringValue(default="default")
  30
+        list_semi_colon = dbsettings.MultiSeparatorValue(default=['one','two'])
  31
+        list_comma = dbsettings.MultiSeparatorValue(separator=',',default=('one','two'))
  32
+        date = dbsettings.DateValue(default=datetime.date(2012, 3, 14))
  33
+        time = dbsettings.TimeValue(default=datetime.time(12, 3, 14))
  34
+        datetime = dbsettings.DateTimeValue(default=datetime.datetime(2012, 3, 14, 12, 3, 14))
  35
+    settings = settings()
  36
+
  37
+# These will be populated by the fixture data
  38
+class Populated(models.Model):
  39
+    settings = TestSettings()
  40
+
  41
+# These will be empty after startup
  42
+class Unpopulated(models.Model):
  43
+    settings = TestSettings()
  44
+
  45
+# These will allow blank values
  46
+class Blankable(models.Model):
  47
+    settings = TestSettings()
  48
+
  49
+class Editable(models.Model):
  50
+    settings = TestSettings()
  51
+
  52
+class Combined(models.Model):
  53
+    class settings(dbsettings.Group):
  54
+        enabled = dbsettings.BooleanValue()
  55
+    settings = TestSettings() + settings()
  56
+
  57
+# For registration testing
  58
+class ClashSettings1(dbsettings.Group):
  59
+    clash1 = dbsettings.BooleanValue()
  60
+
  61
+class ClashSettings2(dbsettings.Group):
  62
+    clash2 = dbsettings.BooleanValue()
  63
+
  64
+class ClashSettings1_2(dbsettings.Group):
  65
+    clash1 = dbsettings.IntegerValue()
  66
+    clash2 = dbsettings.IntegerValue()
  67
+
  68
+module_clash1 = ClashSettings1()
  69
+
  70
+class ModelClash(models.Model):
  71
+    settings = ClashSettings1_2()
  72
+
  73
+module_clash2 = ClashSettings2()
  74
+
  75
+
  76
+class SettingsTestCase(test.TestCase):
  77
+    urls = 'dbsettings.tests.test_urls'
  78
+
  79
+    def setUp(self):
  80
+        # Standard test fixtures don't update the in-memory cache.
  81
+        # So we have to do it ourselves this time.
  82
+        loading.set_setting_value('dbsettings.tests', 'Populated', 'boolean', True)
  83
+        loading.set_setting_value('dbsettings.tests', 'Populated', 'integer', 42)
  84
+        loading.set_setting_value('dbsettings.tests', 'Populated', 'string', 'Ni!')
  85
+        loading.set_setting_value('dbsettings.tests', 'Populated', 'list_semi_colon', 'a@b.com;c@d.com;e@f.com')
  86
+        loading.set_setting_value('dbsettings.tests', 'Populated', 'list_comma', 'a@b.com,c@d.com,e@f.com')
  87
+        loading.set_setting_value('dbsettings.tests', 'Populated', 'date', '2012-06-28')
  88
+        loading.set_setting_value('dbsettings.tests', 'Populated', 'time', '16:19:17')
  89
+        loading.set_setting_value('dbsettings.tests', 'Populated', 'datetime', '2012-06-28 16:19:17')
  90
+        loading.set_setting_value('dbsettings.tests', '', 'boolean', False)
  91
+        loading.set_setting_value('dbsettings.tests', '', 'integer', 14)
  92
+        loading.set_setting_value('dbsettings.tests', '', 'string', 'Module')
  93
+        loading.set_setting_value('dbsettings.tests', '', 'list_semi_colon', 'g@h.com;i@j.com;k@l.com')
  94
+        loading.set_setting_value('dbsettings.tests', '', 'list_comma', 'g@h.com,i@j.com,k@l.com')
  95
+        loading.set_setting_value('dbsettings.tests', '', 'date', '2011-05-27')
  96
+        loading.set_setting_value('dbsettings.tests', '', 'time', '15:18:16')
  97
+        loading.set_setting_value('dbsettings.tests', '', 'datetime', '2011-05-27 15:18:16')
  98
+        loading.set_setting_value('dbsettings.tests', 'Combined', 'boolean', False)
  99
+        loading.set_setting_value('dbsettings.tests', 'Combined', 'integer', 1138)
  100
+        loading.set_setting_value('dbsettings.tests', 'Combined', 'string', 'THX')
  101
+        loading.set_setting_value('dbsettings.tests', 'Combined', 'list_semi_colon', 'm@n.com;o@p.com;q@r.com')
  102
+        loading.set_setting_value('dbsettings.tests', 'Combined', 'list_comma', 'm@n.com,o@p.com,q@r.com')
  103
+        loading.set_setting_value('dbsettings.tests', 'Combined', 'date', '2010-04-26')
  104
+        loading.set_setting_value('dbsettings.tests', 'Combined', 'time', '14:17:15')
  105
+        loading.set_setting_value('dbsettings.tests', 'Combined', 'datetime', '2010-04-26 14:17:15')
  106
+        loading.set_setting_value('dbsettings.tests', 'Combined', 'enabled', True)
  107
+
  108
+    def test_settings(self):
  109
+        "Make sure settings groups are initialized properly"
  110
+
  111
+        # Settings already in the database are available immediately
  112
+        self.assertEqual(Populated.settings.boolean, True)
  113
+        self.assertEqual(Populated.settings.integer, 42)
  114
+        self.assertEqual(Populated.settings.string, 'Ni!')
  115
+        self.assertEqual(Populated.settings.list_semi_colon, ['a@b.com', 'c@d.com', 'e@f.com'])
  116
+        self.assertEqual(Populated.settings.list_comma, ['a@b.com', 'c@d.com', 'e@f.com'])
  117
+        self.assertEqual(Populated.settings.date, datetime.date(2012, 6, 28))
  118
+        self.assertEqual(Populated.settings.time, datetime.time(16, 19, 17))
  119
+        self.assertEqual(Populated.settings.datetime, datetime.datetime(2012, 6, 28, 16, 19, 17))
  120
+
  121
+        # Module settings are kept separate from model settings
  122
+        self.assertEqual(module_settings.boolean, False)
  123
+        self.assertEqual(module_settings.integer, 14)
  124
+        self.assertEqual(module_settings.string, 'Module')
  125
+        self.assertEqual(module_settings.list_semi_colon, ['g@h.com', 'i@j.com', 'k@l.com'])
  126
+        self.assertEqual(module_settings.list_comma, ['g@h.com', 'i@j.com', 'k@l.com'])
  127
+        self.assertEqual(module_settings.date, datetime.date(2011, 5, 27))
  128
+        self.assertEqual(module_settings.time, datetime.time(15, 18, 16))
  129
+        self.assertEqual(module_settings.datetime, datetime.datetime(2011, 5, 27, 15, 18, 16))
  130
+
  131
+        # Settings can be added together
  132
+        self.assertEqual(Combined.settings.boolean, False)
  133
+        self.assertEqual(Combined.settings.integer, 1138)
  134
+        self.assertEqual(Combined.settings.string, 'THX')
  135
+        self.assertEqual(Combined.settings.enabled, True)
  136
+        self.assertEqual(Combined.settings.list_semi_colon, ['m@n.com', 'o@p.com', 'q@r.com'])
  137
+        self.assertEqual(Combined.settings.list_comma, ['m@n.com', 'o@p.com', 'q@r.com'])
  138
+        self.assertEqual(Combined.settings.date, datetime.date(2010, 4, 26))
  139
+        self.assertEqual(Combined.settings.time, datetime.time(14, 17, 15))
  140
+        self.assertEqual(Combined.settings.datetime, datetime.datetime(2010, 4, 26, 14, 17, 15))
  141
+
  142
+        # Settings not in the database use empty defaults
  143
+        self.assertEqual(Unpopulated.settings.boolean, False)
  144
+        self.assertEqual(Unpopulated.settings.integer, None)
  145
+        self.assertEqual(Unpopulated.settings.string, '')
  146
+        self.assertEqual(Unpopulated.settings.list_semi_colon, [])
  147
+        self.assertEqual(Unpopulated.settings.list_comma, [])
  148
+
  149
+        # ...Unless a default paramter was specified, then they use that
  150
+        self.assertEqual(Defaults.settings.boolean, True)
  151
+        self.assertEqual(Defaults.settings.boolean_false, False)
  152
+        self.assertEqual(Defaults.settings.integer, 1)
  153
+        self.assertEqual(Defaults.settings.string, 'default')
  154
+        self.assertEqual(Defaults.settings.list_semi_colon, ['one','two'])
  155
+        self.assertEqual(Defaults.settings.list_comma, ['one','two'])
  156
+        self.assertEqual(Defaults.settings.date, datetime.date(2012, 3, 14))
  157
+        self.assertEqual(Defaults.settings.time, datetime.time(12, 3, 14))
  158
+        self.assertEqual(Defaults.settings.datetime, datetime.datetime(2012, 3, 14, 12, 3, 14))
  159
+
  160
+
  161
+        # Settings should be retrieved in the order of definition
  162
+        self.assertEqual(Populated.settings.keys(),
  163
+                         ['boolean', 'integer', 'string', 'list_semi_colon',
  164
+                          'list_comma', 'date', 'time', 'datetime'])
  165
+        self.assertEqual(Combined.settings.keys(),
  166
+                         ['boolean', 'integer', 'string', 'list_semi_colon',
  167
+                          'list_comma', 'date', 'time', 'datetime', 'enabled'])
  168
+
  169
+        # Values should be coerced to the proper Python types
  170
+        self.assert_(isinstance(Populated.settings.boolean, bool))
  171
+        self.assert_(isinstance(Populated.settings.integer, int))
  172
+        self.assert_(isinstance(Populated.settings.string, basestring))
  173
+
  174
+        # Settings can not be accessed directly from models, only instances
  175
+        self.assertRaises(AttributeError, lambda: Populated().settings)
  176
+        self.assertRaises(AttributeError, lambda: Unpopulated().settings)
  177
+
  178
+        # Updates are reflected in the live settings
  179
+        loading.set_setting_value('dbsettings.tests', 'Unpopulated', 'boolean', True)
  180
+        loading.set_setting_value('dbsettings.tests', 'Unpopulated', 'integer', 13)
  181
+        loading.set_setting_value('dbsettings.tests', 'Unpopulated', 'string', 'Friday')
  182
+        loading.set_setting_value('dbsettings.tests', 'Unpopulated', 'list_semi_colon', 'aa@bb.com;cc@dd.com')
  183
+        loading.set_setting_value('dbsettings.tests', 'Unpopulated', 'list_comma', 'aa@bb.com,cc@dd.com')
  184
+        # for date/time you can specify string (as above) or proper object
  185
+        loading.set_setting_value('dbsettings.tests', 'Unpopulated', 'date', datetime.date(1912, 6, 23))
  186
+        loading.set_setting_value('dbsettings.tests', 'Unpopulated', 'time', datetime.time(1, 2, 3))
  187
+        loading.set_setting_value('dbsettings.tests', 'Unpopulated', 'datetime', datetime.datetime(1912, 6, 23, 1, 2, 3))
  188
+
  189
+        self.assertEqual(Unpopulated.settings.boolean, True)
  190
+        self.assertEqual(Unpopulated.settings.integer, 13)
  191
+        self.assertEqual(Unpopulated.settings.string, 'Friday')
  192
+        self.assertEqual(Unpopulated.settings.list_semi_colon, ['aa@bb.com', 'cc@dd.com'])
  193
+        self.assertEqual(Unpopulated.settings.list_comma, ['aa@bb.com', 'cc@dd.com'])
  194
+        self.assertEqual(Unpopulated.settings.date, datetime.date(1912, 6, 23))
  195
+        self.assertEqual(Unpopulated.settings.time, datetime.time(1, 2, 3))
  196
+        self.assertEqual(Unpopulated.settings.datetime, datetime.datetime(1912, 6, 23, 1, 2, 3))
  197
+
  198
+        # Updating settings with defaults
  199
+        loading.set_setting_value('dbsettings.tests', 'Defaults', 'boolean', False)
  200
+        self.assertEqual(Defaults.settings.boolean, False)
  201
+        loading.set_setting_value('dbsettings.tests', 'Defaults', 'boolean_false', True)
  202
+        self.assertEqual(Defaults.settings.boolean_false, True)
  203
+
  204
+
  205
+        # Updating blankable settings
  206
+        self.assertEqual(Blankable.settings.string, '')
  207
+        loading.set_setting_value('dbsettings.tests', 'Blankable', 'string', 'Eli')
  208
+        self.assertEqual(Blankable.settings.string, 'Eli')
  209
+        loading.set_setting_value('dbsettings.tests', 'Blankable', 'string', '')
  210
+        self.assertEqual(Blankable.settings.string, '')
  211
+
  212
+        # And they can be modified in-place
  213
+        Unpopulated.settings.boolean = False
  214
+        Unpopulated.settings.integer = 42
  215
+        Unpopulated.settings.string = 'Caturday'
  216
+        Unpopulated.settings.date = datetime.date(1939, 9, 1)
  217
+        Unpopulated.settings.time = '03:47:00'
  218
+        Unpopulated.settings.datetime = datetime.datetime(1939, 9, 1, 3, 47, 0)
  219
+        # Test correct stripping while we're at it.
  220
+        Unpopulated.settings.list_semi_colon = 'ee@ff.com; gg@hh.com'
  221
+        Unpopulated.settings.list_comma = 'ee@ff.com ,gg@hh.com'
  222
+        self.assertEqual(Unpopulated.settings.boolean, False)
  223
+        self.assertEqual(Unpopulated.settings.integer, 42)
  224
+        self.assertEqual(Unpopulated.settings.string, 'Caturday')
  225
+        self.assertEqual(Unpopulated.settings.list_semi_colon, ['ee@ff.com', 'gg@hh.com'])
  226
+        self.assertEqual(Unpopulated.settings.list_comma, ['ee@ff.com', 'gg@hh.com'])
  227
+        self.assertEqual(Unpopulated.settings.date, datetime.date(1939, 9, 1))
  228
+        self.assertEqual(Unpopulated.settings.time, datetime.time(3, 47, 0))
  229
+        self.assertEqual(Unpopulated.settings.datetime, datetime.datetime(1939, 9, 1, 3, 47, 0))
  230
+
  231
+    def test_declaration(self):
  232
+        "Group declarations can only contain values and a docstring"
  233
+        # This definition is fine
  234
+        attrs = {
  235
+            '__doc__': "This is a docstring",
  236
+            'test': dbsettings.IntegerValue(),
  237
+        }
  238
+        # So this should succeed
  239
+        type('GoodGroup', (dbsettings.Group,), attrs)
  240
+
  241
+        # By adding an invalid attribute
  242
+        attrs['problem'] = 'not a Value'
  243
+        # This should fail
  244
+        self.assertRaises(TypeError, curry(type, 'BadGroup', (dbsettings.Group,), attrs))
  245
+
  246
+        # Make sure affect models get the new permissions
  247
+        self.assert_('can_edit_populated_settings' in dict(Populated._meta.permissions))
  248
+        self.assert_('can_edit_unpopulated_settings' in dict(Unpopulated._meta.permissions))
  249
+
  250
+    def assertCorrectSetting(self, value_class, *key):
  251
+        from dbsettings import loading
  252
+        setting = loading.get_setting(*key)
  253
+        self.assertEqual(key, setting.key)  # Check if setting is registered with proper key
  254
+        self.assertTrue(isinstance(setting, value_class))
  255
+
  256
+    def test_registration(self):
  257
+        "Module and class settings can be mixed up"
  258
+        from dbsettings import BooleanValue, IntegerValue
  259
+        self.assertCorrectSetting(BooleanValue, 'dbsettings.tests', '', 'clash1')
  260
+        self.assertCorrectSetting(IntegerValue, 'dbsettings.tests', 'ModelClash', 'clash1')
  261
+        self.assertCorrectSetting(IntegerValue, 'dbsettings.tests', 'ModelClash', 'clash2')
  262
+        self.assertCorrectSetting(BooleanValue, 'dbsettings.tests', '', 'clash2')
  263
+
  264
+    def test_forms(self):
  265
+        "Forms should display only the appropriate settings"
  266
+        from django.contrib.auth.models import User, Permission
  267
+        from django.core.urlresolvers import reverse
  268
+
  269
+        site_form = reverse(views.site_settings)
  270
+
  271
+        # Set up a users to test the editor forms
  272
+        user = User.objects.create_user('dbsettings', '', 'dbsettings')
  273
+
  274
+        # First test without any authenticated user
  275
+        response = self.client.get(site_form)
  276
+        self.assertTemplateUsed(response, 'admin/login.html')
  277
+
  278
+        # Then test a standard non-staff user
  279
+        self.client.login(username='dbsettings', password='dbsettings')
  280
+        response = self.client.get(site_form)
  281
+        self.assertTemplateUsed(response, 'admin/login.html')
  282
+
  283
+        # Add staff status, but no settings permissions
  284
+        user.is_staff = True
  285
+        user.save()
  286
+
  287
+        # Test the site-wide settings editor
  288
+        response = self.client.get(site_form)
  289
+        self.assertTemplateUsed(response, 'dbsettings/site_settings.html')
  290
+        self.assertEqual(response.context[0]['title'], 'Site settings')
  291
+        # No settings should show up without proper permissions
  292
+        self.assertEqual(len(response.context[0]['form'].fields), 0)
  293
+
  294
+        # Add permissions so that settings will show up
  295
+        perm = Permission.objects.get(codename='can_edit_editable_settings')
  296
+        user.user_permissions.add(perm)
  297
+
  298
+        # Erroneous submissions should be caught by newforms
  299
+        data = {
  300
+            'dbsettings.tests__Editable__integer': '3.5',
  301
+            'dbsettings.tests__Editable__string': '',
  302
+            'dbsettings.tests__Editable__list_semi_colon': '',
  303
+            'dbsettings.tests__Editable__list_comma': '',
  304
+            'dbsettings.tests__Editable__date': '3-77-99',
  305
+            'dbsettings.tests__Editable__time': 'abc',
  306
+            'dbsettings.tests__Editable__datetime': '',
  307
+        }
  308
+        response = self.client.post(site_form, data)
  309
+        self.assertFormError(response, 'form', 'dbsettings.tests__Editable__integer', 'Enter a whole number.')
  310
+        self.assertFormError(response, 'form', 'dbsettings.tests__Editable__string', 'This field is required.')
  311
+        self.assertFormError(response, 'form', 'dbsettings.tests__Editable__list_semi_colon', 'This field is required.')
  312
+        self.assertFormError(response, 'form', 'dbsettings.tests__Editable__list_comma', 'This field is required.')
  313
+        self.assertFormError(response, 'form', 'dbsettings.tests__Editable__date', 'Enter a valid date.')
  314
+        self.assertFormError(response, 'form', 'dbsettings.tests__Editable__time', 'Enter a valid time.')
  315
+        self.assertFormError(response, 'form', 'dbsettings.tests__Editable__datetime', 'This field is required.')
  316
+
  317
+        # Successful submissions should redirect
  318
+        data = {
  319
+            'dbsettings.tests__Editable__integer': '4',
  320
+            'dbsettings.tests__Editable__string': 'Success!',
  321
+            'dbsettings.tests__Editable__list_semi_colon': 'jj@kk.com;ll@mm.com',
  322
+            'dbsettings.tests__Editable__list_comma': 'jj@kk.com,ll@mm.com',
  323
+            'dbsettings.tests__Editable__date': '2012-06-28',
  324
+            'dbsettings.tests__Editable__time': '16:37:45',
  325
+            'dbsettings.tests__Editable__datetime': '2012-06-28 16:37:45',
  326
+        }
  327
+        response = self.client.post(site_form, data)
  328
+        self.assertRedirects(response, site_form)
  329
+
  330
+        # And the data submitted should be immediately available in Python
  331
+        self.assertEqual(Editable.settings.integer, 4)
  332
+        self.assertEqual(Editable.settings.string, 'Success!')
  333
+        self.assertEqual(Editable.settings.list_semi_colon, ['jj@kk.com', 'll@mm.com'])
  334
+        self.assertEqual(Editable.settings.list_comma, ['jj@kk.com', 'll@mm.com'])
  335
+        self.assertEqual(Editable.settings.date, datetime.date(2012, 6, 28))
  336
+        self.assertEqual(Editable.settings.time, datetime.time(16, 37, 45))
  337
+        self.assertEqual(Editable.settings.datetime, datetime.datetime(2012, 6, 28, 16, 37, 45))
0  dbsettings/tests/models.py
No changes.
337  dbsettings/tests/tests.py
... ...
@@ -1,337 +0,0 @@
1  
-import datetime
2  
-
3  
-from django.db import models
4  
-from django import test
5  
-from django.utils.functional import curry
6  
-
7  
-import dbsettings
8  
-from dbsettings import loading, views
9  
-
10  
-# Set up some settings to test
11  
-class TestSettings(dbsettings.Group):
12  
-    boolean = dbsettings.BooleanValue()
13  
-    integer = dbsettings.IntegerValue()
14  
-    string = dbsettings.StringValue()
15  
-    list_semi_colon = dbsettings.MultiSeparatorValue()
16  
-    list_comma = dbsettings.MultiSeparatorValue(separator=',')
17  
-    date = dbsettings.DateValue()
18  
-    time = dbsettings.TimeValue()
19  
-    datetime = dbsettings.DateTimeValue()
20  
-
21  
-# This is assigned to module, rather than a model
22  
-module_settings = TestSettings()
23  
-
24  
-class Defaults(models.Model):
25  
-    class settings(dbsettings.Group):
26  
-        boolean = dbsettings.BooleanValue(default=True)
27  
-        boolean_false = dbsettings.BooleanValue(default=False)
28  
-        integer = dbsettings.IntegerValue(default=1)
29  
-        string = dbsettings.StringValue(default="default")
30  
-        list_semi_colon = dbsettings.MultiSeparatorValue(default=['one','two'])
31  
-        list_comma = dbsettings.MultiSeparatorValue(separator=',',default=('one','two'))
32  
-        date = dbsettings.DateValue(default=datetime.date(2012, 3, 14))
33  
-        time = dbsettings.TimeValue(default=datetime.time(12, 3, 14))
34  
-        datetime = dbsettings.DateTimeValue(default=datetime.datetime(2012, 3, 14, 12, 3, 14))
35  
-    settings = settings()
36  
-
37  
-# These will be populated by the fixture data
38  
-class Populated(models.Model):
39  
-    settings = TestSettings()
40  
-
41  
-# These will be empty after startup
42  
-class Unpopulated(models.Model):
43  
-    settings = TestSettings()
44  
-
45  
-# These will allow blank values
46  
-class Blankable(models.Model):
47  
-    settings = TestSettings()
48  
-
49  
-class Editable(models.Model):
50  
-    settings = TestSettings()
51  
-
52  
-class Combined(models.Model):
53  
-    class settings(dbsettings.Group):
54  
-        enabled = dbsettings.BooleanValue()
55  
-    settings = TestSettings() + settings()
56  
-
57  
-# For registration testing
58  
-class ClashSettings1(dbsettings.Group):
59  
-    clash1 = dbsettings.BooleanValue()
60  
-
61  
-class ClashSettings2(dbsettings.Group):
62  
-    clash2 = dbsettings.BooleanValue()
63  
-
64  
-class ClashSettings1_2(dbsettings.Group):
65  
-    clash1 = dbsettings.IntegerValue()
66  
-    clash2 = dbsettings.IntegerValue()
67  
-
68  
-module_clash1 = ClashSettings1()
69  
-
70  
-class ModelClash(models.Model):
71  
-    settings = ClashSettings1_2()
72  
-
73  
-module_clash2 = ClashSettings2()
74  
-
75  
-
76  
-class SettingsTestCase(test.TestCase):
77  
-    urls = 'dbsettings.tests.test_urls'
78  
-
79  
-    def setUp(self):
80  
-        # Standard test fixtures don't update the in-memory cache.
81  
-        # So we have to do it ourselves this time.
82  
-        loading.set_setting_value('dbsettings.tests.tests', 'Populated', 'boolean', True)
83  
-        loading.set_setting_value('dbsettings.tests.tests', 'Populated', 'integer', 42)
84  
-        loading.set_setting_value('dbsettings.tests.tests', 'Populated', 'string', 'Ni!')
85  
-        loading.set_setting_value('dbsettings.tests.tests', 'Populated', 'list_semi_colon', 'a@b.com;c@d.com;e@f.com')
86  
-        loading.set_setting_value('dbsettings.tests.tests', 'Populated', 'list_comma', 'a@b.com,c@d.com,e@f.com')
87  
-        loading.set_setting_value('dbsettings.tests.tests', 'Populated', 'date', '2012-06-28')
88  
-        loading.set_setting_value('dbsettings.tests.tests', 'Populated', 'time', '16:19:17')
89  
-        loading.set_setting_value('dbsettings.tests.tests', 'Populated', 'datetime', '2012-06-28 16:19:17')
90  
-        loading.set_setting_value('dbsettings.tests.tests', '', 'boolean', False)
91  
-        loading.set_setting_value('dbsettings.tests.tests', '', 'integer', 14)
92  
-        loading.set_setting_value('dbsettings.tests.tests', '', 'string', 'Module')
93  
-        loading.set_setting_value('dbsettings.tests.tests', '', 'list_semi_colon', 'g@h.com;i@j.com;k@l.com')
94  
-        loading.set_setting_value('dbsettings.tests.tests', '', 'list_comma', 'g@h.com,i@j.com,k@l.com')
95  
-        loading.set_setting_value('dbsettings.tests.tests', '', 'date', '2011-05-27')
96  
-        loading.set_setting_value('dbsettings.tests.tests', '', 'time', '15:18:16')
97  
-        loading.set_setting_value('dbsettings.tests.tests', '', 'datetime', '2011-05-27 15:18:16')
98  
-        loading.set_setting_value('dbsettings.tests.tests', 'Combined', 'boolean', False)
99  
-        loading.set_setting_value('dbsettings.tests.tests', 'Combined', 'integer', 1138)
100  
-        loading.set_setting_value('dbsettings.tests.tests', 'Combined', 'string', 'THX')
101  
-        loading.set_setting_value('dbsettings.tests.tests', 'Combined', 'list_semi_colon', 'm@n.com;o@p.com;q@r.com')
102  
-        loading.set_setting_value('dbsettings.tests.tests', 'Combined', 'list_comma', 'm@n.com,o@p.com,q@r.com')
103  
-        loading.set_setting_value('dbsettings.tests.tests', 'Combined', 'date', '2010-04-26')
104  
-        loading.set_setting_value('dbsettings.tests.tests', 'Combined', 'time', '14:17:15')
105  
-        loading.set_setting_value('dbsettings.tests.tests', 'Combined', 'datetime', '2010-04-26 14:17:15')
106  
-        loading.set_setting_value('dbsettings.tests.tests', 'Combined', 'enabled', True)
107  
-
108  
-    def test_settings(self):
109  
-        "Make sure settings groups are initialized properly"
110  
-
111  
-        # Settings already in the database are available immediately
112  
-        self.assertEqual(Populated.settings.boolean, True)
113  
-        self.assertEqual(Populated.settings.integer, 42)
114  
-        self.assertEqual(Populated.settings.string, 'Ni!')
115  
-        self.assertEqual(Populated.settings.list_semi_colon, ['a@b.com', 'c@d.com', 'e@f.com'])
116  
-        self.assertEqual(Populated.settings.list_comma, ['a@b.com', 'c@d.com', 'e@f.com'])
117  
-        self.assertEqual(Populated.settings.date, datetime.date(2012, 6, 28))
118  
-        self.assertEqual(Populated.settings.time, datetime.time(16, 19, 17))
119  
-        self.assertEqual(Populated.settings.datetime, datetime.datetime(2012, 6, 28, 16, 19, 17))
120  
-
121  
-        # Module settings are kept separate from model settings
122  
-        self.assertEqual(module_settings.boolean, False)
123  
-        self.assertEqual(module_settings.integer, 14)
124  
-        self.assertEqual(module_settings.string, 'Module')
125  
-        self.assertEqual(module_settings.list_semi_colon, ['g@h.com', 'i@j.com', 'k@l.com'])
126  
-        self.assertEqual(module_settings.list_comma, ['g@h.com', 'i@j.com', 'k@l.com'])
127  
-        self.assertEqual(module_settings.date, datetime.date(2011, 5, 27))
128  
-        self.assertEqual(module_settings.time, datetime.time(15, 18, 16))
129  
-        self.assertEqual(module_settings.datetime, datetime.datetime(2011, 5, 27, 15, 18, 16))
130  
-
131  
-        # Settings can be added together
132  
-        self.assertEqual(Combined.settings.boolean, False)
133  
-        self.assertEqual(Combined.settings.integer, 1138)
134  
-        self.assertEqual(Combined.settings.string, 'THX')
135  
-        self.assertEqual(Combined.settings.enabled, True)
136  
-        self.assertEqual(Combined.settings.list_semi_colon, ['m@n.com', 'o@p.com', 'q@r.com'])
137  
-        self.assertEqual(Combined.settings.list_comma, ['m@n.com', 'o@p.com', 'q@r.com'])
138  
-        self.assertEqual(Combined.settings.date, datetime.date(2010, 4, 26))
139  
-        self.assertEqual(Combined.settings.time, datetime.time(14, 17, 15))
140  
-        self.assertEqual(Combined.settings.datetime, datetime.datetime(2010, 4, 26, 14, 17, 15))
141  
-
142  
-        # Settings not in the database use empty defaults
143  
-        self.assertEqual(Unpopulated.settings.boolean, False)
144  
-        self.assertEqual(Unpopulated.settings.integer, None)
145  
-        self.assertEqual(Unpopulated.settings.string, '')
146  
-        self.assertEqual(Unpopulated.settings.list_semi_colon, [])
147  
-        self.assertEqual(Unpopulated.settings.list_comma, [])
148  
-
149  
-        # ...Unless a default paramter was specified, then they use that
150  
-        self.assertEqual(Defaults.settings.boolean, True)
151  
-        self.assertEqual(Defaults.settings.boolean_false, False)
152  
-        self.assertEqual(Defaults.settings.integer, 1)
153  
-        self.assertEqual(Defaults.settings.string, 'default')
154  
-        self.assertEqual(Defaults.settings.list_semi_colon, ['one','two'])
155  
-        self.assertEqual(Defaults.settings.list_comma, ['one','two'])
156  
-        self.assertEqual(Defaults.settings.date, datetime.date(2012, 3, 14))
157  
-        self.assertEqual(Defaults.settings.time, datetime.time(12, 3, 14))
158  
-        self.assertEqual(Defaults.settings.datetime, datetime.datetime(2012, 3, 14, 12, 3, 14))
159  
-
160  
-
161  
-        # Settings should be retrieved in the order of definition
162  
-        self.assertEqual(Populated.settings.keys(),
163  
-                         ['boolean', 'integer', 'string', 'list_semi_colon',
164  
-                          'list_comma', 'date', 'time', 'datetime'])
165  
-        self.assertEqual(Combined.settings.keys(),
166  
-                         ['boolean', 'integer', 'string', 'list_semi_colon',
167  
-                          'list_comma', 'date', 'time', 'datetime', 'enabled'])
168  
-
169  
-        # Values should be coerced to the proper Python types
170  
-        self.assert_(isinstance(Populated.settings.boolean, bool))
171  
-        self.assert_(isinstance(Populated.settings.integer, int))
172  
-        self.assert_(isinstance(Populated.settings.string, basestring))
173  
-
174  
-        # Settings can not be accessed directly from models, only instances
175  
-        self.assertRaises(AttributeError, lambda: Populated().settings)
176  
-        self.assertRaises(AttributeError, lambda: Unpopulated().settings)
177  
-
178  
-        # Updates are reflected in the live settings
179  
-        loading.set_setting_value('dbsettings.tests.tests', 'Unpopulated', 'boolean', True)
180  
-        loading.set_setting_value('dbsettings.tests.tests', 'Unpopulated', 'integer', 13)
181  
-        loading.set_setting_value('dbsettings.tests.tests', 'Unpopulated', 'string', 'Friday')
182  
-        loading.set_setting_value('dbsettings.tests.tests', 'Unpopulated', 'list_semi_colon', 'aa@bb.com;cc@dd.com')
183  
-        loading.set_setting_value('dbsettings.tests.tests', 'Unpopulated', 'list_comma', 'aa@bb.com,cc@dd.com')
184  
-        # for date/time you can specify string (as above) or proper object
185  
-        loading.set_setting_value('dbsettings.tests.tests', 'Unpopulated', 'date', datetime.date(1912, 6, 23))
186  
-        loading.set_setting_value('dbsettings.tests.tests', 'Unpopulated', 'time', datetime.time(1, 2, 3))
187  
-        loading.set_setting_value('dbsettings.tests.tests', 'Unpopulated', 'datetime', datetime.datetime(1912, 6, 23, 1, 2, 3))
188  
-
189  
-        self.assertEqual(Unpopulated.settings.boolean, True)
190  
-        self.assertEqual(Unpopulated.settings.integer, 13)
191  
-        self.assertEqual(Unpopulated.settings.string, 'Friday')
192  
-        self.assertEqual(Unpopulated.settings.list_semi_colon, ['aa@bb.com', 'cc@dd.com'])
193  
-        self.assertEqual(Unpopulated.settings.list_comma, ['aa@bb.com', 'cc@dd.com'])
194  
-        self.assertEqual(Unpopulated.settings.date, datetime.date(1912, 6, 23))
195  
-        self.assertEqual(Unpopulated.settings.time, datetime.time(1, 2, 3))
196  
-        self.assertEqual(Unpopulated.settings.datetime, datetime.datetime(1912, 6, 23, 1, 2, 3))
197  
-
198  
-        # Updating settings with defaults
199  
-        loading.set_setting_value('dbsettings.tests.tests', 'Defaults', 'boolean', False)
200  
-        self.assertEqual(Defaults.settings.boolean, False)
201  
-        loading.set_setting_value('dbsettings.tests.tests', 'Defaults', 'boolean_false', True)
202  
-        self.assertEqual(Defaults.settings.boolean_false, True)
203  
-
204  
-
205  
-        # Updating blankable settings
206  
-        self.assertEqual(Blankable.settings.string, '')
207  
-        loading.set_setting_value('dbsettings.tests.tests', 'Blankable', 'string', 'Eli')
208  
-        self.assertEqual(Blankable.settings.string, 'Eli')
209  
-        loading.set_setting_value('dbsettings.tests.tests', 'Blankable', 'string', '')
210  
-        self.assertEqual(Blankable.settings.string, '')
211  
-
212  
-        # And they can be modified in-place
213  
-        Unpopulated.settings.boolean = False
214  
-        Unpopulated.settings.integer = 42
215  
-        Unpopulated.settings.string = 'Caturday'
216  
-        Unpopulated.settings.date = datetime.date(1939, 9, 1)
217  
-        Unpopulated.settings.time = '03:47:00'
218  
-        Unpopulated.settings.datetime = datetime.datetime(1939, 9, 1, 3, 47, 0)
219  
-        # Test correct stripping while we're at it.
220  
-        Unpopulated.settings.list_semi_colon = 'ee@ff.com; gg@hh.com'
221  
-        Unpopulated.settings.list_comma = 'ee@ff.com ,gg@hh.com'
222  
-        self.assertEqual(Unpopulated.settings.boolean, False)
223  
-        self.assertEqual(Unpopulated.settings.integer, 42)
224  
-        self.assertEqual(Unpopulated.settings.string, 'Caturday')
225  
-        self.assertEqual(Unpopulated.settings.list_semi_colon, ['ee@ff.com', 'gg@hh.com'])
226  
-        self.assertEqual(Unpopulated.settings.list_comma, ['ee@ff.com', 'gg@hh.com'])
227  
-        self.assertEqual(Unpopulated.settings.date, datetime.date(1939, 9, 1))
228  
-        self.assertEqual(Unpopulated.settings.time, datetime.time(3, 47, 0))
229  
-        self.assertEqual(Unpopulated.settings.datetime, datetime.datetime(1939, 9, 1, 3, 47, 0))
230  
-
231  
-    def test_declaration(self):
232  
-        "Group declarations can only contain values and a docstring"
233  
-        # This definition is fine
234  
-        attrs = {
235  
-            '__doc__': "This is a docstring",
236  
-            'test': dbsettings.IntegerValue(),
237  
-        }
238  
-        # So this should succeed
239  
-        type('GoodGroup', (dbsettings.Group,), attrs)
240  
-
241  
-        # By adding an invalid attribute
242  
-        attrs['problem'] = 'not a Value'
243  
-        # This should fail
244  
-        self.assertRaises(TypeError, curry(type, 'BadGroup', (dbsettings.Group,), attrs))
245  
-
246  
-        # Make sure affect models get the new permissions
247  
-        self.assert_('can_edit_populated_settings' in dict(Populated._meta.permissions))
248  
-        self.assert_('can_edit_unpopulated_settings' in dict(Unpopulated._meta.permissions))
249  
-
250  
-    def assertCorrectSetting(self, value_class, *key):
251  
-        from dbsettings import loading
252  
-        setting = loading.get_setting(*key)
253  
-        self.assertEqual(key, setting.key)  # Check if setting is registered with proper key
254  
-        self.assertTrue(isinstance(setting, value_class))
255  
-
256  
-    def test_registration(self):
257  
-        "Module and class settings can be mixed up"
258  
-        from dbsettings import BooleanValue, IntegerValue
259  
-        self.assertCorrectSetting(BooleanValue, 'dbsettings.tests.tests', '', 'clash1')
260  
-        self.assertCorrectSetting(IntegerValue, 'dbsettings.tests.tests', 'ModelClash', 'clash1')
261  
-        self.assertCorrectSetting(IntegerValue, 'dbsettings.tests.tests', 'ModelClash', 'clash2')
262  
-        self.assertCorrectSetting(BooleanValue, 'dbsettings.tests.tests', '', 'clash2')
263  
-
264  
-    def test_forms(self):
265  
-        "Forms should display only the appropriate settings"
266  
-        from django.contrib.auth.models import User, Permission
267  
-        from django.core.urlresolvers import reverse
268  
-
269  
-        site_form = reverse(views.site_settings)
270  
-
271  
-        # Set up a users to test the editor forms
272  
-        user = User.objects.create_user('dbsettings', '', 'dbsettings')
273  
-
274  
-        # First test without any authenticated user
275  
-        response = self.client.get(site_form)
276  
-        self.assertTemplateUsed(response, 'admin/login.html')
277  
-
278  
-        # Then test a standard non-staff user
279  
-        self.client.login(username='dbsettings', password='dbsettings')
280  
-        response = self.client.get(site_form)
281  
-        self.assertTemplateUsed(response, 'admin/login.html')
282  
-
283  
-        # Add staff status, but no settings permissions
284  
-        user.is_staff = True
285  
-        user.save()
286  
-
287  
-        # Test the site-wide settings editor
288  
-        response = self.client.get(site_form)
289  
-        self.assertTemplateUsed(response, 'dbsettings/site_settings.html')
290  
-        self.assertEqual(response.context[0]['title'], 'Site settings')
291  
-        # No settings should show up without proper permissions
292  
-        self.assertEqual(len(response.context[0]['form'].fields), 0)
293  
-
294  
-        # Add permissions so that settings will show up
295  
-        perm = Permission.objects.get(codename='can_edit_editable_settings')
296  
-        user.user_permissions.add(perm)
297  
-
298  
-        # Erroneous submissions should be caught by newforms
299  
-        data = {
300  
-            'dbsettings.tests.tests__Editable__integer': '3.5',
301  
-            'dbsettings.tests.tests__Editable__string': '',
302  
-            'dbsettings.tests.tests__Editable__list_semi_colon': '',
303  
-            'dbsettings.tests.tests__Editable__list_comma': '',
304  
-            'dbsettings.tests.tests__Editable__date': '3-77-99',
305  
-            'dbsettings.tests.tests__Editable__time': 'abc',
306  
-            'dbsettings.tests.tests__Editable__datetime': '',
307  
-        }
308  
-        response = self.client.post(site_form, data)
309  
-        self.assertFormError(response, 'form', 'dbsettings.tests.tests__Editable__integer', 'Enter a whole number.')
310  
-        self.assertFormError(response, 'form', 'dbsettings.tests.tests__Editable__string', 'This field is required.')
311  
-        self.assertFormError(response, 'form', 'dbsettings.tests.tests__Editable__list_semi_colon', 'This field is required.')
312  
-        self.assertFormError(response, 'form', 'dbsettings.tests.tests__Editable__list_comma', 'This field is required.')
313  
-        self.assertFormError(response, 'form', 'dbsettings.tests.tests__Editable__date', 'Enter a valid date.')
314  
-        self.assertFormError(response, 'form', 'dbsettings.tests.tests__Editable__time', 'Enter a valid time.')
315  
-        self.assertFormError(response, 'form', 'dbsettings.tests.tests__Editable__datetime', 'This field is required.')
316  
-
317  
-        # Successful submissions should redirect
318  
-        data = {
319  
-            'dbsettings.tests.tests__Editable__integer': '4',
320  
-            'dbsettings.tests.tests__Editable__string': 'Success!',
321  
-            'dbsettings.tests.tests__Editable__list_semi_colon': 'jj@kk.com;ll@mm.com',
322  
-            'dbsettings.tests.tests__Editable__list_comma': 'jj@kk.com,ll@mm.com',
323  
-            'dbsettings.tests.tests__Editable__date': '2012-06-28',
324  
-            'dbsettings.tests.tests__Editable__time': '16:37:45',
325  
-            'dbsettings.tests.tests__Editable__datetime': '2012-06-28 16:37:45',
326  
-        }
327  
-        response = self.client.post(site_form, data)
328  
-        self.assertRedirects(response, site_form)
329  
-
330  
-        # And the data submitted should be immediately available in Python
331  
-        self.assertEqual(Editable.settings.integer, 4)
332  
-        self.assertEqual(Editable.settings.string, 'Success!')
333  
-        self.assertEqual(Editable.settings.list_semi_colon, ['jj@kk.com', 'll@mm.com'])
334  
-        self.assertEqual(Editable.settings.list_comma, ['jj@kk.com', 'll@mm.com'])
335  
-        self.assertEqual(Editable.settings.date, datetime.date(2012, 6, 28))
336  
-        self.assertEqual(Editable.settings.time, datetime.time(16, 37, 45))
337  
-        self.assertEqual(Editable.settings.datetime, datetime.datetime(2012, 6, 28, 16, 37, 45))
29  dbsettings/tests/runtests.py → runtests.py 100644 → 100755
... ...
@@ -1,9 +1,7 @@
1 1
 #!/usr/bin/env python
2  
-import os
3  
-import sys
4  
-
5 2
 from django import VERSION as DJANGO_VERSION
6 3
 from django.conf import settings
  4
+from django.core.management import call_command
7 5
 
8 6
 
9 7
 INSTALLED_APPS = (
@@ -15,7 +13,6 @@
15 13
     'django.contrib.sessions',
16 14
     # Our app and it's test app.
17 15
     'dbsettings',
18  
-    'dbsettings.tests'
19 16
 )
20 17
 
21 18
 SETTINGS = {
@@ -32,7 +29,6 @@
32 29
             'NAME': ':memory:',
33 30
         }
34 31
     }
35  
-
36 32
 else:
37 33
     # Pre multi-db settings.
38 34
     SETTINGS['DATABASE_ENGINE'] = 'sqlite3'
@@ -41,25 +37,4 @@
41 37
 if not settings.configured:
42 38
     settings.configure(**SETTINGS)
43 39
 
44  
-try:
45  
-    from django.test.simple import run_tests
46  
-except ImportError:
47  
-    from django.test.simple import DjangoTestSuiteRunner
48  
-    run_tests = DjangoTestSuiteRunner(verbosity=1, interactive=True).run_tests
49  
-
50  
-
51  
-def runtests(*test_args):
52  
-    if not test_args:
53  
-        test_args = ['tests']
54  
-    parent = os.path.join(
55  
-        os.path.dirname(os.path.abspath(__file__)),
56  
-        "..",
57  
-        "..",
58  
-    )
59  
-    sys.path.insert(0, parent)
60  
-    failures = run_tests(test_args, verbosity=1, interactive=True)
61  
-    sys.exit(failures)
62  
-
63  
-
64  
-if __name__ == '__main__':
65  
-    runtests(*sys.argv[1:])
  40
+call_command('test', 'dbsettings')

0 notes on commit e7613ca

Please sign in to comment.
Something went wrong with that request. Please try again.