Betterment / journaled

A Rails engine to reliably deliver loosely-ordered schematized events to Amazon Kinesis via Delayed::Job
MIT License
13 stars 9 forks source link

Test helper: `expect { }.to journal_event_including(...)` #24

Closed smudge closed 2 years ago

smudge commented 2 years ago

Summary

This introduces a new test helper. It allows you to check for one or more matching event being journaled:

expect { my_code }.to journal_event_including(name: 'foo')
expect { my_code }.to journal_events_including({ name: 'foo', value: 1 }, { name: 'foo', value: 2 })

This will only do exact matches on the specified fields (and will not match one way or the other against unspecified fields). Part of the reason I didn't do exact matching across all fields is that things like ID and git commit are generated on the fly, and I figured that what we really want is a way to concisely specify only the fields under test.

It also supports negative assertions (in two forms):

expect { my_code }.not_to journal_event
expect { my_code }.to not_journal_event # supports chaining with `.and`

And it supports several chainable modifiers:

expect { my_code }.to journal_event_including(name: 'foo')
  .with_schema_name('my_event_schema')
  .with_partition_key(user.id)
  .with_stream_name('my_stream_name')
  .with_enqueue_opts(run_at: future_time)
  .with_priority(999)

All of this can be chained together to test for multiple sets of events with multiple sets of options:

expect { subject.journal! }
  .to journal_events_including({ name: 'event1', value: 300 }, { name: 'event2', value: 200 })
    .with_schema_name('set_1_schema')
    .with_partition_key('set_1_partition_key')
    .with_stream_name('set_1_stream_name')
    .with_priority(10)
  .and journal_event_including(name: 'event3', value: 100)
    .with_schema_name('set_2_schema')
    .with_partition_key('set_2_partition_key')
    .with_stream_name('set_2_stream_name')
    .with_priority(20)
  .and not_journal_event_including(name: 'other_event')

Other Information

As a bonus, this emits a new ActiveSupport::Notification that could be consumed to, say, emit a StatsD event, etc. I added a callout in the README.

/domain @Betterment/journaled-owners /platform @jmileham @coreyja @ceslami @danf1024

nanda-prbot commented 2 years ago

Needs somebody from @Betterment/journaled-owners to claim domain review Needs somebody from @jmileham to claim platform review

Use the shovel operator to claim, e.g.:

@myname << domain && platform

HOW TO: Claim a Review

(Ignored request for platform review from coreyja, ceslami, danf1024. See here for our list of platform experts.)

jmileham commented 2 years ago

is it worth considering making the partial-match quality clearer by naming the matcher journal_event_matching or something? Also, should we make it clear in the readme whether the singular (or plural) forms will fail to match if additional journal entries are logged? Should there be an option to make it work the other way if requested?

nanda-prbot commented 2 years ago

Needs somebody from @Betterment/journaled-owners to claim domain review Needs somebody from @jmileham to claim platform review

Use the shovel operator to claim, e.g.:

@myname << domain && platform

HOW TO: Claim a Review

(Ignored request for platform review from coreyja, ceslami, danf1024. See here for our list of platform experts.)

nanda-prbot commented 2 years ago

Needs somebody from @Betterment/journaled-owners to claim domain review Needs somebody from @jmileham to claim platform review

Use the shovel operator to claim, e.g.:

@myname << domain && platform

HOW TO: Claim a Review

(Ignored request for platform review from coreyja, ceslami, danf1024. See here for our list of platform experts.)

nanda-prbot commented 2 years ago

Needs somebody from @Betterment/journaled-owners to claim domain review Needs somebody from @jmileham to claim platform review

Use the shovel operator to claim, e.g.:

@myname << domain && platform

HOW TO: Claim a Review

(Ignored request for platform review from coreyja, ceslami, danf1024. See here for our list of platform experts.)

nanda-prbot commented 2 years ago

This PR requires additional review because of new changes

Please get another domain review from @jmileham, or another reviewer with write access if unavailable.

nanda-prbot commented 2 years ago

This PR requires additional review because of new changes

Please get another domain review from @jmileham, or another reviewer with write access if unavailable.

smudge commented 2 years ago

Sorry for the churn! I've been throwing the matcher at a very large test suite, and I think I've covered all the corner cases now.

matsuimp commented 2 years ago

domainlgtm!