-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Biniam Admikew
committed
Jul 23, 2019
1 parent
2bd6344
commit bbca2aa
Showing
19 changed files
with
955 additions
and
3,811 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
acceptance/repository-mysql/src/__tests__/mysql.transaction.acceptance.ts
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
packages/repository-tests/src/crud/transactions.suite.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// 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, | ||
TransactionalRepository, | ||
} from '@loopback/repository'; | ||
import {expect, skipIf, toJSON} from '@loopback/testlab'; | ||
import {Suite} from 'mocha'; | ||
import {withCrudCtx} from '../helpers.repository-tests'; | ||
import { | ||
CrudConnectorFeatures, | ||
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 | ||
export function transactionSuite( | ||
dataSourceOptions: DataSourceOptions, | ||
repositoryClass: TransactionalRepositoryCtor, | ||
connectorFeatures: CrudConnectorFeatures, | ||
) { | ||
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: number | string; | ||
|
||
@property({type: 'string', required: true}) | ||
name: string; | ||
|
||
constructor(data?: Partial<Product>) { | ||
super(data); | ||
} | ||
} | ||
|
||
describe('create-retrieve with transactions', () => { | ||
let repo: TransactionalRepository<Product, typeof Product.prototype.id>; | ||
before( | ||
withCrudCtx(async function setupRepository(ctx: CrudTestContext) { | ||
repo = new repositoryClass(Product, ctx.dataSource); | ||
await ctx.dataSource.automigrate(Product.name); | ||
}), | ||
); | ||
|
||
it('retrieves model instance once transaction is committed', async () => { | ||
const tx: juggler.Transaction = await repo.beginTransaction({ | ||
isolationLevel: 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 () => { | ||
const tx: juggler.Transaction = await repo.beginTransaction({ | ||
isolationLevel: 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', | ||
message: /Entity not found/, | ||
}); | ||
}); | ||
|
||
it('ensures transactions are isolated', async () => { | ||
const tx: juggler.Transaction = await repo.beginTransaction({ | ||
isolationLevel: 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', | ||
message: /Entity not found/, | ||
}); | ||
}); | ||
}); | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
80 changes: 0 additions & 80 deletions
80
packages/repository-tests/src/transaction/transactions.suite.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.