-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
transactions.suite.ts
191 lines (174 loc) · 6.57 KB
/
transactions.suite.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/repository-tests
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {
Entity,
IsolationLevel,
juggler,
model,
property,
Transaction,
TransactionalEntityRepository,
} from '@loopback/repository';
import {expect, skipIf, toJSON} from '@loopback/testlab';
import {Suite} from 'mocha';
import {MixedIdType, withCrudCtx} from '../helpers.repository-tests';
import {
CrudFeatures,
CrudTestContext,
DataSourceOptions,
TransactionalRepositoryCtor,
} from '../types.repository-tests';
// Core scenarios for testing CRUD functionalities of Transactional connectors
// Please keep this file short, put any advanced scenarios to other files
/* istanbul ignore file */
export function transactionSuite(
dataSourceOptions: DataSourceOptions,
repositoryClass: TransactionalRepositoryCtor,
connectorFeatures: CrudFeatures,
) {
skipIf<[(this: Suite) => void], void>(
!connectorFeatures.supportsTransactions,
describe,
`transactions`,
() => {
@model()
class Product extends Entity {
@property({
type: connectorFeatures.idType,
id: true,
generated: true,
description: 'The unique identifier for a product',
})
id: MixedIdType;
@property({type: 'string', required: true})
name: string;
constructor(data?: Partial<Product>) {
super(data);
}
}
describe('create-retrieve with transactions', () => {
let repo: TransactionalEntityRepository<
Product,
typeof Product.prototype.id
>;
let tx: Transaction | undefined;
let ds: juggler.DataSource;
before(
withCrudCtx(async function setupRepository(ctx: CrudTestContext) {
ds = ctx.dataSource;
repo = new repositoryClass(Product, ds);
await ds.automigrate(Product.name);
}),
);
beforeEach(() => {
tx = undefined;
});
afterEach(async () => {
// FIXME: replace tx.connection with tx.isActive when it becomes
// available
// see https://github.com/strongloop/loopback-next/issues/3471
tx = setTransactionToUndefined(tx);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (tx !== undefined && (tx as any).connection) {
await tx.rollback();
}
});
it('retrieves model instance once transaction is committed', async () => {
tx = await repo.beginTransaction(IsolationLevel.READ_COMMITTED);
const created = await repo.create(
{name: 'Pencil'},
{transaction: tx},
);
expect(created.toObject()).to.have.properties('id', 'name');
expect(created.id).to.be.ok();
await tx.commit();
const foundOutsideTransaction = await repo.findById(created.id, {});
expect(toJSON(created)).to.deepEqual(toJSON(foundOutsideTransaction));
});
it('can rollback transaction', async () => {
tx = await repo.beginTransaction(IsolationLevel.READ_COMMITTED);
const created = await repo.create(
{name: 'Pencil'},
{transaction: tx},
);
expect(created.toObject()).to.have.properties('id', 'name');
expect(created.id).to.be.ok();
const foundInsideTransaction = await repo.findById(
created.id,
{},
{
transaction: tx,
},
);
expect(toJSON(created)).to.deepEqual(toJSON(foundInsideTransaction));
await tx.rollback();
await expect(repo.findById(created.id, {})).to.be.rejectedWith({
code: 'ENTITY_NOT_FOUND',
});
});
it('ensures transactions are isolated', async () => {
tx = await repo.beginTransaction(IsolationLevel.READ_COMMITTED);
const created = await repo.create(
{name: 'Pencil'},
{transaction: tx},
);
expect(created.toObject()).to.have.properties('id', 'name');
expect(created.id).to.be.ok();
const foundInsideTransaction = await repo.findById(
created.id,
{},
{
transaction: tx,
},
);
expect(toJSON(created)).to.deepEqual(toJSON(foundInsideTransaction));
await expect(repo.findById(created.id, {})).to.be.rejectedWith({
code: 'ENTITY_NOT_FOUND',
});
});
it('should not use transaction with another repository', async () => {
const ds2Options = Object.assign({}, dataSourceOptions);
ds2Options.name = 'anotherDataSource';
ds2Options.database = ds2Options.database + '_new';
const ds2 = new juggler.DataSource(ds2Options);
const anotherRepo = new repositoryClass(Product, ds2);
await ds2.automigrate(Product.name);
tx = await repo.beginTransaction(IsolationLevel.READ_COMMITTED);
// we should reject this call with a clear error message
// stating that transaction doesn't belong to the repository
// and that only local transactions are supported
// expect(
// await anotherRepo.create({name: 'Pencil'}, {transaction: tx}),
// ).to.throw(/some error/);
// see https://github.com/strongloop/loopback-next/issues/3483
const created = await anotherRepo.create(
{name: 'Pencil'},
{transaction: tx},
);
expect(created.toObject()).to.have.properties('id', 'name');
expect(created.id).to.be.ok();
// for now, LB ignores the transaction so the instance
// should be created regardless
await tx.rollback();
const foundOutsideTransaction = await anotherRepo.findById(
created.id,
{},
);
expect(toJSON(created)).to.deepEqual(toJSON(foundOutsideTransaction));
});
// temporary workaround for "Release called on client which has
// already been released to the pool." for PostgreSQL until
// tx.isActive is available
//FIXME: remove this once tx.isActive becomes available
// see https://github.com/strongloop/loopback-next/issues/3471
function setTransactionToUndefined(t: Transaction | undefined) {
return ds.connector && ds.connector.name === 'postgresql'
? undefined
: t;
}
});
},
);
}