firebase / firebase-functions-test

MIT License
232 stars 48 forks source link

Refactor MockCloudEvent generation to include partial #146

Closed TheIronDev closed 2 years ago

TheIronDev commented 2 years ago

Description

The goal of this commit is to incorporate the user's partial into the generated CloudEvent.

All the AbstractFactories for Partial Cloud Events now return Cloud Events, and they all incorporate the user provided partial when creating the Mock Cloud Event..

Theres a few key benefits to this:

  1. Type safety. The Partial Factories were not providing all the necessary fields required (as is the nature of the Partial)
  2. Consistency with other fields. Eg: If a user updates the bucket for storage event, lets try to update the other fields too.
  3. Opaque fields - If we want the user provided partial to take precedence, we can easily control that. (see PubSub and EventArc factories)
  4. Easier to handle one-off behavior. Not all the events behave the same way, particularly with PubSub.

Although this is a "larger" change, the end-user contract remains intact.

This does fix several bugs. Output is different for PubSub, EventArc, and Storage.

TheIronDev commented 2 years ago

Generated CloudEvents will use user-provided Partial to infer other fields.

If you called the wrap handler with {bucket: 'bucket2'}...

Previously the returned CloudEvent would look like:

{
  bucket: 'bucket2',
  data: {
    bucket: 'bucket', // 'bucket' because the user never explicitly overwrote it
    // ...
  },
  // ...
}

With this PR, it will look like:

{
  bucket: 'bucket2',
  data: {
    bucket: 'bucket2', // inferred from the bucket
    // ...
  },
  // ...
}