Skip to content

Commit

Permalink
feat(repository): transparently ensure foreign key source in inclusio…
Browse files Browse the repository at this point in the history
…n resolvers
  • Loading branch information
InvictusMB committed May 31, 2020
1 parent 655c7fe commit bc189e6
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ describe('HasMany relation', () => {
});

const customer = await customerRepo.findById(existingCustomerId, {
fields: {
name: true,
},
include: [
{
relation: 'orders',
Expand All @@ -100,13 +103,18 @@ describe('HasMany relation', () => {
});

withProtoCheck(false, () => {
expect(customer.orders).length(1);
expect(customer.orders).to.matchEach((v: Partial<Order>) => {
expect(v).to.deepEqual({
id: undefined,
description: order.description,
customerId: undefined,
});
expect(customer).to.deepEqual({
id: undefined,
name: 'a customer',
orders: [
{
id: undefined,
description: order.description,
customerId: undefined,
},
],
reviewsApproved: undefined,
reviewsAuthored: undefined,
});
});
});
Expand All @@ -116,6 +124,10 @@ describe('HasMany relation', () => {
description: 'an order desc',
});
const customer = await customerRepo.findById(existingCustomerId, {
fields: {
id: false,
name: true,
},
include: [
{
relation: 'orders',
Expand All @@ -130,13 +142,18 @@ describe('HasMany relation', () => {
});

withProtoCheck(false, () => {
expect(customer.orders).length(1);
expect(customer.orders).to.matchEach((v: Partial<Order>) => {
expect(v).to.deepEqual({
id: undefined,
description: order.description,
customerId: undefined,
});
expect(customer).to.deepEqual({
id: undefined,
name: 'a customer',
orders: [
{
id: undefined,
description: order.description,
customerId: undefined,
},
],
reviewsApproved: undefined,
reviewsAuthored: undefined,
});
});
});
Expand All @@ -146,6 +163,9 @@ describe('HasMany relation', () => {
description: 'an order desc',
});
const customer = await customerRepo.findById(existingCustomerId, {
fields: {
id: false,
},
include: [
{
relation: 'orders',
Expand All @@ -159,13 +179,18 @@ describe('HasMany relation', () => {
});

withProtoCheck(false, () => {
expect(customer.orders).length(1);
expect(customer.orders).to.matchEach((v: Partial<Order>) => {
expect(v).to.deepEqual({
id: order.id,
description: order.description,
customerId: undefined,
});
expect(customer).to.deepEqual({
id: undefined,
name: 'a customer',
orders: [
{
id: order.id,
description: order.description,
customerId: undefined,
},
],
reviewsApproved: undefined,
reviewsAuthored: undefined,
});
});
});
Expand All @@ -175,6 +200,10 @@ describe('HasMany relation', () => {
description: 'an order desc',
});
const customer = await customerRepo.findById(existingCustomerId, {
fields: {
id: true,
name: true,
},
include: [
{
relation: 'orders',
Expand All @@ -189,22 +218,28 @@ describe('HasMany relation', () => {
});

withProtoCheck(false, () => {
expect(customer.orders).length(1);
expect(customer.orders).to.matchEach((v: Partial<Order>) => {
expect(v).to.deepEqual({
id: undefined,
description: order.description,
customerId: order.customerId,
});
expect(customer).to.deepEqual({
id: 1,
name: 'a customer',
orders: [
{
id: undefined,
description: order.description,
customerId: order.customerId,
},
],
reviewsApproved: undefined,
reviewsAuthored: undefined,
});
});
});

it('includes only fields set in filter', async () => {
it('does not include fields not set in filter', async () => {
await customerOrderRepo.create({
description: 'an order desc',
});
const customer = await customerRepo.findById(existingCustomerId, {
fields: {},
include: [
{
relation: 'orders',
Expand All @@ -216,13 +251,18 @@ describe('HasMany relation', () => {
});

withProtoCheck(false, () => {
expect(customer.orders).length(1);
expect(customer.orders).to.matchEach((v: Partial<Order>) => {
expect(v).to.deepEqual({
id: undefined,
description: undefined,
customerId: undefined,
});
expect(customer).to.deepEqual({
id: undefined,
name: undefined,
orders: [
{
id: undefined,
description: undefined,
customerId: undefined,
},
],
reviewsApproved: undefined,
reviewsAuthored: undefined,
});
});
});
Expand Down Expand Up @@ -345,6 +385,9 @@ describe('BelongsTo relation', () => {

it('can include the related model when the foreign key is omitted in filter', async () => {
const orderWithRelations = (await orderRepo.findById(order.id, {
fields: {
description: true,
},
include: [
{
relation: 'customer',
Expand All @@ -358,18 +401,27 @@ describe('BelongsTo relation', () => {
})) as OrderWithRelations;

withProtoCheck(false, () => {
expect(orderWithRelations.customer).to.deepEqual({
expect(orderWithRelations).to.deepEqual({
id: undefined,
name: customer.name,
orders: undefined,
reviewsApproved: undefined,
reviewsAuthored: undefined,
description: order.description,
customerId: undefined,
customer: {
id: undefined,
name: customer.name,
orders: undefined,
reviewsApproved: undefined,
reviewsAuthored: undefined,
},
});
});
});

it('can include the related model when the foreign key is disabled in filter', async () => {
const orderWithRelations = (await orderRepo.findById(order.id, {
fields: {
description: true,
customerId: false,
},
include: [
{
relation: 'customer',
Expand All @@ -384,18 +436,26 @@ describe('BelongsTo relation', () => {
})) as OrderWithRelations;

withProtoCheck(false, () => {
expect(orderWithRelations.customer).to.deepEqual({
expect(orderWithRelations).to.deepEqual({
id: undefined,
name: customer.name,
orders: undefined,
reviewsApproved: undefined,
reviewsAuthored: undefined,
description: order.description,
customerId: undefined,
customer: {
id: undefined,
name: customer.name,
orders: undefined,
reviewsApproved: undefined,
reviewsAuthored: undefined,
},
});
});
});

it('can include the related model when only the foreign key is disabled in filter', async () => {
const orderWithRelations = (await orderRepo.findById(order.id, {
fields: {
customerId: false,
},
include: [
{
relation: 'customer',
Expand All @@ -409,18 +469,27 @@ describe('BelongsTo relation', () => {
})) as OrderWithRelations;

withProtoCheck(false, () => {
expect(orderWithRelations.customer).to.deepEqual({
id: undefined,
name: customer.name,
orders: undefined,
reviewsApproved: undefined,
reviewsAuthored: undefined,
expect(orderWithRelations).to.deepEqual({
id: order.id,
description: order.description,
customerId: undefined,
customer: {
id: undefined,
name: customer.name,
orders: undefined,
reviewsApproved: undefined,
reviewsAuthored: undefined,
},
});
});
});

it('preserves the foreign key value when set in filter', async () => {
const orderWithRelations = (await orderRepo.findById(order.id, {
fields: {
description: true,
customerId: true,
},
include: [
{
relation: 'customer',
Expand All @@ -435,18 +504,24 @@ describe('BelongsTo relation', () => {
})) as OrderWithRelations;

withProtoCheck(false, () => {
expect(orderWithRelations.customer).to.deepEqual({
id: customer.id,
name: customer.name,
orders: undefined,
reviewsApproved: undefined,
reviewsAuthored: undefined,
expect(orderWithRelations).to.deepEqual({
id: undefined,
description: order.description,
customerId: order.customerId,
customer: {
id: customer.id,
name: customer.name,
orders: undefined,
reviewsApproved: undefined,
reviewsAuthored: undefined,
},
});
});
});

it('includes only fields set in filter', async () => {
it('does not include fields not set in filter', async () => {
const orderWithRelations = (await orderRepo.findById(order.id, {
fields: {},
include: [
{
relation: 'customer',
Expand All @@ -458,12 +533,17 @@ describe('BelongsTo relation', () => {
})) as OrderWithRelations;

withProtoCheck(false, () => {
expect(orderWithRelations.customer).to.deepEqual({
expect(orderWithRelations).to.deepEqual({
id: undefined,
name: undefined,
orders: undefined,
reviewsApproved: undefined,
reviewsAuthored: undefined,
description: undefined,
customerId: undefined,
customer: {
id: undefined,
name: undefined,
orders: undefined,
reviewsApproved: undefined,
reviewsAuthored: undefined,
},
});
});
});
Expand Down
Loading

0 comments on commit bc189e6

Please sign in to comment.