From 8d2b1f308847ff1e68e8ad062c359114531a99f6 Mon Sep 17 00:00:00 2001 From: Stephan Halarewicz Date: Sat, 2 Jul 2022 15:29:50 -0400 Subject: [PATCH 1/5] added test cases using non-null operator to buildTypeWeight and weightFunction tests --- test/analysis/buildTypeWeights.test.ts | 72 ++++++++++++++++++++++++++ test/analysis/weightFunction.test.ts | 20 ++++++- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/test/analysis/buildTypeWeights.test.ts b/test/analysis/buildTypeWeights.test.ts index a3f07c5..038e803 100644 --- a/test/analysis/buildTypeWeights.test.ts +++ b/test/analysis/buildTypeWeights.test.ts @@ -443,6 +443,78 @@ describe('Test buildTypeWeightsFromSchema function', () => { }); }); + describe('Not null operator (!) is used', () => { + test('on a scalar, enum or object type', () => { + schema = buildSchema(` + type Human{ + homePlanet: String! + age: Int! + isHero: Boolean! + droids: Droid! + episode: Episode! + } + type Droid { + primaryFunction: String + } + enum Episode { + NEWHOPE + EMPIRE + JEDI + } + `); + + expect(buildTypeWeightsFromSchema(schema)).toEqual({ + human: { + weight: 1, + fields: { + homePlanet: 0, + age: 0, + isHero: 0, + }, + }, + droid: { + weight: 1, + fields: { + primaryFunction: 0, + }, + }, + }); + }); + + test('on list types', () => { + schema = buildSchema(` + type Planet{ + droids(first: Int!): [Droid]! + heroDroids(first: Int!): [Droid!] + villainDroids(first: Int!):[Droid!]! + } + type Droid { + primaryFunction: String + }`); + + expect(buildTypeWeightsFromSchema(schema)).toEqual({ + planet: { + weight: 1, + fields: { + droids: expect.any(Function), + heroDroids: expect.any(Function), + villainDroids: expect.any(Function), + }, + }, + droid: { + weight: 1, + fields: { + primaryFunction: 0, + }, + }, + episode: { + weight: 0, + fields: {}, + }, + }); + }); + }); + // TODO: Tests should be written to account for the additional scenarios possible in a schema // Mutation type // Input types (a part of mutations?) diff --git a/test/analysis/weightFunction.test.ts b/test/analysis/weightFunction.test.ts index 15b943f..c7a9c87 100644 --- a/test/analysis/weightFunction.test.ts +++ b/test/analysis/weightFunction.test.ts @@ -18,8 +18,10 @@ describe('Weight Function correctly parses Argument Nodes if', () => { type Query { reviews(episode: Episode!, first: Int = 5): [Review] heroes(episode: Episode!, first: Int): [Review] - villains(episode: Episode!, limit: Int! = 3): [Review] - characters(episode: Episode!, limit: Int!): [Review] + villains(episode: Episode!, limit: Int! = 3): [Review]! + characters(episode: Episode!, limit: Int!): [Review!] + droids(episode: Episode!, limit: Int!): [Review!]! + } type Review { episode: Episode @@ -78,6 +80,20 @@ describe('Weight Function correctly parses Argument Nodes if', () => { }); }); + test('the list is defined with non-null operators (!)', () => { + const villainsQuery = `query { villains(episode: NEWHOPE, limit: 3) { stars, episode } }`; + const willainsQueryAST: DocumentNode = parse(villainsQuery); + expect(getQueryTypeComplexity(willainsQueryAST, {}, typeWeights)).toBe(4); + + const charQuery = `query { characters(episode: NEWHOPE, limit: 3) { stars, episode } }`; + const charQueryAST: DocumentNode = parse(charQuery); + expect(getQueryTypeComplexity(charQueryAST, {}, typeWeights)).toBe(4); + + const droidsQuery = `droidsQuery { droids(episode: NEWHOPE, limit: 3) { stars, episode } }`; + const droidsQueryAST: DocumentNode = parse(droidsQuery); + expect(getQueryTypeComplexity(droidsQueryAST, {}, typeWeights)).toBe(4); + }); + test('a custom object weight was configured', () => { const customTypeWeights: TypeWeightObject = buildTypeWeightsFromSchema(schema, { object: 3, From 198d7184994d7d9c33cdc0e1c1783100484c4b0c Mon Sep 17 00:00:00 2001 From: Stephan Halarewicz Date: Sat, 2 Jul 2022 15:34:53 -0400 Subject: [PATCH 2/5] type complexity test for nonnull wrappers. --- test/analysis/typeComplexityAnalysis.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/analysis/typeComplexityAnalysis.test.ts b/test/analysis/typeComplexityAnalysis.test.ts index dcafbe2..78894d0 100644 --- a/test/analysis/typeComplexityAnalysis.test.ts +++ b/test/analysis/typeComplexityAnalysis.test.ts @@ -13,6 +13,7 @@ import { TypeWeightObject, Variables } from '../../src/@types/buildTypeWeights'; droid(id: ID!): Droid human(id: ID!): Human scalars: Scalars + nonNull: [Droid!]! } enum Episode { @@ -100,6 +101,7 @@ import { TypeWeightObject, Variables } from '../../src/@types/buildTypeWeights'; const mockWeightFunction = jest.fn(); const mockHumanFriendsFunction = jest.fn(); const mockDroidFriendsFunction = jest.fn(); +const nonNullMockWeightFunction = jest.fn(); // this object is created by the schema above for use in all the tests below const typeWeights: TypeWeightObject = { @@ -114,6 +116,7 @@ const typeWeights: TypeWeightObject = { droid: 1, human: 1, scalars: 1, + nonNull: nonNullMockWeightFunction, }, }, episode: { @@ -295,6 +298,14 @@ describe('Test getQueryTypeComplexity function', () => { expect(mockWeightFunction.mock.calls[1].length).toBe(1); }); + test('with bounded lists including non-null operators', () => { + query = `query {nonNull(episode: EMPIRE, first: 3) { stars, commentary } }`; + nonNullMockWeightFunction.mockReturnValueOnce(3); + expect(getQueryTypeComplexity(parse(query), {}, typeWeights)).toBe(4); // 1 Query + 3 reviews + expect(nonNullMockWeightFunction.mock.calls.length).toBe(1); + expect(nonNullMockWeightFunction.mock.calls[0].length).toBe(1); + }); + xdescribe('with nested lists', () => { test('and simple nesting', () => { query = ` From 751edc4223c771a4cc875687bd7038d74c2efd37 Mon Sep 17 00:00:00 2001 From: Stephan Halarewicz Date: Sat, 2 Jul 2022 15:37:03 -0400 Subject: [PATCH 3/5] disabled non-null tests pending implementation --- test/analysis/buildTypeWeights.test.ts | 2 +- test/analysis/weightFunction.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/analysis/buildTypeWeights.test.ts b/test/analysis/buildTypeWeights.test.ts index 038e803..de7aee7 100644 --- a/test/analysis/buildTypeWeights.test.ts +++ b/test/analysis/buildTypeWeights.test.ts @@ -443,7 +443,7 @@ describe('Test buildTypeWeightsFromSchema function', () => { }); }); - describe('Not null operator (!) is used', () => { + xdescribe('Not null operator (!) is used', () => { test('on a scalar, enum or object type', () => { schema = buildSchema(` type Human{ diff --git a/test/analysis/weightFunction.test.ts b/test/analysis/weightFunction.test.ts index c7a9c87..e0322e9 100644 --- a/test/analysis/weightFunction.test.ts +++ b/test/analysis/weightFunction.test.ts @@ -80,7 +80,7 @@ describe('Weight Function correctly parses Argument Nodes if', () => { }); }); - test('the list is defined with non-null operators (!)', () => { + xtest('the list is defined with non-null operators (!)', () => { const villainsQuery = `query { villains(episode: NEWHOPE, limit: 3) { stars, episode } }`; const willainsQueryAST: DocumentNode = parse(villainsQuery); expect(getQueryTypeComplexity(willainsQueryAST, {}, typeWeights)).toBe(4); From f07b9be81406198d40be0740739aa6e11a7961ae Mon Sep 17 00:00:00 2001 From: Stephan Halarewicz Date: Thu, 7 Jul 2022 22:33:01 -0400 Subject: [PATCH 4/5] updated non-null tests for buildTypeWeightTests with updated typeWeight shape --- test/analysis/buildTypeWeights.test.ts | 35 ++++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/test/analysis/buildTypeWeights.test.ts b/test/analysis/buildTypeWeights.test.ts index 580ba1a..9c305ff 100644 --- a/test/analysis/buildTypeWeights.test.ts +++ b/test/analysis/buildTypeWeights.test.ts @@ -513,15 +513,23 @@ describe('Test buildTypeWeightsFromSchema function', () => { human: { weight: 1, fields: { - homePlanet: 0, - age: 0, - isHero: 0, + homePlanet: { + weight: 0, + }, + age: { + weight: 0, + }, + isHero: { + weight: 0, + }, }, }, droid: { weight: 1, fields: { - primaryFunction: 0, + primaryFunction: { + weight: 0, + }, }, }, }); @@ -542,15 +550,26 @@ describe('Test buildTypeWeightsFromSchema function', () => { planet: { weight: 1, fields: { - droids: expect.any(Function), - heroDroids: expect.any(Function), - villainDroids: expect.any(Function), + droids: { + resolveTo: 'droid', + weight: expect.any(Function), + }, + heroDroids: { + resolveTo: 'droid', + weight: expect.any(Function), + }, + villainDroids: { + resolveTo: 'droid', + weight: expect.any(Function), + }, }, }, droid: { weight: 1, fields: { - primaryFunction: 0, + primaryFunction: { + weight: 0, + }, }, }, episode: { From 90ee2833a10f756ee4b40753d61d40448e1185ad Mon Sep 17 00:00:00 2001 From: Stephan Halarewicz Date: Sat, 9 Jul 2022 21:56:37 -0400 Subject: [PATCH 5/5] corrected type weight non-null tests --- test/analysis/buildTypeWeights.test.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/analysis/buildTypeWeights.test.ts b/test/analysis/buildTypeWeights.test.ts index 48b57cc..0f31137 100644 --- a/test/analysis/buildTypeWeights.test.ts +++ b/test/analysis/buildTypeWeights.test.ts @@ -522,6 +522,12 @@ describe('Test buildTypeWeightsFromSchema function', () => { isHero: { weight: 0, }, + droids: { + resolveTo: 'droid', + }, + episode: { + resolveTo: 'episode', + }, }, }, droid: { @@ -532,6 +538,10 @@ describe('Test buildTypeWeightsFromSchema function', () => { }, }, }, + episode: { + weight: 0, + fields: {}, + }, }); }); @@ -572,10 +582,6 @@ describe('Test buildTypeWeightsFromSchema function', () => { }, }, }, - episode: { - weight: 0, - fields: {}, - }, }); }); });