Repository navigation
Expand file tree
/
Copy pathtest_dataintegrity.py
More file actions
299 lines (208 loc) · 8.8 KB
/
Copy pathtest_dataintegrity.py
File metadata and controls
299 lines (208 loc) · 8.8 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python -O
""" Script to test database capabilities and the DB-API interface
for functionality and data integrity for some of the basic data types.
Adapted from a script taken from the MySQL python driver.
"""
import random
import time
from math import fabs
import pytest
import pytz
from snowflake.connector.compat import PY2
from snowflake.connector.dbapi import (
DateFromTicks, TimestampFromTicks, TimeFromTicks)
def table_exists(conn_cnx, name):
with conn_cnx() as cnx:
with cnx.cursor() as cursor:
try:
cursor.execute("select * from %s where 1=0" % name)
except:
cnx.rollback()
return False
else:
return True
def create_table(conn_cnx, columndefs, partial_name):
table = '"dbabi_dibasic_{0}"'.format(partial_name)
with conn_cnx() as cnx:
cnx.cursor().execute(
"CREATE OR REPLACE TABLE {table} ({columns})".format(
table=table, columns='\n'.join(columndefs)))
return table
def check_data_integrity(conn_cnx, columndefs, partial_name, generator):
rows = random.randrange(10, 15)
# floating_point_types = ('REAL','DOUBLE','DECIMAL')
floating_point_types = ('REAL', 'DOUBLE')
table = create_table(conn_cnx, columndefs, partial_name)
with conn_cnx() as cnx:
with cnx.cursor() as cursor:
# insert some data as specified by generator passed in
insert_statement = (
'INSERT INTO %s VALUES (%s)' %
(table,
','.join(['%s'] * len(columndefs))))
data = [[generator(i, j) for j in range(len(columndefs))]
for i in range(rows)]
cursor.executemany(insert_statement, data)
cnx.commit()
# verify 2 things: correct number of rows, correct values for
# each row
cursor.execute('select * from {0} order by 1'.format(table))
result_sequences = cursor.fetchall()
results = []
for i in result_sequences:
results.append(i)
# verify the right number of rows were returned
assert len(results) == rows, ('fetchall did not return '
'expected number of rows')
# verify the right values were returned
# for numbers, allow a difference of .000001
for i, (x, y) in enumerate(zip(results, sorted(data))):
if any(data_type in partial_name for data_type in
floating_point_types):
for i in range(rows):
df = fabs(float(x[0]) - float(y[0]))
if float(y[0]) != 0.0:
df = df / float(y[0])
assert df <= 0.00000001, \
("fetchall did not return correct values within "
"the expected range")
else:
assert list(x) == list(y), \
"fetchall did not return correct values"
cursor.execute('drop table if exists {0}'.format(table))
def test_INT(conn_cnx):
# Number data
def generator(row, col):
return row * row
check_data_integrity(conn_cnx, ('col1 INT',), 'INT', generator)
def test_DECIMAL(conn_cnx):
# DECIMAL
def generator(row, col):
from decimal import Decimal
return Decimal("%d.%02d" % (row, col))
check_data_integrity(conn_cnx, ('col1 DECIMAL(5,2)',), 'DECIMAL', generator)
def test_REAL(conn_cnx):
def generator(row, col):
return row * 1000.0
check_data_integrity(conn_cnx, ('col1 REAL',), 'REAL', generator)
def test_REAL2(conn_cnx):
def generator(row, col):
return row * 3.14
check_data_integrity(conn_cnx, ('col1 REAL',), 'REAL', generator)
def test_DOUBLE(conn_cnx):
def generator(row, col):
return row / 1e-99
check_data_integrity(conn_cnx, ('col1 DOUBLE',), 'DOUBLE', generator)
def test_FLOAT(conn_cnx):
def generator(row, col):
return row * 2.0
check_data_integrity(conn_cnx, ('col1 FLOAT(67)',), 'FLOAT', generator)
def test_DATE(conn_cnx):
ticks = time.time()
def generator(row, col):
return DateFromTicks(ticks + row * 86400 - col * 1313)
check_data_integrity(conn_cnx, ('col1 DATE',), 'DATE', generator)
def test_STRING(conn_cnx):
def generator(row, col):
import string
rstr = ''.join(
[random.choice(string.ascii_letters + string.digits) for n in
range(1024)])
return rstr
check_data_integrity(conn_cnx, ('col2 STRING',), 'STRING', generator)
def test_TEXT(conn_cnx):
def generator(row, col):
rstr = ''.join([chr(i) for i in range(33, 127)] * 100);
return rstr
check_data_integrity(conn_cnx, ('col2 TEXT',), 'TEXT', generator)
def test_VARCHAR(conn_cnx):
def generator(row, col):
import string
rstr = ''.join(
[random.choice(string.ascii_letters + string.digits) for n in
range(50)])
return rstr
check_data_integrity(conn_cnx, ('col2 VARCHAR',), 'VARCHAR', generator)
@pytest.mark.skipif(PY2, reason="""
Binary not supported in Python 2 connector.
""")
def test_BINARY(conn_cnx):
def generator(row, col):
return bytes(random.getrandbits(8) for _ in range(50))
check_data_integrity(conn_cnx, ('col1 BINARY',), 'BINARY', generator)
def test_TIMESTAMPNTZ(conn_cnx):
ticks = time.time()
def generator(row, col):
return TimestampFromTicks(ticks + row * 86400 - col * 1313)
check_data_integrity(conn_cnx, ('col1 TIMESTAMPNTZ',), 'TIMESTAMPNTZ',
generator)
def test_TIMESTAMPNTZ_EXPLICIT(conn_cnx):
ticks = time.time()
def generator(row, col):
return TimestampFromTicks(ticks + row * 86400 - col * 1313)
check_data_integrity(conn_cnx, ('col1 TIMESTAMP without time zone',),
'TIMESTAMPNTZ_EXPLICIT',
generator)
# string that contains control characters (white spaces), etc.
def test_DATETIME(conn_cnx):
ticks = time.time()
def generator(row, col):
ret = TimestampFromTicks(ticks + row * 86400 - col * 1313)
myzone = pytz.timezone("US/Pacific")
ret = myzone.localize(ret)
check_data_integrity(conn_cnx, ('col1 TIMESTAMP',), 'DATETIME', generator)
def test_TIMESTAMP(conn_cnx):
ticks = time.time()
def generator(row, col):
ret = TimestampFromTicks(ticks + row * 86400 - col * 1313)
myzone = pytz.timezone("US/Pacific")
return (myzone.localize(ret))
check_data_integrity(conn_cnx, ('col1 TIMESTAMP_LTZ',), 'TIMESTAMP',
generator)
def test_TIMESTAMP_EXPLICIT(conn_cnx):
ticks = time.time()
def generator(row, col):
ret = TimestampFromTicks(ticks + row * 86400 - col * 1313)
myzone = pytz.timezone("Australia/Sydney")
return (myzone.localize(ret))
check_data_integrity(conn_cnx, ('col1 TIMESTAMP with local time zone',),
'TIMESTAMP_EXPLICIT', generator)
def test_TIMESTAMPTZ(conn_cnx):
ticks = time.time()
def generator(row, col):
ret = TimestampFromTicks(ticks + row * 86400 - col * 1313)
myzone = pytz.timezone("America/Vancouver")
return (myzone.localize(ret))
check_data_integrity(conn_cnx, ('col1 TIMESTAMPTZ',), 'TIMESTAMPTZ',
generator)
def test_TIMESTAMPTZ_EXPLICIT(conn_cnx):
ticks = time.time()
def generator(row, col):
ret = TimestampFromTicks(ticks + row * 86400 - col * 1313)
myzone = pytz.timezone("America/Vancouver")
return (myzone.localize(ret))
check_data_integrity(conn_cnx, ('col1 TIMESTAMP with time zone',),
'TIMESTAMPTZ_EXPLICIT', generator)
def test_TIMESTAMPLTZ(conn_cnx):
ticks = time.time()
def generator(row, col):
ret = TimestampFromTicks(ticks + row * 86400 - col * 1313)
myzone = pytz.timezone("America/New_York")
return (myzone.localize(ret))
check_data_integrity(conn_cnx, ('col1 TIMESTAMPLTZ',), 'TIMESTAMPLTZ',
generator)
def test_fractional_TIMESTAMP(conn_cnx):
ticks = time.time()
def generator(row, col):
ret = TimestampFromTicks(
ticks + row * 86400 - col * 1313 + row * 0.7 * col / 3.0)
myzone = pytz.timezone("Europe/Paris")
return (myzone.localize(ret))
check_data_integrity(conn_cnx, ('col1 TIMESTAMP_LTZ',),
'TIMESTAMP_fractional', generator)
def test_TIME(conn_cnx):
ticks = time.time()
def generator(row, col):
ret = TimeFromTicks(ticks + row * 86400 - col * 1313)
return (ret)
check_data_integrity(conn_cnx, ('col1 TIME',), 'TIME', generator)