@@ -23,12 +23,43 @@ module.exports = {
23
23
querySequence ( conn . db , [
24
24
'DROP TABLE IF EXISTS ' + escId ( table ) ,
25
25
'CREATE TABLE ' + escId ( table ) + ' (col INT UNSIGNED)' ,
26
- 'INSERT INTO ' + escId ( table ) + ' (col) VALUES (10)' ,
27
26
] , function ( results ) {
28
27
queries . splice ( 0 , queries . length ) ;
29
28
30
29
var query = 'SELECT * FROM ' + escId ( table ) ;
30
+ var conditionCheckIndex = 0 ;
31
31
var triggers = [ {
32
+ database : server . database ,
33
+ table : table ,
34
+ condition : function ( row , newRow , isDeleted ) {
35
+ // Ensure that each call of this condition function receives
36
+ // the correct arguments
37
+ conditionCheckIndex ++ ;
38
+ switch ( conditionCheckIndex ) {
39
+ case 1 :
40
+ // Row has been inserted
41
+ test . equal ( row . col , 10 ) ;
42
+ test . equal ( newRow , null ) ;
43
+ test . equal ( isDeleted , false ) ;
44
+ break ;
45
+ case 2 :
46
+ // Row has been updated
47
+ test . equal ( row . col , 10 ) ;
48
+ test . equal ( newRow . col , 15 ) ;
49
+ test . equal ( isDeleted , null ) ;
50
+ break ;
51
+ case 3 :
52
+ // Row has been deleted
53
+ test . equal ( row . col , 15 ) ;
54
+ test . equal ( newRow , null ) ;
55
+ test . equal ( isDeleted , true ) ;
56
+ break ;
57
+ }
58
+ return true ;
59
+ }
60
+ } ] ;
61
+ // Second, resultsBuffer check query doesn't need the condition
62
+ var triggersSimple = [ {
32
63
database : server . database ,
33
64
table : table
34
65
} ] ;
@@ -37,12 +68,13 @@ module.exports = {
37
68
// After initial update
38
69
if ( data . length > 0 && data [ 0 ] . col === 10 ) {
39
70
// Second select instance to check resultsBuffer
40
- conn . select ( query , triggers ) . on ( 'update' , function ( data ) {
71
+ conn . select ( query , triggersSimple ) . on ( 'update' , function ( data ) {
41
72
if ( data . length > 0 && data [ 0 ] . col === 15 ) {
42
73
// [1] Test in LiveMysqlSelect created later,
43
74
// Ensure only First select, update, second select occurred
75
+ // Along with the INSERT and UPDATE queries, 5 total
44
76
// i.e. No duplicate selects, resultsBuffer working
45
- test . equal ( queries . length , 3 ) ;
77
+ test . equal ( queries . length , 5 ) ;
46
78
conn . db . query ( 'DELETE FROM ' + escId ( table ) ) ;
47
79
}
48
80
} ) . on ( 'added' , function ( row , index ) {
@@ -74,12 +106,6 @@ module.exports = {
74
106
}
75
107
} ) ;
76
108
77
- querySequence ( conn . db , [
78
- 'UPDATE ' + escId ( table ) +
79
- ' SET `col` = 15'
80
- ] , function ( results ) {
81
- // ...
82
- } ) ;
83
109
}
84
110
} ) . on ( 'added' , function ( row , index ) {
85
111
test . equal ( index , 0 ) ;
@@ -94,6 +120,20 @@ module.exports = {
94
120
test . done ( ) ;
95
121
} ) ;
96
122
123
+ // Perform database operation sequence
124
+ querySequence ( conn . db , [
125
+ 'INSERT INTO ' + escId ( table ) + ' (col) VALUES (10)'
126
+ ] , function ( results ) {
127
+ // Wait before updating the row
128
+ setTimeout ( function ( ) {
129
+ querySequence ( conn . db , [
130
+ 'UPDATE ' + escId ( table ) + ' SET `col` = 15'
131
+ ] , function ( results ) {
132
+ // ...
133
+ } ) ;
134
+ } , 100 ) ;
135
+ } ) ;
136
+
97
137
} ) ;
98
138
} ) ;
99
139
} ,
0 commit comments