[tests] additional tweaks to firestore test helpers
This commit is contained in:
parent
b918eea630
commit
4f5f617de0
@ -7,5 +7,7 @@ before(async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
after(async () => {
|
after(async () => {
|
||||||
|
console.log('Cleaning up...');
|
||||||
|
await TestHelpers.firestore.cleanup();
|
||||||
await detox.cleanup();
|
await detox.cleanup();
|
||||||
});
|
});
|
||||||
|
@ -1,10 +1,27 @@
|
|||||||
|
const TEST_COLLECTION_NAME = 'collection-tests';
|
||||||
|
|
||||||
|
let shouldCleanup = false;
|
||||||
|
const ONE_HOUR = 60 * 60 * 1000;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
async cleanup() {
|
||||||
|
if (!shouldCleanup) return Promise.resolve();
|
||||||
|
await module.exports.cleanCollection(TEST_COLLECTION_NAME);
|
||||||
|
// TODO add any others?
|
||||||
|
return Promise.resolve();
|
||||||
|
},
|
||||||
|
|
||||||
|
TEST_COLLECTION_NAME,
|
||||||
|
|
||||||
DOC_1: { name: 'doc1' },
|
DOC_1: { name: 'doc1' },
|
||||||
|
DOC_1_PATH: `collection-tests/doc1${testRunId}`,
|
||||||
|
|
||||||
DOC_2: { name: 'doc2', title: 'Document 2' },
|
DOC_2: { name: 'doc2', title: 'Document 2' },
|
||||||
|
DOC_2_PATH: `collection-tests/doc2${testRunId}`,
|
||||||
|
|
||||||
// needs to be a fn as firebase may not yet be available
|
// needs to be a fn as firebase may not yet be available
|
||||||
COL_DOC_1() {
|
COL_DOC_1() {
|
||||||
|
shouldCleanup = true;
|
||||||
return {
|
return {
|
||||||
baz: true,
|
baz: true,
|
||||||
daz: 123,
|
daz: 123,
|
||||||
@ -19,24 +36,73 @@ module.exports = {
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
COL_DOC_1_PATH: `collection-tests/col1${testRunId}`,
|
||||||
|
COL_DOC_1_ID: `col1${testRunId}`,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Removes all documents on the collection for the current testId or
|
||||||
|
* documents older than 24 hours
|
||||||
*
|
*
|
||||||
* @param collectionName
|
* @param collectionName
|
||||||
* @return {Promise<*>}
|
* @return {Promise<*>}
|
||||||
*/
|
*/
|
||||||
async cleanCollection(collectionName = 'collection-tests') {
|
async cleanCollection(collectionName) {
|
||||||
const firestore = firebaseAdmin.firestore();
|
const firestore = firebaseAdmin.firestore();
|
||||||
const batch = firestore.batch();
|
const collection = firestore.collection(
|
||||||
const collection = firestore.collection(collectionName);
|
collectionName || TEST_COLLECTION_NAME
|
||||||
const docsToDelete = await collection.get();
|
);
|
||||||
|
|
||||||
for (let i = 0, len = docsToDelete.length; i < len; i++) {
|
const docsToDelete = (await collection.get()).docs;
|
||||||
const { ref, path } = docsToDelete[i];
|
const yesterday = new Date(new Date() - 24 * ONE_HOUR);
|
||||||
if (path.includes(testRunId)) {
|
|
||||||
batch.delete(ref);
|
if (docsToDelete.length) {
|
||||||
|
const batch = firestore.batch();
|
||||||
|
|
||||||
|
for (let i = 0, len = docsToDelete.length; i < len; i++) {
|
||||||
|
const { ref } = docsToDelete[i];
|
||||||
|
|
||||||
|
if (
|
||||||
|
ref.path.includes(testRunId) ||
|
||||||
|
new Date(docsToDelete[i].createTime) <= yesterday
|
||||||
|
) {
|
||||||
|
batch.delete(ref);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return batch.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
return writeBatch.commit();
|
return Promise.resolve();
|
||||||
|
},
|
||||||
|
|
||||||
|
testCollection() {
|
||||||
|
shouldCleanup = true;
|
||||||
|
return firebase.firestore().collection(TEST_COLLECTION_NAME);
|
||||||
|
},
|
||||||
|
|
||||||
|
testCollectionDoc() {
|
||||||
|
shouldCleanup = true;
|
||||||
|
return firebase.firestore().doc(module.exports.COL_DOC_1_PATH);
|
||||||
|
},
|
||||||
|
|
||||||
|
testCollectionDocAdmin() {
|
||||||
|
shouldCleanup = true;
|
||||||
|
return firebaseAdmin.firestore().doc(module.exports.COL_DOC_1_PATH);
|
||||||
|
},
|
||||||
|
|
||||||
|
resetTestCollectionDoc() {
|
||||||
|
shouldCleanup = true;
|
||||||
|
return firebase
|
||||||
|
.firestore()
|
||||||
|
.doc(module.exports.COL_DOC_1_PATH)
|
||||||
|
.set(module.exports.COL_DOC_1());
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// call a get request without waiting to force firestore to connect
|
||||||
|
// so the first test isn't delayed whilst connecting
|
||||||
|
module.exports
|
||||||
|
.testCollectionDocAdmin()
|
||||||
|
.get()
|
||||||
|
.then(() => {})
|
||||||
|
.catch(() => {});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user