Code

Ticket #7198: 7198_fix_with_tests.diff

File 7198_fix_with_tests.diff, 2.5 KB (added by gabrielhurley, 3 years ago)

fixes string interpolation, adds tests (with improvements by carljm)

Line 
1Index: tests/modeltests/empty/no_models/__init__.py
2===================================================================
3Index: tests/modeltests/empty/no_models/tests.py
4===================================================================
5--- tests/modeltests/empty/no_models/tests.py   (revision 0)
6+++ tests/modeltests/empty/no_models/tests.py   (revision 0)
7@@ -0,0 +1,5 @@
8+from django.test import TestCase
9+
10+class NoModelTests(TestCase):
11+    """ A placeholder test case. See modeltests.empty.tests for more info. """
12+    pass
13Index: tests/modeltests/empty/tests.py
14===================================================================
15--- tests/modeltests/empty/tests.py     (revision 16887)
16+++ tests/modeltests/empty/tests.py     (working copy)
17@@ -1,4 +1,10 @@
18+from __future__ import with_statement
19+
20+from django.conf import settings
21+from django.core.exceptions import ImproperlyConfigured
22+from django.db.models.loading import get_app
23 from django.test import TestCase
24+from django.test.utils import override_settings
25 
26 from models import Empty
27 
28@@ -13,3 +19,19 @@
29         self.assertTrue(m.id is not None)
30         existing = Empty(m.id)
31         existing.save()
32+
33+class NoModelTests(TestCase):
34+    """
35+    Test for #7198 to ensure that the proper error message is raised
36+    when attempting to load an app with no models.py file.
37+
38+    Becuase the test runner won't currently load a test module with no
39+    models.py file, this TestCase instead lives in this module.
40+
41+    It seemed like an appropriate home for it.
42+    """
43+    @override_settings(INSTALLED_APPS=("modeltests.empty.no_models",))
44+    def test_no_models(self):
45+        with self.assertRaisesRegexp(ImproperlyConfigured,
46+                    'App with label no_models is missing a models.py module.'):
47+            get_app('no_models')
48Index: django/db/models/loading.py
49===================================================================
50--- django/db/models/loading.py (revision 16887)
51+++ django/db/models/loading.py (working copy)
52@@ -146,7 +146,7 @@
53                     if mod is None:
54                         if emptyOK:
55                             return None
56-                        raise ImproperlyConfigured("App with label %s is missing a models.py module.")
57+                        raise ImproperlyConfigured("App with label %s is missing a models.py module." % app_label)
58                     else:
59                         return mod
60             raise ImproperlyConfigured("App with label %s could not be found" % app_label)