Code

Ticket #12308: t12308_2.diff

File t12308_2.diff, 3.3 KB (added by andrewsk, 4 years ago)

added tests

Line 
1diff --git a/django/db/backends/postgresql/operations.py b/django/db/backends/postgresql/operations.py
2index f517436..1f5fb6e 100644
3--- a/django/db/backends/postgresql/operations.py
4+++ b/django/db/backends/postgresql/operations.py
5@@ -133,6 +133,10 @@ class DatabaseOperations(BaseDatabaseOperations):
6                         style.SQL_TABLE(qn(f.m2m_db_table()))))
7         return output
8 
9+    def tablespace_sql(self, tablespace, inline=False):
10+        return "%sTABLESPACE %s" % ((inline and "USING INDEX " or ""),
11+            self.quote_name(tablespace))
12+
13     def savepoint_create_sql(self, sid):
14         return "SAVEPOINT %s" % sid
15 
16diff --git a/tests/modeltests/tablespaces/__init__.py b/tests/modeltests/tablespaces/__init__.py
17new file mode 100644
18index 0000000..e69de29
19diff --git a/tests/modeltests/tablespaces/models.py b/tests/modeltests/tablespaces/models.py
20new file mode 100644
21index 0000000..e69de29
22diff --git a/tests/modeltests/tablespaces/tablespaces_app/__init__.py b/tests/modeltests/tablespaces/tablespaces_app/__init__.py
23new file mode 100644
24index 0000000..e69de29
25diff --git a/tests/modeltests/tablespaces/tablespaces_app/models.py b/tests/modeltests/tablespaces/tablespaces_app/models.py
26new file mode 100644
27index 0000000..947a0b2
28--- /dev/null
29+++ b/tests/modeltests/tablespaces/tablespaces_app/models.py
30@@ -0,0 +1,16 @@
31+from django.db import models
32+
33+class TestModel1(models.Model):
34+    """
35+    Model with overriden tablespace
36+    """
37+    name = models.CharField(max_length=255, default='')
38+
39+    class Meta:
40+        db_tablespace = "custom1"
41+
42+class TestModel2(models.Model):
43+    """
44+    Model with default tablespace
45+    """
46+    name = models.CharField(max_length=255, default='')
47diff --git a/tests/modeltests/tablespaces/tests.py b/tests/modeltests/tablespaces/tests.py
48new file mode 100644
49index 0000000..3fc3c58
50--- /dev/null
51+++ b/tests/modeltests/tablespaces/tests.py
52@@ -0,0 +1,27 @@
53+from django.core.management import call_command
54+from django.test import TestCase
55+from django.conf import settings
56+import sys
57+from StringIO import StringIO
58+from django.db import DEFAULT_DB_ALIAS
59+
60+if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'].startswith('django.db.backends.postgresql'):
61+    class TablespacesTests(TestCase):
62+
63+        def setUp(self):
64+            self.old_default_tablespace = settings.DEFAULT_TABLESPACE
65+            settings.DEFAULT_TABLESPACE = "custom"
66+            self.old_installed_apps = settings.INSTALLED_APPS
67+            settings.INSTALLED_APPS+=['modeltests.tablespaces.tablespaces_app']
68+
69+        def tearDown(self):
70+            settings.DEFAULT_TABLESPACE = self.old_default_tablespace
71+            settings.INSTALLED_APPS = self.old_installed_apps
72+
73+        def test_tablespace(self):
74+            sys.stdout = StringIO()
75+            call_command("sql", "tablespaces_app")
76+            output = sys.stdout.getvalue().strip()
77+            sys.stdout = sys.__stdout__
78+            result = 'BEGIN;\nCREATE TABLE "tablespaces_app_testmodel1" (\n    "id" serial NOT NULL PRIMARY KEY USING INDEX TABLESPACE "custom1",\n    "name" varchar(255) NOT NULL\n)\nTABLESPACE "custom1"\n;\nCREATE TABLE "tablespaces_app_testmodel2" (\n    "id" serial NOT NULL PRIMARY KEY USING INDEX TABLESPACE "custom",\n    "name" varchar(255) NOT NULL\n)\nTABLESPACE "custom"\n;\nCOMMIT;'
79+            self.assertEqual(output, result)