5
5
} from '@message-queue-toolkit/schemas'
6
6
import { PrismaClient } from '@prisma/client'
7
7
import { uuidv7 } from 'uuidv7'
8
- import { afterAll , beforeAll , describe , expect , it } from 'vitest'
8
+ import { afterAll , beforeAll , beforeEach , describe , expect , it } from 'vitest'
9
9
import { z } from 'zod'
10
10
import { OutboxPrismaAdapter } from '../lib/outbox-prisma-adapter'
11
11
@@ -26,8 +26,42 @@ describe('outbox-prisma-adapter', () => {
26
26
let prisma : PrismaClient
27
27
let outboxPrismaAdapter : OutboxPrismaAdapter < SupportedEvents >
28
28
29
+ const ENTRY_1 = {
30
+ id : uuidv7 ( ) ,
31
+ event : events . created ,
32
+ status : 'CREATED' ,
33
+ data : {
34
+ id : uuidv7 ( ) ,
35
+ payload : {
36
+ message : 'TEST EVENT' ,
37
+ } ,
38
+ metadata : { } ,
39
+ timestamp : new Date ( ) . toISOString ( ) ,
40
+ } ,
41
+ retryCount : 0 ,
42
+ created : new Date ( ) ,
43
+ } satisfies OutboxEntry < SupportedEvents [ number ] >
44
+
45
+ const ENTRY_2 = {
46
+ id : uuidv7 ( ) ,
47
+ event : events . created ,
48
+ status : 'CREATED' ,
49
+ data : {
50
+ id : uuidv7 ( ) ,
51
+ payload : {
52
+ message : 'TEST EVENT 2' ,
53
+ } ,
54
+ metadata : { } ,
55
+ timestamp : new Date ( ) . toISOString ( ) ,
56
+ } ,
57
+ retryCount : 0 ,
58
+ created : new Date ( ) ,
59
+ } satisfies OutboxEntry < SupportedEvents [ number ] >
60
+
29
61
beforeAll ( async ( ) => {
30
- prisma = new PrismaClient ( )
62
+ prisma = new PrismaClient ( {
63
+ log : [ 'query' ] ,
64
+ } )
31
65
32
66
outboxPrismaAdapter = new OutboxPrismaAdapter < SupportedEvents > ( prisma , 'OutboxEntry' )
33
67
@@ -45,6 +79,10 @@ describe('outbox-prisma-adapter', () => {
45
79
`
46
80
} )
47
81
82
+ beforeEach ( async ( ) => {
83
+ await prisma . $queryRaw `DELETE FROM prisma.outbox_entry;`
84
+ } )
85
+
48
86
afterAll ( async ( ) => {
49
87
await prisma . $queryRaw `DROP TABLE prisma.outbox_entry;`
50
88
await prisma . $queryRaw `DROP SCHEMA prisma;`
@@ -92,119 +130,78 @@ describe('outbox-prisma-adapter', () => {
92
130
93
131
it ( 'should insert successful entries from accumulator' , async ( ) => {
94
132
const accumulator = new InMemoryOutboxAccumulator < SupportedEvents > ( )
95
-
96
- const entry1 = {
97
- id : uuidv7 ( ) ,
98
- event : events . created ,
99
- status : 'CREATED' ,
100
- data : {
101
- id : uuidv7 ( ) ,
102
- payload : {
103
- message : 'TEST EVENT' ,
104
- } ,
105
- metadata : { } ,
106
- timestamp : new Date ( ) . toISOString ( ) ,
107
- } ,
108
- retryCount : 0 ,
109
- created : new Date ( ) ,
110
- } satisfies OutboxEntry < SupportedEvents [ number ] >
111
- accumulator . add ( entry1 )
112
-
113
- const entry2 = {
114
- id : uuidv7 ( ) ,
115
- event : events . created ,
116
- status : 'CREATED' ,
117
- data : {
118
- id : uuidv7 ( ) ,
119
- payload : {
120
- message : 'TEST EVENT 2' ,
121
- } ,
122
- metadata : { } ,
123
- timestamp : new Date ( ) . toISOString ( ) ,
124
- } ,
125
- retryCount : 0 ,
126
- created : new Date ( ) ,
127
- } satisfies OutboxEntry < SupportedEvents [ number ] >
128
- accumulator . add ( entry2 )
133
+ accumulator . add ( ENTRY_1 )
134
+ accumulator . add ( ENTRY_2 )
129
135
130
136
await outboxPrismaAdapter . flush ( accumulator )
131
137
132
138
const entriesAfterFlush = await outboxPrismaAdapter . getEntries ( 10 )
133
139
134
140
expect ( entriesAfterFlush ) . toMatchObject ( [
135
141
{
136
- id : entry1 . id ,
142
+ id : ENTRY_1 . id ,
137
143
status : 'SUCCESS' ,
138
144
} ,
139
145
{
140
- id : entry2 . id ,
146
+ id : ENTRY_2 . id ,
141
147
status : 'SUCCESS' ,
142
148
} ,
143
149
] )
144
150
} )
145
151
146
- it ( "should update successful entries' status to 'SUCCESS'" , async ( ) => {
152
+ it ( "should update existing entries' status to 'SUCCESS'" , async ( ) => {
147
153
const accumulator = new InMemoryOutboxAccumulator < SupportedEvents > ( )
154
+ accumulator . add ( ENTRY_1 )
155
+ accumulator . add ( ENTRY_2 )
148
156
149
- const entry1 = {
150
- id : uuidv7 ( ) ,
151
- event : events . created ,
152
- status : 'CREATED' ,
153
- data : {
154
- id : uuidv7 ( ) ,
155
- payload : {
156
- message : 'TEST EVENT' ,
157
- } ,
158
- metadata : { } ,
159
- timestamp : new Date ( ) . toISOString ( ) ,
160
- } ,
161
- retryCount : 0 ,
162
- created : new Date ( ) ,
163
- } satisfies OutboxEntry < SupportedEvents [ number ] >
164
- accumulator . add ( entry1 )
165
-
166
- const entry2 = {
167
- id : uuidv7 ( ) ,
168
- event : events . created ,
169
- status : 'CREATED' ,
170
- data : {
171
- id : uuidv7 ( ) ,
172
- payload : {
173
- message : 'TEST EVENT 2' ,
174
- } ,
175
- metadata : { } ,
176
- timestamp : new Date ( ) . toISOString ( ) ,
177
- } ,
178
- retryCount : 0 ,
179
- created : new Date ( ) ,
180
- } satisfies OutboxEntry < SupportedEvents [ number ] >
181
- accumulator . add ( entry2 )
182
-
183
- await outboxPrismaAdapter . createEntry ( entry1 )
184
- await outboxPrismaAdapter . createEntry ( entry2 )
157
+ await outboxPrismaAdapter . createEntry ( ENTRY_1 )
158
+ await outboxPrismaAdapter . createEntry ( ENTRY_2 )
185
159
186
160
const beforeFlush = await outboxPrismaAdapter . getEntries ( 10 )
187
161
expect ( beforeFlush ) . toMatchObject ( [
188
162
{
189
- id : entry1 . id ,
163
+ id : ENTRY_1 . id ,
190
164
status : 'CREATED' ,
191
165
} ,
192
166
{
193
- id : entry2 . id ,
167
+ id : ENTRY_2 . id ,
194
168
status : 'CREATED' ,
195
169
} ,
196
170
] )
197
171
198
- outboxPrismaAdapter . flush ( accumulator )
172
+ await outboxPrismaAdapter . flush ( accumulator )
199
173
200
174
const afterFlush = await outboxPrismaAdapter . getEntries ( 10 )
201
175
expect ( afterFlush ) . toMatchObject ( [
202
176
{
203
- id : entry1 . id ,
177
+ id : ENTRY_1 . id ,
178
+ status : 'SUCCESS' ,
179
+ } ,
180
+ {
181
+ id : ENTRY_2 . id ,
182
+ status : 'SUCCESS' ,
183
+ } ,
184
+ ] )
185
+ } )
186
+
187
+ it ( 'should handle mix of entries, non existing and existing, and change their status to SUCCESS' , async ( ) => {
188
+ const accumulator = new InMemoryOutboxAccumulator < SupportedEvents > ( )
189
+ accumulator . add ( ENTRY_1 )
190
+ accumulator . add ( ENTRY_2 )
191
+
192
+ //Only one exists in DB.
193
+ await outboxPrismaAdapter . createEntry ( ENTRY_2 )
194
+
195
+ await outboxPrismaAdapter . flush ( accumulator )
196
+
197
+ const afterFirstFlush = await outboxPrismaAdapter . getEntries ( 10 )
198
+ expect ( afterFirstFlush ) . toMatchObject ( [
199
+ {
200
+ id : ENTRY_1 . id ,
204
201
status : 'SUCCESS' ,
205
202
} ,
206
203
{
207
- id : entry2 . id ,
204
+ id : ENTRY_2 . id ,
208
205
status : 'SUCCESS' ,
209
206
} ,
210
207
] )
0 commit comments