-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathcreate-retrieve.suite.ts
60 lines (53 loc) · 1.92 KB
/
create-retrieve.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
// 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, model, property} from '@loopback/repository';
import {EntityCrudRepository} from '@loopback/repository/src';
import {expect, toJSON} from '@loopback/testlab';
import {withCrudCtx} from '../helpers.repository-tests';
import {
CrudConnectorFeatures,
CrudRepositoryCtor,
CrudTestContext,
DataSourceOptions,
} from '../types.repository-tests';
// Core scenarios around creating new model instances and retrieving them back
// Please keep this file short, put any advanced scenarios to other files
export function createRetrieveSuite(
dataSourceOptions: DataSourceOptions,
repositoryClass: CrudRepositoryCtor,
connectorFeatures: CrudConnectorFeatures,
) {
@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', () => {
let repo: EntityCrudRepository<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 a newly created model with id set by the database', async () => {
const created = await repo.create({name: 'Pencil'});
expect(created.toObject()).to.have.properties('id', 'name');
expect(created.id).to.be.ok();
const found = await repo.findById(created.id);
expect(toJSON(created)).to.deepEqual(toJSON(found));
});
});
}