Skip to content

Commit

Permalink
chore: do not return null as a not existed entities of referencesMa…
Browse files Browse the repository at this point in the history
…ny relation

Signed-off-by: Mike Evstropov <[email protected]>
  • Loading branch information
mikeevstropov committed Mar 2, 2022
1 parent 06a7abd commit e257bbf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('ReferencesManyApplication', () => {
await accountRepo.deleteAll();
});

it('creates an customer', async function () {
it('creates a customer', async function () {
const customer = givenCustomer();
const response = await client.post('/customers').send(customer).expect(200);
expect(response.body).to.containDeep(customer);
Expand All @@ -66,14 +66,14 @@ describe('ReferencesManyApplication', () => {
persistedCustomer = await givenCustomerInstance(customerRepo);
});

it('gets an customer by ID', () => {
it('gets a customer by ID', () => {
return client
.get(`/customers/${persistedCustomer.id}`)
.send()
.expect(200, toJSON(persistedCustomer));
});

it('returns 404 when getting an customer that does not exist', () => {
it('returns 404 when getting a customer that does not exist', () => {
return client.get('/customers/99999').expect(404);
});

Expand All @@ -90,7 +90,7 @@ describe('ReferencesManyApplication', () => {
expect(result).to.containEql(updatedCustomer);
});

it('returns 404 when replacing an customer that does not exist', () => {
it('returns 404 when replacing a customer that does not exist', () => {
return client.put('/customers/99999').send(givenCustomer()).expect(404);
});

Expand All @@ -107,7 +107,7 @@ describe('ReferencesManyApplication', () => {
expect(result).to.containEql(updatedCustomer);
});

it('returns 404 when updating an customer that does not exist', () => {
it('returns 404 when updating a customer that does not exist', () => {
return client.patch('/customer/99999').send(givenCustomer()).expect(404);
});

Expand All @@ -118,7 +118,7 @@ describe('ReferencesManyApplication', () => {
).to.be.rejectedWith(EntityNotFoundError);
});

it('returns 404 when deleting an customer that does not exist', async () => {
it('returns 404 when deleting a customer that does not exist', async () => {
await client.del(`/customers/99999`).expect(404);
});
});
Expand Down Expand Up @@ -170,10 +170,29 @@ describe('ReferencesManyApplication', () => {
const response = await client.get('/customers').query({filter: filter});

expect(response.body).to.have.length(2);
expect(response.body[0]).to.deepEqual(toJSON(customer));
expect(response.body[0]).to.deepEqual({
...toJSON(customer),
accounts: [],
});
expect(response.body[1]).to.deepEqual({
...toJSON(customerWithAccounts),
accounts: [toJSON(firstAccount), toJSON(secondAccount)],
});
});

it('not includes a not existed Account in query result', async () => {
const notExistedId = 1;
const customer = await givenCustomerInstance(customerRepo, {
accountIds: [notExistedId],
});
const filter = JSON.stringify({include: ['accounts']});

const response = await client.get('/customers').query({filter: filter});

expect(response.body).to.have.length(1);
expect(response.body[0]).to.deepEqual({
...toJSON(customer),
accounts: [],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function createReferencesManyInclusionResolver<
entities: Entity[],
inclusion: InclusionFilter,
options?: Options,
): Promise<((Target & TargetRelations)[] | undefined)[]> {
): Promise<(Target & TargetRelations)[][]> {
if (!entities.length) return [];

const sourceKey = relationMeta.keyFrom;
Expand All @@ -68,12 +68,13 @@ export function createReferencesManyInclusionResolver<
);

return sourceMap.map(chainIds => {
if (chainIds === undefined) return chainIds;
return flattenTargetsOfOneToOneRelation(
if (!chainIds) return [];
const targets = flattenTargetsOfOneToOneRelation(
chainIds,
targetsFound,
targetKey,
);
return targets.filter((v): v is Target & TargetRelations => v != null);
});
};
}

0 comments on commit e257bbf

Please sign in to comment.