Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Share Integration tests between firestore and firestore-exp #3338

Closed
wants to merge 12 commits into from
7 changes: 5 additions & 2 deletions packages/firestore/exp/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ export function setLogLevel(logLevel: LogLevel): void;
export interface FirestoreDataConverter<T> {
toFirestore(modelObject: T): DocumentData;
toFirestore(modelObject: Partial<T>, options: SetOptions): DocumentData;
fromFirestore(snapshot: QueryDocumentSnapshot<DocumentData>): T;
fromFirestore(
snapshot: QueryDocumentSnapshot<DocumentData>,
options: SnapshotOptions
): T;
}

export class FirebaseFirestore {
Expand Down Expand Up @@ -249,7 +252,7 @@ export class DocumentSnapshot<T = DocumentData> {
export class QueryDocumentSnapshot<T = DocumentData> extends DocumentSnapshot<
T
> {
data(): T;
data(options?: SnapshotOptions): T;
}

export type OrderByDirection = 'desc' | 'asc';
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/exp/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export {
parent
} from '../lite/src/api/reference';

export { runTransaction, Transaction } from '../lite/src/api/transaction';
export { runTransaction, Transaction } from './src/api/transaction';

export {
getDoc,
Expand Down
46 changes: 29 additions & 17 deletions packages/firestore/exp/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,27 @@ import { LruParams } from '../../../src/local/lru_garbage_collector';
export class Firestore extends LiteFirestore
implements firestore.FirebaseFirestore, _FirebaseService {
private readonly _queue = new AsyncQueue();
private readonly _firestoreClient: FirestoreClient;
private readonly _persistenceKey: string;
private _componentProvider: ComponentProvider = new MemoryComponentProvider();

// Assigned via _getFirestoreClient()
private _firestoreClientPromise?: Promise<FirestoreClient>;
private _deferredInitialization?: Promise<void>;

protected _persistenceSettings: PersistenceSettings = { durable: false };
// We override the Settings property of the Lite SDK since the full Firestore
// SDK supports more settings.
protected _settings?: firestore.Settings;

_terminated: boolean = false;
private _terminated: boolean = false;

constructor(
app: FirebaseApp,
authProvider: Provider<FirebaseAuthInternalName>
) {
super(app, authProvider);
this._persistenceKey = app.name;
this._firestoreClient = new FirestoreClient(this._credentials, this._queue);
}

_getSettings(): firestore.Settings {
Expand All @@ -75,26 +77,29 @@ export class Firestore extends LiteFirestore
}

_getFirestoreClient(): Promise<FirestoreClient> {
if (!this._firestoreClientPromise) {
if (this._terminated) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'The client has already been terminated.'
);
}

if (!this._deferredInitialization) {
const settings = this._getSettings();
const databaseInfo = this._makeDatabaseInfo(
settings.host,
settings.ssl,
settings.experimentalForceLongPolling
);

const firestoreClient = new FirestoreClient(
this._deferredInitialization = this._firestoreClient.start(
databaseInfo,
this._credentials,
this._queue
this._componentProvider,
this._persistenceSettings
);

this._firestoreClientPromise = firestoreClient
.start(this._componentProvider, this._persistenceSettings)
.then(() => firestoreClient);
}

return this._firestoreClientPromise;
return this._deferredInitialization.then(() => this._firestoreClient);
}

// TODO(firestorexp): Factor out MultiTabComponentProvider and remove
Expand All @@ -103,7 +108,7 @@ export class Firestore extends LiteFirestore
persistenceProvider: ComponentProvider,
synchronizeTabs: boolean
): Promise<void> {
if (this._firestoreClientPromise) {
if (this._deferredInitialization) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'Firestore has already been started and persistence can no longer ' +
Expand Down Expand Up @@ -131,7 +136,7 @@ export class Firestore extends LiteFirestore
}

_clearPersistence(): Promise<void> {
if (this._firestoreClientPromise !== undefined && !this._terminated) {
if (this._firestoreClient !== undefined && !this._terminated) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'Persistence can only be cleared before the Firestore instance is ' +
Expand All @@ -156,6 +161,16 @@ export class Firestore extends LiteFirestore
});
return deferred.promise;
}

_terminate(): Promise<void> {
this._terminated = true;
if (this._deferredInitialization) {
return this._deferredInitialization.then(() =>
this._firestoreClient.terminate()
);
}
return Promise.resolve();
}
}

export function initializeFirestore(
Expand Down Expand Up @@ -233,8 +248,5 @@ export function terminate(
): Promise<void> {
_removeServiceInstance(firestore.app, 'firestore/lite');
const firestoreImpl = cast(firestore, Firestore);
firestoreImpl._terminated = true;
return firestoreImpl
._getFirestoreClient()
.then(firestoreClient => firestoreClient.terminate());
return firestoreImpl._terminate();
}
6 changes: 3 additions & 3 deletions packages/firestore/exp/src/api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
} from '../../../lite/src/api/reference';
import { Document } from '../../../src/model/document';
import { DeleteMutation, Precondition } from '../../../src/model/mutation';
import { FieldPath } from '../../../src/api/field_path';
import { FieldPath } from '../../../lite/src/api/field_path';
import {
CompleteFn,
ErrorFn,
Expand Down Expand Up @@ -256,7 +256,7 @@ export function addDoc<T>(
data: T
): Promise<firestore.DocumentReference<T>> {
const collRef = cast<CollectionReference<T>>(reference, CollectionReference);
const firestore = cast(collRef, Firestore);
const firestore = cast(collRef.firestore, Firestore);
const docRef = doc(collRef);

const convertedValue = applyFirestoreDataConverter(collRef._converter, data);
Expand Down Expand Up @@ -396,7 +396,7 @@ export function onSnapshot<T>(
);
} else {
const query = cast<Query<T>>(ref, Query);
const firestore = cast(query, Firestore);
const firestore = cast(query.firestore, Firestore);

const observer: PartialObserver<ViewSnapshot> = {
next: snapshot => {
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/exp/src/api/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class DocumentSnapshot<T = firestore.DocumentData>
this.metadata,
/* converter= */ null
);
return this._converter.fromFirestore(snapshot);
return this._converter.fromFirestore(snapshot, options);
} else {
const userDataWriter = new UserDataWriter(
this._firestoreImpl._databaseId,
Expand Down
75 changes: 75 additions & 0 deletions packages/firestore/exp/src/api/transaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @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 firestore from '../../index';

import { BaseTransaction } from '../../../lite/src/api/transaction';
import { DocumentSnapshot } from './snapshot';
import { TransactionRunner } from '../../../src/core/transaction_runner';
import { AsyncQueue } from '../../../src/util/async_queue';
import { cast } from '../../../lite/src/api/util';
import { Firestore } from './database';
import { Deferred } from '../../../src/util/promise';
import { SnapshotMetadata } from '../../../src/api/database';
import { Transaction as InternalTransaction } from '../../../src/core/transaction';

export class Transaction extends BaseTransaction
implements firestore.Transaction {
constructor(
protected readonly _firestore: Firestore,
_transaction: InternalTransaction
) {
super(_firestore, _transaction);
}

get<T>(
documentRef: firestore.DocumentReference<T>
): Promise<firestore.DocumentSnapshot<T>> {
return this._getHelper<firestore.DocumentSnapshot<T>, T>(
documentRef,
(ref, doc) =>
new DocumentSnapshot<T>(
this._firestore,
ref._key,
doc,
new SnapshotMetadata(
/* hasPendingWrites= */ false,
/* fromCache= */ false
),
ref._converter
)
);
}
}

export function runTransaction<T>(
firestore: firestore.FirebaseFirestore,
updateFunction: (transaction: firestore.Transaction) => Promise<T>
): Promise<T> {
const firestoreClient = cast(firestore, Firestore);
return firestoreClient._getDatastore().then(async datastore => {
const deferred = new Deferred<T>();
new TransactionRunner<T>(
new AsyncQueue(),
datastore,
internalTransaction =>
updateFunction(new Transaction(firestoreClient, internalTransaction)),
deferred
).run();
return deferred.promise;
});
}
18 changes: 18 additions & 0 deletions packages/firestore/exp/test/array_transforms.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @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 '../../test/integration/api/array_transforms.test';
18 changes: 18 additions & 0 deletions packages/firestore/exp/test/batch_writes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @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 '../../test/integration/api/batch_writes.test';
18 changes: 18 additions & 0 deletions packages/firestore/exp/test/cursor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @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 '../../test/integration/api/cursor.test';
18 changes: 18 additions & 0 deletions packages/firestore/exp/test/database.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @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 '../../test/integration/api/database.test';
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import * as dependencies from './dependencies.json';
import * as pkg from '../../package.json';
import { forEach } from '../../../src/util/obj';

describe('Dependencies', () => {
describe.skip('Dependencies', () => {
forEach(dependencies, (api, { dependencies }) => {
it(api, () => {
return extractDependencies(api, resolve('exp', pkg.main)).then(
Expand Down
18 changes: 18 additions & 0 deletions packages/firestore/exp/test/fields.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @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 '../../test/integration/api/fields.test';
18 changes: 18 additions & 0 deletions packages/firestore/exp/test/get_options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @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 '../../test/integration/api/get_options.test';
Loading