forked from coleifer/peewee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests_postgres.py
484 lines (376 loc) · 16.2 KB
/
tests_postgres.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#coding:utf-8
import datetime
import json
import os
import unittest
import uuid
import psycopg2
from peewee import print_
from playhouse.postgres_ext import *
TEST_VERBOSITY = int(os.environ.get('PEEWEE_TEST_VERBOSITY') or 1)
test_db = PostgresqlExtDatabase('peewee_test', user='postgres')
test_ss_db = PostgresqlExtDatabase(
'peewee_test',
server_side_cursors=True,
user='postgres')
class BaseModel(Model):
class Meta:
database = test_db
class Testing(BaseModel):
name = CharField()
data = HStoreField()
class Meta:
order_by = ('name',)
try:
class TestingJson(BaseModel):
data = JSONField()
except:
TestingJson = None
class TestingID(BaseModel):
uniq = UUIDField()
class TZModel(BaseModel):
dt = DateTimeTZField()
class ArrayModel(BaseModel):
tags = ArrayField(CharField)
ints = ArrayField(IntegerField, dimensions=2)
class SSCursorModel(Model):
data = CharField()
class Meta:
database = test_ss_db
class NormalModel(BaseModel):
data = CharField()
class PostgresExtTestCase(unittest.TestCase):
def setUp(self):
Testing.drop_table(True)
Testing.create_table()
TestingID.drop_table(True)
TestingID.create_table()
ArrayModel.drop_table(True)
ArrayModel.create_table(True)
self.t1 = None
self.t2 = None
def test_uuid(self):
uuid_str = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'
uuid_obj = uuid.UUID(uuid_str)
t1 = TestingID.create(uniq=uuid_obj)
t1_db = TestingID.get(TestingID.uniq == uuid_str)
self.assertEqual(t1, t1_db)
t2 = TestingID.get(TestingID.uniq == uuid_obj)
self.assertEqual(t1, t2)
def test_tz_field(self):
TZModel.drop_table(True)
TZModel.create_table()
test_db.execute_sql('set time zone "us/central";')
dt = datetime.datetime.now()
tz = TZModel.create(dt=dt)
self.assertTrue(tz.dt.tzinfo is None)
tz = TZModel.get(TZModel.id == tz.id)
self.assertFalse(tz.dt.tzinfo is None)
def create(self):
self.t1 = Testing.create(name='t1', data={'k1': 'v1', 'k2': 'v2'})
self.t2 = Testing.create(name='t2', data={'k2': 'v2', 'k3': 'v3'})
def test_storage(self):
self.create()
self.assertEqual(Testing.get(name='t1').data, {'k1': 'v1', 'k2': 'v2'})
self.assertEqual(Testing.get(name='t2').data, {'k2': 'v2', 'k3': 'v3'})
self.t1.data = {'k4': 'v4'}
self.t1.save()
self.assertEqual(Testing.get(name='t1').data, {'k4': 'v4'})
t = Testing.create(name='t3', data={})
self.assertEqual(Testing.get(name='t3').data, {})
def test_selecting(self):
self.create()
sq = Testing.select(Testing.name, Testing.data.keys().alias('keys'))
self.assertEqual([(x.name, sorted(x.keys)) for x in sq], [
('t1', ['k1', 'k2']), ('t2', ['k2', 'k3'])
])
sq = Testing.select(Testing.name, Testing.data.values().alias('vals'))
self.assertEqual([(x.name, sorted(x.vals)) for x in sq], [
('t1', ['v1', 'v2']), ('t2', ['v2', 'v3'])
])
sq = Testing.select(Testing.name, Testing.data.items().alias('mtx'))
self.assertEqual([(x.name, sorted(x.mtx)) for x in sq], [
('t1', [['k1', 'v1'], ['k2', 'v2']]),
('t2', [['k2', 'v2'], ['k3', 'v3']]),
])
sq = Testing.select(Testing.name, Testing.data.slice('k2', 'k3').alias('kz'))
self.assertEqual([(x.name, x.kz) for x in sq], [
('t1', {'k2': 'v2'}),
('t2', {'k2': 'v2', 'k3': 'v3'}),
])
sq = Testing.select(Testing.name, Testing.data.slice('k4').alias('kz'))
self.assertEqual([(x.name, x.kz) for x in sq], [
('t1', {}),
('t2', {}),
])
sq = Testing.select(Testing.name, Testing.data.exists('k3').alias('ke'))
self.assertEqual([(x.name, x.ke) for x in sq], [
('t1', False),
('t2', True),
])
sq = Testing.select(Testing.name, Testing.data.defined('k3').alias('ke'))
self.assertEqual([(x.name, x.ke) for x in sq], [
('t1', False),
('t2', True),
])
sq = Testing.select(Testing.name, Testing.data['k1'].alias('k1'))
self.assertEqual([(x.name, x.k1) for x in sq], [
('t1', 'v1'),
('t2', None),
])
sq = Testing.select(Testing.name).where(Testing.data['k1'] == 'v1')
self.assertEqual([x.name for x in sq], ['t1'])
def test_filtering(self):
self.create()
sq = Testing.select().where(Testing.data == {'k1': 'v1', 'k2': 'v2'})
self.assertEqual([x.name for x in sq], ['t1'])
sq = Testing.select().where(Testing.data == {'k2': 'v2'})
self.assertEqual([x.name for x in sq], [])
# test single key
sq = Testing.select().where(Testing.data.contains('k3'))
self.assertEqual([x.name for x in sq], ['t2'])
# test list of keys
sq = Testing.select().where(Testing.data.contains(['k2', 'k3']))
self.assertEqual([x.name for x in sq], ['t2'])
sq = Testing.select().where(Testing.data.contains(['k2']))
self.assertEqual([x.name for x in sq], ['t1', 't2'])
# test dict
sq = Testing.select().where(Testing.data.contains({'k2': 'v2', 'k3': 'v3'}))
self.assertEqual([x.name for x in sq], ['t2'])
sq = Testing.select().where(Testing.data.contains({'k2': 'v2'}))
self.assertEqual([x.name for x in sq], ['t1', 't2'])
sq = Testing.select().where(Testing.data.contains({'k2': 'v3'}))
self.assertEqual([x.name for x in sq], [])
def test_filter_functions(self):
self.create()
sq = Testing.select().where(Testing.data.exists('k2') == True)
self.assertEqual([x.name for x in sq], ['t1', 't2'])
sq = Testing.select().where(Testing.data.exists('k3') == True)
self.assertEqual([x.name for x in sq], ['t2'])
sq = Testing.select().where(Testing.data.defined('k2') == True)
self.assertEqual([x.name for x in sq], ['t1', 't2'])
sq = Testing.select().where(Testing.data.defined('k3') == True)
self.assertEqual([x.name for x in sq], ['t2'])
def test_update_functions(self):
self.create()
rc = Testing.update(data=Testing.data.update(k4='v4')).where(
Testing.name == 't1'
).execute()
self.assertEqual(rc, 1)
self.assertEqual(Testing.get(name='t1').data, {'k1': 'v1', 'k2': 'v2', 'k4': 'v4'})
rc = Testing.update(data=Testing.data.update(k5='v5', k6='v6')).where(
Testing.name == 't2'
).execute()
self.assertEqual(rc, 1)
self.assertEqual(Testing.get(name='t2').data, {'k2': 'v2', 'k3': 'v3', 'k5': 'v5', 'k6': 'v6'})
rc = Testing.update(data=Testing.data.update(k2='vxxx')).execute()
self.assertEqual(rc, 2)
self.assertEqual([x.data for x in Testing.select()], [
{'k1': 'v1', 'k2': 'vxxx', 'k4': 'v4'},
{'k2': 'vxxx', 'k3': 'v3', 'k5': 'v5', 'k6': 'v6'}
])
rc = Testing.update(data=Testing.data.delete('k4')).where(
Testing.name == 't1'
).execute()
self.assertEqual(rc, 1)
self.assertEqual(Testing.get(name='t1').data, {'k1': 'v1', 'k2': 'vxxx'})
rc = Testing.update(data=Testing.data.delete('k5')).execute()
self.assertEqual(rc, 2)
self.assertEqual([x.data for x in Testing.select()], [
{'k1': 'v1', 'k2': 'vxxx'},
{'k2': 'vxxx', 'k3': 'v3', 'k6': 'v6'}
])
rc = Testing.update(data=Testing.data.delete('k1', 'k2')).execute()
self.assertEqual(rc, 2)
self.assertEqual([x.data for x in Testing.select()], [
{},
{'k3': 'v3', 'k6': 'v6'}
])
def _create_am(self):
return ArrayModel.create(
tags=['alpha', 'beta', 'gamma', 'delta'],
ints=[[1, 2], [3, 4], [5, 6]])
def test_array_storage_retrieval(self):
am = self._create_am()
am_db = ArrayModel.get(ArrayModel.id == am.id)
self.assertEqual(am_db.tags, ['alpha', 'beta', 'gamma', 'delta'])
self.assertEqual(am_db.ints, [[1, 2], [3, 4], [5, 6]])
def test_array_search(self):
def assertAM(where, *instances):
query = (ArrayModel
.select()
.where(where)
.order_by(ArrayModel.id))
self.assertEqual([x.id for x in query], [x.id for x in instances])
am = self._create_am()
am2 = ArrayModel.create(tags=['alpha', 'beta'], ints=[[1, 1]])
am3 = ArrayModel.create(tags=['delta'], ints=[[3, 4]])
am4 = ArrayModel.create(tags=['中文'], ints=[[3, 4]])
am5 = ArrayModel.create(tags=['中文', '汉语'], ints=[[3, 4]])
assertAM((Param('beta') == fn.Any(ArrayModel.tags)), am, am2)
assertAM((Param('delta') == fn.Any(ArrayModel.tags)), am, am3)
assertAM((Param('omega') == fn.Any(ArrayModel.tags)))
# Check the contains operator.
assertAM(SQL("tags @> ARRAY['beta']::varchar[]"), am, am2)
# Use the nicer API.
assertAM(ArrayModel.tags.contains('beta'), am, am2)
assertAM(ArrayModel.tags.contains('omega', 'delta'))
assertAM(ArrayModel.tags.contains('汉语'), am5)
assertAM(ArrayModel.tags.contains('alpha', 'delta'), am)
# Check for any.
assertAM(ArrayModel.tags.contains_any('beta'), am, am2)
assertAM(ArrayModel.tags.contains_any('中文'), am4, am5)
assertAM(ArrayModel.tags.contains_any('omega', 'delta'), am, am3)
assertAM(ArrayModel.tags.contains_any('alpha', 'delta'), am, am2, am3)
def test_array_index_slice(self):
self._create_am()
res = (ArrayModel
.select(ArrayModel.tags[1].alias('arrtags'))
.dicts()
.get())
self.assertEqual(res['arrtags'], 'beta')
res = (ArrayModel
.select(ArrayModel.tags[2:4].alias('foo'))
.dicts()
.get())
self.assertEqual(res['foo'], ['gamma', 'delta'])
res = (ArrayModel
.select(ArrayModel.ints[1][1].alias('ints'))
.dicts()
.get())
self.assertEqual(res['ints'], 4)
res = (ArrayModel
.select(ArrayModel.ints[1:2][0].alias('ints'))
.dicts()
.get())
self.assertEqual(res['ints'], [[3], [5]])
class SSCursorTestCase(unittest.TestCase):
counter = 0
def setUp(self):
self.close_conn() # Close open connection.
SSCursorModel.drop_table(True)
NormalModel.drop_table(True)
SSCursorModel.create_table()
NormalModel.create_table()
self.counter = 0
for i in range(3):
self.create()
def create(self):
self.counter += 1
SSCursorModel.create(data=self.counter)
NormalModel.create(data=self.counter)
def close_conn(self):
if not test_ss_db.is_closed():
test_ss_db.close()
def assertList(self, iterable):
self.assertEqual(
[x.data for x in iterable],
[str(i) for i in range(1, self.counter + 1)])
def test_model_interaction(self):
query = SSCursorModel.select().order_by(SSCursorModel.data)
self.assertList(query)
query2 = query.clone()
qr = query2.execute()
self.assertList(qr)
# The cursor is named and is still "alive" because we can still try
# to fetch results.
self.assertTrue(qr.cursor.name is not None)
self.assertEqual(qr.cursor.fetchone(), None)
# Execute the query in a transaction.
with test_ss_db.transaction():
query3 = query.clone()
qr2 = query3.execute()
# Different named cursor
self.assertFalse(qr2.cursor.name == qr.cursor.name)
self.assertList(qr2)
# After the transaction we cannot fetch a result because the cursor
# is dead.
self.assertRaises(psycopg2.ProgrammingError, qr2.cursor.fetchone)
# Try using the helper.
query4 = query.clone()
self.assertList(ServerSide(query4))
# Named cursor is dead.
self.assertRaises(
psycopg2.ProgrammingError, query4._qr.cursor.fetchone)
def test_serverside_normal_model(self):
query = NormalModel.select().order_by(NormalModel.data)
self.assertList(query)
# We can ask for more results from a normal query.
self.assertEqual(query._qr.cursor.fetchone(), None)
clone = query.clone()
self.assertList(ServerSide(clone))
# Named cursor is dead.
self.assertRaises(psycopg2.ProgrammingError, clone._qr.cursor.fetchone)
# Ensure where clause is preserved.
query = query.where(NormalModel.data == '2')
data = [x.data for x in ServerSide(query)]
self.assertEqual(data, ['2'])
def test_ss_cursor(self):
tbl = SSCursorModel._meta.db_table
name = str(uuid.uuid1())
# Get a named cursor and execute a select query.
cursor = test_ss_db.get_cursor(name=name)
cursor.execute('select data from %s order by id' % tbl)
# Ensure the cursor attributes are as we expect.
self.assertEqual(cursor.description, None)
self.assertEqual(cursor.name, name)
self.assertFalse(cursor.withhold) # Close cursor after commit.
# Cursor works and populates description after fetching one row.
self.assertEqual(cursor.fetchone(), ('1',))
self.assertEqual(cursor.description[0].name, 'data')
# Explicitly close the cursor.
test_ss_db.commit()
self.assertRaises(psycopg2.ProgrammingError, cursor.fetchone)
# This would not work is the named cursor was still holding a ref to
# the table.
test_ss_db.execute_sql('truncate table %s;' % tbl)
test_ss_db.commit()
def json_ok():
if TestingJson is None:
return False
conn = test_db.get_conn()
return conn.server_version >= 90300
if json_ok():
from psycopg2.extras import Json
class TestJsonField(unittest.TestCase):
def setUp(self):
TestingJson.drop_table(True)
TestingJson.create_table()
def test_json_field(self):
data = {'k1': ['a1', 'a2'], 'k2': {'k3': 'v3'}}
tj = TestingJson.create(data=data)
tj_db = TestingJson.get(tj.pk_expr())
self.assertEqual(tj_db.data, data)
def test_json_field_sql(self):
tj = TestingJson.select().where(TestingJson.data == {'foo': 'bar'})
sql, params = tj.sql()
self.assertEqual(sql, (
'SELECT t1."id", t1."data" '
'FROM "testingjson" AS t1 WHERE (t1."data" = %s)'))
self.assertEqual(params[0].adapted, {'foo': 'bar'})
tj = TestingJson.select().where(TestingJson.data['foo'] == 'bar')
sql, params = tj.sql()
self.assertEqual(sql, (
'SELECT t1."id", t1."data" '
'FROM "testingjson" AS t1 WHERE (t1."data"->>%s = %s)'))
self.assertEqual(params, ['foo', 'bar'])
def assertItems(self, where, *items):
query = TestingJson.select().where(where).order_by(TestingJson.id)
self.assertEqual(
[item.id for item in query],
[item.id for item in items])
def test_lookup(self):
t1 = TestingJson.create(data={'k1': 'v1', 'k2': {'k3': 'v3'}})
t2 = TestingJson.create(data={'k1': 'x1', 'k2': {'k3': 'x3'}})
t3 = TestingJson.create(data={'k1': 'v1', 'j2': {'j3': 'v3'}})
self.assertItems((TestingJson.data['k2']['k3'] == 'v3'), t1)
self.assertItems((TestingJson.data['k1'] == 'v1'), t1, t3)
# Valid key, no matching value.
self.assertItems((TestingJson.data['k2'] == 'v1'))
# Non-existent key.
self.assertItems((TestingJson.data['not-here'] == 'v1'))
# Non-existent nested key.
self.assertItems((TestingJson.data['not-here']['xxx'] == 'v1'))
self.assertItems((TestingJson.data['k2']['xxx'] == 'v1'))
elif TEST_VERBOSITY > 0:
print_('Skipping postgres "Json" tests, unsupported version.')