-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathreader.js
123 lines (105 loc) · 3.23 KB
/
reader.js
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
/*
tests-settings.json:
{
"hostname": "localhost",
"user": "test",
"password": "test"
}
*/
var nodeunit = require("nodeunit");
var oracle = require("../");
var settings;
try {
settings = JSON.parse(require('fs').readFileSync('./tests-settings-custom.json', 'utf8'));
} catch (ex) {}
settings = settings || JSON.parse(require('fs').readFileSync('./tests-settings.json', 'utf8'));
function initDb(connection, max, cb) {
connection.setPrefetchRowCount(50);
connection.execute("DROP TABLE test_table", [], function(err) {
// ignore error
connection.execute("CREATE TABLE test_table (X INT)", [], function(err) {
if (err) throw err;
function insert(i, cb) {
connection.execute("INSERT INTO test_table (X) VALUES (:1)", [i], function(err) {
if (err) throw err;
if (i < max) insert(i + 1, cb);
else cb();
});
}
insert(1, cb);
});
});
}
function doRead(reader, fn, count, cb) {
if (count === 0) return cb();
reader.nextRow(function(err, row) {
if (err) return cb(err);
if (row) {
fn(row);
return doRead(reader, fn, count - 1, cb)
} else {
return cb();
}
});
}
function testNextRow(test, connection, prefetch, requested, expected) {
connection.setPrefetchRowCount(prefetch);
var reader = connection.reader("SELECT X FROM test_table ORDER BY X", []);
var total = 0;
doRead(reader, function(row) {
total += row.X;
}, requested, function(err) {
if (err) return console.error(err);
test.equal(total, expected * (expected + 1) / 2);
test.done();
});
}
function testNextRows(test, connection, requested, len1, len2) {
var reader = connection.reader("SELECT X FROM test_table ORDER BY X", []);
reader.nextRows(requested, function(err, rows) {
if (err) return console.error(err);
test.equal(rows.length, len1);
reader.nextRows(requested, function(err, rows) {
if (err) return console.error(err);
test.equal(rows.length, len2);
test.done();
});
});
}
exports['reader'] = nodeunit.testCase({
setUp: function(callback) {
var self = this;
oracle.connect(settings, function(err, connection) {
if (err) return callback(err);
self.connection = connection;
initDb(self.connection, 100, callback);
});
},
tearDown: function(callback) {
if (this.connection) {
this.connection.close();
}
callback();
},
"reader.nextRow - request 20 with prefetch of 5": function(test) {
testNextRow(test, this.connection, 5, 20, 20);
},
"reader.nextRow - request 20 with prefetch of 200": function(test) {
testNextRow(test, this.connection, 200, 20, 20);
},
"reader.nextRow - request 200 with prefetch of 5": function(test) {
testNextRow(test, this.connection, 5, 200, 100);
},
"reader.nextRow - request 200 with prefetch of 200": function(test) {
testNextRow(test, this.connection, 200, 200, 100);
},
"reader.nextRows - request 20": function(test) {
testNextRows(test, this.connection, 20, 20, 20);
},
"reader.nextRows - request 70": function(test) {
testNextRows(test, this.connection, 70, 70, 30);
},
"reader.nextRows - request 200": function(test) {
testNextRows(test, this.connection, 200, 100, 0);
},
});