Code

Ticket #7835: 7835.test_extra_apps.2.diff

File 7835.test_extra_apps.2.diff, 13.0 KB (added by julien, 5 years ago)

patch+test+doc

Line 
1Index: django/django/contrib/comments/tests/__init__.py
2===================================================================
3--- django/django/contrib/comments/tests/__init__.py    (revision 0)
4+++ django/django/contrib/comments/tests/__init__.py    (working copy)
5@@ -4,7 +4,7 @@
6 from django.contrib.contenttypes.models import ContentType
7 from django.contrib.sites.models import Site
8 from django.test import TestCase
9-from regressiontests.comment_tests.models import Article, Author
10+from django.contrib.comments.tests.test_comments.models import Article, Author
11 
12 # Shortcut
13 CT = ContentType.objects.get_for_model
14@@ -13,6 +13,15 @@
15 class CommentTestCase(TestCase):
16     fixtures = ["comment_tests"]
17     urls = 'django.contrib.comments.urls'
18+    installed_apps = (
19+            'django.contrib.sessions',
20+            'django.contrib.sites',
21+            'django.contrib.admin',
22+            'django.contrib.auth',
23+            'django.contrib.comments',
24+            'django.contrib.contenttypes',
25+            'django.contrib.comments.tests.test_comments'
26+        )
27 
28     def createSomeComments(self):
29         # Two anonymous comments on two different objects
30@@ -80,9 +89,9 @@
31         d.update(f.initial)
32         return d
33 
34-from regressiontests.comment_tests.tests.app_api_tests import *
35-from regressiontests.comment_tests.tests.model_tests import *
36-from regressiontests.comment_tests.tests.comment_form_tests import *
37-from regressiontests.comment_tests.tests.templatetag_tests import *
38-from regressiontests.comment_tests.tests.comment_view_tests import *
39-from regressiontests.comment_tests.tests.moderation_view_tests import *
40+from django.contrib.comments.tests.app_api_tests import *
41+from django.contrib.comments.tests.model_tests import *
42+from django.contrib.comments.tests.comment_form_tests import *
43+from django.contrib.comments.tests.templatetag_tests import *
44+from django.contrib.comments.tests.comment_view_tests import *
45+from django.contrib.comments.tests.moderation_view_tests import *
46Index: django/django/contrib/comments/tests/app_api_tests.py
47===================================================================
48--- django/django/contrib/comments/tests/app_api_tests.py       (revision 0)
49+++ django/django/contrib/comments/tests/app_api_tests.py       (working copy)
50@@ -2,7 +2,7 @@
51 from django.contrib import comments
52 from django.contrib.comments.models import Comment
53 from django.contrib.comments.forms import CommentForm
54-from regressiontests.comment_tests.tests import CommentTestCase
55+from django.contrib.comments.tests import CommentTestCase
56 
57 class CommentAppAPITests(CommentTestCase):
58     """Tests for the "comment app" API"""
59Index: django/django/contrib/comments/tests/comment_form_tests.py
60===================================================================
61--- django/django/contrib/comments/tests/comment_form_tests.py  (revision 0)
62+++ django/django/contrib/comments/tests/comment_form_tests.py  (working copy)
63@@ -2,8 +2,8 @@
64 from django.conf import settings
65 from django.contrib.comments.models import Comment
66 from django.contrib.comments.forms import CommentForm
67-from regressiontests.comment_tests.models import Article
68-from regressiontests.comment_tests.tests import CommentTestCase
69+from django.contrib.comments.tests.test_comments.models import Article
70+from django.contrib.comments.tests import CommentTestCase
71 
72 class CommentFormTests(CommentTestCase):
73 
74Index: django/django/contrib/comments/tests/comment_view_tests.py
75===================================================================
76--- django/django/contrib/comments/tests/comment_view_tests.py  (revision 0)
77+++ django/django/contrib/comments/tests/comment_view_tests.py  (working copy)
78@@ -2,8 +2,8 @@
79 from django.contrib.auth.models import User
80 from django.contrib.comments import signals
81 from django.contrib.comments.models import Comment
82-from regressiontests.comment_tests.models import Article
83-from regressiontests.comment_tests.tests import CommentTestCase
84+from django.contrib.comments.tests.test_comments.models import Article
85+from django.contrib.comments.tests import CommentTestCase
86 
87 class CommentViewTests(CommentTestCase):
88 
89Index: django/django/contrib/comments/tests/model_tests.py
90===================================================================
91--- django/django/contrib/comments/tests/model_tests.py (revision 0)
92+++ django/django/contrib/comments/tests/model_tests.py (working copy)
93@@ -1,6 +1,6 @@
94 from django.contrib.comments.models import Comment
95-from regressiontests.comment_tests.models import Author, Article
96-from regressiontests.comment_tests.tests import CommentTestCase
97+from django.contrib.comments.tests.test_comments.models import Author, Article
98+from django.contrib.comments.tests import CommentTestCase
99 
100 class CommentModelTests(CommentTestCase):
101 
102Index: django/django/contrib/comments/tests/moderation_view_tests.py
103===================================================================
104--- django/django/contrib/comments/tests/moderation_view_tests.py       (revision 0)
105+++ django/django/contrib/comments/tests/moderation_view_tests.py       (working copy)
106@@ -1,7 +1,7 @@
107 from django.contrib.comments.models import Comment, CommentFlag
108 from django.contrib.auth.models import User, Permission
109 from django.contrib.contenttypes.models import ContentType
110-from regressiontests.comment_tests.tests import CommentTestCase
111+from django.contrib.comments.tests import CommentTestCase
112 from django.contrib.comments import signals
113 
114 class FlagViewTests(CommentTestCase):
115Index: django/django/contrib/comments/tests/templatetag_tests.py
116===================================================================
117--- django/django/contrib/comments/tests/templatetag_tests.py   (revision 0)
118+++ django/django/contrib/comments/tests/templatetag_tests.py   (working copy)
119@@ -1,8 +1,8 @@
120 from django.contrib.comments.forms import CommentForm
121 from django.contrib.comments.models import Comment
122 from django.template import Template, Context
123-from regressiontests.comment_tests.models import Article, Author
124-from regressiontests.comment_tests.tests import CommentTestCase
125+from django.contrib.comments.tests.test_comments.models import Article, Author
126+from django.contrib.comments.tests import CommentTestCase
127 
128 class CommentTemplateTagTests(CommentTestCase):
129 
130@@ -16,7 +16,7 @@
131         self.assertEqual(out, "/post/")
132 
133     def testGetCommentForm(self, tag=None):
134-        t = "{% load comments %}" + (tag or "{% get_comment_form for comment_tests.article a.id as form %}")
135+        t = "{% load comments %}" + (tag or "{% get_comment_form for test_comments.article a.id as form %}")
136         ctx, out = self.render(t, a=Article.objects.get(pk=1))
137         self.assertEqual(out, "")
138         self.assert_(isinstance(ctx["form"], CommentForm))
139@@ -22,7 +22,7 @@
140         self.assert_(isinstance(ctx["form"], CommentForm))
141 
142     def testGetCommentFormFromLiteral(self):
143-        self.testGetCommentForm("{% get_comment_form for comment_tests.article 1 as form %}")
144+        self.testGetCommentForm("{% get_comment_form for test_comments.article 1 as form %}")
145 
146     def testGetCommentFormFromObject(self):
147         self.testGetCommentForm("{% get_comment_form for a as form %}")
148@@ -28,7 +28,7 @@
149         self.testGetCommentForm("{% get_comment_form for a as form %}")
150 
151     def testRenderCommentForm(self, tag=None):
152-        t = "{% load comments %}" + (tag or "{% render_comment_form for comment_tests.article a.id %}")
153+        t = "{% load comments %}" + (tag or "{% render_comment_form for test_comments.article a.id %}")
154         ctx, out = self.render(t, a=Article.objects.get(pk=1))
155         self.assert_(out.strip().startswith("<form action="))
156         self.assert_(out.strip().endswith("</form>"))
157@@ -34,7 +34,7 @@
158         self.assert_(out.strip().endswith("</form>"))
159 
160     def testRenderCommentFormFromLiteral(self):
161-        self.testRenderCommentForm("{% render_comment_form for comment_tests.article 1 %}")
162+        self.testRenderCommentForm("{% render_comment_form for test_comments.article 1 %}")
163 
164     def testRenderCommentFormFromObject(self):
165         self.testRenderCommentForm("{% render_comment_form for a %}")
166@@ -41,7 +41,7 @@
167 
168     def testGetCommentCount(self, tag=None):
169         self.createSomeComments()
170-        t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}"
171+        t = "{% load comments %}" + (tag or "{% get_comment_count for test_comments.article a.id as cc %}") + "{{ cc }}"
172         ctx, out = self.render(t, a=Article.objects.get(pk=1))
173         self.assertEqual(out, "2")
174 
175@@ -46,7 +46,7 @@
176         self.assertEqual(out, "2")
177 
178     def testGetCommentCountFromLiteral(self):
179-        self.testGetCommentCount("{% get_comment_count for comment_tests.article 1 as cc %}")
180+        self.testGetCommentCount("{% get_comment_count for test_comments.article 1 as cc %}")
181 
182     def testGetCommentCountFromObject(self):
183         self.testGetCommentCount("{% get_comment_count for a as cc %}")
184@@ -53,7 +53,7 @@
185 
186     def testGetCommentList(self, tag=None):
187         c1, c2, c3, c4 = self.createSomeComments()
188-        t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")
189+        t = "{% load comments %}" + (tag or "{% get_comment_list for test_comments.author a.id as cl %}")
190         ctx, out = self.render(t, a=Author.objects.get(pk=1))
191         self.assertEqual(out, "")
192         self.assertEqual(list(ctx["cl"]), [c2])
193@@ -59,7 +59,7 @@
194         self.assertEqual(list(ctx["cl"]), [c2])
195 
196     def testGetCommentListFromLiteral(self):
197-        self.testGetCommentList("{% get_comment_list for comment_tests.author 1 as cl %}")
198+        self.testGetCommentList("{% get_comment_list for test_comments.author 1 as cl %}")
199 
200     def testGetCommentListFromObject(self):
201         self.testGetCommentList("{% get_comment_list for a as cl %}")
202Index: django/django/test/testcases.py
203===================================================================
204--- django/django/test/testcases.py     (revision 9536)
205+++ django/django/test/testcases.py     (working copy)
206@@ -8,6 +8,7 @@
207 from django.core.management import call_command
208 from django.core.urlresolvers import clear_url_caches
209 from django.db import transaction
210+from django.db.models.loading import load_app
211 from django.http import QueryDict
212 from django.test import _doctest as doctest
213 from django.test.client import Client
214@@ -173,6 +174,11 @@
215     def _pre_setup(self):
216         """Performs any pre-test setup. This includes:
217 
218+            * If the Test Case class has a 'installed_apps' member, replace the
219+              INSTALLED_APPS with it.
220+            * If the Test Case class has a 'extra_apps' member, append it to
221+              INSTALLED_APPS.
222+            * Create tables and load models for test apps.
223             * Flushing the database.
224             * If the Test Case class has a 'fixtures' member, installing the
225               named fixtures.
226@@ -180,6 +186,21 @@
227               ROOT_URLCONF with it.
228             * Clearing the mail test outbox.
229         """
230+        if hasattr(self, 'installed_apps'):
231+            self._old_installed_apps = settings.INSTALLED_APPS
232+            settings.INSTALLED_APPS = list(self.installed_apps)
233+        if hasattr(self, 'extra_apps'):
234+            if hasattr(self, '_old_installed_apps'):
235+                # extra_apps and installed_apps can coexist
236+                settings.INSTALLED_APPS += list(self.extra_apps)
237+            else:
238+                self._old_installed_apps = settings.INSTALLED_APPS
239+                settings.INSTALLED_APPS += list(self.extra_apps)
240+        if hasattr(self, '_old_installed_apps'):
241+            for app_label in settings.INSTALLED_APPS:
242+                load_app(app_label)
243+            # Create the new tables in db
244+            call_command('syncdb', verbosity=0, interactive=False)
245         call_command('flush', verbosity=0, interactive=False)
246         if hasattr(self, 'fixtures'):
247             # We have to use this slightly awkward syntax due to the fact
248@@ -220,6 +241,8 @@
249         """ Performs any post-test things. This includes:
250 
251             * Putting back the original ROOT_URLCONF if it was changed.
252+            * Putting back the original INSTALLED_APPS if it was changed.
253+            * Unload models and destroy tables for test apps.
254         """
255         if hasattr(self, '_old_root_urlconf'):
256             settings.ROOT_URLCONF = self._old_root_urlconf
257@@ -224,6 +247,9 @@
258         if hasattr(self, '_old_root_urlconf'):
259             settings.ROOT_URLCONF = self._old_root_urlconf
260             clear_url_caches()
261+        if hasattr(self, '_old_installed_apps'):
262+            settings.INSTALLED_APPS = self._old_installed_apps
263+            #TODO: test apps should be unloaded from the app cache, and the extra tables destroyed.
264 
265     def assertRedirects(self, response, expected_url, status_code=302,
266                         target_status_code=200, host=None):