Skip to content

Commit 364aa84

Browse files
committed
add extra test helpers
1 parent aa01d4a commit 364aa84

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

.github/workflows/test-changed-firestore-integration.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@ jobs:
2121
uses: hashicorp/setup-terraform@v2
2222
- name: Terraform Init
2323
run: |
24+
cp config/ci.config.json config/project.json
25+
pwd
26+
ls
2427
cd packages/firestore
2528
terraform init
2629
continue-on-error: true
2730
- name: Terraform Apply
2831
if: github.event_name == 'pull_request'
2932
run: |
30-
cp ../../config/ci.config.json config/project.json
31-
terraform apply -var-file=../../config/project.json -auto-approve
33+
pwd
34+
ls
35+
terraform apply -var-file=config/project.json -auto-approve
3236
cd ../..
3337
continue-on-error: true
3438
- name: Set up Node (16)

packages/firestore/test/integration/util/composite_index_test_helper.ts

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ import {
3939
updateDoc as updateDocument,
4040
UpdateData,
4141
getDocs as getDocuments,
42-
QuerySnapshot
42+
QuerySnapshot,
43+
deleteDoc as deleteDocument,
44+
doc
4345
} from './firebase_export';
4446
import {
4547
batchCommitDocsToCollection,
@@ -86,16 +88,16 @@ export class CompositeIndexTestHelper {
8688
}
8789

8890
// Hash the document key with testId.
89-
toHashedId(docId: string): string {
91+
private toHashedId(docId: string): string {
9092
return docId + '-' + this.testId;
9193
}
9294

93-
toHashedIds(docs: string[]): string[] {
95+
private toHashedIds(docs: string[]): string[] {
9496
return docs.map(docId => this.toHashedId(docId));
9597
}
9698

9799
// Adds test-specific fields to a document, including the testId and expiration date.
98-
addTestSpecificFieldsToDoc(doc: DocumentData): DocumentData {
100+
private addTestSpecificFieldsToDoc(doc: DocumentData): DocumentData {
99101
return {
100102
...doc,
101103
[this.TEST_ID_FIELD]: this.testId,
@@ -107,7 +109,7 @@ export class CompositeIndexTestHelper {
107109
}
108110

109111
// Remove test-specific fields from a document, including the testId and expiration date.
110-
removeTestSpecificFieldsFromDoc(doc: DocumentData): DocumentData {
112+
private removeTestSpecificFieldsFromDoc(doc: DocumentData): DocumentData {
111113
doc._document?.data?.delete(field(this.TTL_FIELD));
112114
doc._document?.data?.delete(field(this.TEST_ID_FIELD));
113115
return doc;
@@ -142,10 +144,7 @@ export class CompositeIndexTestHelper {
142144
}
143145

144146
// Adds a filter on test id for a query.
145-
query<AppModelType, DbModelType extends DocumentData>(
146-
query_: Query<AppModelType, DbModelType>,
147-
...queryConstraints: QueryConstraint[]
148-
): Query<AppModelType, DbModelType> {
147+
query<T>(query_: Query<T>, ...queryConstraints: QueryConstraint[]): Query<T> {
149148
return internalQuery(
150149
query_,
151150
where(this.TEST_ID_FIELD, '==', this.testId),
@@ -154,63 +153,67 @@ export class CompositeIndexTestHelper {
154153
}
155154

156155
// Adds a filter on test id for a composite query.
157-
compositeQuery<AppModelType, DbModelType extends DocumentData>(
158-
query_: Query<AppModelType, DbModelType>,
156+
compositeQuery<T>(
157+
query_: Query<T>,
159158
compositeFilter: QueryCompositeFilterConstraint,
160159
...queryConstraints: QueryNonFilterConstraint[]
161-
): Query<AppModelType, DbModelType> {
160+
): Query<T> {
162161
return internalQuery(
163162
query_,
164163
and(where(this.TEST_ID_FIELD, '==', this.testId), compositeFilter),
165164
...queryConstraints
166165
);
167166
}
167+
// Get document reference from a document key.
168+
getDocRef<T>(
169+
coll: CollectionReference<T>,
170+
docId: string
171+
): DocumentReference<T> {
172+
if (!docId.includes('test-id-')) {
173+
docId = this.toHashedId(docId);
174+
}
175+
return doc(coll, docId);
176+
}
168177

169178
// Adds a document to a Firestore collection with test-specific fields.
170-
addDoc<T, DbModelType extends DocumentData>(
171-
reference: CollectionReference<T, DbModelType>,
179+
addDoc<T>(
180+
reference: CollectionReference<T>,
172181
data: object
173-
): Promise<DocumentReference<T, DbModelType>> {
182+
): Promise<DocumentReference<T>> {
174183
const processedData = this.addTestSpecificFieldsToDoc(
175184
data
176185
) as WithFieldValue<T>;
177186
return addDocument(reference, processedData);
178187
}
179188

180189
// Sets a document in Firestore with test-specific fields.
181-
setDoc<T, DbModelType extends DocumentData>(
182-
reference: DocumentReference<T, DbModelType>,
183-
data: object
184-
): Promise<void> {
190+
setDoc<T>(reference: DocumentReference<T>, data: object): Promise<void> {
185191
const processedData = this.addTestSpecificFieldsToDoc(
186192
data
187193
) as WithFieldValue<T>;
188194
return setDocument(reference, processedData);
189195
}
190196

191-
// This is is the same as making the update on the doc directly with merge=true.
192197
updateDoc<T, DbModelType extends DocumentData>(
193198
reference: DocumentReference<T, DbModelType>,
194199
data: UpdateData<DbModelType>
195200
): Promise<void> {
196-
const processedData = this.addTestSpecificFieldsToDoc(
197-
data
198-
) as UpdateData<DbModelType>;
199-
return updateDocument(reference, processedData);
201+
return updateDocument(reference, data);
202+
}
203+
204+
deleteDoc<T>(reference: DocumentReference<T>): Promise<void> {
205+
return deleteDocument(reference);
200206
}
201207

202-
203-
async getDoc<T, DbModelType extends DocumentData>(
204-
reference: DocumentReference<T, DbModelType>
205-
): Promise<DocumentSnapshot<T, DbModelType>> {
206-
const docSnapshot = await getDocument<T, DbModelType>(reference);
208+
// Retrieves a single document from Firestore with test-specific fields removed.
209+
async getDoc<T>(docRef: DocumentReference<T>): Promise<DocumentSnapshot<T>> {
210+
const docSnapshot = await getDocument(docRef);
207211
this.removeTestSpecificFieldsFromDoc(docSnapshot);
208212
return docSnapshot;
209213
}
210214

211-
async getDocs<T, DbModelType extends DocumentData>(
212-
query_: Query<T, DbModelType>
213-
): Promise<QuerySnapshot<T, DbModelType>> {
215+
// Retrieves multiple documents from Firestore with test-specific fields removed.
216+
async getDocs<T>(query_: Query<T>): Promise<QuerySnapshot<T>> {
214217
const querySnapshot = await getDocuments(this.query(query_));
215218
querySnapshot.forEach(doc => {
216219
this.removeTestSpecificFieldsFromDoc(doc);

0 commit comments

Comments
 (0)