@@ -6,133 +6,167 @@ import {
6
6
ExpectedVersionConflictError ,
7
7
} from '@event-driven-io/emmett' ;
8
8
import fs from 'fs' ;
9
- import { afterEach , describe , it } from 'node:test' ;
10
- import { dirname } from 'path' ;
9
+ import { afterEach , beforeEach , describe , it } from 'node:test' ;
10
+ import path from 'path' ;
11
11
import { fileURLToPath } from 'url' ;
12
12
import { v4 as uuid } from 'uuid' ;
13
- import { sqliteConnection , type AbsolutePath } from '../sqliteConnection' ;
13
+ import { InMemorySQLiteDatabase , sqliteConnection } from '../sqliteConnection' ;
14
14
import {
15
15
type DiscountApplied ,
16
16
type PricedProductItem ,
17
17
type ProductItemAdded ,
18
18
type ShoppingCartEvent ,
19
19
} from '../testing/shoppingCart.domain' ;
20
20
import { createEventStoreSchema } from './schema' ;
21
- import { getSQLiteEventStore } from './SQLiteEventStore' ;
22
-
23
- const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) as AbsolutePath ;
21
+ import {
22
+ getSQLiteEventStore ,
23
+ type SQLiteEventStoreOptions ,
24
+ } from './SQLiteEventStore' ;
24
25
25
26
void describe ( 'SQLiteEventStore' , ( ) => {
26
- const testDatabasePath : AbsolutePath = __dirname + '/../testing/database/' ;
27
+ const testDatabasePath = path . resolve (
28
+ path . dirname ( fileURLToPath ( import . meta. url ) ) ,
29
+ '..' ,
30
+ 'testing' ,
31
+ ) ;
32
+ const fileName = path . resolve ( testDatabasePath , 'test.db' ) ;
27
33
28
34
afterEach ( ( ) => {
29
- if ( ! fs . existsSync ( ` ${ testDatabasePath } /test.db` ) ) {
35
+ if ( ! fs . existsSync ( fileName ) ) {
30
36
return ;
31
37
}
32
- fs . unlink ( `${ testDatabasePath } /test.db` , ( err ) => {
33
- if ( err ) console . error ( 'Error deleting file:' , err ) ;
34
- } ) ;
38
+ fs . unlinkSync ( fileName ) ;
35
39
} ) ;
36
40
37
- void it ( 'should append events' , async ( ) => {
38
- await createEventStoreSchema (
39
- sqliteConnection ( { location : `/${ testDatabasePath } /test.db` } ) ,
40
- ) ;
41
- const eventStore = getSQLiteEventStore ( {
42
- databaseLocation : `${ testDatabasePath } /test.db` ,
43
- } ) ;
44
-
45
- const productItem : PricedProductItem = {
46
- productId : '123' ,
47
- quantity : 10 ,
48
- price : 3 ,
41
+ void describe ( 'With manual Schema Creation' , ( ) => {
42
+ const config : SQLiteEventStoreOptions = {
43
+ schema : {
44
+ autoMigration : 'None' ,
45
+ } ,
46
+ fileName,
49
47
} ;
50
- const discount = 10 ;
51
- const shoppingCartId = `shopping_cart-${ uuid ( ) } ` ;
52
48
53
- const result = await eventStore . appendToStream < ShoppingCartEvent > (
54
- shoppingCartId ,
55
- [ { type : 'ProductItemAdded' , data : { productItem } } ] ,
56
- ) ;
57
-
58
- const result2 = await eventStore . appendToStream < ShoppingCartEvent > (
59
- shoppingCartId ,
60
- [ { type : 'ProductItemAdded' , data : { productItem } } ] ,
61
- { expectedStreamVersion : result . nextExpectedStreamVersion } ,
62
- ) ;
63
-
64
- await eventStore . appendToStream < ShoppingCartEvent > (
65
- shoppingCartId ,
66
- [
67
- {
68
- type : 'DiscountApplied' ,
69
- data : { percent : discount , couponId : uuid ( ) } ,
70
- } ,
71
- ] ,
72
- { expectedStreamVersion : result2 . nextExpectedStreamVersion } ,
73
- ) ;
49
+ beforeEach ( ( ) => createEventStoreSchema ( sqliteConnection ( { fileName } ) ) ) ;
74
50
75
- const { events } = await eventStore . readStream ( shoppingCartId ) ;
51
+ void it ( 'should append events' , async ( ) => {
52
+ const eventStore = getSQLiteEventStore ( config ) ;
76
53
77
- assertIsNotNull ( events ) ;
78
- assertEqual ( 3 , events . length ) ;
79
- } ) ;
54
+ const productItem : PricedProductItem = {
55
+ productId : '123' ,
56
+ quantity : 10 ,
57
+ price : 3 ,
58
+ } ;
59
+ const discount = 10 ;
60
+ const shoppingCartId = `shopping_cart-${ uuid ( ) } ` ;
80
61
81
- void it ( 'should aggregate stream' , async ( ) => {
82
- await createEventStoreSchema (
83
- sqliteConnection ( { location : `${ testDatabasePath } /test.db` } ) ,
84
- ) ;
85
- const eventStore = getSQLiteEventStore ( {
86
- databaseLocation : `${ testDatabasePath } /test.db` ,
62
+ const result = await eventStore . appendToStream < ShoppingCartEvent > (
63
+ shoppingCartId ,
64
+ [ { type : 'ProductItemAdded' , data : { productItem } } ] ,
65
+ ) ;
66
+
67
+ const result2 = await eventStore . appendToStream < ShoppingCartEvent > (
68
+ shoppingCartId ,
69
+ [ { type : 'ProductItemAdded' , data : { productItem } } ] ,
70
+ { expectedStreamVersion : result . nextExpectedStreamVersion } ,
71
+ ) ;
72
+
73
+ await eventStore . appendToStream < ShoppingCartEvent > (
74
+ shoppingCartId ,
75
+ [
76
+ {
77
+ type : 'DiscountApplied' ,
78
+ data : { percent : discount , couponId : uuid ( ) } ,
79
+ } ,
80
+ ] ,
81
+ { expectedStreamVersion : result2 . nextExpectedStreamVersion } ,
82
+ ) ;
83
+
84
+ const { events } = await eventStore . readStream ( shoppingCartId ) ;
85
+
86
+ assertIsNotNull ( events ) ;
87
+ assertEqual ( 3 , events . length ) ;
87
88
} ) ;
88
89
89
- const productItem : PricedProductItem = {
90
- productId : '123' ,
91
- quantity : 10 ,
92
- price : 3 ,
93
- } ;
94
- const discount = 10 ;
95
- const shoppingCartId = `shopping_cart-${ uuid ( ) } ` ;
90
+ void it ( 'should aggregate stream' , async ( ) => {
91
+ const eventStore = getSQLiteEventStore ( config ) ;
96
92
97
- const result = await eventStore . appendToStream < ShoppingCartEvent > (
98
- shoppingCartId ,
99
- [ { type : 'ProductItemAdded' , data : { productItem } } ] ,
100
- ) ;
101
-
102
- const result2 = await eventStore . appendToStream < ShoppingCartEvent > (
103
- shoppingCartId ,
104
- [ { type : 'ProductItemAdded' , data : { productItem } } ] ,
105
- { expectedStreamVersion : result . nextExpectedStreamVersion } ,
106
- ) ;
107
-
108
- await eventStore . appendToStream < ShoppingCartEvent > (
109
- shoppingCartId ,
110
- [
111
- {
112
- type : 'DiscountApplied' ,
113
- data : { percent : discount , couponId : uuid ( ) } ,
114
- } ,
115
- ] ,
116
- { expectedStreamVersion : result2 . nextExpectedStreamVersion } ,
117
- ) ;
93
+ const productItem : PricedProductItem = {
94
+ productId : '123' ,
95
+ quantity : 10 ,
96
+ price : 3 ,
97
+ } ;
98
+ const discount = 10 ;
99
+ const shoppingCartId = `shopping_cart-${ uuid ( ) } ` ;
100
+
101
+ const result = await eventStore . appendToStream < ShoppingCartEvent > (
102
+ shoppingCartId ,
103
+ [ { type : 'ProductItemAdded' , data : { productItem } } ] ,
104
+ ) ;
118
105
119
- const aggregation = await eventStore . aggregateStream ( shoppingCartId , {
120
- evolve,
121
- initialState : ( ) => null ,
106
+ const result2 = await eventStore . appendToStream < ShoppingCartEvent > (
107
+ shoppingCartId ,
108
+ [ { type : 'ProductItemAdded' , data : { productItem } } ] ,
109
+ { expectedStreamVersion : result . nextExpectedStreamVersion } ,
110
+ ) ;
111
+
112
+ await eventStore . appendToStream < ShoppingCartEvent > (
113
+ shoppingCartId ,
114
+ [
115
+ {
116
+ type : 'DiscountApplied' ,
117
+ data : { percent : discount , couponId : uuid ( ) } ,
118
+ } ,
119
+ ] ,
120
+ { expectedStreamVersion : result2 . nextExpectedStreamVersion } ,
121
+ ) ;
122
+
123
+ const aggregation = await eventStore . aggregateStream ( shoppingCartId , {
124
+ evolve,
125
+ initialState : ( ) => null ,
126
+ } ) ;
127
+
128
+ assertDeepEqual (
129
+ { totalAmount : 54 , productItemsCount : 20 } ,
130
+ aggregation . state ,
131
+ ) ;
122
132
} ) ;
123
133
124
- assertDeepEqual (
125
- { totalAmount : 54 , productItemsCount : 20 } ,
126
- aggregation . state ,
127
- ) ;
134
+ void it ( 'should throw an error if concurrency check has failed when appending stream' , async ( ) => {
135
+ const eventStore = getSQLiteEventStore ( config ) ;
136
+
137
+ const productItem : PricedProductItem = {
138
+ productId : '123' ,
139
+ quantity : 10 ,
140
+ price : 3 ,
141
+ } ;
142
+
143
+ const shoppingCartId = `shopping_cart-${ uuid ( ) } ` ;
144
+
145
+ await assertThrowsAsync < ExpectedVersionConflictError < bigint > > (
146
+ async ( ) => {
147
+ await eventStore . appendToStream < ShoppingCartEvent > (
148
+ shoppingCartId ,
149
+ [
150
+ {
151
+ type : 'ProductItemAdded' ,
152
+ data : { productItem } ,
153
+ } ,
154
+ ] ,
155
+ {
156
+ expectedStreamVersion : 5n ,
157
+ } ,
158
+ ) ;
159
+ } ,
160
+ ) ;
161
+ } ) ;
128
162
} ) ;
129
163
130
164
void it ( 'should automatically create schema' , async ( ) => {
131
165
const eventStore = getSQLiteEventStore ( {
132
166
schema : {
133
167
autoMigration : 'CreateOrUpdate' ,
134
168
} ,
135
- databaseLocation : ` ${ testDatabasePath } /test.db` ,
169
+ fileName ,
136
170
} ) ;
137
171
138
172
const productItem : PricedProductItem = {
@@ -158,7 +192,7 @@ void describe('SQLiteEventStore', () => {
158
192
schema : {
159
193
autoMigration : 'CreateOrUpdate' ,
160
194
} ,
161
- databaseLocation : ':memory:' ,
195
+ fileName : InMemorySQLiteDatabase ,
162
196
} ) ;
163
197
const productItem : PricedProductItem = {
164
198
productId : '123' ,
@@ -183,7 +217,7 @@ void describe('SQLiteEventStore', () => {
183
217
schema : {
184
218
autoMigration : 'CreateOrUpdate' ,
185
219
} ,
186
- databaseLocation : ` ${ testDatabasePath } /test.db` ,
220
+ fileName ,
187
221
} ) ;
188
222
189
223
const productItem : PricedProductItem = {
@@ -206,46 +240,14 @@ void describe('SQLiteEventStore', () => {
206
240
schema : {
207
241
autoMigration : 'CreateOrUpdate' ,
208
242
} ,
209
- databaseLocation : ` ${ testDatabasePath } /test.db` ,
243
+ fileName ,
210
244
} ) ;
211
245
212
246
const stream = await sameEventStore . readStream ( shoppingCartId ) ;
213
247
214
248
assertIsNotNull ( stream . events ) ;
215
249
assertEqual ( 1 , stream . events . length ) ;
216
250
} ) ;
217
-
218
- void it ( 'should throw an error if concurrency check has failed when appending stream' , async ( ) => {
219
- const eventStore = getSQLiteEventStore ( {
220
- schema : {
221
- autoMigration : 'CreateOrUpdate' ,
222
- } ,
223
- databaseLocation : `${ testDatabasePath } /test.db` ,
224
- } ) ;
225
-
226
- const productItem : PricedProductItem = {
227
- productId : '123' ,
228
- quantity : 10 ,
229
- price : 3 ,
230
- } ;
231
-
232
- const shoppingCartId = `shopping_cart-${ uuid ( ) } ` ;
233
-
234
- await assertThrowsAsync < ExpectedVersionConflictError < bigint > > ( async ( ) => {
235
- await eventStore . appendToStream < ShoppingCartEvent > (
236
- shoppingCartId ,
237
- [
238
- {
239
- type : 'ProductItemAdded' ,
240
- data : { productItem } ,
241
- } ,
242
- ] ,
243
- {
244
- expectedStreamVersion : 5n ,
245
- } ,
246
- ) ;
247
- } ) ;
248
- } ) ;
249
251
} ) ;
250
252
251
253
type ShoppingCartShortInfo = {
0 commit comments