-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff4ebaf
commit 5d89d83
Showing
3 changed files
with
155 additions
and
6 deletions.
There are no files selected for viewing
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
153 changes: 153 additions & 0 deletions
153
src/packages/emmett-mongodb/src/eventStore/mongoDBEventstore.onAfterCommit.unit.spec.ts
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,153 @@ | ||
import { after, before, describe, it } from 'node:test'; | ||
import { v7 as uuid } from 'uuid'; | ||
import { type Event, assertEqual } from '@event-driven-io/emmett'; | ||
import { | ||
getMongoDBEventStore, | ||
type MongoDBReadEvent, | ||
} from './mongoDBEventStore'; | ||
import { | ||
MongoDBContainer, | ||
StartedMongoDBContainer, | ||
} from '@testcontainers/mongodb'; | ||
import { MongoClient } from 'mongodb'; | ||
|
||
type TestEvent = Event<'test', { counter: number }, { some: boolean }>; | ||
|
||
void describe('MongoDBEventStore onAfterCommit', () => { | ||
let mongodb: StartedMongoDBContainer; | ||
let client: MongoClient; | ||
|
||
before(async () => { | ||
mongodb = await new MongoDBContainer().start(); | ||
client = new MongoClient(mongodb.getConnectionString(), { | ||
directConnection: true, | ||
}); | ||
|
||
await client.connect(); | ||
}); | ||
|
||
after(async () => { | ||
try { | ||
await client.close(); | ||
await mongodb.stop(); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}); | ||
|
||
void it('calls onAfterCommit hook after events append', async () => { | ||
// Given | ||
const appendedEvents: MongoDBReadEvent[] = []; | ||
const eventStore = getMongoDBEventStore({ | ||
client, | ||
hooks: { | ||
onAfterCommit: (events) => { | ||
appendedEvents.push(...events); | ||
}, | ||
}, | ||
}); | ||
const streamName = `test:${uuid()}`; | ||
let counter = 0; | ||
const events: TestEvent[] = [ | ||
{ | ||
type: 'test', | ||
data: { counter: ++counter }, | ||
metadata: { some: true }, | ||
}, | ||
{ | ||
type: 'test', | ||
data: { counter: ++counter }, | ||
metadata: { some: false }, | ||
}, | ||
]; | ||
|
||
// When | ||
await eventStore.appendToStream(streamName, events); | ||
|
||
// Then | ||
assertEqual(2, appendedEvents.length); | ||
}); | ||
|
||
void it('calls onAfterCommit hook exactly once for each events append', async () => { | ||
// Given | ||
const appendedEvents: MongoDBReadEvent[] = []; | ||
const eventStore = getMongoDBEventStore({ | ||
client, | ||
hooks: { | ||
onAfterCommit: (events) => { | ||
appendedEvents.push(...events); | ||
}, | ||
}, | ||
}); | ||
const streamName = `test:${uuid()}`; | ||
let counter = 0; | ||
const events: TestEvent[] = [ | ||
{ | ||
type: 'test', | ||
data: { counter: ++counter }, | ||
metadata: { some: true }, | ||
}, | ||
{ | ||
type: 'test', | ||
data: { counter: ++counter }, | ||
metadata: { some: false }, | ||
}, | ||
]; | ||
const nextEvents: TestEvent[] = [ | ||
{ | ||
type: 'test', | ||
data: { counter: ++counter }, | ||
metadata: { some: true }, | ||
}, | ||
{ | ||
type: 'test', | ||
data: { counter: ++counter }, | ||
metadata: { some: false }, | ||
}, | ||
]; | ||
|
||
// When | ||
await eventStore.appendToStream(streamName, events); | ||
await eventStore.appendToStream(streamName, nextEvents); | ||
|
||
// Then | ||
assertEqual(4, appendedEvents.length); | ||
}); | ||
|
||
void it('silently fails when onAfterCommit hook failed but still keeps events', async () => { | ||
// Given | ||
const appendedEvents: MongoDBReadEvent[] = []; | ||
const eventStore = getMongoDBEventStore({ | ||
client, | ||
hooks: { | ||
onAfterCommit: (events) => { | ||
appendedEvents.push(...events); | ||
throw new Error('onAfterCommit failed!'); | ||
}, | ||
}, | ||
}); | ||
|
||
const streamName = `test:${uuid()}`; | ||
let counter = 0; | ||
const events: TestEvent[] = [ | ||
{ | ||
type: 'test', | ||
data: { counter: ++counter }, | ||
metadata: { some: true }, | ||
}, | ||
{ | ||
type: 'test', | ||
data: { counter: ++counter }, | ||
metadata: { some: false }, | ||
}, | ||
]; | ||
|
||
// When | ||
await eventStore.appendToStream(streamName, events); | ||
|
||
// Then | ||
assertEqual(2, appendedEvents.length); | ||
const { events: eventsInStore } = await eventStore.readStream(streamName); | ||
assertEqual(2, eventsInStore.length); | ||
}); | ||
}); |
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