-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathbelongs-to.relation.acceptance.ts
95 lines (81 loc) · 2.91 KB
/
belongs-to.relation.acceptance.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
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {Application} from '@loopback/core';
import {expect} from '@loopback/testlab';
import {
ApplicationWithRepositories,
juggler,
repository,
RepositoryMixin,
} from '../..';
import {
CustomerRepository,
OrderRepository,
ShipmentRepository,
} from '../fixtures/repositories';
describe('BelongsTo relation', () => {
// Given a Customer and Order models - see definitions at the bottom
let app: ApplicationWithRepositories;
let controller: OrderController;
let customerRepo: CustomerRepository;
let orderRepo: OrderRepository;
let shipmentRepo: ShipmentRepository;
before(givenApplicationWithMemoryDB);
before(givenBoundCrudRepositories);
before(givenOrderController);
beforeEach(async () => {
await orderRepo.deleteAll();
});
it('can find customer of order', async () => {
const customer = await customerRepo.create({name: 'Order McForder'});
const order = await orderRepo.create({
customerId: customer.id,
description: 'Order from Order McForder, the hoarder of Mordor',
});
const result = await controller.findOwnerOfOrder(order.id);
expect(result).to.deepEqual(customer);
});
it('can find shipment of order with a custom foreign key name', async () => {
const shipment = await shipmentRepo.create({
name: 'Tuesday morning shipment',
});
const order = await orderRepo.create({
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: shipment.id,
description: 'Order that is shipped Tuesday morning',
});
const result = await controller.findOrderShipment(order.id);
expect(result).to.deepEqual(shipment);
});
//--- HELPERS ---//
class OrderController {
constructor(
@repository(OrderRepository) protected orderRepository: OrderRepository,
) {}
async findOwnerOfOrder(orderId: string) {
return await this.orderRepository.customer(orderId);
}
async findOrderShipment(orderId: string) {
return await this.orderRepository.shipment(orderId);
}
}
function givenApplicationWithMemoryDB() {
class TestApp extends RepositoryMixin(Application) {}
app = new TestApp();
app.dataSource(new juggler.DataSource({name: 'db', connector: 'memory'}));
}
async function givenBoundCrudRepositories() {
app.repository(CustomerRepository);
app.repository(OrderRepository);
app.repository(ShipmentRepository);
customerRepo = await app.getRepository(CustomerRepository);
orderRepo = await app.getRepository(OrderRepository);
shipmentRepo = await app.getRepository(ShipmentRepository);
}
async function givenOrderController() {
app.controller(OrderController);
controller = await app.get<OrderController>('controllers.OrderController');
}
});