Skip to content

Commit cdd7952

Browse files
KalleVdhmlau
authored andcommitted
test(sequelize): update tests to cover hasMany relation
Signed-off-by: KalleV <[email protected]>
1 parent 54d1df2 commit cdd7952

File tree

3 files changed

+59
-28
lines changed

3 files changed

+59
-28
lines changed

extensions/sequelize/src/__tests__/fixtures/models/patient.model.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ export class Patient extends Entity {
1515
})
1616
name: string;
1717

18+
@property({
19+
type: 'string',
20+
hidden: true,
21+
})
22+
password?: string;
23+
1824
constructor(data?: Partial<Patient>) {
1925
super(data);
2026
}

extensions/sequelize/src/__tests__/integration/repository.integration.ts

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -868,27 +868,6 @@ describe('Sequelize CRUD Repository (integration)', () => {
868868
]);
869869
});
870870

871-
it('hides hidden properties in related entities', async () => {
872-
await migrateSchema(['todos', 'todo-lists', 'users']);
873-
874-
const userRes = await client.post('/users').send(getDummyUser());
875-
876-
await client.post('/todo-lists').send(
877-
getDummyTodoList({
878-
title: 'Todo list one',
879-
userId: userRes.body.id,
880-
}),
881-
);
882-
883-
const filter = {include: ['user']};
884-
const relationRes = await client.get(`/todo-lists`).query({
885-
filter: JSON.stringify(filter),
886-
});
887-
888-
expect(relationRes.body.length).to.be.equal(1);
889-
expect(relationRes.body.at(0).user).not.to.have.property('password');
890-
});
891-
892871
it('supports `order` filter by associations', async () => {
893872
await migrateSchema(['todos', 'todo-lists']);
894873

@@ -1026,6 +1005,27 @@ describe('Sequelize CRUD Repository (integration)', () => {
10261005
);
10271006
});
10281007

1008+
it('hides hidden properties in related entities included with @belongsTo relation', async () => {
1009+
await migrateSchema(['todos', 'todo-lists', 'users']);
1010+
1011+
const userRes = await client.post('/users').send(getDummyUser());
1012+
1013+
await client.post('/todo-lists').send(
1014+
getDummyTodoList({
1015+
title: 'Todo list one',
1016+
userId: userRes.body.id,
1017+
}),
1018+
);
1019+
1020+
const filter = {include: ['user']};
1021+
const relationRes = await client.get(`/todo-lists`).query({
1022+
filter: JSON.stringify(filter),
1023+
});
1024+
1025+
expect(relationRes.body.length).to.be.equal(1);
1026+
expect(relationRes.body.at(0).user).not.to.have.property('password');
1027+
});
1028+
10291029
it('supports @belongsTo using keyfrom and keyto', async () => {
10301030
await migrateSchema(['users', 'todos', 'todo-lists']);
10311031

@@ -1078,13 +1078,13 @@ describe('Sequelize CRUD Repository (integration)', () => {
10781078

10791079
const doctorRes = await client.post('/doctors').send(getDummyDoctor());
10801080
const patientRes = await client
1081-
.post(`/doctors/${1}/patients`)
1081+
.post(`/doctors/${doctorRes.body.id}/patients`)
10821082
.send(getDummyPatient());
10831083

10841084
const filter = {include: ['patients']};
1085-
const relationRes = await client.get(
1086-
`/doctors?filter=${encodeURIComponent(JSON.stringify(filter))}`,
1087-
);
1085+
const relationRes = await client
1086+
.get(`/doctors`)
1087+
.query({filter: JSON.stringify(filter)});
10881088

10891089
/**
10901090
* Manually Remove through table data as sqlite3 doesn't support `attributes: []` using sequelize
@@ -1099,6 +1099,29 @@ describe('Sequelize CRUD Repository (integration)', () => {
10991099
]);
11001100
});
11011101

1102+
it('hides hidden props for nested entities included with @hasMany relation', async () => {
1103+
await migrateSchema(['doctors']);
1104+
1105+
const doctorRes = await client.post('/doctors').send(getDummyDoctor());
1106+
1107+
await client.post(`/doctors/${doctorRes.body.id}/patients`).send(
1108+
getDummyPatient({
1109+
password: 'secret',
1110+
}),
1111+
);
1112+
1113+
const filter = {include: ['patients']};
1114+
const relationRes = await client
1115+
.get(`/doctors`)
1116+
.query({filter: JSON.stringify(filter)});
1117+
1118+
expect(relationRes.body.length).to.be.equal(1);
1119+
expect(relationRes.body.at(0)).to.have.property('patients');
1120+
expect(relationRes.body.at(0).patients.at(0)).to.not.have.property(
1121+
'password',
1122+
);
1123+
});
1124+
11021125
it('supports @referencesMany', async () => {
11031126
await migrateSchema(['developers']);
11041127

extensions/sequelize/src/sequelize/sequelize.repository.base.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,11 @@ export class SequelizeCrudRepository<
10921092
}
10931093
}
10941094

1095+
/**
1096+
* Transform related entities queried through relations into their corresponding Loopback models.
1097+
* This ensures hidden properties defined in the nested Loopback models are excluded from the response.
1098+
* @see https://loopback.io/doc/en/lb4/Model.html#hidden-properties
1099+
*/
10951100
function transformRelatedEntitiesToLoopbackModels(
10961101
entities: T[],
10971102
entityClass: typeof Entity,
@@ -1135,9 +1140,6 @@ export class SequelizeCrudRepository<
11351140
});
11361141
}
11371142

1138-
// Ensure models queried through relations are transformed into Loopback models so that the
1139-
// hidden properties are removed from the response during the "toJSON" transformation.
1140-
// See: https://loopback.io/doc/en/lb4/Model.html#hidden-properties
11411143
transformRelatedEntitiesToLoopbackModels(
11421144
parentEntityInstances,
11431145
parentEntityClass,

0 commit comments

Comments
 (0)