Skip to content

Commit 6bb2b6d

Browse files
author
Michael Lehenbauer
committed
CR Feedback.
1 parent 721b83b commit 6bb2b6d

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

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

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ import {
8080
describe('Serializer', () => {
8181
const partition = new DatabaseId('p', 'd');
8282
const s = new JsonProtoSerializer(partition, { useProto3Json: false });
83+
const proto3JsonSerializer = new JsonProtoSerializer(partition, {
84+
useProto3Json: true
85+
});
8386
const emptyResumeToken = new Uint8Array(0);
8487
const protos = loadRawProtos();
8588

@@ -124,36 +127,47 @@ describe('Serializer', () => {
124127
/** The expected JSON value for the field (e.g. 'NULL_VALUE') */
125128
jsonValue: unknown;
126129
/**
127-
* The expected protobufJs value for the field (e.g. `0`).
128-
* If omitted, it's expected to be the same as jsonValue.
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.
129134
*/
130135
protobufJsValue?: unknown;
136+
/**
137+
* If true, uses the proto3Json serializer (and skips the round-trip
138+
* through protobufJs).
139+
*/
140+
useProto3Json?: boolean;
131141
}): void {
132142
const { value, valueType, jsonValue } = opts;
133143
const protobufJsValue =
134144
opts.protobufJsValue !== undefined
135145
? opts.protobufJsValue
136146
: opts.jsonValue;
147+
const serializer = opts.useProto3Json ? proto3JsonSerializer : s;
137148

138149
// Convert FieldValue to JSON and verify.
139-
const actualJsonProto = s.toValue(value);
150+
const actualJsonProto = serializer.toValue(value);
140151
expect(actualJsonProto).to.deep.equal({ [valueType]: jsonValue });
141152

142-
// Convert JSON to protobufjs and verify value.
143-
const actualProtobufjsProto: ProtobufJS.Message = ValueMessage.fromObject(
144-
actualJsonProto
145-
);
146-
expect(actualProtobufjsProto[valueType]).to.deep.equal(protobufJsValue);
153+
// If we're using protobufJs JSON (not Proto3Json), then round-trip through protobufjs.
154+
if (!opts.useProto3Json) {
155+
// Convert JSON to protobufjs and verify value.
156+
const actualProtobufjsProto: ProtobufJS.Message = ValueMessage.fromObject(
157+
actualJsonProto
158+
);
159+
expect(actualProtobufjsProto[valueType]).to.deep.equal(protobufJsValue);
147160

148-
// Convert protobufjs back to JSON.
149-
const returnJsonProto = ValueMessage.toObject(
150-
actualProtobufjsProto,
151-
protoLoaderOptions
152-
);
153-
expect(returnJsonProto).to.deep.equal({ [valueType]: jsonValue });
161+
// Convert protobufjs back to JSON.
162+
const returnJsonProto = ValueMessage.toObject(
163+
actualProtobufjsProto,
164+
protoLoaderOptions
165+
);
166+
expect(returnJsonProto).to.deep.equal(actualJsonProto);
167+
}
154168

155169
// Convert JSON back to FieldValue.
156-
const actualReturnFieldValue = s.fromValue(returnJsonProto);
170+
const actualReturnFieldValue = serializer.fromValue(actualJsonProto);
157171
expect(actualReturnFieldValue.isEqual(value)).to.be.true;
158172
}
159173

@@ -316,16 +330,12 @@ describe('Serializer', () => {
316330

317331
it('converts BlobValue to Base64 string (useProto3Json=true)', () => {
318332
const base64 = 'AAECAwQF';
319-
const bytes = [0, 1, 2, 3, 4, 5];
320-
const example = Blob.fromUint8Array(new Uint8Array(bytes));
321-
322-
const expected = { bytesValue: base64 };
323-
324-
const serializer = new JsonProtoSerializer(partition, {
333+
verifyFieldValueRoundTrip({
334+
value: new fieldValue.BlobValue(Blob.fromBase64String(base64)),
335+
valueType: 'bytesValue',
336+
jsonValue: base64,
325337
useProto3Json: true
326338
});
327-
const obj = serializer.toValue(new fieldValue.BlobValue(example));
328-
expect(obj).to.deep.equal(expected);
329339
});
330340

331341
it('converts ArrayValue', () => {

0 commit comments

Comments
 (0)