-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtest_redshift_backend.py
170 lines (139 loc) · 5.76 KB
/
test_redshift_backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: utf-8 -*-
import unittest
from django.db import connections
from django.db.utils import NotSupportedError
from django.core.management.color import no_style
from conftest import skipif_no_database
def norm_sql(sql):
return ' '.join(sql.split()).replace('( ', '(').replace(' )', ')').replace(' ;', ';')
class DatabaseWrapperTest(unittest.TestCase):
def test_load_redshift_backend(self):
db = connections['default']
self.assertIsNotNone(db)
expected_ddl_normal = norm_sql(
u'''CREATE TABLE "testapp_testmodel" (
"id" integer identity(1, 1) NOT NULL PRIMARY KEY,
"ctime" timestamp with time zone NOT NULL,
"text" varchar(max) NOT NULL,
"uuid" varchar(36) NOT NULL
)
;''')
expected_ddl_meta_keys = norm_sql(
u'''CREATE TABLE "testapp_testmodelwithmetakeys" (
"id" integer identity(1, 1) NOT NULL PRIMARY KEY,
"name" varchar(100) NOT NULL,
"age" integer NOT NULL,
"created_at" timestamp with time zone NOT NULL,
"fk_id" integer NOT NULL
) DISTKEY("fk_id") SORTKEY("created_at", "id")
;''')
expected_dml_annotate = norm_sql(
u'''SELECT
"testapp_testparentmodel"."id",
"testapp_testparentmodel"."age",
COUNT("testapp_testchildmodel"."id") AS "cnt"
FROM "testapp_testparentmodel"
LEFT OUTER JOIN "testapp_testchildmodel"
ON ("testapp_testparentmodel"."id" = "testapp_testchildmodel"."parent_id")
GROUP BY
"testapp_testparentmodel"."id",
"testapp_testparentmodel"."age"
''')
expected_aggregate_filter_emulated = norm_sql(
u'''SELECT
"testapp_testparentmodel"."id",
"testapp_testparentmodel"."age",
COUNT(
CASE WHEN "testapp_testparentmodel"."age" < %s
THEN "testapp_testchildmodel"."id" ELSE NULL END
) AS "cnt"
FROM "testapp_testparentmodel"
LEFT OUTER JOIN "testapp_testchildmodel"
ON ("testapp_testparentmodel"."id" = "testapp_testchildmodel"."parent_id")
GROUP BY
"testapp_testparentmodel"."id",
"testapp_testparentmodel"."age"
''')
expected_dml_distinct = norm_sql(
u'''SELECT DISTINCT
"testapp_testmodel"."id",
"testapp_testmodel"."ctime",
"testapp_testmodel"."text",
"testapp_testmodel"."uuid"
FROM "testapp_testmodel"
''')
class ModelTest(unittest.TestCase):
def check_model_creation(self, model, expected_ddl):
conn = connections['default']
statements, params = conn.creation.sql_create_model(model, no_style(), set())
sql = norm_sql(''.join(statements))
self.assertEqual(sql, expected_ddl)
def test_annotate(self):
from django.db.models import Count
from testapp.models import TestParentModel
query = TestParentModel.objects.annotate(cnt=Count('testchildmodel')).query
compiler = query.get_compiler(using='default')
sql = norm_sql(compiler.as_sql()[0])
self.assertEqual(sql, expected_dml_annotate)
def test_emulate_aggregate_filter(self):
self.maxDiff = None
from django.db.models import Count, Q
from testapp.models import TestParentModel
query = TestParentModel.objects.annotate(
cnt=Count('testchildmodel', filter=Q(age__lt=10))
).query
compiler = query.get_compiler(using='default')
sql = norm_sql(compiler.as_sql()[0])
self.assertEqual(sql, expected_aggregate_filter_emulated)
def test_insert_uuid_field(self):
import uuid
from django.db.models import sql
from testapp.models import TestModel
obj = TestModel(uuid=uuid.uuid4())
q = sql.InsertQuery(obj)
q.insert_values(obj._meta.local_fields, [obj])
statements = q.get_compiler('default').as_sql()
# uuid is the last field of TestModel
uuid_insert_value = statements[0][1][-1]
# the Python value for insertion must be a string whose length is 32
self.assertEqual(type(uuid_insert_value), str)
self.assertEqual(len(uuid_insert_value), 32)
def test_distinct(self):
from testapp.models import TestModel
query = TestModel.objects.distinct().query
compiler = query.get_compiler(using='default')
sql = norm_sql(compiler.as_sql()[0])
self.assertEqual(sql, expected_dml_distinct)
def test_distinct_with_fields(self):
from testapp.models import TestModel
query = TestModel.objects.distinct('text').query
compiler = query.get_compiler(using='default')
with self.assertRaises(NotSupportedError):
compiler.as_sql()
class MigrationTest(unittest.TestCase):
def check_model_creation(self, model, expected_ddl):
conn = connections['default']
schema_editor = conn.schema_editor(collect_sql=True)
schema_editor.deferred_sql = []
schema_editor.create_model(model)
sql = norm_sql(''.join(schema_editor.collected_sql))
self.assertEqual(sql, expected_ddl)
def test_create_model(self):
from testapp.models import TestModel
self.check_model_creation(TestModel, expected_ddl_normal)
def test_create_table_meta_keys(self):
from testapp.models import TestModelWithMetaKeys
self.check_model_creation(TestModelWithMetaKeys, expected_ddl_meta_keys)
@skipif_no_database
def test_sqlmigrate(self):
from django.db import connection
from django.db.migrations.loader import MigrationLoader
loader = MigrationLoader(connection)
collect_sql = loader.collect_sql
app_label, migration_name = 'testapp', '0001'
migration = loader.get_migration_by_prefix(app_label, migration_name)
target = (app_label, migration.name)
plan = [(loader.graph.nodes[target], False)]
sql_statements = collect_sql(plan)
print('\n'.join(sql_statements))
assert sql_statements # It doesn't matter what SQL is generated.