Skip to content

Commit edfa3b5

Browse files
committed
add assert.calls to async functions within tests
1 parent 1e3107a commit edfa3b5

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

test/integration/client/copy-tests.js

+22-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ if(helper.args.native) {
44
pg = require(__dirname + '/../../../lib').native;
55
}
66
var ROWS_TO_INSERT = 1000;
7+
78
var prepareTable = function (client, callback) {
89
client.query(
910
'CREATE TEMP TABLE copy_test (id SERIAL, name CHARACTER VARYING(10), age INT)',
@@ -13,8 +14,9 @@ var prepareTable = function (client, callback) {
1314
})
1415
);
1516
};
17+
1618
test('COPY FROM', function () {
17-
pg.connect(helper.config, function (error, client) {
19+
pg.connect(helper.config, assert.calls(function (error, client) {
1820
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
1921
prepareTable(client, function () {
2022
var stream = client.copyFrom("COPY copy_test (name, age) FROM stdin WITH CSV");
@@ -25,20 +27,21 @@ test('COPY FROM', function () {
2527
stream.write( String(Date.now() + Math.random()).slice(0,10) + ',' + i + '\n');
2628
}
2729
assert.emits(stream, 'close', function () {
28-
client.query("SELECT count(*), sum(age) from copy_test", function (err, result) {
30+
client.query("SELECT count(*), sum(age) from copy_test", assert.calls(function (err, result) {
2931
assert.equal(err, null, "Query should not fail");
3032
assert.lengthIs(result.rows, 1)
3133
assert.equal(result.rows[0].sum, ROWS_TO_INSERT * (0 + ROWS_TO_INSERT -1)/2);
3234
assert.equal(result.rows[0].count, ROWS_TO_INSERT);
3335
pg.end(helper.config);
34-
});
36+
}));
3537
}, "COPY FROM stream should emit close after query end");
3638
stream.end();
3739
});
38-
});
40+
}));
3941
});
42+
4043
test('COPY TO', function () {
41-
pg.connect(helper.config, function (error, client) {
44+
pg.connect(helper.config, assert.calls(function (error, client) {
4245
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
4346
prepareTable(client, function () {
4447
var stream = client.copyTo("COPY person (id, name, age) TO stdin WITH CSV");
@@ -56,10 +59,11 @@ test('COPY TO', function () {
5659
pg.end(helper.config);
5760
}, "COPY IN stream should emit end event after all rows");
5861
});
59-
});
62+
}));
6063
});
64+
6165
test('COPY TO, queue queries', function () {
62-
pg.connect(helper.config, function (error, client) {
66+
pg.connect(helper.config, assert.calls(function (error, client) {
6367
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
6468
prepareTable(client, function () {
6569
var query1Done = false,
@@ -73,10 +77,10 @@ test('COPY TO, queue queries', function () {
7377
//imitate long query, to make impossible,
7478
//that copy query end callback runs after
7579
//second query callback
76-
client.query("SELECT pg_sleep(5)", function () {
80+
client.query("SELECT pg_sleep(1)", assert.calls(function () {
7781
query2Done = true;
7882
assert.ok(copyQueryDone && query2Done, "second query has to be executed after others");
79-
});
83+
}));
8084
var buf = new Buffer(0);
8185
stream.on('error', function (error) {
8286
assert.ok(false, "COPY TO stream should not emit errors" + helper.sys.inspect(error));
@@ -93,15 +97,16 @@ test('COPY TO, queue queries', function () {
9397
pg.end(helper.config);
9498
}, "COPY IN stream should emit end event after all rows");
9599
});
96-
});
100+
}));
97101
});
102+
98103
test("COPY TO incorrect usage with large data", function () {
99104
//when many data is loaded from database (and it takes a lot of time)
100105
//there are chance, that query will be canceled before it ends
101106
//but if there are not so much data, cancel message may be
102107
//send after copy query ends
103108
//so we need to test both situations
104-
pg.connect(helper.config, function (error, client) {
109+
pg.connect(helper.config, assert.calls(function (error, client) {
105110
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
106111
//intentionally incorrect usage of copy.
107112
//this has to report error in standart way, instead of just throwing exception
@@ -116,10 +121,11 @@ test("COPY TO incorrect usage with large data", function () {
116121
}));
117122
})
118123
);
119-
});
124+
}));
120125
});
126+
121127
test("COPY TO incorrect usage with small data", function () {
122-
pg.connect(helper.config, function (error, client) {
128+
pg.connect(helper.config, assert.calls(function (error, client) {
123129
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
124130
//intentionally incorrect usage of copy.
125131
//this has to report error in standart way, instead of just throwing exception
@@ -134,11 +140,11 @@ test("COPY TO incorrect usage with small data", function () {
134140
}));
135141
})
136142
);
137-
});
143+
}));
138144
});
139145

140146
test("COPY FROM incorrect usage", function () {
141-
pg.connect(helper.config, function (error, client) {
147+
pg.connect(helper.config, assert.calls(function (error, client) {
142148
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
143149
prepareTable(client, function () {
144150
//intentionally incorrect usage of copy.
@@ -155,6 +161,6 @@ test("COPY FROM incorrect usage", function () {
155161
})
156162
);
157163
});
158-
});
164+
}));
159165
});
160166

0 commit comments

Comments
 (0)