1
1
import { Ddl } from "./../ddl/ddl" ;
2
- import { expect } from " chai" ;
2
+ import { expect } from ' chai' ;
3
3
import { ObjectToTest } from "./objeto-to-test" ;
4
4
import { getMapper } from "./mappers-table-new" ;
5
5
import { Crud } from "../crud" ;
@@ -8,14 +8,20 @@ import { SQLiteDatabase } from "./database/sqlite-database";
8
8
import { DatabaseObject } from "../definitions" ;
9
9
import { QueryCompiled } from "../core" ;
10
10
import { 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" ;
12
14
13
15
describe ( "Managed Transaction" , ( ) => {
14
16
let crud : Crud ;
15
17
let ddl : Ddl ;
16
18
let database : DatabaseObject ;
17
19
20
+ let sandbox : SinonSandbox ;
21
+
18
22
before ( async ( ) => {
23
+ sandbox = sinon . createSandbox ( ) ;
24
+
19
25
const mapper = getMapper ( ) ;
20
26
21
27
database = await new SQLiteDatabase ( ) . init ( ) ;
@@ -25,14 +31,14 @@ describe("Managed Transaction", () => {
25
31
26
32
beforeEach ( async ( ) => {
27
33
await ddl . create ( GuidClazz ) . execute ( ) . toPromise ( ) ;
34
+ sandbox . restore ( ) ;
28
35
} ) ;
29
36
30
37
afterEach ( async ( ) => {
31
38
await ddl . drop ( GuidClazz ) . execute ( ) . toPromise ( ) ;
32
39
} ) ;
33
40
34
41
it ( "Transaction Simple" , async ( ) => {
35
-
36
42
const transaction = database . managedTransaction ( ) ;
37
43
38
44
const obj1 = Object . assign ( { } , ObjectToTest . guidClazz ) ;
@@ -68,7 +74,6 @@ describe("Managed Transaction", () => {
68
74
} ) ;
69
75
70
76
it ( "Transaction inactive" , async ( ) => {
71
-
72
77
const transaction = database . managedTransaction ( ) ;
73
78
74
79
const obj1 = Object . assign ( { } , ObjectToTest . guidClazz ) ;
@@ -85,13 +90,91 @@ describe("Managed Transaction", () => {
85
90
expect ( queryUpdateResult [ 0 ] . description ) . to . equal ( obj1 . description ) ;
86
91
expect ( queryUpdateResult [ 0 ] . guid ) . to . equal ( obj1 . guid ) ;
87
92
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 ` ) ;
89
94
90
95
const deleteResult = await ddl . drop ( GuidClazz ) . execute ( ) . toPromise ( ) ;
91
96
expect ( deleteResult . length ) . to . equal ( 1 ) ;
92
97
expect ( deleteResult [ 0 ] . rowsAffected ) . to . equal ( 1 ) ;
93
98
} ) ;
94
99
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
+
95
178
it ( "Transaction execute immediate" , async ( ) => {
96
179
97
180
const transaction = database . managedTransaction ( ) ;
0 commit comments