@@ -733,6 +733,49 @@ defmodule EctoWatchTest do
733
733
assert_receive { :things_inserted , % { id: 3 } }
734
734
assert_receive { { Other , :inserted } , % { weird_id: 1234 } }
735
735
assert_receive { :other_inserted , % { weird_id: 1234 } }
736
+
737
+ EctoWatch . unsubscribe ( { Thing , :inserted } )
738
+
739
+ Ecto.Adapters.SQL . query! (
740
+ TestRepo ,
741
+ "INSERT INTO things (the_string, the_integer, the_float, inserted_at, updated_at) VALUES ('the value', 4455, 84.52, NOW(), NOW())" ,
742
+ [ ]
743
+ )
744
+
745
+ refute_receive { { Thing , :inserted } , _ }
746
+ assert_receive { :things_inserted , % { id: 4 } }
747
+
748
+ EctoWatch . unsubscribe ( :things_inserted )
749
+
750
+ Ecto.Adapters.SQL . query! (
751
+ TestRepo ,
752
+ "INSERT INTO things (the_string, the_integer, the_float, inserted_at, updated_at) VALUES ('the value', 4455, 84.52, NOW(), NOW())" ,
753
+ [ ]
754
+ )
755
+
756
+ refute_receive { { Thing , :inserted } , _ }
757
+ refute_receive { :things_inserted , _ }
758
+
759
+ EctoWatch . unsubscribe ( { Other , :inserted } )
760
+
761
+ Ecto.Adapters.SQL . query! (
762
+ TestRepo ,
763
+ "INSERT INTO \" 0xabcd\" .other (weird_id, the_string, inserted_at, updated_at) VALUES (2345, 'the value', NOW(), NOW())" ,
764
+ [ ]
765
+ )
766
+
767
+ refute_receive { { Other , :inserted } , % { weird_id: 2345 } }
768
+ assert_receive { :other_inserted , % { weird_id: 2345 } }
769
+
770
+ EctoWatch . unsubscribe ( :other_inserted )
771
+
772
+ Ecto.Adapters.SQL . query! (
773
+ TestRepo ,
774
+ "INSERT INTO \" 0xabcd\" .other (weird_id, the_string, inserted_at, updated_at) VALUES (3456, 'the value', NOW(), NOW())" ,
775
+ [ ]
776
+ )
777
+
778
+ refute_receive { :other_inserted , _ }
736
779
end
737
780
738
781
test "empty extra_columns list" do
@@ -769,6 +812,28 @@ defmodule EctoWatchTest do
769
812
assert_receive { :things_inserted , % { id: 3 } }
770
813
assert_receive { { Other , :inserted } , % { weird_id: 1234 } }
771
814
assert_receive { :other_inserted , % { weird_id: 1234 } }
815
+
816
+ EctoWatch . unsubscribe ( { Thing , :inserted } )
817
+ EctoWatch . unsubscribe ( { Other , :inserted } )
818
+ EctoWatch . unsubscribe ( :things_inserted )
819
+ EctoWatch . unsubscribe ( :other_inserted )
820
+
821
+ Ecto.Adapters.SQL . query! (
822
+ TestRepo ,
823
+ "INSERT INTO things (the_string, the_integer, the_float, inserted_at, updated_at) VALUES ('the value', 4455, 84.52, NOW(), NOW())" ,
824
+ [ ]
825
+ )
826
+
827
+ Ecto.Adapters.SQL . query! (
828
+ TestRepo ,
829
+ "INSERT INTO \" 0xabcd\" .other (weird_id, the_string, inserted_at, updated_at) VALUES (2345, 'the value', NOW(), NOW())" ,
830
+ [ ]
831
+ )
832
+
833
+ refute_receive { { Thing , :inserted } , _ }
834
+ refute_receive { :things_inserted , _ }
835
+ refute_receive { { Other , :inserted } , _ }
836
+ refute_receive { :other_inserted , _ }
772
837
end
773
838
774
839
test "inserts for an association column" , % { already_existing_id2: already_existing_id2 } do
@@ -803,6 +868,23 @@ defmodule EctoWatchTest do
803
868
804
869
assert_receive { :things_parent_id_inserted ,
805
870
% { id: 3 , parent_thing_id: ^ already_existing_id2 } }
871
+
872
+ EctoWatch . unsubscribe ( { Thing , :inserted } , { :parent_thing_id , already_existing_id2 } )
873
+
874
+ EctoWatch . unsubscribe (
875
+ :things_parent_id_inserted ,
876
+ { :parent_thing_id , already_existing_id2 }
877
+ )
878
+
879
+ Ecto.Adapters.SQL . query! (
880
+ TestRepo ,
881
+ "INSERT INTO things (the_string, the_integer, the_float, parent_thing_id, extra_field, inserted_at, updated_at) VALUES ('the other value', 8900, 24.53, #{ already_existing_id2 } , 'hey', NOW(), NOW())" ,
882
+ [ ]
883
+ )
884
+
885
+ refute_receive { { Thing , :inserted } , _ }
886
+
887
+ refute_receive { :things_parent_id_inserted , _ }
806
888
end
807
889
808
890
test "column is not in list of extra_columns" , % { already_existing_id2: already_existing_id2 } do
@@ -920,6 +1002,31 @@ defmodule EctoWatchTest do
920
1002
921
1003
assert_receive { { Thing , :updated } , % { id: ^ already_existing_id2 } }
922
1004
assert_receive { :things_updated , % { id: ^ already_existing_id2 } }
1005
+
1006
+ EctoWatch . unsubscribe ( { Thing , :updated } )
1007
+
1008
+ Ecto.Adapters.SQL . query! (
1009
+ TestRepo ,
1010
+ "UPDATE things SET the_string = 'the second new value'" ,
1011
+ [ ]
1012
+ )
1013
+
1014
+ refute_receive { { Thing , :updated } , % { id: ^ already_existing_id1 } }
1015
+ assert_receive { :things_updated , % { id: ^ already_existing_id1 } }
1016
+
1017
+ refute_receive { { Thing , :updated } , % { id: ^ already_existing_id2 } }
1018
+ assert_receive { :things_updated , % { id: ^ already_existing_id2 } }
1019
+
1020
+ EctoWatch . unsubscribe ( :things_updated )
1021
+
1022
+ Ecto.Adapters.SQL . query! (
1023
+ TestRepo ,
1024
+ "UPDATE things SET the_string = 'the third new value'" ,
1025
+ [ ]
1026
+ )
1027
+
1028
+ refute_receive { :things_updated , % { id: ^ already_existing_id1 } }
1029
+ refute_receive { :things_updated , % { id: ^ already_existing_id2 } }
923
1030
end
924
1031
925
1032
test "updates for the primary key" , % {
@@ -942,6 +1049,12 @@ defmodule EctoWatchTest do
942
1049
assert_receive { { Thing , :updated } , % { id: ^ already_existing_id1 } }
943
1050
944
1051
refute_receive { { Thing , :updated } , % { id: ^ already_existing_id2 } }
1052
+
1053
+ EctoWatch . unsubscribe ( { Thing , :updated } , already_existing_id1 )
1054
+
1055
+ Ecto.Adapters.SQL . query! ( TestRepo , "UPDATE things SET the_string = 'another new value'" , [ ] )
1056
+
1057
+ refute_receive { { Thing , :updated } , % { id: ^ already_existing_id1 } }
945
1058
end
946
1059
947
1060
test "updates for an association column" , % {
@@ -979,6 +1092,28 @@ defmodule EctoWatchTest do
979
1092
980
1093
assert_receive { :things_parent_id_updated ,
981
1094
% { id: ^ already_existing_id2 , parent_thing_id: ^ already_existing_id1 } }
1095
+
1096
+ EctoWatch . unsubscribe ( { Thing , :updated } , { :parent_thing_id , already_existing_id1 } )
1097
+
1098
+ Ecto.Adapters.SQL . query! ( TestRepo , "UPDATE things SET the_string = 'another new value'" , [ ] )
1099
+
1100
+ refute_receive { { Thing , :updated } , % { id: ^ already_existing_id2 } }
1101
+
1102
+ assert_receive { :things_parent_id_updated ,
1103
+ % { id: ^ already_existing_id2 , parent_thing_id: ^ already_existing_id1 } }
1104
+
1105
+ EctoWatch . unsubscribe (
1106
+ :things_parent_id_updated ,
1107
+ { :parent_thing_id , already_existing_id1 }
1108
+ )
1109
+
1110
+ Ecto.Adapters.SQL . query! (
1111
+ TestRepo ,
1112
+ "UPDATE things SET the_string = 'yet another new value'" ,
1113
+ [ ]
1114
+ )
1115
+
1116
+ refute_receive { :things_parent_id_updated , % { id: ^ already_existing_id2 } }
982
1117
end
983
1118
984
1119
test "column is not in list of extra_columns" , % { already_existing_id2: already_existing_id2 } do
@@ -1048,6 +1183,15 @@ defmodule EctoWatchTest do
1048
1183
1049
1184
assert_receive { :thing_custom_event , % { id: ^ already_existing_id1 } }
1050
1185
refute_receive { _ , % { id: ^ already_existing_id2 } }
1186
+
1187
+ EctoWatch . unsubscribe ( :thing_custom_event , already_existing_id1 )
1188
+
1189
+ Ecto.Adapters.SQL . query! ( TestRepo , "UPDATE things SET the_string = 'the new value'" , [ ] )
1190
+ Ecto.Adapters.SQL . query! ( TestRepo , "UPDATE things SET the_integer = 9998" , [ ] )
1191
+ Ecto.Adapters.SQL . query! ( TestRepo , "UPDATE things SET the_float = 99.899" , [ ] )
1192
+
1193
+ refute_receive { _ , % { id: ^ already_existing_id1 } }
1194
+ refute_receive { _ , % { id: ^ already_existing_id2 } }
1051
1195
end
1052
1196
1053
1197
test "extra_columns option" , % {
@@ -1093,6 +1237,26 @@ defmodule EctoWatchTest do
1093
1237
% { id: ^ already_existing_id1 , the_integer: 9999 , the_float: 99.999 } }
1094
1238
1095
1239
refute_receive { { _ , :updated } , % { id: ^ already_existing_id2 } }
1240
+
1241
+ EctoWatch . unsubscribe ( { Thing , :updated } , already_existing_id1 )
1242
+
1243
+ Ecto.Adapters.SQL . query! (
1244
+ TestRepo ,
1245
+ "UPDATE things SET the_string = 'another new value' WHERE id = $1" ,
1246
+ [ already_existing_id1 ]
1247
+ )
1248
+
1249
+ Ecto.Adapters.SQL . query! ( TestRepo , "UPDATE things SET the_integer = 9999 WHERE id = $1" , [
1250
+ already_existing_id1
1251
+ ] )
1252
+
1253
+ Ecto.Adapters.SQL . query! ( TestRepo , "UPDATE things SET the_float = 99.999 WHERE id = $1" , [
1254
+ already_existing_id1
1255
+ ] )
1256
+
1257
+ refute_receive { { Thing , :updated } , % { id: ^ already_existing_id1 } }
1258
+
1259
+ refute_receive { { _ , :updated } , % { id: ^ already_existing_id2 } }
1096
1260
end
1097
1261
1098
1262
test "no notifications without subscribe" , % {
@@ -1131,6 +1295,33 @@ defmodule EctoWatchTest do
1131
1295
1132
1296
assert_receive { { Thing , :deleted } , % { id: ^ already_existing_id2 } }
1133
1297
assert_receive { :things_deleted , % { id: ^ already_existing_id2 } }
1298
+
1299
+ EctoWatch . unsubscribe ( { Thing , :deleted } )
1300
+
1301
+ Ecto.Adapters.SQL . query! (
1302
+ TestRepo ,
1303
+ "INSERT INTO things (the_string, the_integer, the_float, inserted_at, updated_at) VALUES ('the value', 4455, 84.52, NOW(), NOW())" ,
1304
+ [ ]
1305
+ )
1306
+
1307
+ Ecto.Adapters.SQL . query! ( TestRepo , "DELETE FROM things" , [ ] )
1308
+
1309
+ refute_receive { { Thing , :deleted } , _ }
1310
+
1311
+ assert_receive { :things_deleted , % { id: 3 } }
1312
+
1313
+ EctoWatch . unsubscribe ( :things_deleted )
1314
+
1315
+ Ecto.Adapters.SQL . query! (
1316
+ TestRepo ,
1317
+ "INSERT INTO things (the_string, the_integer, the_float, inserted_at, updated_at) VALUES ('the value', 4455, 84.52, NOW(), NOW())" ,
1318
+ [ ]
1319
+ )
1320
+
1321
+ Ecto.Adapters.SQL . query! ( TestRepo , "DELETE FROM things" , [ ] )
1322
+
1323
+ refute_receive { { Thing , :deleted } , _ }
1324
+ refute_receive { :things_deleted , _ }
1134
1325
end
1135
1326
1136
1327
test "empty extra_columns" , % {
@@ -1156,6 +1347,32 @@ defmodule EctoWatchTest do
1156
1347
1157
1348
assert_receive { { Thing , :deleted } , % { id: ^ already_existing_id2 } }
1158
1349
assert_receive { :things_deleted , % { id: ^ already_existing_id2 } }
1350
+
1351
+ EctoWatch . unsubscribe ( { Thing , :deleted } )
1352
+
1353
+ Ecto.Adapters.SQL . query! (
1354
+ TestRepo ,
1355
+ "INSERT INTO things (the_string, the_integer, the_float, inserted_at, updated_at) VALUES ('the value', 4455, 84.52, NOW(), NOW())" ,
1356
+ [ ]
1357
+ )
1358
+
1359
+ Ecto.Adapters.SQL . query! ( TestRepo , "DELETE FROM things" , [ ] )
1360
+
1361
+ refute_receive { { Thing , :deleted } , _ }
1362
+ assert_receive { :things_deleted , % { id: 3 } }
1363
+
1364
+ EctoWatch . unsubscribe ( :things_deleted )
1365
+
1366
+ Ecto.Adapters.SQL . query! (
1367
+ TestRepo ,
1368
+ "INSERT INTO things (the_string, the_integer, the_float, inserted_at, updated_at) VALUES ('the value', 4455, 84.52, NOW(), NOW())" ,
1369
+ [ ]
1370
+ )
1371
+
1372
+ Ecto.Adapters.SQL . query! ( TestRepo , "DELETE FROM things" , [ ] )
1373
+
1374
+ refute_receive { { Thing , :deleted } , _ }
1375
+ refute_receive { :things_deleted , _ }
1159
1376
end
1160
1377
1161
1378
test "deletes for the primary key" , % {
@@ -1178,6 +1395,19 @@ defmodule EctoWatchTest do
1178
1395
assert_receive { { Thing , :deleted } , % { id: ^ already_existing_id1 } }
1179
1396
1180
1397
refute_receive { { Thing , :deleted } , % { id: ^ already_existing_id2 } }
1398
+
1399
+ Ecto.Adapters.SQL . query! (
1400
+ TestRepo ,
1401
+ "INSERT INTO things (the_string, the_integer, the_float, inserted_at, updated_at) VALUES ('the value', 4455, 84.52, NOW(), NOW())" ,
1402
+ [ ]
1403
+ )
1404
+
1405
+ EctoWatch . subscribe ( { Thing , :deleted } , 3 )
1406
+ EctoWatch . unsubscribe ( { Thing , :deleted } , 3 )
1407
+
1408
+ Ecto.Adapters.SQL . query! ( TestRepo , "DELETE FROM things" , [ ] )
1409
+
1410
+ refute_receive { { Thing , :deleted } , _ }
1181
1411
end
1182
1412
1183
1413
test "deletes for an association column" , % {
0 commit comments