-
Notifications
You must be signed in to change notification settings - Fork 59
Add testing support for 2nd gen firestore triggers #200
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c5a9aca
firestore onDocumentCreated trigger
blidd-google 1d9e8b4
on doc updated, written, deleted & unit tests
blidd-google e2ecfa1
on doc written unit tests
blidd-google 23c5662
cleanup
blidd-google b17dbdd
don't overwrite user's mocked before/after data if they didn't mock t…
blidd-google 2154638
add comments and changelog
blidd-google d9b8115
update functions sdk dep
blidd-google 9347c17
fix formatting
blidd-google File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ | |
"@types/mocha": "^5.2.7", | ||
"chai": "^4.2.0", | ||
"firebase-admin": "^10.1.0", | ||
"firebase-functions": "^4.0.0-rc.0", | ||
"firebase-functions": "file:../firebase-functions", | ||
"firebase-tools": "^8.9.2", | ||
"mocha": "^6.2.2", | ||
"prettier": "^1.19.1", | ||
|
@@ -57,7 +57,7 @@ | |
}, | ||
"peerDependencies": { | ||
"firebase-admin": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0", | ||
"firebase-functions": "^4.0.0", | ||
"firebase-functions": "file:../firebase-functions", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here - |
||
"jest": ">=28.0.0" | ||
}, | ||
"engines": { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,9 +34,12 @@ import { | |
testLab, | ||
eventarc, | ||
https, | ||
firestore, | ||
} from 'firebase-functions/v2'; | ||
import { defineString } from 'firebase-functions/params'; | ||
import { makeDataSnapshot } from '../src/providers/database'; | ||
import { makeDocumentSnapshot } from '../src/providers/firestore'; | ||
import {inspect } from 'util'; | ||
|
||
describe('v2', () => { | ||
describe('#wrapV2', () => { | ||
|
@@ -460,6 +463,265 @@ describe('v2', () => { | |
}); | ||
}); | ||
|
||
describe('firestore', () => { | ||
describe('should resolve document path', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove the "should resolve" ( |
||
it('should resolve default document path', () => { | ||
const cloudFn = firestore.onDocumentCreated('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const cloudEvent = cloudFnWrap().cloudEvent; | ||
expect(cloudEvent.document).equal('foo/bar/baz'); | ||
TheIronDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
it('should resolve default document given StringParam', () => { | ||
process.env.doc_path = 'foo/StringParam/baz'; | ||
const cloudFn = firestore.onDocumentCreated('', handler); | ||
cloudFn.__endpoint.eventTrigger.eventFilterPathPatterns.document = defineString( | ||
'doc_path' | ||
); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const cloudEvent = cloudFnWrap().cloudEvent; | ||
expect(cloudEvent.document).equal('foo/StringParam/baz'); | ||
}); | ||
|
||
it('should resolve using params', () => { | ||
const cloudFn = firestore.onDocumentCreated('users/{user}', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const partial = { | ||
params: { | ||
user: '123', | ||
} | ||
}; | ||
const cloudEvent = cloudFnWrap(partial).cloudEvent; | ||
expect(cloudEvent.document).equal('users/123'); | ||
}); | ||
|
||
it('should resolve with undefined string if variable is missing', () => { | ||
const cloudFn = firestore.onDocumentCreated('users/{user}', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const partial = { | ||
params: {}, | ||
}; | ||
const cloudEvent = cloudFnWrap(partial).cloudEvent; | ||
expect(cloudEvent.document).equal('users/undefined'); | ||
}); | ||
}); | ||
|
||
describe('document options', () => { | ||
it('resolves default document options correctly', () => { | ||
const cloudFn = firestore.onDocumentCreated('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const cloudEvent = cloudFnWrap().cloudEvent; | ||
expect(cloudEvent.document).equal('foo/bar/baz'); | ||
expect(cloudEvent.database).equal('(default)'); | ||
expect(cloudEvent.namespace).equal('(default)'); | ||
}); | ||
|
||
it('reads custom DocumentOptions correctly', () => { | ||
const documentOptions = { | ||
document: 'foo/bar/baz', | ||
database: 'custom-database', | ||
namespace: 'custom-namespace' | ||
}; | ||
const cloudFn = firestore.onDocumentCreated(documentOptions, handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const cloudEvent = cloudFnWrap().cloudEvent; | ||
expect(cloudEvent.document).equal(documentOptions.document); | ||
expect(cloudEvent.database).equal(documentOptions.database); | ||
expect(cloudEvent.namespace).equal(documentOptions.namespace); | ||
}); | ||
}); | ||
|
||
describe('firestore.onDocumentCreated', () => { | ||
it('should update CloudEvent appropriately', () => { | ||
const cloudFn = firestore.onDocumentCreated('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const cloudEvent = cloudFnWrap().cloudEvent; | ||
|
||
expect(cloudEvent).deep.equal({ | ||
id: cloudEvent.id, | ||
time: cloudEvent.time, | ||
specversion: '1.0', | ||
type: 'google.cloud.firestore.document.v1.created', | ||
source: '', | ||
|
||
data: cloudEvent.data, | ||
location: 'us-central1', | ||
project: 'testproject', | ||
database: '(default)', | ||
namespace: '(default)', | ||
document: 'foo/bar/baz', | ||
params: {}, | ||
}); | ||
}); | ||
|
||
it('should use overridden data', () => { | ||
const cloudFn = firestore.onDocumentCreated('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const docData = { foo: 'bar' }; | ||
const data = makeDocumentSnapshot(docData, 'foo/bar/baz'); | ||
const cloudEvent = cloudFnWrap({ data }).cloudEvent; | ||
expect(cloudEvent.data.data()).deep.equal(docData); | ||
}); | ||
|
||
it('should accept json data', () => { | ||
const cloudFn = firestore.onDocumentCreated('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const docData = { foo: 'bar' }; | ||
const cloudEvent = cloudFnWrap({ data: docData }).cloudEvent; | ||
expect(cloudEvent.data.data()).deep.equal(docData); | ||
}); | ||
}); | ||
|
||
describe('firestore.onDocumentDeleted()', () => { | ||
it('should update CloudEvent appropriately', () => { | ||
const cloudFn = firestore.onDocumentDeleted('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const cloudEvent = cloudFnWrap().cloudEvent; | ||
|
||
expect(cloudEvent).deep.equal({ | ||
id: cloudEvent.id, | ||
time: cloudEvent.time, | ||
specversion: '1.0', | ||
type: 'google.cloud.firestore.document.v1.deleted', | ||
source: '', | ||
|
||
data: cloudEvent.data, | ||
location: 'us-central1', | ||
project: 'testproject', | ||
database: '(default)', | ||
namespace: '(default)', | ||
document: 'foo/bar/baz', | ||
params: {}, | ||
}); | ||
}); | ||
|
||
it('should use overridden data', () => { | ||
const cloudFn = firestore.onDocumentDeleted('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const docData = { foo: 'bar' }; | ||
const data = makeDocumentSnapshot(docData, 'foo/bar/baz'); | ||
const cloudEvent = cloudFnWrap({ data }).cloudEvent; | ||
expect(cloudEvent.data.data()).deep.equal(docData); | ||
}); | ||
|
||
it('should accept json data', () => { | ||
const cloudFn = firestore.onDocumentDeleted('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const docData = { foo: 'bar' }; | ||
const cloudEvent = cloudFnWrap({ data: docData }).cloudEvent; | ||
expect(cloudEvent.data.data()).deep.equal(docData); | ||
}); | ||
}); | ||
|
||
describe('firestore.onDocumentUpdated', () => { | ||
it('should update CloudEvent appropriately', () => { | ||
const cloudFn = firestore.onDocumentUpdated('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const cloudEvent = cloudFnWrap().cloudEvent; | ||
|
||
expect(cloudEvent).deep.equal({ | ||
id: cloudEvent.id, | ||
time: cloudEvent.time, | ||
specversion: '1.0', | ||
type: 'google.cloud.firestore.document.v1.updated', | ||
source: '', | ||
|
||
data: cloudEvent.data, | ||
location: 'us-central1', | ||
project: 'testproject', | ||
database: '(default)', | ||
namespace: '(default)', | ||
document: 'foo/bar/baz', | ||
params: {}, | ||
}); | ||
}); | ||
|
||
it('should use overridden data', () => { | ||
const cloudFn = firestore.onDocumentUpdated('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
|
||
const afterDataVal = { snapshot: 'after' }; | ||
const after = makeDocumentSnapshot(afterDataVal, 'foo/bar/baz'); | ||
|
||
const beforeDataVal = { snapshot: 'before' }; | ||
const before = makeDocumentSnapshot(beforeDataVal, 'foo/bar/baz'); | ||
|
||
const data = { before, after }; | ||
const cloudEvent = cloudFnWrap({ data }).cloudEvent; | ||
|
||
expect(cloudEvent.data.before.data()).deep.equal(beforeDataVal); | ||
expect(cloudEvent.data.after.data()).deep.equal(afterDataVal); | ||
}); | ||
|
||
it('should accept json data', () => { | ||
const cloudFn = firestore.onDocumentUpdated('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const afterDataVal = { snapshot: 'after' }; | ||
const beforeDataVal = { snapshot: 'before' }; | ||
|
||
const data = { before: beforeDataVal, after: afterDataVal }; | ||
const cloudEvent = cloudFnWrap({ data }).cloudEvent; | ||
|
||
expect(cloudEvent.data.before.data()).deep.equal(beforeDataVal); | ||
expect(cloudEvent.data.after.data()).deep.equal(afterDataVal); | ||
}); | ||
}); | ||
|
||
describe('firestore.onDocumentWritten', () => { | ||
it('should update CloudEvent appropriately', () => { | ||
const cloudFn = firestore.onDocumentWritten('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const cloudEvent = cloudFnWrap().cloudEvent; | ||
|
||
expect(cloudEvent).deep.equal({ | ||
id: cloudEvent.id, | ||
time: cloudEvent.time, | ||
specversion: '1.0', | ||
type: 'google.cloud.firestore.document.v1.written', | ||
source: '', | ||
|
||
data: cloudEvent.data, | ||
location: 'us-central1', | ||
project: 'testproject', | ||
database: '(default)', | ||
namespace: '(default)', | ||
document: 'foo/bar/baz', | ||
params: {}, | ||
}); | ||
}); | ||
|
||
it('should use overridden data', () => { | ||
const cloudFn = firestore.onDocumentWritten('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
|
||
const afterDataVal = { snapshot: 'after' }; | ||
const after = makeDocumentSnapshot(afterDataVal, 'foo/bar/baz'); | ||
|
||
const beforeDataVal = { snapshot: 'before' }; | ||
const before = makeDocumentSnapshot(beforeDataVal, 'foo/bar/baz'); | ||
|
||
const data = { before, after }; | ||
const cloudEvent = cloudFnWrap({ data }).cloudEvent; | ||
|
||
expect(cloudEvent.data.before.data()).deep.equal(beforeDataVal); | ||
expect(cloudEvent.data.after.data()).deep.equal(afterDataVal); | ||
}); | ||
|
||
it('should accept json data', () => { | ||
const cloudFn = firestore.onDocumentWritten('foo/bar/baz', handler); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const afterDataVal = { snapshot: 'after' }; | ||
const beforeDataVal = { snapshot: 'before' }; | ||
|
||
const data = { before: beforeDataVal, after: afterDataVal }; | ||
const cloudEvent = cloudFnWrap({ data }).cloudEvent; | ||
|
||
expect(cloudEvent.data.before.data()).deep.equal(beforeDataVal); | ||
expect(cloudEvent.data.after.data()).deep.equal(afterDataVal); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('database', () => { | ||
describe('ref', () => { | ||
it('should resolve default ref', () => { | ||
|
@@ -488,24 +750,6 @@ describe('v2', () => { | |
expect(cloudEvent.ref).equal('foo/StringParam/baz'); | ||
}); | ||
|
||
it.skip('should resolve default ref given TernaryExpression', () => { | ||
TheIronDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const ref1 = defineString('rtdb_ref_1'); | ||
process.env.rtdb_ref_1 = 'foo/StringParam/1'; | ||
const ref2 = defineString('rtdb_ref_2'); | ||
process.env.rtdb_ref_2 = 'foo/StringParam/2'; | ||
const referenceOptions = { | ||
ref: '', | ||
instance: 'instance-1', | ||
}; | ||
const cloudFn = database.onValueCreated(referenceOptions, handler); | ||
cloudFn.__endpoint.eventTrigger.eventFilterPathPatterns.ref = ref1 | ||
.equals('aa') | ||
.then('rtdb_ref_1', 'rtdb_ref_2'); | ||
const cloudFnWrap = wrapV2(cloudFn); | ||
const cloudEvent = cloudFnWrap().cloudEvent; | ||
expect(cloudEvent.ref).equal('rtdb_ref_2'); | ||
}); | ||
|
||
it('should resolve using params', () => { | ||
const referenceOptions = { | ||
ref: 'users/{user}', | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/cloudevent/mocks/firestore/firestore-on-document-created.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { MockCloudEventAbstractFactory } from "../../types"; | ||
import { CloudEvent, CloudFunction, firestore } from "firebase-functions/v2"; | ||
import { getEventType } from "../helpers"; | ||
import { QueryDocumentSnapshot } from "firebase-admin/firestore"; | ||
import { getDocumentSnapshotCloudEvent } from "./helpers"; | ||
|
||
export const firestoreOnDocumentCreated: MockCloudEventAbstractFactory<firestore.FirestoreEvent<QueryDocumentSnapshot>> = { | ||
generateMock: getDocumentSnapshotCloudEvent, | ||
match(cloudFunction: CloudFunction<CloudEvent<unknown>>): boolean { | ||
return ( | ||
getEventType(cloudFunction) === 'google.cloud.firestore.document.v1.created' | ||
); | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
woah that was the fastest PR review of all time!! will do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should be good to grab v 4.3.0 now