-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
test_copy_async.py
711 lines (560 loc) · 21.5 KB
/
test_copy_async.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
import gc
import string
import hashlib
from io import BytesIO, StringIO
from itertools import cycle
import pytest
import psycopg
from psycopg import pq
from psycopg import sql
from psycopg import errors as e
from psycopg.copy import AsyncCopy
from psycopg.pq import Format
from psycopg.types import TypeInfo
from psycopg.adapt import PyFormat
from psycopg.types.hstore import register_hstore
from psycopg.types.numeric import Int4
from .utils import gc_collect
from .test_copy import sample_text, sample_binary, sample_binary_rows # noqa
from .test_copy import eur, sample_values, sample_records, sample_tabledef
from .test_copy import py_to_raw
@pytest.fixture
def aconn(any_aconn):
return any_aconn
@pytest.mark.parametrize("format", Format)
async def test_copy_out_read(aconn, format):
if format == pq.Format.TEXT:
want = [row + b"\n" for row in sample_text.splitlines()]
else:
want = sample_binary_rows
cur = aconn.cursor()
async with cur.copy(
f"copy ({sample_values}) to stdout (format {format.name})"
) as copy:
for row in want:
got = await copy.read()
assert got == row
assert (
aconn.pgconn.transaction_status
== aconn.TransactionStatus.ACTIVE
)
assert await copy.read() == b""
assert await copy.read() == b""
assert await copy.read() == b""
assert aconn.pgconn.transaction_status == aconn.TransactionStatus.INTRANS
@pytest.mark.parametrize("format", Format)
async def test_copy_out_iter(aconn, format):
if format == pq.Format.TEXT:
want = [row + b"\n" for row in sample_text.splitlines()]
else:
want = sample_binary_rows
cur = aconn.cursor()
async with cur.copy(
f"copy ({sample_values}) to stdout (format {format.name})"
) as copy:
assert await alist(copy) == want
assert aconn.pgconn.transaction_status == aconn.TransactionStatus.INTRANS
@pytest.mark.parametrize("format", Format)
@pytest.mark.parametrize("typetype", ["names", "oids"])
async def test_read_rows(aconn, format, typetype):
cur = aconn.cursor()
async with cur.copy(
f"""copy (
select 10::int4, 'hello'::text, '{{0.0,1.0}}'::float8[]
) to stdout (format {format.name})"""
) as copy:
copy.set_types(["int4", "text", "float8[]"])
row = await copy.read_row()
assert (await copy.read_row()) is None
assert row == (10, "hello", [0.0, 1.0])
assert aconn.pgconn.transaction_status == aconn.TransactionStatus.INTRANS
@pytest.mark.parametrize("format", Format)
async def test_rows(aconn, format):
cur = aconn.cursor()
async with cur.copy(
f"copy ({sample_values}) to stdout (format {format.name})"
) as copy:
copy.set_types("int4 int4 text".split())
rows = await alist(copy.rows())
assert rows == sample_records
assert aconn.pgconn.transaction_status == aconn.TransactionStatus.INTRANS
async def test_set_custom_type(aconn, hstore):
command = """copy (select '"a"=>"1", "b"=>"2"'::hstore) to stdout"""
cur = aconn.cursor()
async with cur.copy(command) as copy:
rows = await alist(copy.rows())
assert rows == [('"a"=>"1", "b"=>"2"',)]
register_hstore(await TypeInfo.fetch(aconn, "hstore"), cur)
async with cur.copy(command) as copy:
copy.set_types(["hstore"])
rows = await alist(copy.rows())
assert rows == [({"a": "1", "b": "2"},)]
@pytest.mark.parametrize("format", Format)
async def test_copy_out_allchars(aconn, format):
cur = aconn.cursor()
chars = list(map(chr, range(1, 256))) + [eur]
await aconn.execute("set client_encoding to utf8")
rows = []
query = sql.SQL(
"copy (select unnest({}::text[])) to stdout (format {})"
).format(chars, sql.SQL(format.name))
async with cur.copy(query) as copy:
copy.set_types(["text"])
while 1:
row = await copy.read_row()
if not row:
break
assert len(row) == 1
rows.append(row[0])
assert rows == chars
@pytest.mark.parametrize("format", Format)
async def test_read_row_notypes(aconn, format):
cur = aconn.cursor()
async with cur.copy(
f"copy ({sample_values}) to stdout (format {format.name})"
) as copy:
rows = []
while 1:
row = await copy.read_row()
if not row:
break
rows.append(row)
ref = [
tuple(py_to_raw(i, format) for i in record)
for record in sample_records
]
assert rows == ref
@pytest.mark.parametrize("format", Format)
async def test_rows_notypes(aconn, format):
cur = aconn.cursor()
async with cur.copy(
f"copy ({sample_values}) to stdout (format {format.name})"
) as copy:
rows = await alist(copy.rows())
ref = [
tuple(py_to_raw(i, format) for i in record)
for record in sample_records
]
assert rows == ref
@pytest.mark.parametrize("err", [-1, 1])
@pytest.mark.parametrize("format", Format)
async def test_copy_out_badntypes(aconn, format, err):
cur = aconn.cursor()
async with cur.copy(
f"copy ({sample_values}) to stdout (format {format.name})"
) as copy:
copy.set_types([0] * (len(sample_records[0]) + err))
with pytest.raises(e.ProgrammingError):
await copy.read_row()
@pytest.mark.parametrize(
"format, buffer",
[(Format.TEXT, "sample_text"), (Format.BINARY, "sample_binary")],
)
async def test_copy_in_buffers(aconn, format, buffer):
cur = aconn.cursor()
await ensure_table(cur, sample_tabledef)
async with cur.copy(
f"copy copy_in from stdin (format {format.name})"
) as copy:
await copy.write(globals()[buffer])
await cur.execute("select * from copy_in order by 1")
data = await cur.fetchall()
assert data == sample_records
async def test_copy_in_buffers_pg_error(aconn):
cur = aconn.cursor()
await ensure_table(cur, sample_tabledef)
with pytest.raises(e.UniqueViolation):
async with cur.copy("copy copy_in from stdin (format text)") as copy:
await copy.write(sample_text)
await copy.write(sample_text)
assert aconn.pgconn.transaction_status == aconn.TransactionStatus.INERROR
async def test_copy_bad_result(aconn):
await aconn.set_autocommit(True)
cur = aconn.cursor()
with pytest.raises(e.SyntaxError):
async with cur.copy("wat"):
pass
with pytest.raises(e.ProgrammingError):
async with cur.copy("select 1"):
pass
with pytest.raises(e.ProgrammingError):
async with cur.copy("reset timezone"):
pass
with pytest.raises(e.ProgrammingError):
async with cur.copy("copy (select 1) to stdout; select 1") as copy:
await alist(copy)
with pytest.raises(e.ProgrammingError):
async with cur.copy("select 1; copy (select 1) to stdout"):
pass
async def test_copy_in_str(aconn):
cur = aconn.cursor()
await ensure_table(cur, sample_tabledef)
async with cur.copy("copy copy_in from stdin (format text)") as copy:
await copy.write(sample_text.decode())
await cur.execute("select * from copy_in order by 1")
data = await cur.fetchall()
assert data == sample_records
async def test_copy_in_str_binary(aconn):
cur = aconn.cursor()
await ensure_table(cur, sample_tabledef)
with pytest.raises(e.QueryCanceled):
async with cur.copy("copy copy_in from stdin (format binary)") as copy:
await copy.write(sample_text.decode())
assert aconn.pgconn.transaction_status == aconn.TransactionStatus.INERROR
@pytest.mark.parametrize("format", Format)
async def test_copy_in_empty(aconn, format):
cur = aconn.cursor()
await ensure_table(cur, sample_tabledef)
async with cur.copy(f"copy copy_in from stdin (format {format.name})"):
pass
assert aconn.pgconn.transaction_status == aconn.TransactionStatus.INTRANS
assert cur.rowcount == 0
@pytest.mark.parametrize("format", Format)
async def test_subclass_adapter(aconn, format):
if format == Format.TEXT:
from psycopg.types.string import StrDumper as BaseDumper
else:
from psycopg.types.string import ( # type: ignore[no-redef]
StrBinaryDumper as BaseDumper,
)
class MyStrDumper(BaseDumper):
def dump(self, obj):
return super().dump(obj) * 2
aconn.adapters.register_dumper(str, MyStrDumper)
cur = aconn.cursor()
await ensure_table(cur, sample_tabledef)
async with cur.copy(
f"copy copy_in (data) from stdin (format {format.name})"
) as copy:
await copy.write_row(("hello",))
await cur.execute("select data from copy_in")
rec = await cur.fetchone()
assert rec[0] == "hellohello"
@pytest.mark.parametrize("format", Format)
async def test_copy_in_error_empty(aconn, format):
cur = aconn.cursor()
await ensure_table(cur, sample_tabledef)
with pytest.raises(e.QueryCanceled) as exc:
async with cur.copy(f"copy copy_in from stdin (format {format.name})"):
raise Exception("mannaggiamiseria")
assert "mannaggiamiseria" in str(exc.value)
assert aconn.pgconn.transaction_status == aconn.TransactionStatus.INERROR
async def test_copy_in_buffers_with_pg_error(aconn):
cur = aconn.cursor()
await ensure_table(cur, sample_tabledef)
with pytest.raises(e.UniqueViolation):
async with cur.copy("copy copy_in from stdin (format text)") as copy:
await copy.write(sample_text)
await copy.write(sample_text)
assert aconn.pgconn.transaction_status == aconn.TransactionStatus.INERROR
async def test_copy_in_buffers_with_py_error(aconn):
cur = aconn.cursor()
await ensure_table(cur, sample_tabledef)
with pytest.raises(e.QueryCanceled) as exc:
async with cur.copy("copy copy_in from stdin (format text)") as copy:
await copy.write(sample_text)
raise Exception("nuttengoggenio")
assert "nuttengoggenio" in str(exc.value)
assert aconn.pgconn.transaction_status == aconn.TransactionStatus.INERROR
@pytest.mark.parametrize("format", Format)
async def test_copy_in_records(aconn, format):
cur = aconn.cursor()
await ensure_table(cur, sample_tabledef)
async with cur.copy(
f"copy copy_in from stdin (format {format.name})"
) as copy:
for row in sample_records:
if format == Format.BINARY:
row = tuple(
Int4(i) if isinstance(i, int) else i for i in row
) # type: ignore[assignment]
await copy.write_row(row)
await cur.execute("select * from copy_in order by 1")
data = await cur.fetchall()
assert data == sample_records
@pytest.mark.parametrize("format", Format)
async def test_copy_in_records_set_types(aconn, format):
cur = aconn.cursor()
await ensure_table(cur, sample_tabledef)
async with cur.copy(
f"copy copy_in from stdin (format {format.name})"
) as copy:
copy.set_types(["int4", "int4", "text"])
for row in sample_records:
await copy.write_row(row)
await cur.execute("select * from copy_in order by 1")
data = await cur.fetchall()
assert data == sample_records
@pytest.mark.parametrize("format", Format)
async def test_copy_in_records_binary(aconn, format):
cur = aconn.cursor()
await ensure_table(cur, "col1 serial primary key, col2 int, data text")
async with cur.copy(
f"copy copy_in (col2, data) from stdin (format {format.name})"
) as copy:
for row in sample_records:
await copy.write_row((None, row[2]))
await cur.execute("select * from copy_in order by 1")
data = await cur.fetchall()
assert data == [(1, None, "hello"), (2, None, "world")]
async def test_copy_in_allchars(aconn):
cur = aconn.cursor()
await ensure_table(cur, sample_tabledef)
await aconn.execute("set client_encoding to utf8")
async with cur.copy("copy copy_in from stdin (format text)") as copy:
for i in range(1, 256):
await copy.write_row((i, None, chr(i)))
await copy.write_row((ord(eur), None, eur))
await cur.execute(
"""
select col1 = ascii(data), col2 is null, length(data), count(*)
from copy_in group by 1, 2, 3
"""
)
data = await cur.fetchall()
assert data == [(True, True, 1, 256)]
@pytest.mark.slow
async def test_copy_from_to(aconn):
# Roundtrip from file to database to file blockwise
gen = DataGenerator(aconn, nrecs=1024, srec=10 * 1024)
await gen.ensure_table()
cur = aconn.cursor()
async with cur.copy("copy copy_in from stdin") as copy:
for block in gen.blocks():
await copy.write(block)
await gen.assert_data()
f = BytesIO()
async with cur.copy("copy copy_in to stdout") as copy:
async for block in copy:
f.write(block)
f.seek(0)
assert gen.sha(f) == gen.sha(gen.file())
@pytest.mark.slow
async def test_copy_from_to_bytes(aconn):
# Roundtrip from file to database to file blockwise
gen = DataGenerator(aconn, nrecs=1024, srec=10 * 1024)
await gen.ensure_table()
cur = aconn.cursor()
async with cur.copy("copy copy_in from stdin") as copy:
for block in gen.blocks():
await copy.write(block.encode())
await gen.assert_data()
f = BytesIO()
async with cur.copy("copy copy_in to stdout") as copy:
async for block in copy:
f.write(block)
f.seek(0)
assert gen.sha(f) == gen.sha(gen.file())
@pytest.mark.slow
async def test_copy_from_insane_size(aconn):
# Trying to trigger a "would block" error
gen = DataGenerator(
aconn, nrecs=4 * 1024, srec=10 * 1024, block_size=20 * 1024 * 1024
)
await gen.ensure_table()
cur = aconn.cursor()
async with cur.copy("copy copy_in from stdin") as copy:
for block in gen.blocks():
await copy.write(block)
await gen.assert_data()
async def test_copy_rowcount(aconn):
gen = DataGenerator(aconn, nrecs=3, srec=10)
await gen.ensure_table()
cur = aconn.cursor()
async with cur.copy("copy copy_in from stdin") as copy:
for block in gen.blocks():
await copy.write(block)
assert cur.rowcount == 3
gen = DataGenerator(aconn, nrecs=2, srec=10, offset=3)
async with cur.copy("copy copy_in from stdin") as copy:
for rec in gen.records():
await copy.write_row(rec)
assert cur.rowcount == 2
async with cur.copy("copy copy_in to stdout") as copy:
async for block in copy:
pass
assert cur.rowcount == 5
with pytest.raises(e.BadCopyFileFormat):
async with cur.copy("copy copy_in (id) from stdin") as copy:
for rec in gen.records():
await copy.write_row(rec)
assert cur.rowcount == -1
async def test_copy_query(aconn):
cur = aconn.cursor()
async with cur.copy("copy (select 1) to stdout") as copy:
assert cur._query.query == b"copy (select 1) to stdout"
assert not cur._query.params
await alist(copy)
async def test_cant_reenter(aconn):
cur = aconn.cursor()
async with cur.copy("copy (select 1) to stdout") as copy:
await alist(copy)
with pytest.raises(TypeError):
async with copy:
await alist(copy)
async def test_str(aconn):
cur = aconn.cursor()
async with cur.copy("copy (select 1) to stdout") as copy:
assert "[ACTIVE]" in str(copy)
await alist(copy)
assert "[INTRANS]" in str(copy)
@pytest.mark.parametrize(
"format, buffer",
[(Format.TEXT, "sample_text"), (Format.BINARY, "sample_binary")],
)
async def test_worker_life(aconn, format, buffer):
if aconn._copycls != AsyncCopy:
pytest.skip("only applicable for AsyncCopy")
cur = aconn.cursor()
await ensure_table(cur, sample_tabledef)
async with cur.copy(
f"copy copy_in from stdin (format {format.name})"
) as copy:
assert not copy._worker
await copy.write(globals()[buffer])
assert copy._worker
assert not copy._worker
await cur.execute("select * from copy_in order by 1")
data = await cur.fetchall()
assert data == sample_records
@pytest.mark.slow
@pytest.mark.asyncio
@pytest.mark.parametrize(
"fmt, set_types",
[(Format.TEXT, True), (Format.TEXT, False), (Format.BINARY, True)],
)
@pytest.mark.parametrize("method", ["read", "iter", "row", "rows"])
async def test_copy_to_leaks(dsn, faker, fmt, set_types, method, retries):
faker.format = PyFormat.from_pq(fmt)
faker.choose_schema(ncols=20)
faker.make_records(20)
async def work():
async with await psycopg.AsyncConnection.connect(dsn) as conn:
async with conn.cursor(binary=fmt) as cur:
await cur.execute(faker.drop_stmt)
await cur.execute(faker.create_stmt)
async with faker.find_insert_problem_async(conn):
await cur.executemany(faker.insert_stmt, faker.records)
stmt = sql.SQL(
"copy (select {} from {} order by id) to stdout (format {})"
).format(
sql.SQL(", ").join(faker.fields_names),
faker.table_name,
sql.SQL(fmt.name),
)
async with cur.copy(stmt) as copy:
if set_types:
copy.set_types(faker.types_names)
if method == "read":
while 1:
tmp = await copy.read()
if not tmp:
break
elif method == "iter":
await alist(copy)
elif method == "row":
while 1:
tmp = await copy.read_row() # type: ignore[assignment]
if tmp is None:
break
elif method == "rows":
await alist(copy.rows())
gc_collect()
async for retry in retries:
with retry:
n = []
for i in range(3):
await work()
gc_collect()
n.append(len(gc.get_objects()))
assert (
n[0] == n[1] == n[2]
), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
@pytest.mark.slow
@pytest.mark.asyncio
@pytest.mark.parametrize(
"fmt, set_types",
[(Format.TEXT, True), (Format.TEXT, False), (Format.BINARY, True)],
)
async def test_copy_from_leaks(dsn, faker, fmt, set_types, retries):
faker.format = PyFormat.from_pq(fmt)
faker.choose_schema(ncols=20)
faker.make_records(20)
async def work():
async with await psycopg.AsyncConnection.connect(dsn) as conn:
async with conn.cursor(binary=fmt) as cur:
await cur.execute(faker.drop_stmt)
await cur.execute(faker.create_stmt)
stmt = sql.SQL("copy {} ({}) from stdin (format {})").format(
faker.table_name,
sql.SQL(", ").join(faker.fields_names),
sql.SQL(fmt.name),
)
async with cur.copy(stmt) as copy:
if set_types:
copy.set_types(faker.types_names)
for row in faker.records:
await copy.write_row(row)
await cur.execute(faker.select_stmt)
recs = await cur.fetchall()
for got, want in zip(recs, faker.records):
faker.assert_record(got, want)
gc_collect()
async for retry in retries:
with retry:
n = []
for i in range(3):
await work()
gc_collect()
n.append(len(gc.get_objects()))
assert (
n[0] == n[1] == n[2]
), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
async def ensure_table(cur, tabledef, name="copy_in"):
await cur.execute(f"drop table if exists {name}")
await cur.execute(f"create table {name} ({tabledef})")
class DataGenerator:
def __init__(self, conn, nrecs, srec, offset=0, block_size=8192):
self.conn = conn
self.nrecs = nrecs
self.srec = srec
self.offset = offset
self.block_size = block_size
async def ensure_table(self):
cur = self.conn.cursor()
await ensure_table(cur, "id integer primary key, data text")
def records(self):
for i, c in zip(range(self.nrecs), cycle(string.ascii_letters)):
s = c * self.srec
yield (i + self.offset, s)
def file(self):
f = StringIO()
for i, s in self.records():
f.write("%s\t%s\n" % (i, s))
f.seek(0)
return f
def blocks(self):
f = self.file()
while True:
block = f.read(self.block_size)
if not block:
break
yield block
async def assert_data(self):
cur = self.conn.cursor()
await cur.execute("select id, data from copy_in order by id")
for record in self.records():
assert record == await cur.fetchone()
assert await cur.fetchone() is None
def sha(self, f):
m = hashlib.sha256()
while 1:
block = f.read()
if not block:
break
if isinstance(block, str):
block = block.encode()
m.update(block)
return m.hexdigest()
async def alist(it):
return [i async for i in it]