Skip to content

Commit 813a174

Browse files
Speed up FieldPath validation tests (#1147)
1 parent 76c2772 commit 813a174

File tree

1 file changed

+64
-52
lines changed

1 file changed

+64
-52
lines changed

packages/firestore/test/integration/api/validation.test.ts

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -560,37 +560,53 @@ apiDescribe('Validation:', persistence => {
560560
);
561561

562562
validationIt(persistence, 'Field paths must not have empty segments', db => {
563-
const badFieldPaths = ['', 'foo..baz', '.foo', 'foo.'];
564-
const promises = [];
565-
for (const fieldPath of badFieldPaths) {
566-
const reason =
567-
`Invalid field path (${fieldPath}). Paths must not be ` +
568-
`empty, begin with '.', end with '.', or contain '..'`;
569-
promises.push(expectFieldPathToFail(db, fieldPath, reason));
570-
}
571-
return Promise.all(promises);
563+
const docRef = db.collection('test').doc();
564+
return docRef
565+
.set({ test: 1 })
566+
.then(() => {
567+
return docRef.get();
568+
})
569+
.then(snapshot => {
570+
const badFieldPaths = ['', 'foo..baz', '.foo', 'foo.'];
571+
const promises = [];
572+
for (const fieldPath of badFieldPaths) {
573+
const reason =
574+
`Invalid field path (${fieldPath}). Paths must not be ` +
575+
`empty, begin with '.', end with '.', or contain '..'`;
576+
promises.push(expectFieldPathToFail(snapshot, fieldPath, reason));
577+
}
578+
return Promise.all(promises);
579+
});
572580
});
573581

574582
validationIt(
575583
persistence,
576584
'Field paths must not have invalid segments',
577585
db => {
578-
const badFieldPaths = [
579-
'foo~bar',
580-
'foo*bar',
581-
'foo/bar',
582-
'foo[1',
583-
'foo]1',
584-
'foo[1]'
585-
];
586-
const promises = [];
587-
for (const fieldPath of badFieldPaths) {
588-
const reason =
589-
`Invalid field path (${fieldPath}). Paths must not ` +
590-
`contain '~', '*', '/', '[', or ']'`;
591-
promises.push(expectFieldPathToFail(db, fieldPath, reason));
592-
}
593-
return Promise.all(promises);
586+
const docRef = db.collection('test').doc();
587+
return docRef
588+
.set({ test: 1 })
589+
.then(() => {
590+
return docRef.get();
591+
})
592+
.then(snapshot => {
593+
const badFieldPaths = [
594+
'foo~bar',
595+
'foo*bar',
596+
'foo/bar',
597+
'foo[1',
598+
'foo]1',
599+
'foo[1]'
600+
];
601+
const promises = [];
602+
for (const fieldPath of badFieldPaths) {
603+
const reason =
604+
`Invalid field path (${fieldPath}). Paths must not ` +
605+
`contain '~', '*', '/', '[', or ']'`;
606+
promises.push(expectFieldPathToFail(snapshot, fieldPath, reason));
607+
}
608+
return Promise.all(promises);
609+
});
594610
}
595611
);
596612

@@ -953,37 +969,33 @@ function expectWriteToFail(
953969
* they fail with the specified reason.
954970
*/
955971
function expectFieldPathToFail(
956-
db: firestore.FirebaseFirestore,
972+
snapshot: firestore.DocumentSnapshot,
957973
path: string,
958974
reason: string
959975
): Promise<void> {
960976
// Get an arbitrary snapshot we can use for testing.
961-
const docRef = db.collection('test').doc();
962-
return docRef
963-
.set({ test: 1 })
964-
.then(() => {
965-
return docRef.get();
966-
})
967-
.then(snapshot => {
968-
// Snapshot paths.
969-
expect(() => snapshot.get(path)).to.throw(
970-
'Function DocumentSnapshot.get() called with invalid data. ' + reason
971-
);
977+
return Promise.resolve().then(() => {
978+
// Snapshot paths.
979+
expect(() => snapshot.get(path)).to.throw(
980+
'Function DocumentSnapshot.get() called with invalid data. ' + reason
981+
);
972982

973-
// Query filter / order fields.
974-
const coll = db.collection('test-collection');
975-
// <=, etc omitted for brevity since the code path is trivially
976-
// shared.
977-
expect(() => coll.where(path, '==', 1)).to.throw(
978-
'Function Query.where() called with invalid data. ' + reason
979-
);
980-
expect(() => coll.orderBy(path)).to.throw(
981-
'Function Query.orderBy() called with invalid data. ' + reason
982-
);
983+
const db = snapshot.ref.firestore;
983984

984-
// Update paths.
985-
const data = {} as { [field: string]: number };
986-
data[path] = 1;
987-
return expectUpdateToFail(db, data, reason);
988-
});
985+
// Query filter / order fields.
986+
const coll = db.collection('test-collection');
987+
// <=, etc omitted for brevity since the code path is trivially
988+
// shared.
989+
expect(() => coll.where(path, '==', 1)).to.throw(
990+
'Function Query.where() called with invalid data. ' + reason
991+
);
992+
expect(() => coll.orderBy(path)).to.throw(
993+
'Function Query.orderBy() called with invalid data. ' + reason
994+
);
995+
996+
// Update paths.
997+
const data = {} as { [field: string]: number };
998+
data[path] = 1;
999+
return expectUpdateToFail(db, data, reason);
1000+
});
9891001
}

0 commit comments

Comments
 (0)