Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minitest assertions for RES #659

Open
mostlyobvious opened this issue Jul 25, 2019 · 11 comments
Open

Minitest assertions for RES #659

mostlyobvious opened this issue Jul 25, 2019 · 11 comments
Assignees

Comments

@mostlyobvious
Copy link
Member

Gathering ideas and various implementations of assertions which help when working with RailsEventStore 馃憞

@mostlyobvious
Copy link
Member Author

@mostlyobvious
Copy link
Member Author

mostlyobvious commented Jul 25, 2019

before-after-reject can probably be implemented with https://railseventstore.org/docs/subscribe/#temporary-subscriptions (and subscribe_to_all_events), also being a thread safe version by default

@mostlyobvious
Copy link
Member Author

Using a bit older API:

    def assert_exact_new_events(event_store, new_events)
      events_so_far = event_store.read_all_streams_forward
      yield
      assert_equal(
        new_events,
        (event_store.read_all_streams_forward - events_so_far).map(&:class)
      )
    end

    def assert_new_events_include(event_store, expected_events)
      events_so_far = event_store.read_all_streams_forward
      yield
      new_events = (event_store.read_all_streams_forward - events_so_far).map(&:class)
      assert(
        (expected_events - new_events).empty?,
        "didn't include all of: #{expected_events} in #{(event_store.read_all_streams_forward - events_so_far).map(&:class)}"
      )
    end

@mostlyobvious
Copy link
Member Author

def assert_items(actual_elements, expected_procs)
  assert_equal(
    expected_procs.length,
    actual_elements.length,
    "number of actual elements does not match expected"
  )
  actual_elements.zip(expected_procs) do |actual, expected|
    expected.call(actual)
  end
end

@mostlyobvious
Copy link
Member Author

def assert_published(event_kind)
  event = event_store.read.of_type(event_kind).last
  assert_not_nil event
  yield event
end

def assert_not_published(event_kind)
  assert_nil event_store.read.of_type(event_kind).last
end

@mostlyobvious
Copy link
Member Author

#659 (comment)

^^ that would be more of assert_dispatched, there no information about stream when dispatching to handlers

@mostlyobvious mostlyobvious mentioned this issue Jul 26, 2019
5 tasks
@tomaszwro
Copy link
Contributor

tomaszwro commented Jul 26, 2019

Let me throw following idea:

assert_has_items(events_of_type(OrderPlaced), [
  -> event {
    assert_equal 1, event.data[:quantity]
  },
])

where

  • assert_has_items is a generic helper for checking if a collection has expected number of items (actually exactly as in above comment Minitest assertions for RES聽#659 (comment)); if the number is ok, it passes items to respective lambdas, where respective items (events in this case) can have any further assertions
  • events_of_type just gets you all events of certain type from the event store

@tomaszwro
Copy link
Contributor

tomaszwro commented Jul 26, 2019

Additionally above idea can be extended to operate within blocks

assert_events_during do
  perform_a_command
end.of_type(OrderPlaced).has_items(
  -> event {
    assert_equal 1, event.data[:quantity]
  }
)

@mostlyobvious
Copy link
Member Author

mostlyobvious commented Jul 29, 2019

A few more samples from the wild:

  def assert_equal_event(expected, checked, verify_id: false)
    if verify_id
      assert_equal expected.event_id, checked.event_id
    end
    assert_equal expected.class, checked.class
    assert_equal expected.data, checked.data
  end

  def assert_equal_events(expected, checked, verify_id: false)
    assert_equal expected.size, checked.size
    expected.zip(checked).each do |e, c|
      assert_equal_event(e, c, verify_id: verify_id)
    end
  end

  def assert_equal_unpublished_events(expected, model)
    assert_equal_events(expected, model.unpublished_events)
  end

@mostlyobvious
Copy link
Member Author

In the meantime I've rebased and merged PR with initial gem. See it at:
https://github.com/RailsEventStore/rails_event_store/tree/master/contrib/minitest-ruby_event_store

@mostlyobvious
Copy link
Member Author

Related: palkan/active_event_store#2 (comment)

@lukaszreszke lukaszreszke self-assigned this Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

4 participants