Skip to content

Commit

Permalink
fixup! apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Biniam Admikew committed Jul 23, 2019
1 parent 2bd6344 commit bbca2aa
Show file tree
Hide file tree
Showing 19 changed files with 955 additions and 3,811 deletions.
630 changes: 630 additions & 0 deletions acceptance/repository-mysql/package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export const MYSQL_CONFIG: DataSourceOptions = {
export const MYSQL_FEATURES: Partial<CrudConnectorFeatures> = {
idType: 'number',
freeFormProperties: false,
supportsTransactions: true,
};

This file was deleted.

32 changes: 32 additions & 0 deletions packages/repository-tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/repository-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build": "lb-tsc",
"clean": "lb-clean loopback-repository-tests*.tgz dist tsconfig.build.tsbuildinfo package",
"pretest": "npm run build",
"test": "lb-mocha \"dist/__tests__/**/*.js\" --reporter=spec",
"test": "lb-mocha \"dist/__tests__/**/*.js\"",
"verify": "npm pack && tar xf loopback-repository*.tgz && tree package && npm run clean"
},
"author": "IBM Corp.",
Expand Down
132 changes: 132 additions & 0 deletions packages/repository-tests/src/crud/transactions.suite.ts
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/,
});
});
});
},
);
}
1 change: 0 additions & 1 deletion packages/repository-tests/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// License text available at https://opensource.org/licenses/MIT

export * from './crud-test-suite';
export * from './transaction-test-suite';
export * from './types.repository-tests';

// TODO(bajtos) Implement test suite for KeyValue repository
80 changes: 0 additions & 80 deletions packages/repository-tests/src/transaction-test-suite.ts

This file was deleted.

80 changes: 0 additions & 80 deletions packages/repository-tests/src/transaction/transactions.suite.ts

This file was deleted.

Loading

0 comments on commit bbca2aa

Please sign in to comment.