16
16
*/
17
17
18
18
import { expect } from 'chai' ;
19
- import * as Long from 'long' ;
20
- import * as ProtobufJS from 'protobufjs' ;
21
19
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' ;
26
24
import {
27
25
ArrayContainsAnyFilter ,
28
26
ArrayContainsFilter ,
@@ -33,35 +31,30 @@ import {
33
31
Operator ,
34
32
OrderBy ,
35
33
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' ;
41
39
import {
42
40
DeleteMutation ,
43
41
FieldMask ,
44
42
Mutation ,
45
43
Precondition ,
46
44
SetMutation ,
47
45
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' ;
56
50
import {
57
51
DocumentWatchChange ,
58
52
WatchTargetChange ,
59
53
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' ;
65
58
import {
66
59
bound ,
67
60
byteStringFromString ,
@@ -81,24 +74,26 @@ import {
81
74
version ,
82
75
wrap ,
83
76
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
+ }
86
90
87
91
describe ( 'Serializer' , ( ) => {
88
92
const partition = new DatabaseId ( 'p' , 'd' ) ;
89
93
const s = new JsonProtoSerializer ( partition , { useProto3Json : false } ) ;
90
94
const proto3JsonSerializer = new JsonProtoSerializer ( partition , {
91
95
useProto3Json : true
92
96
} ) ;
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
102
97
103
98
/**
104
99
* Wraps the given target in TargetData. This is useful because the APIs we're
@@ -125,24 +120,13 @@ describe('Serializer', () => {
125
120
valueType : string ;
126
121
/** The expected JSON value for the field (e.g. 'NULL_VALUE') */
127
122
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 ;
135
123
/**
136
124
* If true, uses the proto3Json serializer (and skips the round-trip
137
125
* through protobufJs).
138
126
*/
139
127
useProto3Json ?: boolean ;
140
128
} ) : void {
141
129
const { value, valueType, jsonValue } = opts ;
142
- const protobufJsValue =
143
- opts . protobufJsValue !== undefined
144
- ? opts . protobufJsValue
145
- : opts . jsonValue ;
146
130
const serializer = opts . useProto3Json ? proto3JsonSerializer : s ;
147
131
148
132
// Convert FieldValue to JSON and verify.
@@ -151,20 +135,7 @@ describe('Serializer', () => {
151
135
152
136
// If we're using protobufJs JSON (not Proto3Json), then round-trip through protobufjs.
153
137
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 ) ;
168
139
}
169
140
170
141
// Convert JSON back to FieldValue.
@@ -176,8 +147,7 @@ describe('Serializer', () => {
176
147
verifyFieldValueRoundTrip ( {
177
148
value : fieldValue . NullValue . INSTANCE ,
178
149
valueType : 'nullValue' ,
179
- jsonValue : 'NULL_VALUE' ,
180
- protobufJsValue : 0
150
+ jsonValue : 'NULL_VALUE'
181
151
} ) ;
182
152
} ) ;
183
153
@@ -206,11 +176,7 @@ describe('Serializer', () => {
206
176
verifyFieldValueRoundTrip ( {
207
177
value : new fieldValue . IntegerValue ( example ) ,
208
178
valueType : 'integerValue' ,
209
- jsonValue : '' + example ,
210
- protobufJsValue : Long . fromString (
211
- example . toString ( ) ,
212
- /*unsigned=*/ false
213
- )
179
+ jsonValue : '' + example
214
180
} ) ;
215
181
}
216
182
} ) ;
@@ -270,8 +236,7 @@ describe('Serializer', () => {
270
236
verifyFieldValueRoundTrip ( {
271
237
value : new fieldValue . TimestampValue ( Timestamp . fromDate ( examples [ i ] ) ) ,
272
238
valueType : 'timestampValue' ,
273
- jsonValue : expectedJson [ i ] ,
274
- protobufJsValue : TimestampMessage . fromObject ( expectedJson [ i ] )
239
+ jsonValue : expectedJson [ i ]
275
240
} ) ;
276
241
}
277
242
} ) ;
@@ -344,8 +309,7 @@ describe('Serializer', () => {
344
309
verifyFieldValueRoundTrip ( {
345
310
value : new fieldValue . GeoPointValue ( example ) ,
346
311
valueType : 'geoPointValue' ,
347
- jsonValue : expected ,
348
- protobufJsValue : LatLngMessage . fromObject ( expected )
312
+ jsonValue : expected
349
313
} ) ;
350
314
} ) ;
351
315
@@ -379,26 +343,23 @@ describe('Serializer', () => {
379
343
verifyFieldValueRoundTrip ( {
380
344
value,
381
345
valueType : 'arrayValue' ,
382
- jsonValue,
383
- protobufJsValue : ArrayValueMessage . fromObject ( jsonValue )
346
+ jsonValue
384
347
} ) ;
385
348
} ) ;
386
349
387
350
it ( 'converts empty ArrayValue' , ( ) => {
388
351
verifyFieldValueRoundTrip ( {
389
352
value : wrap ( [ ] ) ,
390
353
valueType : 'arrayValue' ,
391
- jsonValue : { values : [ ] } ,
392
- protobufJsValue : ArrayValueMessage . fromObject ( { } )
354
+ jsonValue : { values : [ ] }
393
355
} ) ;
394
356
} ) ;
395
357
396
358
it ( 'converts ObjectValue.EMPTY' , ( ) => {
397
359
verifyFieldValueRoundTrip ( {
398
360
value : wrap ( { } ) ,
399
361
valueType : 'mapValue' ,
400
- jsonValue : { fields : { } } ,
401
- protobufJsValue : MapValueMessage . fromObject ( { } )
362
+ jsonValue : { fields : { } }
402
363
} ) ;
403
364
} ) ;
404
365
@@ -466,8 +427,7 @@ describe('Serializer', () => {
466
427
verifyFieldValueRoundTrip ( {
467
428
value : objValue ,
468
429
valueType : 'mapValue' ,
469
- jsonValue : expectedJson . mapValue ,
470
- protobufJsValue : MapValueMessage . fromObject ( expectedJson . mapValue ! )
430
+ jsonValue : expectedJson . mapValue
471
431
} ) ;
472
432
} ) ;
473
433
0 commit comments