Skip to content

Commit af5cff6

Browse files
lukesneeringerstephenplusplus
authored andcommitted
Additional Spanner tests. (#2155)
1 parent 9650b16 commit af5cff6

2 files changed

Lines changed: 104 additions & 14 deletions

File tree

packages/spanner/test/codec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,5 +441,27 @@ describe('codec', function() {
441441

442442
assert.strictEqual(encoded, 10);
443443
});
444+
445+
it('should encode each key in a dictionary-like object', function() {
446+
var obj = {
447+
f: new codec.Float(10),
448+
i: new codec.Int(10),
449+
};
450+
var encoded = codec.encode(obj);
451+
assert.deepEqual(encoded, {f: 10, i: '10'});
452+
});
453+
454+
it('should only encode public properties of objects', function() {
455+
var obj = {
456+
hasOwnProperty: function(key) { // jshint ignore:line
457+
return key === 'public';
458+
},
459+
_private: new codec.Int(10),
460+
public: new codec.Int(10),
461+
};
462+
var encoded = codec.encode(obj);
463+
assert.deepEqual(encoded._private, obj._private);
464+
assert.deepEqual(encoded.public, 10);
465+
});
444466
});
445467
});

packages/spanner/test/partial-result-stream.js

Lines changed: 82 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ describe('PartialResultStream', function() {
7676
{}
7777
]
7878
};
79+
var RESULT_WITHOUT_VALUE = {
80+
resumeToken: '...',
81+
values: []
82+
};
7983

8084
before(function() {
8185
partialResultStreamModule = proxyquire('../src/partial-result-stream.js', {
@@ -134,21 +138,22 @@ describe('PartialResultStream', function() {
134138
fakeRequestStream.push(null);
135139
});
136140

137-
it('should not queue more than 10 results', function(done) {
138-
fakeRequestStream.push(RESULT_WITHOUT_TOKEN); // 1
139-
fakeRequestStream.push(RESULT_WITHOUT_TOKEN); // 2
140-
fakeRequestStream.push(RESULT_WITHOUT_TOKEN); // 3
141-
fakeRequestStream.push(RESULT_WITHOUT_TOKEN); // 4
142-
fakeRequestStream.push(RESULT_WITHOUT_TOKEN); // 5
143-
144-
fakeRequestStream.push(RESULT_WITHOUT_TOKEN); // 6
145-
fakeRequestStream.push(RESULT_WITHOUT_TOKEN); // 7
146-
fakeRequestStream.push(RESULT_WITHOUT_TOKEN); // 8
147-
fakeRequestStream.push(RESULT_WITHOUT_TOKEN); // 9
148-
fakeRequestStream.push(RESULT_WITHOUT_TOKEN); // 10
141+
it('should effectively skip rows without values', function(done) {
142+
fakeRequestStream.push(RESULT_WITHOUT_VALUE);
143+
fakeRequestStream.push(null);
149144

150-
fakeRequestStream.push(RESULT_WITHOUT_TOKEN); // 11
145+
partialResultStream
146+
.on('error', done)
147+
.pipe(concat(function(rows) {
148+
assert.strictEqual(rows.length, 0);
149+
done();
150+
}));
151+
});
151152

153+
it('should not queue more than 10 results', function(done) {
154+
for (var i = 0; i < 11; i += 1) {
155+
fakeRequestStream.push(RESULT_WITHOUT_TOKEN);
156+
}
152157
fakeRequestStream.push(null);
153158

154159
partialResultStream
@@ -225,6 +230,28 @@ describe('PartialResultStream', function() {
225230
}));
226231
});
227232

233+
it('should correctly handle multiple rows', function(done) {
234+
var formattedRows = [[
235+
{},
236+
{}
237+
]];
238+
239+
partialResultStreamModule.formatRow_ = function() {
240+
return formattedRows;
241+
};
242+
243+
fakeRequestStream.push(RESULT_WITH_TOKEN);
244+
fakeRequestStream.push(null);
245+
246+
partialResultStream
247+
.on('error', done)
248+
.pipe(concat(function(rows) {
249+
assert.strictEqual(rows[0], formattedRows[0][0]);
250+
assert.strictEqual(rows[1], formattedRows[0][1]);
251+
done();
252+
}));
253+
});
254+
228255
it('should resume if there was an error', function(done) {
229256
// This test will emit four rows total:
230257
// - Two rows
@@ -323,6 +350,22 @@ describe('PartialResultStream', function() {
323350
partialResultStream.abort();
324351
});
325352

353+
it('should silently no-op abort if no active request', function(done) {
354+
// If no request is ever made, then there should be no active
355+
// stream to be aborted.
356+
fakeRequestStream.abort = function() {
357+
done(new Error('No request ever made; nothing to abort.'));
358+
};
359+
360+
// Create a partial result stream and then abort it, without
361+
// ever sending a request.
362+
var partialResultStream = partialResultStreamModule(function() {
363+
return fakeRequestStream;
364+
});
365+
partialResultStream.abort();
366+
done();
367+
});
368+
326369
it('should let user abort the most recent request', function(done) {
327370
fakeRequestStream.abort = function() {
328371
done(new Error('Wrong stream was aborted.'));
@@ -384,6 +427,31 @@ describe('PartialResultStream', function() {
384427
values: VALUES
385428
};
386429

430+
it('should omit rows from JSON representation with no name', function() {
431+
// Define the second field to have no name.
432+
var row = {
433+
metadata: {rowType: {fields: [
434+
{name: 'field-1'}, {}
435+
]}},
436+
values: ['value-1', 'value-2'],
437+
};
438+
// Override our `decode` function to pass through the value.
439+
decodeValueOverride = function(value) {
440+
return value;
441+
};
442+
443+
// Format the row.
444+
var formattedRows = partialResultStreamModule.formatRow_(row);
445+
446+
// Both fields should exist in the formattedRows array.
447+
assert.strictEqual(formattedRows.length, 2);
448+
assert.strictEqual(formattedRows[0].value, 'value-1');
449+
assert.strictEqual(formattedRows[1].value, 'value-2');
450+
451+
// Only the field with a name should exist in the JSON serialization.
452+
assert.deepEqual(formattedRows.toJSON(), {'field-1': 'value-1'});
453+
});
454+
387455
it('should chunk rows with more values than fields', function() {
388456
decodeValueOverride = function(value) {
389457
return value;
@@ -457,4 +525,4 @@ describe('PartialResultStream', function() {
457525
});
458526
});
459527
});
460-
});
528+
});

0 commit comments

Comments
 (0)