Skip to content

Commit

Permalink
100% Test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Oct 10, 2017
1 parent 570d766 commit d7c9a27
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 153 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class Firestore extends commonGrpc.Service {
} else {
throw new Error(
`Unsupported encoding format. Expected 'json' or 'protobufJS', ` +
`but was ${encoding}.`
`but was '${encoding}'.`
);
}

Expand Down
165 changes: 16 additions & 149 deletions test/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ const is = require('is');
const through = require('through2');

const Firestore = require('../');
const reference = require('../src/reference')(Firestore);
const DocumentReference = reference.DocumentReference;
const ResourcePath = require('../src/path').ResourcePath;

const DATABASE_ROOT = 'projects/test-project/databases/(default)';

Expand Down Expand Up @@ -194,127 +191,6 @@ function stream() {
return stream;
}

const allSupportedTypesProtobufJs = document(
'arrayValue',
{
valueType: 'arrayValue',
arrayValue: {
values: [
{
valueType: 'stringValue',
stringValue: 'foo',
},
{
valueType: 'integerValue',
integerValue: 42,
},
{
valueType: 'stringValue',
stringValue: 'bar',
},
],
},
},
'dateValue',
{
valueType: 'timestampValue',
timestampValue: {
nanos: 123000000,
seconds: 479978400,
},
},
'doubleValue',
{
valueType: 'doubleValue',
doubleValue: 0.1,
},
'falseValue',
{
valueType: 'booleanValue',
booleanValue: false,
},
'infinityValue',
{
valueType: 'doubleValue',
doubleValue: Infinity,
},
'integerValue',
{
valueType: 'integerValue',
integerValue: 0,
},
'negativeInfinityValue',
{
valueType: 'doubleValue',
doubleValue: -Infinity,
},
'nilValue',
{
valueType: 'nullValue',
nullValue: 'NULL_VALUE',
},
'objectValue',
{
valueType: 'mapValue',
mapValue: {
fields: {
foo: {
valueType: 'stringValue',
stringValue: 'bar',
},
},
},
},
'pathValue',
{
valueType: 'referenceValue',
referenceValue: `${DATABASE_ROOT}/documents/collection/document`,
},
'stringValue',
{
valueType: 'stringValue',
stringValue: 'a',
},
'trueValue',
{
valueType: 'booleanValue',
booleanValue: true,
},
'geoPointValue',
{
valueType: 'geoPointValue',
geoPointValue: {
latitude: 50.1430847,
longitude: -122.947778,
},
},
'bytesValue',
{
valueType: 'bytesValue',
bytesValue: Buffer.from('AQI=', 'base64'),
}
);

const allSupportedTypesObject = {
stringValue: 'a',
trueValue: true,
falseValue: false,
integerValue: 0,
doubleValue: 0.1,
infinityValue: Infinity,
negativeInfinityValue: -Infinity,
objectValue: {foo: 'bar'},
dateValue: new Date('Mar 18, 1985 08:20:00.123 GMT+0100 (CET)'),
pathValue: new DocumentReference(
{formattedName: DATABASE_ROOT},
new ResourcePath('test-project', '(default)', 'collection', 'document')
),
arrayValue: ['foo', 42, 'bar'],
nilValue: null,
geoPointValue: new Firestore.GeoPoint(50.1430847, -122.947778),
bytesValue: Buffer.from([0x1, 0x2]),
};

const defaultWriteResult = {
commitTime: {
nanos: 3,
Expand Down Expand Up @@ -371,15 +247,23 @@ describe('serialize document', function() {
firestore = createInstance();
});

it('serializes all supported types', function() {
it('serializes to Protobuf JS', function() {
firestore.api.Firestore._commit = function(request, options, callback) {
requestEquals(request, set(allSupportedTypesProtobufJs));
requestEquals(
request,
set(
document('bytes', {
valueType: 'bytesValue',
bytesValue: Buffer.from('AG=', 'base64'),
})
)
);
callback(null, defaultWriteResult);
};

return firestore
.doc('collectionId/documentId')
.set(allSupportedTypesObject);
.set({bytes: Buffer.from('AG=', 'base64')});
});

it("doesn't serialize unsupported types", function() {
Expand Down Expand Up @@ -586,34 +470,17 @@ describe('deserialize document', function() {
firestore = createInstance();
});

function verifyAllSupportedTypes(actualObject) {
let expected = extend(true, {}, allSupportedTypesObject);
// Deep Equal doesn't support matching instances of DocumentRefs, so we
// compare them manually and remove them from the resulting object.
assert.equal(
actualObject.get('pathValue').formattedName,
expected.pathValue.formattedName
);
let data = actualObject.data();
delete data.pathValue;
delete expected.pathValue;
assert.deepEqual(data, expected);

// We specifically test the GeoPoint properties here to ensure 100% test
// coverage.
assert.equal(50.1430847, data.geoPointValue.latitude);
assert.equal(-122.947778, data.geoPointValue.longitude);
}

it('deserializes all supported types from Protobuf JS', function() {
it('deserializes Protobuf JS', function() {
firestore.api.Firestore._batchGetDocuments = function() {
return stream(found(allSupportedTypesProtobufJs));
return stream(found(document('foo', 'bar')));
};

return firestore
.doc('collectionId/documentId')
.get()
.then(verifyAllSupportedTypes);
.then(res => {
assert.deepEqual(res.data(), {foo: 'bar'});
});
});

it('ignores intermittent stream failures', function() {
Expand Down
Loading

0 comments on commit d7c9a27

Please # to comment.