11import { Ddl } from "./../ddl/ddl" ;
2- import { expect } from " chai" ;
2+ import { expect } from ' chai' ;
33import { ObjectToTest } from "./objeto-to-test" ;
44import { getMapper } from "./mappers-table-new" ;
55import { Crud } from "../crud" ;
@@ -8,14 +8,20 @@ import { SQLiteDatabase } from "./database/sqlite-database";
88import { DatabaseObject } from "../definitions" ;
99import { QueryCompiled } from "../core" ;
1010import { TestClazz } from "./models/test-clazz" ;
11- import { firstValueFrom } from "rxjs" ;
11+ import { firstValueFrom , lastValueFrom } from "rxjs" ;
12+ import * as sinon from "sinon" ;
13+ import { SinonSandbox } from "sinon" ;
1214
1315describe ( "Managed Transaction" , ( ) => {
1416 let crud : Crud ;
1517 let ddl : Ddl ;
1618 let database : DatabaseObject ;
1719
20+ let sandbox : SinonSandbox ;
21+
1822 before ( async ( ) => {
23+ sandbox = sinon . createSandbox ( ) ;
24+
1925 const mapper = getMapper ( ) ;
2026
2127 database = await new SQLiteDatabase ( ) . init ( ) ;
@@ -25,14 +31,14 @@ describe("Managed Transaction", () => {
2531
2632 beforeEach ( async ( ) => {
2733 await ddl . create ( GuidClazz ) . execute ( ) . toPromise ( ) ;
34+ sandbox . restore ( ) ;
2835 } ) ;
2936
3037 afterEach ( async ( ) => {
3138 await ddl . drop ( GuidClazz ) . execute ( ) . toPromise ( ) ;
3239 } ) ;
3340
3441 it ( "Transaction Simple" , async ( ) => {
35-
3642 const transaction = database . managedTransaction ( ) ;
3743
3844 const obj1 = Object . assign ( { } , ObjectToTest . guidClazz ) ;
@@ -68,7 +74,6 @@ describe("Managed Transaction", () => {
6874 } ) ;
6975
7076 it ( "Transaction inactive" , async ( ) => {
71-
7277 const transaction = database . managedTransaction ( ) ;
7378
7479 const obj1 = Object . assign ( { } , ObjectToTest . guidClazz ) ;
@@ -85,13 +90,91 @@ describe("Managed Transaction", () => {
8590 expect ( queryUpdateResult [ 0 ] . description ) . to . equal ( obj1 . description ) ;
8691 expect ( queryUpdateResult [ 0 ] . guid ) . to . equal ( obj1 . guid ) ;
8792
88- expect ( ( ) => transaction . add ( ddl . drop ( GuidClazz ) ) ) . to . throw ( `Transaction (id: ${ transaction . id } ) is no longer active, and can no longer be used` ) ;
93+ expect ( ( ) => transaction . add ( ddl . drop ( GuidClazz ) ) ) . to . throw ( `Transaction (id: ${ transaction . id } ) is no longer active, and can no longer be used. Current status: COMMITTED ` ) ;
8994
9095 const deleteResult = await ddl . drop ( GuidClazz ) . execute ( ) . toPromise ( ) ;
9196 expect ( deleteResult . length ) . to . equal ( 1 ) ;
9297 expect ( deleteResult [ 0 ] . rowsAffected ) . to . equal ( 1 ) ;
9398 } ) ;
9499
100+ it ( "Transaction two rollback" , async ( ) => {
101+ const warn = sandbox . spy ( console , "warn" ) ;
102+
103+ const transaction = database . managedTransaction ( ) ;
104+
105+ const obj1 = Object . assign ( { } , ObjectToTest . guidClazz ) ;
106+ transaction . add (
107+ crud
108+ . insert ( GuidClazz , { toSave : obj1 } )
109+ ) ;
110+
111+ // first rollback
112+ await transaction . rollback ( ) ;
113+ expect ( warn . notCalled ) . to . true ;
114+
115+ // second rollback
116+ await transaction . rollback ( ) ;
117+
118+ const expectedWarnTransaction = `Transaction (id: ${ transaction . id } ) already rollbacked` ;
119+ sinon . assert . calledWith ( warn , expectedWarnTransaction ) ;
120+ } ) ;
121+
122+ it ( "Transaction two commit" , async ( ) => {
123+ const warn = sandbox . spy ( console , "warn" ) ;
124+
125+ const transaction = database . managedTransaction ( ) ;
126+
127+ const obj1 = Object . assign ( { } , ObjectToTest . guidClazz ) ;
128+ transaction . add (
129+ crud
130+ . insert ( GuidClazz , { toSave : obj1 } )
131+ ) ;
132+
133+ // first commit
134+ await lastValueFrom ( transaction . commit ( ) ) ;
135+ expect ( warn . notCalled ) . to . true ;
136+
137+ // second commit
138+ await lastValueFrom ( transaction . commit ( ) ) ;
139+
140+ const expectedWarnTransaction = `Transaction (id: ${ transaction . id } ) already committed` ;
141+ sinon . assert . calledWith ( warn , expectedWarnTransaction ) ;
142+ } ) ;
143+
144+ it ( "Transaction a commit and a rollback" , async ( ) => {
145+ const transaction = database . managedTransaction ( ) ;
146+
147+ const obj1 = Object . assign ( { } , ObjectToTest . guidClazz ) ;
148+ transaction . add (
149+ crud
150+ . insert ( GuidClazz , { toSave : obj1 } )
151+ ) ;
152+
153+ // commit
154+ await lastValueFrom ( transaction . commit ( ) ) ;
155+
156+ // rollback
157+ await expect ( transaction . rollback ( ) )
158+ . to . rejectedWith ( `Transaction (id: ${ transaction . id } ) is no longer active, and can no longer be used. Current status: COMMITTED` ) ;
159+ } ) ;
160+
161+ it ( "Transaction a rollback and a commit" , async ( ) => {
162+ const transaction = database . managedTransaction ( ) ;
163+
164+ const obj1 = Object . assign ( { } , ObjectToTest . guidClazz ) ;
165+ transaction . add (
166+ crud
167+ . insert ( GuidClazz , { toSave : obj1 } )
168+ ) ;
169+
170+ // rollback
171+ await transaction . rollback ( ) ;
172+
173+ // commit
174+ await expect ( lastValueFrom ( transaction . commit ( ) ) )
175+ . to . rejectedWith ( `Transaction (id: ${ transaction . id } ) is no longer active, and can no longer be used. Current status: ROLLBACKED` ) ;
176+ } ) ;
177+
95178 it ( "Transaction execute immediate" , async ( ) => {
96179
97180 const transaction = database . managedTransaction ( ) ;
0 commit comments