-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example of providing EventStoreDB SubscriptionToAll filter to m…
…atch a specific set of event types
- Loading branch information
1 parent
a65bffe
commit 2fbf052
Showing
3 changed files
with
126 additions
and
6 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
Core.EventStoreDB.Tests/Subscriptions/Filtering/EventTypeFiltersTests.cs
This file contains 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,91 @@ | ||
using Core.Events; | ||
using Core.EventStoreDB.Subscriptions.Checkpoints; | ||
using Core.EventStoreDB.Subscriptions.Filtering; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Core.EventStoreDB.Tests.Subscriptions.Filtering; | ||
using static EventFilters; | ||
|
||
public class EventTypeFiltersTests | ||
{ | ||
[Fact] | ||
public void ExcludeSystemAndCheckpointEventsRegex_Should_NotMatch_SystemEvents() | ||
{ | ||
ExcludeSystemAndCheckpointEventsRegex.IsMatch("$scavengeIndexInitialized").Should().BeFalse(); | ||
ExcludeSystemAndCheckpointEventsRegex.IsMatch(typeof(CheckpointStored).FullName!).Should().BeFalse(); | ||
ExcludeSystemAndCheckpointEventsRegex.IsMatch("Core.EventStoreDB.Subscriptions.Checkpoints.CheckpointStored").Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ExcludeSystemAndCheckpointEventsRegex_Should_Match_OtherEvents() | ||
{ | ||
ExcludeSystemAndCheckpointEventsRegex.IsMatch("ShoppingCartOpened").Should().BeTrue(); | ||
ExcludeSystemAndCheckpointEventsRegex.IsMatch("SomeOtherEvent").Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void OneOfEventTypesRegex_Should_Match_ProvidedEvents() | ||
{ | ||
var regex = OneOfEventTypesRegex("ShoppingCartOpened", "OrderPlaced"); | ||
|
||
regex.IsMatch("ShoppingCartOpened").Should().BeTrue(); | ||
regex.IsMatch("OrderPlaced").Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void OneOfEventTypesRegex_Should_NotMatch_OtherEvents() | ||
{ | ||
var regex = OneOfEventTypesRegex("ShoppingCartOpened", "OrderPlaced"); | ||
|
||
regex.IsMatch("OrderCancelled").Should().BeFalse(); | ||
regex.IsMatch("$systemEvent").Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void OneOfEventTypesRegex_WithEventTypeMapper_Should_Match_ProvidedEventTypesWithCustomMap() | ||
{ | ||
var eventTypeMapper = new EventTypeMapper(); | ||
eventTypeMapper.AddCustomMap<ShoppingCartOpened>("ShoppingCartOpened"); | ||
|
||
var regex = OneOfEventTypesRegex(eventTypeMapper, typeof(ShoppingCartOpened)); | ||
|
||
regex.IsMatch("ShoppingCartOpened").Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void OneOfEventTypesRegex_WithEventTypeMapper_Should_NotMatch_OtherEventTypesWithCustomMap() | ||
{ | ||
var eventTypeMapper = new EventTypeMapper(); | ||
eventTypeMapper.AddCustomMap<ShoppingCartOpened>("ShoppingCartOpened"); | ||
|
||
var regex = OneOfEventTypesRegex(eventTypeMapper, typeof(ShoppingCartOpened)); | ||
|
||
regex.IsMatch("OrderPlaced").Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void OneOfEventTypesRegex_WithEventTypeMapper_Should_Match_ProvidedEventTypes() | ||
{ | ||
var eventTypeMapper = new EventTypeMapper(); | ||
|
||
var regex = OneOfEventTypesRegex(eventTypeMapper, typeof(ShoppingCartOpened)); | ||
|
||
regex.IsMatch(typeof(ShoppingCartOpened).FullName!).Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void OneOfEventTypesRegex_WithEventTypeMapper_Should_NotMatch_OtherEventTypes() | ||
{ | ||
var eventTypeMapper = new EventTypeMapper(); | ||
|
||
var regex = OneOfEventTypesRegex(eventTypeMapper, typeof(ShoppingCartOpened)); | ||
|
||
regex.IsMatch("OrderPlaced").Should().BeFalse(); | ||
} | ||
|
||
private record ShoppingCartOpened( | ||
Guid ShoppingCartId, | ||
Guid ClientId | ||
); | ||
} |
This file contains 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 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,33 @@ | ||
using System.Text.RegularExpressions; | ||
using Core.Events; | ||
using Core.EventStoreDB.Subscriptions.Checkpoints; | ||
using EventStore.Client; | ||
|
||
namespace Core.EventStoreDB.Subscriptions.Filtering; | ||
|
||
public static class EventFilters | ||
{ | ||
public static readonly Regex ExcludeSystemAndCheckpointEventsRegex = | ||
new(@"^(?!\$)(?!" + Regex.Escape(typeof(CheckpointStored).FullName!) + "$).+"); | ||
|
||
public static Regex OneOfEventTypesRegex(params string[] values) => | ||
new("^(" + string.Join("|", values.Select(Regex.Escape)) + ")$"); | ||
|
||
public static Regex OneOfEventTypesRegex(EventTypeMapper eventTypeMapper, params Type[] eventTypes) => | ||
OneOfEventTypesRegex(eventTypes.Select(eventTypeMapper.ToName).ToArray()); | ||
|
||
public static Regex OneOfEventTypesRegex(params Type[] eventTypes) => | ||
OneOfEventTypesRegex(EventTypeMapper.Instance, eventTypes); | ||
|
||
public static readonly IEventFilter ExcludeSystemAndCheckpointEvents = | ||
EventTypeFilter.RegularExpression(ExcludeSystemAndCheckpointEventsRegex); | ||
|
||
public static IEventFilter OneOfEventTypes(params string[] values) => | ||
EventTypeFilter.RegularExpression(OneOfEventTypesRegex(values)); | ||
|
||
public static IEventFilter OneOfEventTypes(EventTypeMapper eventTypeMapper, params Type[] eventTypes) => | ||
EventTypeFilter.RegularExpression(OneOfEventTypesRegex(eventTypeMapper, eventTypes)); | ||
|
||
public static IEventFilter OneOfEventTypes(params Type[] eventTypes) => | ||
EventTypeFilter.RegularExpression(OneOfEventTypesRegex(eventTypes)); | ||
} |