Skip to content

Commit 8d2b1f3

Browse files
committed
added test cases using non-null operator to buildTypeWeight and weightFunction tests
1 parent 4349a67 commit 8d2b1f3

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

test/analysis/buildTypeWeights.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,78 @@ describe('Test buildTypeWeightsFromSchema function', () => {
443443
});
444444
});
445445

446+
describe('Not null operator (!) is used', () => {
447+
test('on a scalar, enum or object type', () => {
448+
schema = buildSchema(`
449+
type Human{
450+
homePlanet: String!
451+
age: Int!
452+
isHero: Boolean!
453+
droids: Droid!
454+
episode: Episode!
455+
}
456+
type Droid {
457+
primaryFunction: String
458+
}
459+
enum Episode {
460+
NEWHOPE
461+
EMPIRE
462+
JEDI
463+
}
464+
`);
465+
466+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
467+
human: {
468+
weight: 1,
469+
fields: {
470+
homePlanet: 0,
471+
age: 0,
472+
isHero: 0,
473+
},
474+
},
475+
droid: {
476+
weight: 1,
477+
fields: {
478+
primaryFunction: 0,
479+
},
480+
},
481+
});
482+
});
483+
484+
test('on list types', () => {
485+
schema = buildSchema(`
486+
type Planet{
487+
droids(first: Int!): [Droid]!
488+
heroDroids(first: Int!): [Droid!]
489+
villainDroids(first: Int!):[Droid!]!
490+
}
491+
type Droid {
492+
primaryFunction: String
493+
}`);
494+
495+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
496+
planet: {
497+
weight: 1,
498+
fields: {
499+
droids: expect.any(Function),
500+
heroDroids: expect.any(Function),
501+
villainDroids: expect.any(Function),
502+
},
503+
},
504+
droid: {
505+
weight: 1,
506+
fields: {
507+
primaryFunction: 0,
508+
},
509+
},
510+
episode: {
511+
weight: 0,
512+
fields: {},
513+
},
514+
});
515+
});
516+
});
517+
446518
// TODO: Tests should be written to account for the additional scenarios possible in a schema
447519
// Mutation type
448520
// Input types (a part of mutations?)

test/analysis/weightFunction.test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
1818
type Query {
1919
reviews(episode: Episode!, first: Int = 5): [Review]
2020
heroes(episode: Episode!, first: Int): [Review]
21-
villains(episode: Episode!, limit: Int! = 3): [Review]
22-
characters(episode: Episode!, limit: Int!): [Review]
21+
villains(episode: Episode!, limit: Int! = 3): [Review]!
22+
characters(episode: Episode!, limit: Int!): [Review!]
23+
droids(episode: Episode!, limit: Int!): [Review!]!
24+
2325
}
2426
type Review {
2527
episode: Episode
@@ -78,6 +80,20 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
7880
});
7981
});
8082

83+
test('the list is defined with non-null operators (!)', () => {
84+
const villainsQuery = `query { villains(episode: NEWHOPE, limit: 3) { stars, episode } }`;
85+
const willainsQueryAST: DocumentNode = parse(villainsQuery);
86+
expect(getQueryTypeComplexity(willainsQueryAST, {}, typeWeights)).toBe(4);
87+
88+
const charQuery = `query { characters(episode: NEWHOPE, limit: 3) { stars, episode } }`;
89+
const charQueryAST: DocumentNode = parse(charQuery);
90+
expect(getQueryTypeComplexity(charQueryAST, {}, typeWeights)).toBe(4);
91+
92+
const droidsQuery = `droidsQuery { droids(episode: NEWHOPE, limit: 3) { stars, episode } }`;
93+
const droidsQueryAST: DocumentNode = parse(droidsQuery);
94+
expect(getQueryTypeComplexity(droidsQueryAST, {}, typeWeights)).toBe(4);
95+
});
96+
8197
test('a custom object weight was configured', () => {
8298
const customTypeWeights: TypeWeightObject = buildTypeWeightsFromSchema(schema, {
8399
object: 3,

0 commit comments

Comments
 (0)