Skip to content

Commit db12269

Browse files
Merge
2 parents 70be6ac + 60e9f4a commit db12269

File tree

4 files changed

+88
-210
lines changed

4 files changed

+88
-210
lines changed

packages/firestore/test/unit/model/field_value.test.ts

Lines changed: 0 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
PrimitiveValue
3131
} from '../../../src/model/proto_field_value';
3232
import { canonicalId, estimateByteSize } from '../../../src/model/proto_values';
33-
import { ByteString } from '../../../src/util/byte_string';
3433
import { primitiveComparator } from '../../../src/util/misc';
3534
import {
3635
blob,
@@ -48,136 +47,6 @@ describe('FieldValue', () => {
4847
const date1 = new Date(2016, 4, 2, 1, 5);
4948
const date2 = new Date(2016, 5, 20, 10, 20, 30);
5049

51-
it('can parse integers', () => {
52-
const primitiveValues = [
53-
Number.MIN_SAFE_INTEGER,
54-
-1,
55-
0,
56-
1,
57-
2,
58-
Number.MAX_SAFE_INTEGER
59-
];
60-
const values = primitiveValues.map(v => wrap(v));
61-
62-
values.forEach(v => {
63-
expect(v.typeOrder).to.equal(TypeOrder.NumberValue);
64-
});
65-
66-
for (let i = 0; i < primitiveValues.length; i++) {
67-
const primitiveValue = primitiveValues[i];
68-
const value = values[i];
69-
expect(value.value()).to.equal(primitiveValue);
70-
}
71-
});
72-
73-
it('can parse doubles', () => {
74-
const primitiveValues = [
75-
Number.MIN_SAFE_INTEGER - 1,
76-
-1.1,
77-
0.1,
78-
Number.MAX_SAFE_INTEGER + 1,
79-
NaN,
80-
Infinity,
81-
-Infinity
82-
];
83-
const values = primitiveValues.map(v => wrap(v));
84-
85-
values.forEach(v => {
86-
expect(v.typeOrder).to.equal(TypeOrder.NumberValue);
87-
});
88-
89-
for (let i = 0; i < primitiveValues.length; i++) {
90-
const primitiveValue = primitiveValues[i];
91-
const value = values[i];
92-
if (isNaN(primitiveValue)) {
93-
expect(isNaN(value.value() as number)).to.equal(isNaN(primitiveValue));
94-
} else {
95-
expect(value.value()).to.equal(primitiveValue);
96-
}
97-
}
98-
});
99-
100-
it('can parse null', () => {
101-
const nullValue = wrap(null);
102-
103-
expect(nullValue.typeOrder).to.equal(TypeOrder.NullValue);
104-
expect(nullValue.value()).to.equal(null);
105-
});
106-
107-
it('can parse booleans', () => {
108-
const trueValue = wrap(true);
109-
const falseValue = wrap(false);
110-
111-
expect(trueValue.typeOrder).to.equal(TypeOrder.BooleanValue);
112-
expect(trueValue.typeOrder).to.equal(TypeOrder.BooleanValue);
113-
114-
expect(trueValue.value()).to.equal(true);
115-
expect(falseValue.value()).to.equal(false);
116-
});
117-
118-
it('can parse dates', () => {
119-
const dateValue1 = wrap(date1);
120-
const dateValue2 = wrap(date2);
121-
122-
expect(dateValue1.typeOrder).to.equal(TypeOrder.TimestampValue);
123-
expect(dateValue2.typeOrder).to.equal(TypeOrder.TimestampValue);
124-
125-
expect(dateValue1.value()).to.deep.equal(Timestamp.fromDate(date1));
126-
expect(dateValue2.value()).to.deep.equal(Timestamp.fromDate(date2));
127-
});
128-
129-
it('can parse geo points', () => {
130-
const latLong1 = new GeoPoint(1.23, 4.56);
131-
const latLong2 = new GeoPoint(-20, 100);
132-
const value1 = wrap(latLong1);
133-
const value2 = wrap(latLong2);
134-
135-
expect(value1.typeOrder).to.equal(TypeOrder.GeoPointValue);
136-
expect(value2.typeOrder).to.equal(TypeOrder.GeoPointValue);
137-
138-
expect((value1.value() as GeoPoint).latitude).to.equal(1.23);
139-
expect((value1.value() as GeoPoint).longitude).to.equal(4.56);
140-
expect((value2.value() as GeoPoint).latitude).to.equal(-20);
141-
expect((value2.value() as GeoPoint).longitude).to.equal(100);
142-
});
143-
144-
it('can parse bytes', () => {
145-
const bytesValue = wrap(blob(0, 1, 2));
146-
147-
expect(bytesValue.typeOrder).to.equal(TypeOrder.BlobValue);
148-
expect((bytesValue.value() as ByteString).toUint8Array()).to.deep.equal(
149-
new Uint8Array([0, 1, 2])
150-
);
151-
});
152-
153-
it('can parse simple objects', () => {
154-
const objValue = wrap({ a: 'foo', b: 1, c: true, d: null });
155-
156-
expect(objValue.typeOrder).to.equal(TypeOrder.ObjectValue);
157-
expect(objValue.value()).to.deep.equal({
158-
a: 'foo',
159-
b: 1,
160-
c: true,
161-
d: null
162-
});
163-
});
164-
165-
it('can parse nested objects', () => {
166-
const objValue = wrap({ foo: { bar: 1, baz: [1, 2, { a: 'b' }] } });
167-
168-
expect(objValue.typeOrder).to.equal(TypeOrder.ObjectValue);
169-
expect(objValue.value()).to.deep.equal({
170-
foo: { bar: 1, baz: [1, 2, { a: 'b' }] }
171-
});
172-
});
173-
174-
it('can parse empty objects', () => {
175-
const objValue = wrap({ foo: {} });
176-
177-
expect(objValue.typeOrder).to.equal(TypeOrder.ObjectValue);
178-
expect(objValue.value()).to.deep.equal({ foo: {} });
179-
});
180-
18150
it('can extract fields', () => {
18251
const objValue = wrapObject({ foo: { a: 1, b: true, c: 'string' } });
18352

packages/firestore/test/unit/remote/node/serializer.test.ts renamed to packages/firestore/test/unit/remote/serializer.test.ts

Lines changed: 39 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616
*/
1717

1818
import { expect } from 'chai';
19-
import * as Long from 'long';
20-
import * as ProtobufJS from 'protobufjs';
2119

22-
import { PublicFieldValue as FieldValue } from '../../../../src/api/field_value';
23-
import { GeoPoint } from '../../../../src/api/geo_point';
24-
import { Timestamp } from '../../../../src/api/timestamp';
25-
import { DatabaseId } from '../../../../src/core/database_info';
20+
import { PublicFieldValue as FieldValue } from '../../../src/api/field_value';
21+
import { GeoPoint } from '../../../src/api/geo_point';
22+
import { Timestamp } from '../../../src/api/timestamp';
23+
import { DatabaseId } from '../../../src/core/database_info';
2624
import {
2725
ArrayContainsAnyFilter,
2826
ArrayContainsFilter,
@@ -33,35 +31,30 @@ import {
3331
Operator,
3432
OrderBy,
3533
Query
36-
} from '../../../../src/core/query';
37-
import { SnapshotVersion } from '../../../../src/core/snapshot_version';
38-
import { Target } from '../../../../src/core/target';
39-
import { TargetData, TargetPurpose } from '../../../../src/local/target_data';
40-
import * as fieldValue from '../../../../src/model/field_value';
34+
} from '../../../src/core/query';
35+
import { SnapshotVersion } from '../../../src/core/snapshot_version';
36+
import { Target } from '../../../src/core/target';
37+
import { TargetData, TargetPurpose } from '../../../src/local/target_data';
38+
import * as fieldValue from '../../../src/model/field_value';
4139
import {
4240
DeleteMutation,
4341
FieldMask,
4442
Mutation,
4543
Precondition,
4644
SetMutation,
4745
VerifyMutation
48-
} from '../../../../src/model/mutation';
49-
import { DOCUMENT_KEY_NAME, FieldPath } from '../../../../src/model/path';
50-
import {
51-
loadRawProtos,
52-
protoLoaderOptions
53-
} from '../../../../src/platform_node/load_protos';
54-
import * as api from '../../../../src/protos/firestore_proto_api';
55-
import { JsonProtoSerializer } from '../../../../src/remote/serializer';
46+
} from '../../../src/model/mutation';
47+
import { DOCUMENT_KEY_NAME, FieldPath } from '../../../src/model/path';
48+
import * as api from '../../../src/protos/firestore_proto_api';
49+
import { JsonProtoSerializer } from '../../../src/remote/serializer';
5650
import {
5751
DocumentWatchChange,
5852
WatchTargetChange,
5953
WatchTargetChangeState
60-
} from '../../../../src/remote/watch_change';
61-
import { Code, FirestoreError } from '../../../../src/util/error';
62-
import { Indexable } from '../../../../src/util/misc';
63-
import * as obj from '../../../../src/util/obj';
64-
import { addEqualityMatcher } from '../../../util/equality_matcher';
54+
} from '../../../src/remote/watch_change';
55+
import { Code, FirestoreError } from '../../../src/util/error';
56+
import * as obj from '../../../src/util/obj';
57+
import { addEqualityMatcher } from '../../util/equality_matcher';
6558
import {
6659
bound,
6760
byteStringFromString,
@@ -81,24 +74,26 @@ import {
8174
version,
8275
wrap,
8376
wrapObject
84-
} from '../../../util/helpers';
85-
import { ByteString } from '../../../../src/util/byte_string';
77+
} from '../../util/helpers';
78+
import { ByteString } from '../../../src/util/byte_string';
79+
import { isNode } from '../../util/test_platform';
80+
81+
let verifyProtobufJsRoundTrip: (jsonValue: api.Value) => void = () => {};
82+
83+
if (isNode()) {
84+
// Note: We cannot use dynamic imports since our Node build uses CJS as its
85+
// module syntax.
86+
// eslint-disable-next-line @typescript-eslint/no-require-imports
87+
verifyProtobufJsRoundTrip = require('../../util/node_helpers')
88+
.verifyProtobufJsRoundTrip;
89+
}
8690

8791
describe('Serializer', () => {
8892
const partition = new DatabaseId('p', 'd');
8993
const s = new JsonProtoSerializer(partition, { useProto3Json: false });
9094
const proto3JsonSerializer = new JsonProtoSerializer(partition, {
9195
useProto3Json: true
9296
});
93-
const protos = loadRawProtos();
94-
95-
// tslint:disable:variable-name
96-
const ValueMessage = protos.lookupType('google.firestore.v1.Value');
97-
const LatLngMessage = protos.lookupType('google.type.LatLng');
98-
const TimestampMessage = protos.lookupType('google.protobuf.Timestamp');
99-
const ArrayValueMessage = protos.lookupType('google.firestore.v1.ArrayValue');
100-
const MapValueMessage = protos.lookupType('google.firestore.v1.MapValue');
101-
// tslint:enable:variable-name
10297

10398
/**
10499
* Wraps the given target in TargetData. This is useful because the APIs we're
@@ -125,24 +120,13 @@ describe('Serializer', () => {
125120
valueType: string;
126121
/** The expected JSON value for the field (e.g. 'NULL_VALUE') */
127122
jsonValue: unknown;
128-
/**
129-
* The expected protobufJs value for the field (e.g. `0`). This is
130-
* largely inconsequential (we only rely on the JSON representation), but
131-
* it can be useful for debugging issues. If omitted, it's assumed to be
132-
* the same as jsonValue.
133-
*/
134-
protobufJsValue?: unknown;
135123
/**
136124
* If true, uses the proto3Json serializer (and skips the round-trip
137125
* through protobufJs).
138126
*/
139127
useProto3Json?: boolean;
140128
}): void {
141129
const { value, valueType, jsonValue } = opts;
142-
const protobufJsValue =
143-
opts.protobufJsValue !== undefined
144-
? opts.protobufJsValue
145-
: opts.jsonValue;
146130
const serializer = opts.useProto3Json ? proto3JsonSerializer : s;
147131

148132
// Convert FieldValue to JSON and verify.
@@ -151,20 +135,7 @@ describe('Serializer', () => {
151135

152136
// If we're using protobufJs JSON (not Proto3Json), then round-trip through protobufjs.
153137
if (!opts.useProto3Json) {
154-
// Convert JSON to protobufjs and verify value.
155-
const actualProtobufjsProto: ProtobufJS.Message = ValueMessage.fromObject(
156-
actualJsonProto
157-
);
158-
expect(
159-
((actualProtobufjsProto as unknown) as Indexable)[valueType]
160-
).to.deep.equal(protobufJsValue);
161-
162-
// Convert protobufjs back to JSON.
163-
const returnJsonProto = ValueMessage.toObject(
164-
actualProtobufjsProto,
165-
protoLoaderOptions
166-
);
167-
expect(returnJsonProto).to.deep.equal(actualJsonProto);
138+
verifyProtobufJsRoundTrip(actualJsonProto);
168139
}
169140

170141
// Convert JSON back to FieldValue.
@@ -176,8 +147,7 @@ describe('Serializer', () => {
176147
verifyFieldValueRoundTrip({
177148
value: fieldValue.NullValue.INSTANCE,
178149
valueType: 'nullValue',
179-
jsonValue: 'NULL_VALUE',
180-
protobufJsValue: 0
150+
jsonValue: 'NULL_VALUE'
181151
});
182152
});
183153

@@ -206,11 +176,7 @@ describe('Serializer', () => {
206176
verifyFieldValueRoundTrip({
207177
value: new fieldValue.IntegerValue(example),
208178
valueType: 'integerValue',
209-
jsonValue: '' + example,
210-
protobufJsValue: Long.fromString(
211-
example.toString(),
212-
/*unsigned=*/ false
213-
)
179+
jsonValue: '' + example
214180
});
215181
}
216182
});
@@ -270,8 +236,7 @@ describe('Serializer', () => {
270236
verifyFieldValueRoundTrip({
271237
value: new fieldValue.TimestampValue(Timestamp.fromDate(examples[i])),
272238
valueType: 'timestampValue',
273-
jsonValue: expectedJson[i],
274-
protobufJsValue: TimestampMessage.fromObject(expectedJson[i])
239+
jsonValue: expectedJson[i]
275240
});
276241
}
277242
});
@@ -344,8 +309,7 @@ describe('Serializer', () => {
344309
verifyFieldValueRoundTrip({
345310
value: new fieldValue.GeoPointValue(example),
346311
valueType: 'geoPointValue',
347-
jsonValue: expected,
348-
protobufJsValue: LatLngMessage.fromObject(expected)
312+
jsonValue: expected
349313
});
350314
});
351315

@@ -379,26 +343,23 @@ describe('Serializer', () => {
379343
verifyFieldValueRoundTrip({
380344
value,
381345
valueType: 'arrayValue',
382-
jsonValue,
383-
protobufJsValue: ArrayValueMessage.fromObject(jsonValue)
346+
jsonValue
384347
});
385348
});
386349

387350
it('converts empty ArrayValue', () => {
388351
verifyFieldValueRoundTrip({
389352
value: wrap([]),
390353
valueType: 'arrayValue',
391-
jsonValue: { values: [] },
392-
protobufJsValue: ArrayValueMessage.fromObject({})
354+
jsonValue: { values: [] }
393355
});
394356
});
395357

396358
it('converts ObjectValue.EMPTY', () => {
397359
verifyFieldValueRoundTrip({
398360
value: wrap({}),
399361
valueType: 'mapValue',
400-
jsonValue: { fields: {} },
401-
protobufJsValue: MapValueMessage.fromObject({})
362+
jsonValue: { fields: {} }
402363
});
403364
});
404365

@@ -466,8 +427,7 @@ describe('Serializer', () => {
466427
verifyFieldValueRoundTrip({
467428
value: objValue,
468429
valueType: 'mapValue',
469-
jsonValue: expectedJson.mapValue,
470-
protobufJsValue: MapValueMessage.fromObject(expectedJson.mapValue!)
430+
jsonValue: expectedJson.mapValue
471431
});
472432
});
473433

0 commit comments

Comments
 (0)