/
test_operations.py
257 lines (210 loc) · 8.35 KB
/
test_operations.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# Copyright 2021 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
from django.db.utils import DatabaseError
from datetime import timedelta
from tests.unit.django_spanner.simple_test import SpannerSimpleTestClass
from decimal import Decimal
class TestOperations(SpannerSimpleTestClass):
def test_max_name_length(self):
self.assertEqual(self.db_operations.max_name_length(), 128)
def test_quote_name(self):
quoted_name = self.db_operations.quote_name("abc")
self.assertEqual(quoted_name, "abc")
def test_quote_name_spanner_reserved_keyword_escaped(self):
quoted_name = self.db_operations.quote_name("ALL")
self.assertEqual(quoted_name, "`ALL`")
def test_bulk_batch_size(self):
self.assertEqual(
self.db_operations.bulk_batch_size(fields=None, objs=None),
self.db_operations.connection.features.max_query_params,
)
def test_sql_flush(self):
from django.core.management.color import no_style
self.assertEqual(
self.db_operations.sql_flush(
style=no_style(), tables=["Table1, Table2"]
),
["DELETE FROM `Table1, Table2`"],
)
def test_sql_flush_empty_table_list(self):
from django.core.management.color import no_style
self.assertEqual(
self.db_operations.sql_flush(style=no_style(), tables=[]), [],
)
def test_adapt_datefield_value(self):
from google.cloud.spanner_dbapi.types import DateStr
self.assertIsInstance(
self.db_operations.adapt_datefield_value("dummy_date"), DateStr,
)
def test_adapt_datefield_value_none(self):
self.assertIsNone(
self.db_operations.adapt_datefield_value(value=None),
)
def test_adapt_decimalfield_value(self):
self.assertIsInstance(
self.db_operations.adapt_decimalfield_value(value=Decimal("1")),
Decimal,
)
def test_adapt_decimalfield_value_none(self):
self.assertIsNone(
self.db_operations.adapt_decimalfield_value(value=None),
)
def test_convert_binaryfield_value(self):
from base64 import b64encode
self.assertEqual(
self.db_operations.convert_binaryfield_value(
value=b64encode(b"abc"), expression=None, connection=None
),
b"abc",
)
def test_convert_binaryfield_value_none(self):
self.assertIsNone(
self.db_operations.convert_binaryfield_value(
value=None, expression=None, connection=None
),
)
def test_adapt_datetimefield_value_none(self):
self.assertIsNone(
self.db_operations.adapt_datetimefield_value(value=None),
)
def test_adapt_timefield_value_none(self):
self.assertIsNone(
self.db_operations.adapt_timefield_value(value=None),
)
def test_convert_uuidfield_value(self):
import uuid
uuid_obj = uuid.uuid4()
self.assertEqual(
self.db_operations.convert_uuidfield_value(
str(uuid_obj), expression=None, connection=None
),
uuid_obj,
)
def test_convert_uuidfield_value_none(self):
self.assertIsNone(
self.db_operations.convert_uuidfield_value(
value=None, expression=None, connection=None
),
)
def test_date_extract_sql(self):
self.assertEqual(
self.db_operations.date_extract_sql("week", "dummy_field"),
"EXTRACT(isoweek FROM dummy_field)",
)
def test_date_extract_sql_lookup_type_dayofweek(self):
self.assertEqual(
self.db_operations.date_extract_sql("dayofweek", "dummy_field"),
"EXTRACT(dayofweek FROM dummy_field)",
)
def test_datetime_extract_sql(self):
from django.conf import settings
settings.USE_TZ = True
self.assertEqual(
self.db_operations.datetime_extract_sql(
"dayofweek", "dummy_field", "IST"
),
'EXTRACT(dayofweek FROM dummy_field AT TIME ZONE "IST")',
)
def test_datetime_extract_sql_use_tz_false(self):
from django.conf import settings
settings.USE_TZ = False
self.assertEqual(
self.db_operations.datetime_extract_sql(
"dayofweek", "dummy_field", "IST"
),
'EXTRACT(dayofweek FROM dummy_field AT TIME ZONE "UTC")',
)
settings.USE_TZ = True # reset changes.
def test_time_extract_sql(self):
self.assertEqual(
self.db_operations.time_extract_sql("dayofweek", "dummy_field"),
'EXTRACT(dayofweek FROM dummy_field AT TIME ZONE "UTC")',
)
def test_time_trunc_sql(self):
self.assertEqual(
self.db_operations.time_trunc_sql("dayofweek", "dummy_field"),
'TIMESTAMP_TRUNC(dummy_field, dayofweek, "UTC")',
)
def test_datetime_cast_date_sql(self):
self.assertEqual(
self.db_operations.datetime_cast_date_sql("dummy_field", "IST"),
'DATE(dummy_field, "IST")',
)
def test_datetime_cast_time_sql(self):
from django.conf import settings
settings.USE_TZ = True
self.assertEqual(
self.db_operations.datetime_cast_time_sql("dummy_field", "IST"),
"TIMESTAMP(FORMAT_TIMESTAMP('%Y-%m-%d %R:%E9S %Z', dummy_field, 'IST'))",
)
def test_datetime_cast_time_sql_use_tz_false(self):
from django.conf import settings
settings.USE_TZ = False
self.assertEqual(
self.db_operations.datetime_cast_time_sql("dummy_field", "IST"),
"TIMESTAMP(FORMAT_TIMESTAMP('%Y-%m-%d %R:%E9S %Z', dummy_field, 'UTC'))",
)
settings.USE_TZ = True # reset changes.
def test_date_interval_sql(self):
self.assertEqual(
self.db_operations.date_interval_sql(timedelta(days=1)),
"INTERVAL 86400000000 MICROSECOND",
)
def test_format_for_duration_arithmetic(self):
self.assertEqual(
self.db_operations.format_for_duration_arithmetic(1200),
"INTERVAL 1200 MICROSECOND",
)
def test_combine_expression_mod(self):
self.assertEqual(
self.db_operations.combine_expression("%%", ["10", "2"]),
"MOD(10, 2)",
)
def test_combine_expression_power(self):
self.assertEqual(
self.db_operations.combine_expression("^", ["10", "2"]),
"POWER(10, 2)",
)
def test_combine_expression_bit_extention(self):
self.assertEqual(
self.db_operations.combine_expression(">>", ["10", "2"]),
"CAST(FLOOR(10 / POW(2, 2)) AS INT64)",
)
def test_combine_expression_multiply(self):
self.assertEqual(
self.db_operations.combine_expression("*", ["10", "2"]), "10 * 2",
)
def test_combine_duration_expression_add(self):
self.assertEqual(
self.db_operations.combine_duration_expression(
"+",
['TIMESTAMP "2008-12-25 15:30:00+00', "INTERVAL 10 MINUTE"],
),
'TIMESTAMP_ADD(TIMESTAMP "2008-12-25 15:30:00+00, INTERVAL 10 MINUTE)',
)
def test_combine_duration_expression_subtract(self):
self.assertEqual(
self.db_operations.combine_duration_expression(
"-",
['TIMESTAMP "2008-12-25 15:30:00+00', "INTERVAL 10 MINUTE"],
),
'TIMESTAMP_SUB(TIMESTAMP "2008-12-25 15:30:00+00, INTERVAL 10 MINUTE)',
)
def test_combine_duration_expression_database_error(self):
msg = "Invalid connector for timedelta:"
with self.assertRaisesRegex(DatabaseError, msg):
self.db_operations.combine_duration_expression(
"*",
['TIMESTAMP "2008-12-25 15:30:00+00', "INTERVAL 10 MINUTE"],
)
def test_lookup_cast_match_lookup_type(self):
self.assertEqual(
self.db_operations.lookup_cast("contains",), "CAST(%s AS STRING)",
)
def test_lookup_cast_unmatched_lookup_type(self):
self.assertEqual(
self.db_operations.lookup_cast("dummy",), "%s",
)