@@ -6,133 +6,167 @@ import {
66 ExpectedVersionConflictError ,
77} from '@event-driven-io/emmett' ;
88import 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' ;
1111import { fileURLToPath } from 'url' ;
1212import { v4 as uuid } from 'uuid' ;
13- import { sqliteConnection , type AbsolutePath } from '../sqliteConnection' ;
13+ import { InMemorySQLiteDatabase , sqliteConnection } from '../sqliteConnection' ;
1414import {
1515 type DiscountApplied ,
1616 type PricedProductItem ,
1717 type ProductItemAdded ,
1818 type ShoppingCartEvent ,
1919} from '../testing/shoppingCart.domain' ;
2020import { 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' ;
2425
2526void 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' ) ;
2733
2834 afterEach ( ( ) => {
29- if ( ! fs . existsSync ( ` ${ testDatabasePath } /test.db` ) ) {
35+ if ( ! fs . existsSync ( fileName ) ) {
3036 return ;
3137 }
32- fs . unlink ( `${ testDatabasePath } /test.db` , ( err ) => {
33- if ( err ) console . error ( 'Error deleting file:' , err ) ;
34- } ) ;
38+ fs . unlinkSync ( fileName ) ;
3539 } ) ;
3640
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,
4947 } ;
50- const discount = 10 ;
51- const shoppingCartId = `shopping_cart-${ uuid ( ) } ` ;
5248
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 } ) ) ) ;
7450
75- const { events } = await eventStore . readStream ( shoppingCartId ) ;
51+ void it ( 'should append events' , async ( ) => {
52+ const eventStore = getSQLiteEventStore ( config ) ;
7653
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 ( ) } ` ;
8061
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 ) ;
8788 } ) ;
8889
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 ) ;
9692
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+ ) ;
118105
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+ ) ;
122132 } ) ;
123133
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+ } ) ;
128162 } ) ;
129163
130164 void it ( 'should automatically create schema' , async ( ) => {
131165 const eventStore = getSQLiteEventStore ( {
132166 schema : {
133167 autoMigration : 'CreateOrUpdate' ,
134168 } ,
135- databaseLocation : ` ${ testDatabasePath } /test.db` ,
169+ fileName ,
136170 } ) ;
137171
138172 const productItem : PricedProductItem = {
@@ -158,7 +192,7 @@ void describe('SQLiteEventStore', () => {
158192 schema : {
159193 autoMigration : 'CreateOrUpdate' ,
160194 } ,
161- databaseLocation : ':memory:' ,
195+ fileName : InMemorySQLiteDatabase ,
162196 } ) ;
163197 const productItem : PricedProductItem = {
164198 productId : '123' ,
@@ -183,7 +217,7 @@ void describe('SQLiteEventStore', () => {
183217 schema : {
184218 autoMigration : 'CreateOrUpdate' ,
185219 } ,
186- databaseLocation : ` ${ testDatabasePath } /test.db` ,
220+ fileName ,
187221 } ) ;
188222
189223 const productItem : PricedProductItem = {
@@ -206,46 +240,14 @@ void describe('SQLiteEventStore', () => {
206240 schema : {
207241 autoMigration : 'CreateOrUpdate' ,
208242 } ,
209- databaseLocation : ` ${ testDatabasePath } /test.db` ,
243+ fileName ,
210244 } ) ;
211245
212246 const stream = await sameEventStore . readStream ( shoppingCartId ) ;
213247
214248 assertIsNotNull ( stream . events ) ;
215249 assertEqual ( 1 , stream . events . length ) ;
216250 } ) ;
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- } ) ;
249251} ) ;
250252
251253type ShoppingCartShortInfo = {
0 commit comments