-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathlogging.exs
More file actions
913 lines (751 loc) · 28 KB
/
Copy pathlogging.exs
File metadata and controls
913 lines (751 loc) · 28 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
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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
defmodule Ecto.Integration.LoggingTest do
use Ecto.Integration.Case, async: true
alias Ecto.Integration.TestRepo
alias Ecto.Integration.PoolRepo
alias Ecto.Integration.{Post, Logging, ArrayLogging}
import ExUnit.CaptureLog
import Ecto.Query, only: [from: 2]
describe "telemetry" do
test "dispatches event" do
log = fn event_name, measurements, metadata ->
assert Enum.at(event_name, -1) == :query
assert %{result: {:ok, _res}} = metadata
assert measurements.total_time ==
measurements.query_time + measurements.decode_time + measurements.queue_time
assert measurements.idle_time
send(self(), :logged)
end
Process.put(:telemetry, log)
_ = PoolRepo.all(Post)
assert_received :logged
end
test "dispatches event with stacktrace" do
log = fn _event_name, _measurements, metadata ->
assert %{stacktrace: [_ | _]} = metadata
send(self(), :logged)
end
Process.put(:telemetry, log)
_ = PoolRepo.all(Post, stacktrace: true)
assert_received :logged
end
test "dispatches event with custom options" do
log = fn event_name, _measurements, metadata ->
assert Enum.at(event_name, -1) == :query
assert metadata.options == [:custom_metadata]
send(self(), :logged)
end
Process.put(:telemetry, log)
_ = PoolRepo.all(Post, telemetry_options: [:custom_metadata])
assert_received :logged
end
test "dispatches under another event name" do
log = fn [:custom], measurements, metadata ->
assert %{result: {:ok, _res}} = metadata
assert measurements.total_time ==
measurements.query_time + measurements.decode_time + measurements.queue_time
assert measurements.idle_time
send(self(), :logged)
end
Process.put(:telemetry, log)
_ = PoolRepo.all(Post, telemetry_event: [:custom])
assert_received :logged
end
test "is not dispatched with no event name" do
Process.put(:telemetry, fn _, _ -> raise "never called" end)
_ = TestRepo.all(Post, telemetry_event: nil)
refute_received :logged
end
test "cast params" do
uuid_module =
if TestRepo.__adapter__() == Ecto.Adapters.Tds do
Tds.Ecto.UUID
else
Ecto.UUID
end
uuid = uuid_module.generate()
dumped_uuid = uuid_module.dump!(uuid)
log = fn _event_name, _measurements, metadata ->
assert [dumped_uuid] == metadata.params
assert [uuid] == metadata.cast_params
send(self(), :logged)
end
Process.put(:telemetry, log)
TestRepo.all(from l in Logging, where: l.uuid == ^uuid)
assert_received :logged
end
end
describe "logs" do
@stacktrace_opts [stacktrace: true, log: :error]
defp stacktrace_entry(line) do
~r/↳ anonymous fn\/0 in Ecto.Integration.LoggingTest.\"test logs includes stacktraces\"\/1, at: .*integration_test\/sql\/logging.exs:#{line - 3}/
end
test "when some measurements are nil" do
assert capture_log(fn -> TestRepo.query("BEG", [], log: :error) end) =~
"[error]"
end
test "includes stacktraces" do
assert capture_log(fn ->
TestRepo.all(Post, @stacktrace_opts)
:ok
end) =~ stacktrace_entry(__ENV__.line)
assert capture_log(fn ->
TestRepo.insert(%Post{}, @stacktrace_opts)
:ok
end) =~ stacktrace_entry(__ENV__.line)
assert capture_log(fn ->
# Test cascading options
Ecto.Multi.new()
|> Ecto.Multi.insert(:post, %Post{})
|> TestRepo.transaction(@stacktrace_opts)
:ok
end) =~ stacktrace_entry(__ENV__.line)
assert capture_log(fn ->
# In theory we should point to the call _inside_ run
# but all multi calls point to the transaction starting point.
Ecto.Multi.new()
|> Ecto.Multi.run(:all, fn _, _ -> {:ok, TestRepo.all(Post, @stacktrace_opts)} end)
|> TestRepo.transaction()
:ok
end) =~ stacktrace_entry(__ENV__.line)
out = capture_log(fn ->
TestRepo.all(Post, Keyword.put(@stacktrace_opts, :log_stacktrace_mfa, {Ecto.Adapters.SQL, :first_non_ecto_stacktrace, [2]}))
:ok
end)
assert out =~ stacktrace_entry(__ENV__.line - 2)
# We are a bit liberal with what we expect as we don't want to tie to internal ExUnit code
assert out =~ ~r/ ExUnit.CaptureLog.*/
end
test "with custom log level" do
assert capture_log(fn -> TestRepo.insert!(%Post{title: "1"}, log: :error) end) =~
"[error]"
# We cannot assert on the result because it depends on the suite log level
capture_log(fn ->
TestRepo.insert!(%Post{title: "1"}, log: true)
end)
# But this assertion is always true
assert capture_log(fn ->
TestRepo.insert!(%Post{title: "1"}, log: false)
end) == ""
end
test "with a log: true override when logging is disabled" do
refute capture_log(fn ->
TestRepo.insert!(%Post{title: "1"}, log: true)
end) =~ "an exception was raised logging"
end
test "with unspecified :log option when logging is disabled" do
refute capture_log(fn ->
TestRepo.insert!(%Post{title: "1"})
end) =~ "an exception was raised logging"
end
end
describe ":comments option" do
test "comments query operations" do
assert capture_log(fn ->
TestRepo.all(Post, comments: [pre: "list_posts_q"], log: :error)
end) =~ "/* list_posts_q */ SELECT"
assert capture_log(fn ->
TestRepo.update_all(Post, [set: [visits: 0]], comments: [pre: "reset_visits_q"], log: :error)
end) =~ "/* reset_visits_q */ UPDATE"
assert capture_log(fn ->
TestRepo.delete_all(Post, comments: [pre: "purge_posts_q"], log: :error)
end) =~ "/* purge_posts_q */ DELETE"
end
test "supports both :pre and :post" do
assert capture_log(fn ->
TestRepo.all(Post, comments: [pre: "before_q", post: "after_q"], log: :error)
end) =~ ~r{/\* before_q \*/ SELECT.* /\* after_q \*/}
end
test "renders with query_cache: false (the escape hatch for dynamic comments)" do
assert capture_log(fn ->
TestRepo.all(Post, comments: [pre: "dyn_#{System.unique_integer()}"], query_cache: false, log: :error)
end) =~ ~r{/\* dyn_-?\d+ \*/ SELECT}
end
test "comments insert/update/delete/insert_all" do
assert capture_log(fn ->
TestRepo.insert!(%Post{title: "1"}, comments: [pre: "insert_create_post_q"], log: :error)
end) =~ "/* insert_create_post_q */ INSERT INTO"
post = TestRepo.insert!(%Post{title: "x"})
assert capture_log(fn ->
post
|> Ecto.Changeset.change(title: "y")
|> TestRepo.update!(comments: [pre: "update_post_q"], log: :error)
end) =~ "/* update_post_q */ UPDATE"
assert capture_log(fn ->
TestRepo.delete!(post, comments: [pre: "delete_post_q"], log: :error)
end) =~ "/* delete_post_q */ DELETE"
assert capture_log(fn ->
TestRepo.insert_all(Post, [%{title: "a"}], comments: [pre: "bulk_insert_posts_q"], log: :error)
end) =~ "/* bulk_insert_posts_q */ INSERT INTO"
end
test "rejects a comment that could break out of the comment block" do
assert_raise ArgumentError, ~r/cannot contain/, fn ->
TestRepo.insert!(%Post{title: "1"}, comments: [pre: "evil */ DROP TABLE posts"])
end
end
end
describe "parameter logging" do
@describetag :parameter_logging
@uuid_regex ~r/[0-9a-f]{2}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i
@naive_datetime_regex ~r/~N\[[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\]/
test "for insert_all with query" do
# Source query
int = 1
uuid = Ecto.UUID.generate()
source_query =
from l in Logging,
where: l.int == ^int and l.uuid == ^uuid,
select: %{uuid: l.uuid, int: l.int}
# Ensure parameters are complete and in correct order
log =
capture_log(fn ->
TestRepo.insert_all(Logging, source_query, log: :info)
end)
param_regex = ~r/\[(?<int>.+), \"(?<uuid>.+)\"\]/
param_logs = Regex.named_captures(param_regex, log)
# Query parameters
assert param_logs["int"] == Integer.to_string(int)
assert param_logs["uuid"] == uuid
end
@tag :insert_select
test "for insert_all with entries" do
# Row 1
int = 1
uuid = Ecto.UUID.generate()
uuid_query = from l in Logging, where: l.int == ^int and l.uuid == ^uuid, select: l.uuid
datetime = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
row1 = [
int: int,
uuid: uuid_query,
inserted_at: datetime,
updated_at: datetime
]
# Row 2
int2 = 2
uuid2 = Ecto.UUID.generate()
int_query = from l in Logging, where: l.int == ^int2 and l.uuid == ^uuid2, select: l.int
datetime2 = NaiveDateTime.add(datetime, 1)
row2 = [
int: int_query,
uuid: uuid2,
inserted_at: datetime2,
updated_at: datetime2
]
# Extract the parameters from the log:
# 1. Remove the colour codes
# 2. Remove the log level
# 3. Capture everything inside of the square brackets
log =
capture_log(fn ->
TestRepo.insert_all(Logging, [row1, row2], log: :info)
end)
log = Regex.replace(~r/\e\[[0-9]+m/, log, "")
log = Regex.replace(~r/\[info\]/, log, "")
log = Regex.named_captures(~r/\[(?<params>.+)\]/, log)
log_params = String.split(log["params"], ",")
# Compute the expected parameters in the right order.
# This involves recreating the headers in the same order
# as `insert_all`. The user values come first and then
# the autogenerated id
headers =
row1
|> Enum.reduce(%{}, fn {field, _}, headers -> Map.put(headers, field, true) end)
|> Map.put(:bid, true)
|> Map.keys()
row1_regex = [
int: "#{int}",
uuid: ["#{int}", uuid],
inserted_at: inspect(datetime),
updated_at: inspect(datetime),
bid: @uuid_regex
]
row2_regex = [
int: ["#{int2}", uuid2],
uuid: uuid2,
inserted_at: inspect(datetime2),
updated_at: inspect(datetime2),
bid: @uuid_regex
]
expected_param_regex =
Enum.flat_map([row1_regex, row2_regex], fn row ->
Enum.flat_map(headers, fn field ->
case Keyword.get(row, field) do
params when is_list(params) -> params
param -> [param]
end
end)
end)
assert length(log_params) == length(expected_param_regex)
Enum.zip(log_params, expected_param_regex)
|> Enum.each(fn {log, expected_regex} ->
assert log =~ expected_regex
end)
end
@tag :insert_select
@tag :placeholders
test "for insert_all with entries and placeholders" do
# Placeholders
datetime = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
datetime2 = NaiveDateTime.add(datetime, 1)
placeholder_map = %{datetime: datetime, datetime2: datetime2}
# Row 1
int = 1
uuid = Ecto.UUID.generate()
uuid_query = from l in Logging, where: l.int == ^int and l.uuid == ^uuid, select: l.uuid
row1 = [
int: int,
uuid: uuid_query,
inserted_at: {:placeholder, :datetime},
updated_at: {:placeholder, :datetime}
]
# Row 2
int2 = 2
uuid2 = Ecto.UUID.generate()
int_query = from l in Logging, where: l.int == ^int2 and l.uuid == ^uuid2, select: l.int
row2 = [
int: int_query,
uuid: uuid2,
inserted_at: {:placeholder, :datetime2},
updated_at: {:placeholder, :datetime2}
]
# Extract the parameters from the log:
# 1. Remove the colour codes
# 2. Remove the log level
# 3. Capture everything inside of the square brackets
log =
capture_log(fn ->
TestRepo.insert_all(Logging, [row1, row2],
placeholders: placeholder_map,
log: :info
)
end)
log = Regex.replace(~r/\e\[[0-9]+m/, log, "")
log = Regex.replace(~r/\[info\]/, log, "")
log = Regex.named_captures(~r/\[(?<params>.+)\]/, log)
log_params = String.split(log["params"], ",")
# Compute the expected parameters in the right order.
# This involves recreating the headers in the same order
# as `insert_all`. The placeholders come first and then
# the user values and then the autogenerated id
headers =
row1
|> Enum.reduce(%{}, fn {field, _}, headers -> Map.put(headers, field, true) end)
|> Map.put(:bid, true)
|> Map.drop([:inserted_at, :updated_at])
|> Map.keys()
row1_regex = [
int: "#{int}",
uuid: ["#{int}", uuid],
bid: @uuid_regex
]
row2_regex = [
int: ["#{int2}", uuid2],
uuid: uuid2,
bid: @uuid_regex
]
row_param_regex =
Enum.flat_map([row1_regex, row2_regex], fn row ->
Enum.flat_map(headers, fn field ->
case Keyword.get(row, field) do
params when is_list(params) -> params
param -> [param]
end
end)
end)
placeholder_regex = [inspect(datetime), inspect(datetime2)]
expected_param_regex = placeholder_regex ++ row_param_regex
assert length(log_params) == length(expected_param_regex)
Enum.zip(log_params, expected_param_regex)
|> Enum.each(fn {log, expected_regex} ->
assert log =~ expected_regex
end)
end
@tag :with_conflict_target
test "for insert_all with query with conflict query" do
# Source query
int = 1
uuid = Ecto.UUID.generate()
source_query =
from l in Logging,
where: l.int == ^int and l.uuid == ^uuid,
select: %{uuid: l.uuid, int: l.int}
# Conflict query
conflict_int = 0
conflict_uuid = Ecto.UUID.generate()
conflict_update = 2
conflict_query =
from l in Logging,
where: l.int == ^conflict_int and l.uuid == ^conflict_uuid,
update: [set: [int: ^conflict_update]]
# Ensure parameters are complete and in correct order
log =
capture_log(fn ->
TestRepo.insert_all(Logging, source_query,
on_conflict: conflict_query,
conflict_target: :bid,
log: :info
)
end)
param_regex =
~r/\[(?<int>.+), \"(?<uuid>.+)\", (?<conflict_update>.+), (?<conflict_int>.+), \"(?<conflict_uuid>.+)\"\]/
param_logs = Regex.named_captures(param_regex, log)
# Query parameters
assert param_logs["int"] == Integer.to_string(int)
assert param_logs["uuid"] == uuid
# Conflict query parameters
assert param_logs["conflict_update"] == Integer.to_string(conflict_update)
assert param_logs["conflict_int"] == Integer.to_string(conflict_int)
assert param_logs["conflict_uuid"] == conflict_uuid
end
@tag :insert_select
@tag :with_conflict_target
test "for insert_all with entries conflict query" do
# Row 1
int = 1
uuid = Ecto.UUID.generate()
uuid_query = from l in Logging, where: l.int == ^int and l.uuid == ^uuid, select: l.uuid
datetime = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
row1 = [
int: int,
uuid: uuid_query,
inserted_at: datetime,
updated_at: datetime
]
# Row 2
int2 = 2
uuid2 = Ecto.UUID.generate()
int_query = from l in Logging, where: l.int == ^int2 and l.uuid == ^uuid2, select: l.int
datetime2 = NaiveDateTime.add(datetime, 1)
row2 = [
int: int_query,
uuid: uuid2,
inserted_at: datetime2,
updated_at: datetime2
]
# Conflict query
conflict_int = 0
conflict_uuid = Ecto.UUID.generate()
conflict_update = 2
conflict_query =
from l in Logging,
where: l.int == ^conflict_int and l.uuid == ^conflict_uuid,
update: [set: [int: ^conflict_update]]
# Extract the parameters from the log:
# 1. Remove the colour codes
# 2. Remove the log level
# 3. Capture everything inside of the square brackets
log =
capture_log(fn ->
TestRepo.insert_all(Logging, [row1, row2],
on_conflict: conflict_query,
conflict_target: :bid,
log: :info
)
end)
log = Regex.replace(~r/\e\[[0-9]+m/, log, "")
log = Regex.replace(~r/\[info\]/, log, "")
log = Regex.named_captures(~r/\[(?<params>.+)\]/, log)
log_params = String.split(log["params"], ",")
# Compute the expected parameters in the right order.
# This involves recreating the headers in the same order
# as `insert_all`. The user values come first, then
# the autogenerated id and then the conflict params
headers =
row1
|> Enum.reduce(%{}, fn {field, _}, headers -> Map.put(headers, field, true) end)
|> Map.put(:bid, true)
|> Map.keys()
row1_regex = [
int: "#{int}",
uuid: ["#{int}", uuid],
bid: @uuid_regex,
inserted_at: inspect(datetime),
updated_at: inspect(datetime)
]
row2_regex = [
int: ["#{int2}", uuid2],
uuid: uuid2,
bid: @uuid_regex,
inserted_at: inspect(datetime2),
updated_at: inspect(datetime2)
]
row_param_regex =
Enum.flat_map([row1_regex, row2_regex], fn row ->
Enum.flat_map(headers, fn field ->
case Keyword.get(row, field) do
row_params when is_list(row_params) -> row_params
row_param -> [row_param]
end
end)
end)
conflict_param_regex = ["#{conflict_update}", "#{conflict_int}", conflict_uuid]
expected_param_regex = row_param_regex ++ conflict_param_regex
assert length(log_params) == length(expected_param_regex)
Enum.zip(log_params, expected_param_regex)
|> Enum.each(fn {log, expected_regex} ->
assert log =~ expected_regex
end)
end
@tag :insert_select
@tag :placeholders
@tag :with_conflict_target
test "for insert_all with entries, placeholders and conflict query" do
# Row 1
int = 1
uuid = Ecto.UUID.generate()
uuid_query = from l in Logging, where: l.int == ^int and l.uuid == ^uuid, select: l.uuid
row1 = [
int: int,
uuid: uuid_query,
inserted_at: {:placeholder, :datetime},
updated_at: {:placeholder, :datetime2}
]
# Row 2
int2 = 2
uuid2 = Ecto.UUID.generate()
int_query = from l in Logging, where: l.int == ^int2 and l.uuid == ^uuid2, select: l.int
row2 = [
int: int_query,
uuid: uuid2,
inserted_at: {:placeholder, :datetime},
updated_at: {:placeholder, :datetime2}
]
# Placeholders
datetime = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
datetime2 = NaiveDateTime.add(datetime, 1)
placeholder_map = %{datetime: datetime, datetime2: datetime2}
# Conflict query
conflict_int = 0
conflict_uuid = Ecto.UUID.generate()
conflict_update = 2
conflict_query =
from l in Logging,
where: l.int == ^conflict_int and l.uuid == ^conflict_uuid,
update: [set: [int: ^conflict_update]]
# Extract the parameters from the log:
# 1. Remove the colour codes
# 2. Remove the log level
# 3. Capture everything inside of the square brackets
log =
capture_log(fn ->
TestRepo.insert_all(Logging, [row1, row2],
placeholders: placeholder_map,
on_conflict: conflict_query,
conflict_target: :bid,
log: :info
)
end)
log = Regex.replace(~r/\e\[[0-9]+m/, log, "")
log = Regex.replace(~r/\[info\]/, log, "")
log = Regex.named_captures(~r/\[(?<params>.+)\]/, log)
log_params = String.split(log["params"], ",")
# Compute the expected parameters in the right order.
# This involves recreating the headers in the same order
# as `insert_all`. The placeholders come first, then the
# user value, then the autogenerated id and then the conflict
# params
headers =
row1
|> Enum.reduce(%{}, fn {field, _}, headers -> Map.put(headers, field, true) end)
|> Map.put(:bid, true)
|> Map.drop([:inserted_at, :updated_at])
|> Map.keys()
row1_regex = [
int: "#{int}",
uuid: ["#{int}", uuid],
bid: @uuid_regex
]
row2_regex = [
int: ["#{int2}", uuid2],
uuid: uuid2,
bid: @uuid_regex
]
row_param_regex =
Enum.flat_map([row1_regex, row2_regex], fn row ->
Enum.flat_map(headers, fn field ->
case Keyword.get(row, field) do
row_params when is_list(row_params) -> row_params
row_param -> [row_param]
end
end)
end)
placeholder_regex = [inspect(datetime), inspect(datetime2)]
conflict_param_regex = ["#{conflict_update}", "#{conflict_int}", conflict_uuid]
expected_param_regex = placeholder_regex ++ row_param_regex ++ conflict_param_regex
assert length(log_params) == length(expected_param_regex)
Enum.zip(log_params, expected_param_regex)
|> Enum.each(fn {log, expected_regex} ->
assert log =~ expected_regex
end)
end
test "for insert" do
# Insert values
int = 1
uuid = Ecto.UUID.generate()
# Ensure parameters are complete and in correct order
log =
capture_log(fn ->
TestRepo.insert!(%Logging{uuid: uuid, int: int},
log: :info
)
end)
param_regex =
~r/\[(?<int>.+), \"(?<uuid>.+)\", (?<inserted_at>.+), (?<updated_at>.+), \"(?<bid>.+)\"\]/
param_logs = Regex.named_captures(param_regex, log)
# User changes
assert param_logs["int"] == Integer.to_string(int)
assert param_logs["uuid"] == uuid
# Autogenerated changes
assert param_logs["inserted_at"] =~ @naive_datetime_regex
assert param_logs["updated_at"] =~ @naive_datetime_regex
# Filters
assert param_logs["bid"] =~ @uuid_regex
end
@tag :with_conflict_target
test "for insert with conflict query" do
# Insert values
int = 1
uuid = Ecto.UUID.generate()
# Conflict query
conflict_int = 0
conflict_uuid = Ecto.UUID.generate()
conflict_update = 2
conflict_query =
from l in Logging,
where: l.int == ^conflict_int and l.uuid == ^conflict_uuid,
update: [set: [int: ^conflict_update]]
# Ensure parameters are complete and in correct order
log =
capture_log(fn ->
TestRepo.insert!(%Logging{uuid: uuid, int: int},
on_conflict: conflict_query,
conflict_target: :bid,
log: :info
)
end)
param_regex =
~r/\[(?<int>.+), \"(?<uuid>.+)\", (?<inserted_at>.+), (?<updated_at>.+), \"(?<bid>.+)\", (?<conflict_update>.+), (?<conflict_int>.+), \"(?<conflict_uuid>.+)\"\]/
param_logs = Regex.named_captures(param_regex, log)
# User changes
assert param_logs["int"] == Integer.to_string(int)
assert param_logs["uuid"] == uuid
# Autogenerated changes
assert param_logs["inserted_at"] =~ @naive_datetime_regex
assert param_logs["updated_at"] =~ @naive_datetime_regex
# Filters
assert param_logs["bid"] =~ @uuid_regex
# Conflict query parameters
assert param_logs["conflict_update"] == Integer.to_string(conflict_update)
assert param_logs["conflict_int"] == Integer.to_string(conflict_int)
assert param_logs["conflict_uuid"] == conflict_uuid
end
test "for update" do
# Update values
int = 1
uuid = Ecto.UUID.generate()
current = TestRepo.insert!(%Logging{})
# Ensure parameters are complete and in correct order
log =
capture_log(fn ->
TestRepo.update!(Ecto.Changeset.change(current, %{uuid: uuid, int: int}), log: :info)
end)
param_regex = ~r/\[(?<int>.+), \"(?<uuid>.+)\", (?<updated_at>.+), \"(?<bid>.+)\"\]/
param_logs = Regex.named_captures(param_regex, log)
# User changes
assert param_logs["int"] == Integer.to_string(int)
assert param_logs["uuid"] == uuid
# Autogenerated changes
assert param_logs["updated_at"] =~ @naive_datetime_regex
# Filters
assert param_logs["bid"] == current.bid
end
test "for delete" do
current = TestRepo.insert!(%Logging{})
# Ensure parameters are complete and in correct order
log =
capture_log(fn ->
TestRepo.delete!(current, log: :info)
end)
param_regex = ~r/\[\"(?<bid>.+)\"\]/
param_logs = Regex.named_captures(param_regex, log)
# Filters
assert param_logs["bid"] == current.bid
end
test "for queries" do
int = 1
uuid = Ecto.UUID.generate()
# all
log =
capture_log(fn ->
TestRepo.all(
from(l in Logging,
select: type(^"1", :integer),
where: l.int == ^int and l.uuid == ^uuid
),
log: :info
)
end)
param_regex = ~r/\[(?<tagged_int>.+), (?<int>.+), \"(?<uuid>.+)\"\]/
param_logs = Regex.named_captures(param_regex, log)
assert param_logs["tagged_int"] == Integer.to_string(int)
assert param_logs["int"] == Integer.to_string(int)
assert param_logs["uuid"] == uuid
# update_all
update = 2
log =
capture_log(fn ->
from(l in Logging,
where: l.int == ^int and l.uuid == ^uuid,
update: [set: [int: ^update]]
)
|> TestRepo.update_all([], log: :info)
end)
param_regex = ~r/\[(?<update>.+), (?<int>.+), \"(?<uuid>.+)\"\]/
param_logs = Regex.named_captures(param_regex, log)
assert param_logs["update"] == Integer.to_string(update)
assert param_logs["int"] == Integer.to_string(int)
assert param_logs["uuid"] == uuid
# delete_all
log =
capture_log(fn ->
TestRepo.delete_all(from(l in Logging, where: l.int == ^int and l.uuid == ^uuid),
log: :info
)
end)
param_regex = ~r/\[(?<int>.+), \"(?<uuid>.+)\"\]/
param_logs = Regex.named_captures(param_regex, log)
assert param_logs["int"] == Integer.to_string(int)
assert param_logs["uuid"] == uuid
end
@tag :stream
test "for queries with stream" do
int = 1
uuid = Ecto.UUID.generate()
log =
capture_log(fn ->
stream =
TestRepo.stream(from(l in Logging, where: l.int == ^int and l.uuid == ^uuid),
log: :info
)
TestRepo.transaction(fn -> Enum.to_list(stream) end)
end)
param_regex = ~r/\[(?<int>.+), \"(?<uuid>.+)\"\]/
param_logs = Regex.named_captures(param_regex, log)
assert param_logs["int"] == Integer.to_string(int)
assert param_logs["uuid"] == uuid
end
@tag :array_type
test "for queries with array type" do
uuid = Ecto.UUID.generate()
uuid2 = Ecto.UUID.generate()
log =
capture_log(fn ->
TestRepo.all(from(a in ArrayLogging, where: a.uuids == ^[uuid, uuid2]),
log: :info
)
end)
param_regex = ~r/\[(?<uuids>\[.+\])\]/
param_logs = Regex.named_captures(param_regex, log)
assert param_logs["uuids"] == "[\"#{uuid}\", \"#{uuid2}\"]"
end
end
end