-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmyxql_test.exs
851 lines (671 loc) · 28.1 KB
/
myxql_test.exs
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
defmodule MyXQLTest do
use ExUnit.Case, async: true
import ExUnit.CaptureLog
@opts TestHelper.opts()
describe "connect" do
@tag ssl: true
test "connect with bad SSL opts" do
assert capture_log(fn ->
opts = [ssl: true, ssl_opts: [ciphers: [:bad]]] ++ @opts
assert_start_and_killed(opts)
end) =~
"** (DBConnection.ConnectionError) (127.0.0.1:3306) Invalid TLS option: {ciphers,[bad]}"
end
test "connect with host down" do
assert capture_log(fn ->
opts = [port: 9999] ++ @opts
assert_start_and_killed(opts)
end) =~
"(DBConnection.ConnectionError) (127.0.0.1:9999) connection refused - :econnrefused"
end
@tag requires_otp_19: true
test "connect using default protocol (:socket)" do
opts =
@opts
|> Keyword.delete(:hostname)
|> Keyword.delete(:port)
|> Keyword.delete(:socket)
{:ok, conn} = MyXQL.start_link(opts)
MyXQL.query!(conn, "SELECT 1")
end
@tag requires_otp_19: true
test "connect using UNIX domain socket" do
socket = System.get_env("MYSQL_UNIX_PORT") || "/tmp/mysql.sock"
opts =
@opts
|> Keyword.delete(:port)
|> Keyword.put(:hostname, "intentionally_bad_host")
|> Keyword.merge(socket: socket)
{:ok, conn} = MyXQL.start_link(opts)
MyXQL.query!(conn, "SELECT 1")
end
@tag requires_otp_19: true
test "connect using bad UNIX domain socket" do
opts =
@opts
|> Keyword.delete(:hostname)
|> Keyword.delete(:port)
|> Keyword.merge(socket: "/bad")
assert capture_log(fn ->
assert_start_and_killed(opts)
end) =~
"** (DBConnection.ConnectionError) (/bad) no such file or directory - :enoent"
end
test "custom socket options" do
opts = [socket_options: [buffer: 4]] ++ @opts
{:ok, conn} = MyXQL.start_link(opts)
MyXQL.query!(conn, "SELECT 1, 2, NOW()")
MyXQL.prepare_execute!(conn, "", "SELECT 1, 2, NOW()")
end
test "after_connect callback" do
pid = self()
opts = [after_connect: fn conn -> send(pid, {:connected, conn}) end] ++ @opts
MyXQL.start_link(opts)
assert_receive {:connected, _}
end
test "handshake timeout" do
%{port: port} =
start_fake_server(fn _ ->
Process.sleep(:infinity)
end)
opts = [port: port, handshake_timeout: 5] ++ @opts
assert capture_log(fn ->
assert_start_and_killed(opts)
end) =~ "timed out because it was handshaking for longer than 5ms"
end
end
describe "query" do
setup [:connect, :truncate]
test "default to binary protocol", c do
self = self()
{:ok, _} = MyXQL.query(c.conn, "SELECT 42", [], log: &send(self, &1))
assert_received %DBConnection.LogEntry{} = entry
assert %MyXQL.Query{} = entry.query
end
test "binary: query with params", c do
assert {:ok, result} = MyXQL.query(c.conn, "SELECT ? * ?", [2, 3])
assert result.rows == [[6]]
end
test "binary: iodata", c do
statement = ["SELECT", [" ", ["42"]]]
assert {:ok, %{rows: [[42]]}} =
MyXQL.query(c.conn, statement, [], query_type: :binary, log: &send(self(), &1))
assert_received %DBConnection.LogEntry{} = entry
assert %MyXQL.Query{} = entry.query
end
test "text: iodata", c do
statement = ["SELECT", [" ", ["42"]]]
assert {:ok, %{rows: [[42]]}} =
MyXQL.query(c.conn, statement, [], query_type: :text, log: &send(self(), &1))
assert_received %DBConnection.LogEntry{} = entry
assert %MyXQL.TextQuery{} = entry.query
end
test "non preparable statement", c do
self = self()
log = &send(self, &1)
assert {:ok, %MyXQL.Result{}} =
MyXQL.query(c.conn, "BEGIN", [], query_type: :text, log: log)
assert_receive %DBConnection.LogEntry{query: %MyXQL.TextQuery{}}
assert {:error, %MyXQL.Error{mysql: %{code: 1295}}} =
MyXQL.query(c.conn, "BEGIN", [], query_type: :binary, log: log)
assert_receive %DBConnection.LogEntry{query: %MyXQL.Query{}}
assert {:ok, %MyXQL.Result{}} =
MyXQL.query(c.conn, "BEGIN", [], query_type: :binary_then_text, log: log)
assert_receive %DBConnection.LogEntry{query: %MyXQL.Query{}}
end
for protocol <- [:binary, :text] do
@protocol protocol
test "#{@protocol}: invalid query", c do
assert {:error, %MyXQL.Error{mysql: %{code: 1054}}} =
MyXQL.query(c.conn, "SELECT bad", [], query_type: @protocol)
end
test "#{@protocol}: query with multiple rows", c do
%MyXQL.Result{num_rows: 2} =
MyXQL.query!(c.conn, "INSERT INTO integers VALUES (10), (20)", [], query_type: @protocol)
assert {:ok, %MyXQL.Result{columns: ["x"], rows: [[10], [20]]}} =
MyXQL.query(c.conn, "SELECT * FROM integers")
end
test "#{@protocol}: many rows", c do
num = 10_000
values = Enum.map_join(1..num, ", ", &"(#{&1})")
result =
MyXQL.query!(c.conn, "INSERT INTO integers VALUES " <> values, [], query_type: @protocol)
assert result.num_rows == num
result = MyXQL.query!(c.conn, "SELECT x FROM integers")
assert List.flatten(result.rows) == Enum.to_list(1..num)
assert result.num_rows == num
end
end
test "query_many/4 with text", c do
assert {:ok, [%MyXQL.Result{rows: [[1]]}, %MyXQL.Result{rows: [[2]]}]} =
MyXQL.query_many(c.conn, "SELECT 1; SELECT 2", [], query_type: :text)
assert {:ok, [%MyXQL.Result{rows: [[1]]}]} =
MyXQL.query_many(c.conn, "SELECT 1;", [], query_type: :text)
assert {:ok, [%MyXQL.Result{num_rows: 0, rows: nil}]} =
MyXQL.query_many(c.conn, "DROP TABLE IF EXISTS not_a_table;", [], query_type: :text)
assert {:ok, [%MyXQL.Result{num_rows: 0, rows: nil}, %MyXQL.Result{rows: [[1]]}]} =
MyXQL.query_many(c.conn, "DROP TABLE IF EXISTS not_a_table; SELECT 1;", [],
query_type: :text
)
assert {:ok, [%MyXQL.Result{rows: [[1]]}, %MyXQL.Result{num_rows: 0, rows: nil}]} =
MyXQL.query_many(c.conn, "SELECT 1; DROP TABLE IF EXISTS not_a_table;", [],
query_type: :text
)
end
test "query_many/4 with text returning error", c do
assert {:error, %MyXQL.Error{mysql: %{code: 1064}}} =
MyXQL.query_many(c.conn, "SELECT 1; BADCOMMAND;", [], query_type: :text)
end
test "query_many!/4 with text returning error", c do
assert_raise MyXQL.Error, ~r"\(1064\)", fn ->
MyXQL.query_many!(c.conn, "SELECT 1; BADCOMMAND;", [], query_type: :text)
end
end
end
describe ":prepare option" do
test ":named" do
{:ok, pid} = MyXQL.start_link(@opts ++ [prepare: :named])
{:ok, query} = MyXQL.prepare(pid, "1", "SELECT 1")
{:ok, query2, _} = MyXQL.execute(pid, query, [])
assert query == query2
{:ok, query3, _} = MyXQL.execute(pid, query, [])
assert query == query3
end
test ":unnamed" do
{:ok, pid} = MyXQL.start_link(@opts ++ [prepare: :unnamed])
{:ok, query} = MyXQL.prepare(pid, "1", "SELECT 1")
{:ok, query2, _} = MyXQL.execute(pid, query, [])
assert query == query2
{:ok, query3, _} = MyXQL.execute(pid, query, [])
assert query2.ref != query3.ref
assert query2.statement_id != query3.statement_id
end
test ":force_named" do
{:ok, pid} = MyXQL.start_link(@opts ++ [prepare: :force_named])
{:ok, query} = MyXQL.prepare(pid, "", "SELECT 1")
{:ok, query2, _} = MyXQL.execute(pid, query, [])
assert query == query2
{:ok, query3, _} = MyXQL.execute(pid, query, [])
assert query == query3
end
end
describe "prepared queries" do
setup [:connect, :truncate]
test "prepare_execute", c do
assert {:ok, query, result} = MyXQL.prepare_execute(c.conn, "", "SELECT ? * ?", [2, 3])
assert %MyXQL.Query{} = query
assert result.rows == [[6]]
end
test "prepare and then execute", c do
{:ok, query} = MyXQL.prepare(c.conn, "", "SELECT ? * ?")
assert query.num_params == 2
assert {:ok, _, result} = MyXQL.execute(c.conn, query, [2, 3])
assert result.rows == [[6]]
end
test "prepare and then execute with name", c do
{:ok, query} = MyXQL.prepare(c.conn, "foo", "SELECT ? * ?")
assert query.num_params == 2
assert {:ok, _, result} = MyXQL.execute(c.conn, query, [2, 3])
assert result.rows == [[6]]
# If we prepare it again, it won't make a difference if the name is the same
{:ok, query} = MyXQL.prepare(c.conn, "foo", "SELECT ? + ?")
assert query.num_params == 2
assert {:ok, _, result} = MyXQL.execute(c.conn, query, [2, 3])
assert result.rows == [[6]]
end
test "query is re-prepared if executed after being closed", c do
{:ok, query1} = MyXQL.prepare(c.conn, "", "SELECT 42")
assert {:ok, _, %MyXQL.Result{rows: [[42]]}} = MyXQL.execute(c.conn, query1, [])
:ok = MyXQL.close(c.conn, query1)
assert {:ok, query2, %MyXQL.Result{rows: [[42]]}} = MyXQL.execute(c.conn, query1, [])
assert query1.ref != query2.ref
assert query1.statement_id != query2.statement_id
end
test "query is re-prepared if executed after being closed with name", c do
{:ok, query1} = MyXQL.prepare(c.conn, "foo", "SELECT 42")
assert {:ok, _, %MyXQL.Result{rows: [[42]]}} = MyXQL.execute(c.conn, query1, [])
:ok = MyXQL.close(c.conn, query1)
assert {:ok, query2, %MyXQL.Result{rows: [[42]]}} = MyXQL.execute(c.conn, query1, [])
assert query1.ref != query2.ref
assert query1.statement_id != query2.statement_id
end
test "query is re-prepared if executed from different connection", c do
conn1 = c.conn
{:ok, query1} = MyXQL.prepare(conn1, "", "SELECT 42")
{:ok, conn2} = MyXQL.start_link(@opts)
{:ok, query2, %{rows: [[42]]}} = MyXQL.execute(conn2, query1, [])
assert query1.ref != query2.ref
assert query1.statement_id != query2.statement_id
end
test "query is re-prepared if executed from different connection with name", c do
conn1 = c.conn
{:ok, query1} = MyXQL.prepare(conn1, "foo", "SELECT 42")
{:ok, conn2} = MyXQL.start_link(@opts)
{:ok, query2, %{rows: [[42]]}} = MyXQL.execute(conn2, query1, [])
assert query1.ref != query2.ref
assert query1.statement_id != query2.statement_id
end
# This test is just describing existing behaviour, we may want to change it in the future.
test "prepared query is not re-prepared after schema change", c do
MyXQL.query!(c.conn, "CREATE TABLE test_prepared_schema_change (x integer)")
MyXQL.query!(c.conn, "INSERT INTO test_prepared_schema_change VALUES (1)")
{:ok, query} = MyXQL.prepare(c.conn, "", "SELECT * FROM test_prepared_schema_change")
MyXQL.query!(c.conn, "ALTER TABLE test_prepared_schema_change ADD y integer DEFAULT 2")
{:ok, query2, result} = MyXQL.execute(c.conn, query, [])
assert result.rows == [[1, 2]]
assert query.ref != query2.ref
after
MyXQL.query!(c.conn, "DROP TABLE IF EXISTS test_prepared_schema_change")
end
test "query not properly prepared", c do
assert_raise ArgumentError, ~r"has not been prepared", fn ->
query = %MyXQL.Query{statement: "SELECT 1", ref: nil, num_params: 0}
MyXQL.execute(c.conn, query, [])
end
assert_raise ArgumentError, ~r"has not been prepared", fn ->
query = %MyXQL.Query{statement: "SELECT 1", ref: make_ref(), num_params: nil}
MyXQL.execute(c.conn, query, [])
end
end
test "invalid params", c do
assert_raise ArgumentError, ~r"expected params count: 2, got values: \[1\]", fn ->
MyXQL.query(c.conn, "SELECT ? * ?", [1])
end
end
test "graceful handling of encode errors", c do
assert_raise Jason.EncodeError, fn ->
MyXQL.query!(c.conn, "SELECT ?", [%{a: <<232>>}])
end
assert_raise Jason.EncodeError, fn ->
MyXQL.query!(c.conn, "SELECT ?", [%{a: <<232>>}])
end
end
test "many rows", c do
num = 10_000
values = Enum.map_join(1..num, ", ", &"(#{&1})")
result = MyXQL.query!(c.conn, "INSERT INTO integers VALUES " <> values)
assert result.num_rows == num
{_query, result} = MyXQL.prepare_execute!(c.conn, "", "SELECT x FROM integers")
assert List.flatten(result.rows) == Enum.to_list(1..num)
assert result.num_rows == num
end
test "statement cache", c do
self = self()
MyXQL.query!(c.conn, "SELECT 1024", [], cache_statement: "select1024", log: &send(self, &1))
assert_receive %DBConnection.LogEntry{} = entry
{:ok, query1, _result} = entry.result
MyXQL.query!(c.conn, "SELECT 1024", [], cache_statement: "select1024", log: &send(self, &1))
assert_receive %DBConnection.LogEntry{} = entry
{:ok, query2, _result} = entry.result
assert query2.statement_id == query1.statement_id
end
test "disconnect on errors" do
Process.flag(:trap_exit, true)
{:ok, pid} = MyXQL.start_link([disconnect_on_error_codes: [:ER_DUP_ENTRY]] ++ @opts)
assert capture_log(fn ->
MyXQL.transaction(pid, fn conn ->
MyXQL.query(conn, "INSERT INTO uniques VALUES (1), (1)")
assert_receive {:EXIT, ^pid, :killed}, 500
end)
end) =~ "disconnected: ** (MyXQL.Error) (1062) "
end
end
describe "transactions" do
setup [:connect, :truncate]
test "commit", c do
assert %MyXQL.Result{rows: [[0]]} = MyXQL.query!(c.conn, "SELECT COUNT(1) FROM integers")
self = self()
{:ok, :success} =
MyXQL.transaction(
c.conn,
fn conn ->
MyXQL.query!(conn, "INSERT INTO integers VALUES (10)", [], log: &send(self(), &1))
MyXQL.query!(conn, "INSERT INTO integers VALUES (20)", [], log: &send(self(), &1))
:success
end,
log: &send(self, &1)
)
assert %MyXQL.Result{rows: [[2]]} = MyXQL.query!(c.conn, "SELECT COUNT(1) FROM integers")
assert_receive %DBConnection.LogEntry{} = begin_entry
assert_receive %DBConnection.LogEntry{} = query1_entry
assert_receive %DBConnection.LogEntry{} = query2_entry
assert_receive %DBConnection.LogEntry{} = commit_entry
assert begin_entry.call == :begin
assert begin_entry.query == :begin
assert {:ok, _, %MyXQL.Result{}} = begin_entry.result
assert query1_entry.call == :prepare_execute
assert query2_entry.call == :prepare_execute
assert commit_entry.call == :commit
assert commit_entry.query == :commit
assert {:ok, %MyXQL.Result{}} = commit_entry.result
end
test "rollback", c do
self = self()
assert %MyXQL.Result{rows: [[0]]} = MyXQL.query!(c.conn, "SELECT COUNT(1) FROM integers")
reason = make_ref()
{:error, ^reason} =
MyXQL.transaction(
c.conn,
fn conn ->
MyXQL.query!(conn, "INSERT INTO integers VALUES (10)", [], log: &send(self, &1))
MyXQL.rollback(conn, reason)
end,
log: &send(self(), &1)
)
assert %MyXQL.Result{rows: [[0]]} = MyXQL.query!(c.conn, "SELECT COUNT(1) FROM integers")
assert_receive %DBConnection.LogEntry{} = begin_entry
assert_receive %DBConnection.LogEntry{} = query_entry
assert_receive %DBConnection.LogEntry{} = rollback_entry
assert begin_entry.call == :begin
assert begin_entry.query == :begin
assert query_entry.call == :prepare_execute
assert rollback_entry.call == :rollback
assert rollback_entry.query == :rollback
end
test "status", c do
MyXQL.query!(c.conn, "SELECT 1")
assert DBConnection.status(c.conn) == :idle
reason = make_ref()
{:error, ^reason} =
MyXQL.transaction(c.conn, fn conn ->
assert DBConnection.status(conn) == :transaction
assert {:error, %MyXQL.Error{mysql: %{code: 1062}}} =
MyXQL.query(conn, "INSERT INTO uniques VALUES (1), (1)")
MyXQL.rollback(conn, reason)
end)
assert DBConnection.status(c.conn) == :idle
MyXQL.query!(c.conn, "SELECT 1")
end
end
describe "stream" do
setup [:connect, :truncate]
test "empty", c do
{:ok, results} =
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, "SELECT * FROM integers")
Enum.to_list(stream)
end)
assert [
%{rows: [], num_rows: 0},
%{rows: [], num_rows: 0}
] = results
# try again for the same query
{:ok, results} =
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, "SELECT * FROM integers")
Enum.to_list(stream)
end)
assert [
%{rows: [], num_rows: 0},
%{rows: [], num_rows: 0}
] = results
end
test "few rows", c do
MyXQL.query!(c.conn, "INSERT INTO integers VALUES (1), (2), (3), (4), (5)")
{:ok, results} =
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, "SELECT * FROM integers", [], max_rows: 2)
Enum.to_list(stream)
end)
assert [
%{rows: [], num_rows: 0},
%{rows: [[1], [2]]},
%{rows: [[3], [4]]},
%{rows: [[5]]}
] = results
end
test "few rows with no leftovers", c do
MyXQL.query!(c.conn, "INSERT INTO integers VALUES (1), (2), (3), (4)")
{:ok, results} =
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, "SELECT * FROM integers", [], max_rows: 2)
Enum.to_list(stream)
end)
assert [
%{rows: [], num_rows: 0},
%{rows: [[1], [2]]},
%{rows: [[3], [4]]},
%{rows: []}
] = results
end
test "many rows", c do
num = 10_000
values = Enum.map_join(1..10_000, ", ", &"(#{&1})")
MyXQL.query!(c.conn, "INSERT INTO integers VALUES " <> values)
{:ok, results} =
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, "SELECT * FROM integers")
Enum.to_list(stream)
end)
[first | results] = results
assert %{rows: [], num_rows: 0} = first
assert length(results) == 21
assert results |> Enum.map(& &1.rows) |> List.flatten() == Enum.to_list(1..num)
end
test "multiple streams", c do
MyXQL.query!(c.conn, "INSERT INTO integers VALUES (1), (2), (3), (4), (5)")
{:ok, results} =
MyXQL.transaction(c.conn, fn conn ->
odd =
MyXQL.stream(conn, "SELECT * FROM integers WHERE x % 2 != 0", [], max_rows: 2)
|> Stream.flat_map(& &1.rows)
even =
MyXQL.stream(conn, "SELECT * FROM integers WHERE x % 2 = 0", [], max_rows: 2)
|> Stream.flat_map(& &1.rows)
Enum.zip(odd, even)
end)
assert results == [{[1], [2]}, {[3], [4]}]
end
test "bad query", c do
assert_raise MyXQL.Error, ~r"\(1054\)", fn ->
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, "SELECT bad")
Enum.to_list(stream)
end)
end
end
test "invalid params", c do
assert_raise ArgumentError, ~r"expected params count: 2, got values: \[1\]", fn ->
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, "SELECT ? * ?", [1])
Enum.to_list(stream)
end)
end
end
test "with prepared query", c do
MyXQL.query!(c.conn, "INSERT INTO integers VALUES (1), (2), (3), (4), (5)")
{:ok, query} = MyXQL.prepare(c.conn, "", "SELECT * FROM integers")
{:ok, result} =
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, query, [], max_rows: 2)
Enum.to_list(stream)
end)
assert [
%{rows: [], num_rows: 0},
%{rows: [[1], [2]]},
%{rows: [[3], [4]]},
%{rows: [[5]]}
] = result
end
test "on another connection", c do
MyXQL.query!(c.conn, "INSERT INTO integers VALUES (1), (2), (3), (4), (5)")
{:ok, query} = MyXQL.prepare(c.conn, "", "SELECT * FROM integers")
{:ok, conn2} = MyXQL.start_link(@opts)
{:ok, results} =
MyXQL.transaction(conn2, fn conn ->
MyXQL.stream(conn, query) |> Enum.to_list()
end)
assert [
%{rows: [], num_rows: 0},
%{rows: [[1], [2], [3], [4], [5]]}
] = results
end
end
describe "stored procedures" do
setup :connect
test "text query", c do
assert %MyXQL.Result{rows: nil} =
MyXQL.query!(c.conn, "CALL single_ok_procedure()", [], query_type: :text)
assert [%MyXQL.Result{rows: [[1]]}, %MyXQL.Result{num_rows: 0, rows: nil}] =
MyXQL.query_many!(c.conn, "CALL one_resultset_one_ok_procedure()")
assert [
%MyXQL.Result{rows: [[1]]},
%MyXQL.Result{rows: [[2]]},
%MyXQL.Result{num_rows: 0, rows: nil}
] = MyXQL.query_many!(c.conn, "CALL multi_procedure()")
end
test "prepared query", c do
assert {%MyXQL.Query{}, %MyXQL.Result{rows: nil}} =
MyXQL.prepare_execute!(c.conn, "", "CALL single_ok_procedure()")
assert {%MyXQL.Queries{},
[%MyXQL.Result{rows: [[1]]}, %MyXQL.Result{num_rows: 0, rows: nil}]} =
MyXQL.prepare_execute_many!(c.conn, "", "CALL one_resultset_one_ok_procedure()")
assert {%MyXQL.Queries{},
[
%MyXQL.Result{rows: [[1]]},
%MyXQL.Result{rows: [[2]]},
%MyXQL.Result{num_rows: 0, rows: nil}
]} = MyXQL.prepare_execute_many!(c.conn, "", "CALL multi_procedure()")
assert %MyXQL.Query{} = query = MyXQL.prepare!(c.conn, "", "CALL single_ok_procedure()")
assert %MyXQL.Result{rows: nil} = MyXQL.execute!(c.conn, query)
assert %MyXQL.Queries{} =
query = MyXQL.prepare_many!(c.conn, "", "CALL one_resultset_one_ok_procedure()")
assert [%MyXQL.Result{rows: [[1]]}, %MyXQL.Result{num_rows: 0, rows: nil}] =
MyXQL.execute_many!(c.conn, query)
assert %MyXQL.Queries{} = query = MyXQL.prepare_many!(c.conn, "", "CALL multi_procedure()")
assert [
%MyXQL.Result{rows: [[1]]},
%MyXQL.Result{rows: [[2]]},
%MyXQL.Result{num_rows: 0, rows: nil}
] = MyXQL.execute_many!(c.conn, query)
end
test "stream stored procedure", c do
statement = "CALL one_resultset_one_ok_procedure()"
assert_raise RuntimeError, ~r"streaming stored procedures is not supported", fn ->
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, statement, [], max_rows: 2)
Enum.to_list(stream)
end)
end
end
end
describe "multiple results" do
setup :connect
test "using query/4 with a multiple result query", c do
assert_raise RuntimeError, ~r"returning multiple results is not supported", fn ->
MyXQL.query(c.conn, "SELECT 1; SELECT 2;", [], query_type: :text)
end
end
test "using prepare/4 with a multiple result query", c do
{:error, error} = MyXQL.prepare(c.conn, "foo", "SELECT 1; SELECT 2;")
assert error.message =~ "You have an error in your SQL syntax"
end
test "using prepare_execute/4 with a multiple result query", c do
{:error, error} = MyXQL.prepare_execute(c.conn, "foo", "SELECT 1; SELECT 2;")
assert error.message =~ "You have an error in your SQL syntax"
end
test "using execute/4 with a multiple result query", c do
%MyXQL.Queries{} =
query = MyXQL.prepare_many!(c.conn, "", "CALL one_resultset_one_ok_procedure()")
assert_raise FunctionClauseError, fn ->
MyXQL.execute(c.conn, query)
end
%MyXQL.Queries{} = query = MyXQL.prepare_many!(c.conn, "", "CALL multi_procedure()")
assert_raise FunctionClauseError, fn ->
MyXQL.execute(c.conn, query)
end
end
test "using query_many/4 with a single result query", c do
assert {:ok, [%MyXQL.Result{rows: [[1]]}]} =
MyXQL.query_many(c.conn, "SELECT 1;", [], query_type: :text)
end
test "using query_many/4 with a multiple result query that is not a stored procedure", c do
{:error, error} = MyXQL.query_many(c.conn, "SELECT 1; SELECT 2", [], query_type: :binary)
assert error.message =~ "You have an error in your SQL syntax"
end
test "using prepare_many/4 with a multiple result query that is not a stored procedure", c do
{:error, error} = MyXQL.prepare_many(c.conn, "foo", "SELECT 1; SELECT 2;")
assert error.message =~ "You have an error in your SQL syntax"
end
test "using prepare_execute_many/4 with a multiple result query that is not a stored procedure",
c do
{:error, error} = MyXQL.prepare_execute_many(c.conn, "foo", "SELECT 1; SELECT 2;")
assert error.message =~ "You have an error in your SQL syntax"
end
test "using prepare_execute_many/4 with a single result query", c do
assert {:ok, %MyXQL.Queries{}, [%MyXQL.Result{rows: [[1]]}]} =
MyXQL.prepare_execute_many(c.conn, "foo", "SELECT 1;")
end
test "using execute_many/4 with a single result query", c do
%MyXQL.Query{} = query = MyXQL.prepare!(c.conn, "", "CALL single_ok_procedure()")
assert_raise FunctionClauseError, fn ->
MyXQL.execute_many(c.conn, query)
end
end
test "close a multiple result prepared statement", c do
assert %MyXQL.Queries{} = query = MyXQL.prepare_many!(c.conn, "", "CALL multi_procedure()")
assert :ok == MyXQL.close(c.conn, query)
end
test "using stream/4 with a multiple result query that is not a stored procedure", c do
statement = "SELECT 1; SELECT 2;"
assert_raise MyXQL.Error, ~r"\(1064\)", fn ->
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, statement, [], max_rows: 2)
Enum.to_list(stream)
end)
end
end
end
@tag :skip
describe "idle ping" do
test "query before and after" do
opts = [backoff_type: :stop, idle_interval: 1] ++ @opts
{:ok, pid} = MyXQL.start_link(opts)
assert {:ok, _} = MyXQL.query(pid, "SELECT 42")
Process.sleep(5)
assert {:ok, _} = MyXQL.query(pid, "SELECT 42")
Process.sleep(5)
assert {:ok, _} = MyXQL.query(pid, "SELECT 42")
end
test "socket receive timeout" do
Process.flag(:trap_exit, true)
opts = [backoff_type: :stop, idle_interval: 1, ping_timeout: 0] ++ @opts
{:ok, pid} = MyXQL.start_link(opts)
assert capture_log(fn ->
assert_receive {:EXIT, ^pid, :killed}, 500
end) =~ "disconnected: ** (DBConnection.ConnectionError) timeout"
end
end
test "warnings" do
after_connect = fn conn ->
MyXQL.query!(conn, "SET SESSION sql_mode = 'ERROR_FOR_DIVISION_BY_ZERO'")
end
{:ok, conn} = MyXQL.start_link([after_connect: after_connect] ++ @opts)
assert %MyXQL.Result{num_warnings: 1, rows: [[nil]]} = MyXQL.query!(conn, "SELECT 1/0")
end
defp assert_start_and_killed(opts) do
Process.flag(:trap_exit, true)
case MyXQL.start_link(opts) do
{:ok, pid} -> assert_receive {:EXIT, ^pid, :killed}, 500
{:error, :killed} -> :ok
end
end
defp connect(c) do
{:ok, conn} = MyXQL.start_link(@opts)
Map.put(c, :conn, conn)
end
defp truncate(c) do
MyXQL.query!(c.conn, "TRUNCATE TABLE integers")
c
end
defp start_fake_server(fun) do
{:ok, listen_socket} = :gen_tcp.listen(0, mode: :binary, active: false)
{:ok, port} = :inet.port(listen_socket)
{:ok, pid} =
Task.start_link(fn ->
{:ok, accept_socket} = :gen_tcp.accept(listen_socket)
fun.(%{accept_socket: accept_socket, listen_socket: listen_socket})
end)
%{pid: pid, port: port}
end
end