16
16
*/
17
17
18
18
import { expect } from 'chai' ;
19
- import * as Long from 'long' ;
20
- import * as ProtobufJS from 'protobufjs' ;
21
-
22
- import { Blob } from '../../../../src/api/blob' ;
23
- import { PublicFieldValue as FieldValue } from '../../../../src/api/field_value' ;
24
- import { GeoPoint } from '../../../../src/api/geo_point' ;
25
- import { Timestamp } from '../../../../src/api/timestamp' ;
26
- import { DatabaseId } from '../../../../src/core/database_info' ;
19
+
20
+ import { Blob } from '../../../src/api/blob' ;
21
+ import { PublicFieldValue as FieldValue } from '../../../src/api/field_value' ;
22
+ import { GeoPoint } from '../../../src/api/geo_point' ;
23
+ import { Timestamp } from '../../../src/api/timestamp' ;
24
+ import { DatabaseId } from '../../../src/core/database_info' ;
27
25
import {
28
26
ArrayContainsAnyFilter ,
29
27
ArrayContainsFilter ,
@@ -34,35 +32,30 @@ import {
34
32
Operator ,
35
33
OrderBy ,
36
34
Query
37
- } from '../../../../ src/core/query' ;
38
- import { SnapshotVersion } from '../../../../ src/core/snapshot_version' ;
39
- import { Target } from '../../../../ src/core/target' ;
40
- import { TargetData , TargetPurpose } from '../../../../ src/local/target_data' ;
41
- import * as fieldValue from '../../../../ src/model/field_value' ;
35
+ } from '../../../src/core/query' ;
36
+ import { SnapshotVersion } from '../../../src/core/snapshot_version' ;
37
+ import { Target } from '../../../src/core/target' ;
38
+ import { TargetData , TargetPurpose } from '../../../src/local/target_data' ;
39
+ import * as fieldValue from '../../../src/model/field_value' ;
42
40
import {
43
41
DeleteMutation ,
44
42
FieldMask ,
45
43
Mutation ,
46
44
Precondition ,
47
45
SetMutation ,
48
46
VerifyMutation
49
- } from '../../../../src/model/mutation' ;
50
- import { DOCUMENT_KEY_NAME , FieldPath } from '../../../../src/model/path' ;
51
- import {
52
- loadRawProtos ,
53
- protoLoaderOptions
54
- } from '../../../../src/platform_node/load_protos' ;
55
- import * as api from '../../../../src/protos/firestore_proto_api' ;
56
- import { JsonProtoSerializer } from '../../../../src/remote/serializer' ;
47
+ } from '../../../src/model/mutation' ;
48
+ import { DOCUMENT_KEY_NAME , FieldPath } from '../../../src/model/path' ;
49
+ import * as api from '../../../src/protos/firestore_proto_api' ;
50
+ import { JsonProtoSerializer } from '../../../src/remote/serializer' ;
57
51
import {
58
52
DocumentWatchChange ,
59
53
WatchTargetChange ,
60
54
WatchTargetChangeState
61
- } from '../../../../src/remote/watch_change' ;
62
- import { Code , FirestoreError } from '../../../../src/util/error' ;
63
- import { Indexable } from '../../../../src/util/misc' ;
64
- import * as obj from '../../../../src/util/obj' ;
65
- import { addEqualityMatcher } from '../../../util/equality_matcher' ;
55
+ } from '../../../src/remote/watch_change' ;
56
+ import { Code , FirestoreError } from '../../../src/util/error' ;
57
+ import * as obj from '../../../src/util/obj' ;
58
+ import { addEqualityMatcher } from '../../util/equality_matcher' ;
66
59
import {
67
60
bound ,
68
61
byteStringFromString ,
@@ -82,24 +75,26 @@ import {
82
75
version ,
83
76
wrap ,
84
77
wrapObject
85
- } from '../../../util/helpers' ;
86
- import { ByteString } from '../../../../src/util/byte_string' ;
78
+ } from '../../util/helpers' ;
79
+ import { ByteString } from '../../../src/util/byte_string' ;
80
+ import { isNode } from '../../util/test_platform' ;
81
+
82
+ let verifyProtobufJsRoundTrip : ( jsonValue : api . Value ) => void = ( ) => { } ;
83
+
84
+ if ( isNode ( ) ) {
85
+ // Note: We cannot use dynamic imports since our Node build uses CJS as its
86
+ // module syntax.
87
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
88
+ verifyProtobufJsRoundTrip = require ( '../../util/node_helpers' )
89
+ . verifyProtobufJsRoundTrip ;
90
+ }
87
91
88
92
describe ( 'Serializer' , ( ) => {
89
93
const partition = new DatabaseId ( 'p' , 'd' ) ;
90
94
const s = new JsonProtoSerializer ( partition , { useProto3Json : false } ) ;
91
95
const proto3JsonSerializer = new JsonProtoSerializer ( partition , {
92
96
useProto3Json : true
93
97
} ) ;
94
- const protos = loadRawProtos ( ) ;
95
-
96
- // tslint:disable:variable-name
97
- const ValueMessage = protos . lookupType ( 'google.firestore.v1.Value' ) ;
98
- const LatLngMessage = protos . lookupType ( 'google.type.LatLng' ) ;
99
- const TimestampMessage = protos . lookupType ( 'google.protobuf.Timestamp' ) ;
100
- const ArrayValueMessage = protos . lookupType ( 'google.firestore.v1.ArrayValue' ) ;
101
- const MapValueMessage = protos . lookupType ( 'google.firestore.v1.MapValue' ) ;
102
- // tslint:enable:variable-name
103
98
104
99
/**
105
100
* Wraps the given target in TargetData. This is useful because the APIs we're
@@ -126,24 +121,13 @@ describe('Serializer', () => {
126
121
valueType : string ;
127
122
/** The expected JSON value for the field (e.g. 'NULL_VALUE') */
128
123
jsonValue : unknown ;
129
- /**
130
- * The expected protobufJs value for the field (e.g. `0`). This is
131
- * largely inconsequential (we only rely on the JSON representation), but
132
- * it can be useful for debugging issues. If omitted, it's assumed to be
133
- * the same as jsonValue.
134
- */
135
- protobufJsValue ?: unknown ;
136
124
/**
137
125
* If true, uses the proto3Json serializer (and skips the round-trip
138
126
* through protobufJs).
139
127
*/
140
128
useProto3Json ?: boolean ;
141
129
} ) : void {
142
130
const { value, valueType, jsonValue } = opts ;
143
- const protobufJsValue =
144
- opts . protobufJsValue !== undefined
145
- ? opts . protobufJsValue
146
- : opts . jsonValue ;
147
131
const serializer = opts . useProto3Json ? proto3JsonSerializer : s ;
148
132
149
133
// Convert FieldValue to JSON and verify.
@@ -152,20 +136,7 @@ describe('Serializer', () => {
152
136
153
137
// If we're using protobufJs JSON (not Proto3Json), then round-trip through protobufjs.
154
138
if ( ! opts . useProto3Json ) {
155
- // Convert JSON to protobufjs and verify value.
156
- const actualProtobufjsProto : ProtobufJS . Message = ValueMessage . fromObject (
157
- actualJsonProto
158
- ) ;
159
- expect (
160
- ( ( actualProtobufjsProto as unknown ) as Indexable ) [ valueType ]
161
- ) . to . deep . equal ( protobufJsValue ) ;
162
-
163
- // Convert protobufjs back to JSON.
164
- const returnJsonProto = ValueMessage . toObject (
165
- actualProtobufjsProto ,
166
- protoLoaderOptions
167
- ) ;
168
- expect ( returnJsonProto ) . to . deep . equal ( actualJsonProto ) ;
139
+ verifyProtobufJsRoundTrip ( actualJsonProto ) ;
169
140
}
170
141
171
142
// Convert JSON back to FieldValue.
@@ -177,8 +148,7 @@ describe('Serializer', () => {
177
148
verifyFieldValueRoundTrip ( {
178
149
value : fieldValue . NullValue . INSTANCE ,
179
150
valueType : 'nullValue' ,
180
- jsonValue : 'NULL_VALUE' ,
181
- protobufJsValue : 0
151
+ jsonValue : 'NULL_VALUE'
182
152
} ) ;
183
153
} ) ;
184
154
@@ -207,11 +177,7 @@ describe('Serializer', () => {
207
177
verifyFieldValueRoundTrip ( {
208
178
value : new fieldValue . IntegerValue ( example ) ,
209
179
valueType : 'integerValue' ,
210
- jsonValue : '' + example ,
211
- protobufJsValue : Long . fromString (
212
- example . toString ( ) ,
213
- /*unsigned=*/ false
214
- )
180
+ jsonValue : '' + example
215
181
} ) ;
216
182
}
217
183
} ) ;
@@ -271,8 +237,7 @@ describe('Serializer', () => {
271
237
verifyFieldValueRoundTrip ( {
272
238
value : new fieldValue . TimestampValue ( Timestamp . fromDate ( examples [ i ] ) ) ,
273
239
valueType : 'timestampValue' ,
274
- jsonValue : expectedJson [ i ] ,
275
- protobufJsValue : TimestampMessage . fromObject ( expectedJson [ i ] )
240
+ jsonValue : expectedJson [ i ]
276
241
} ) ;
277
242
}
278
243
} ) ;
@@ -345,8 +310,7 @@ describe('Serializer', () => {
345
310
verifyFieldValueRoundTrip ( {
346
311
value : new fieldValue . GeoPointValue ( example ) ,
347
312
valueType : 'geoPointValue' ,
348
- jsonValue : expected ,
349
- protobufJsValue : LatLngMessage . fromObject ( expected )
313
+ jsonValue : expected
350
314
} ) ;
351
315
} ) ;
352
316
@@ -380,26 +344,23 @@ describe('Serializer', () => {
380
344
verifyFieldValueRoundTrip ( {
381
345
value,
382
346
valueType : 'arrayValue' ,
383
- jsonValue,
384
- protobufJsValue : ArrayValueMessage . fromObject ( jsonValue )
347
+ jsonValue
385
348
} ) ;
386
349
} ) ;
387
350
388
351
it ( 'converts empty ArrayValue' , ( ) => {
389
352
verifyFieldValueRoundTrip ( {
390
353
value : wrap ( [ ] ) ,
391
354
valueType : 'arrayValue' ,
392
- jsonValue : { values : [ ] } ,
393
- protobufJsValue : ArrayValueMessage . fromObject ( { } )
355
+ jsonValue : { values : [ ] }
394
356
} ) ;
395
357
} ) ;
396
358
397
359
it ( 'converts ObjectValue.EMPTY' , ( ) => {
398
360
verifyFieldValueRoundTrip ( {
399
361
value : wrap ( { } ) ,
400
362
valueType : 'mapValue' ,
401
- jsonValue : { fields : { } } ,
402
- protobufJsValue : MapValueMessage . fromObject ( { } )
363
+ jsonValue : { fields : { } }
403
364
} ) ;
404
365
} ) ;
405
366
@@ -467,8 +428,7 @@ describe('Serializer', () => {
467
428
verifyFieldValueRoundTrip ( {
468
429
value : objValue ,
469
430
valueType : 'mapValue' ,
470
- jsonValue : expectedJson . mapValue ,
471
- protobufJsValue : MapValueMessage . fromObject ( expectedJson . mapValue ! )
431
+ jsonValue : expectedJson . mapValue
472
432
} ) ;
473
433
} ) ;
474
434
0 commit comments