From 49102ea5fc03c1216ffde70a67c047fe5bdc7073 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 25 Feb 2020 12:04:55 -0800 Subject: [PATCH 1/2] Adding test utilities to create Value types This is a port of https://github.com/firebase/firebase-android-sdk/pull/1158 --- packages/firestore/test/util/values.ts | 109 +++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 packages/firestore/test/util/values.ts diff --git a/packages/firestore/test/util/values.ts b/packages/firestore/test/util/values.ts new file mode 100644 index 00000000000..1fa478c2334 --- /dev/null +++ b/packages/firestore/test/util/values.ts @@ -0,0 +1,109 @@ +/** + * @license + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as api from '../../src/protos/firestore_proto_api'; +import * as typeUtils from '../../src/util/types'; +import { Blob } from '../../src/api/blob'; +import { Timestamp } from '../../src/api/timestamp'; +import { GeoPoint } from '../../src/api/geo_point'; +import { DocumentKeyReference } from '../../src/api/user_data_converter'; +import { DatabaseId } from '../../src/core/database_info'; +import { DocumentKey } from '../../src/model/document_key'; +import { fail } from '../../src/util/assert'; + +/** Test helper to create Firestore Value protos from JavaScript types. */ + +// TODO(mrschmidt): Move into UserDataConverter +export function valueOf(input: unknown, useProto3Json: boolean = false): api.Value { + if (input === null) { + return { nullValue: 'NULL_VALUE' }; + } else if (typeof input === 'number') { + if (typeUtils.isSafeInteger(input)) { + return { integerValue: String(input) }; + } else { + if (useProto3Json) { + // Proto 3 let's us encode NaN and Infinity as string values as + // expected by the backend. This is currently not checked by our unit + // tests because they rely on protobuf.js. + if (isNaN(doubleValue)) { + return { doubleValue: 'NaN' } as {}; + } else if (doubleValue === Infinity) { + return { doubleValue: 'Infinity' } as {}; + } else if (doubleValue === -Infinity) { + return { doubleValue: '-Infinity' } as {}; + } + } + return { doubleValue: input }; + } + } else if (typeof input === 'boolean') { + return { booleanValue: input }; + } else if (typeof input === 'string') { + return { stringValue: input }; + } else if (input instanceof Timestamp) { + return { + timestampValue: { + seconds: String(input.seconds), + nanos: input.nanoseconds + } + }; + } else if (input instanceof GeoPoint) { + return { + geoPointValue: { + latitude: input.latitude, + nanos: input.longitude + } + }; + } else if (input instanceof Blob) { + if (useProto3Json) { + return { bytesValue: input._byteString.toBase64() }; + } else { + return { bytesValue: input._byteString.toUint8Array() }; + } + } else if (input instanceof DocumentKeyReference) { + return { + referenceValue: + 'projects/project/databases/(default)/documents/' + input.ref.path + }; + } else if (Array.isArray(input)) { + return { + arrayValue: { values: input.map(el => valueOf(el, useProto3Json)) } + }; + } else if (typeof input === 'object') { + const result: api.Value = { mapValue: { fields: {} } }; + for (const key of input) { + result[key] = valueOf(input[key], useProto3Json); + } + return result; + } else { + fail(`Failed to serialize field: ${input}`); + } +} + +/** Creates a MapValue from a list of key/value arguments. */ +export function mapOf(...entries: unknown[]): api.Value { + const result: api.Value = { mapValue: { fields: {} } }; + for (let i = 0; i < entries.length; i += 2) { + result[entries[i]] = valueOf(entries[i + 1], /* useProto3Json= */ false); + } + return result; +} + +export function refValue(dbId: DatabaseId, key: DocumentKey): api.Value { + return { + referenceValue: `projects/${dbId.projectId}/databases/${dbId.databaseId}/documents/${key.path}` + }; +} From 17f7f9ca7d8eefc34e3df0cb8f7b7677d3f19806 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 25 Feb 2020 13:16:22 -0800 Subject: [PATCH 2/2] Fix compile --- packages/firestore/test/util/values.ts | 29 ++++++++++++++++---------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/firestore/test/util/values.ts b/packages/firestore/test/util/values.ts index 1fa478c2334..c76b81f5d53 100644 --- a/packages/firestore/test/util/values.ts +++ b/packages/firestore/test/util/values.ts @@ -24,11 +24,15 @@ import { DocumentKeyReference } from '../../src/api/user_data_converter'; import { DatabaseId } from '../../src/core/database_info'; import { DocumentKey } from '../../src/model/document_key'; import { fail } from '../../src/util/assert'; +import { Dict, forEach } from '../../src/util/obj'; /** Test helper to create Firestore Value protos from JavaScript types. */ // TODO(mrschmidt): Move into UserDataConverter -export function valueOf(input: unknown, useProto3Json: boolean = false): api.Value { +export function valueOf( + input: unknown, + useProto3Json: boolean = false +): api.Value { if (input === null) { return { nullValue: 'NULL_VALUE' }; } else if (typeof input === 'number') { @@ -39,11 +43,11 @@ export function valueOf(input: unknown, useProto3Json: boolean = false): api.Val // Proto 3 let's us encode NaN and Infinity as string values as // expected by the backend. This is currently not checked by our unit // tests because they rely on protobuf.js. - if (isNaN(doubleValue)) { + if (isNaN(input)) { return { doubleValue: 'NaN' } as {}; - } else if (doubleValue === Infinity) { + } else if (input === Infinity) { return { doubleValue: 'Infinity' } as {}; - } else if (doubleValue === -Infinity) { + } else if (input === -Infinity) { return { doubleValue: '-Infinity' } as {}; } } @@ -64,7 +68,7 @@ export function valueOf(input: unknown, useProto3Json: boolean = false): api.Val return { geoPointValue: { latitude: input.latitude, - nanos: input.longitude + longitude: input.longitude } }; } else if (input instanceof Blob) { @@ -76,7 +80,7 @@ export function valueOf(input: unknown, useProto3Json: boolean = false): api.Val } else if (input instanceof DocumentKeyReference) { return { referenceValue: - 'projects/project/databases/(default)/documents/' + input.ref.path + 'projects/project/databases/(default)/documents/' + input.key.path }; } else if (Array.isArray(input)) { return { @@ -84,9 +88,9 @@ export function valueOf(input: unknown, useProto3Json: boolean = false): api.Val }; } else if (typeof input === 'object') { const result: api.Value = { mapValue: { fields: {} } }; - for (const key of input) { - result[key] = valueOf(input[key], useProto3Json); - } + forEach(input as Dict, (key: string, val: unknown) => { + result.mapValue!.fields![key] = valueOf(val, useProto3Json); + }); return result; } else { fail(`Failed to serialize field: ${input}`); @@ -97,13 +101,16 @@ export function valueOf(input: unknown, useProto3Json: boolean = false): api.Val export function mapOf(...entries: unknown[]): api.Value { const result: api.Value = { mapValue: { fields: {} } }; for (let i = 0; i < entries.length; i += 2) { - result[entries[i]] = valueOf(entries[i + 1], /* useProto3Json= */ false); + result.mapValue!.fields![entries[i] as string] = valueOf( + entries[i + 1], + /* useProto3Json= */ false + ); } return result; } export function refValue(dbId: DatabaseId, key: DocumentKey): api.Value { return { - referenceValue: `projects/${dbId.projectId}/databases/${dbId.databaseId}/documents/${key.path}` + referenceValue: `projects/${dbId.projectId}/databases/${dbId.database}/documents/${key.path}` }; }