Skip to content

Commit e53c402

Browse files
authored
Merge pull request #64 from oslabs-beta/sh/non-null-tests
Add tests for non-null operators
2 parents 8d594cb + 90ee283 commit e53c402

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed

test/analysis/buildTypeWeights.test.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,103 @@ describe('Test buildTypeWeightsFromSchema function', () => {
489489
});
490490
});
491491

492+
xdescribe('Not null operator (!) is used', () => {
493+
test('on a scalar, enum or object type', () => {
494+
schema = buildSchema(`
495+
type Human{
496+
homePlanet: String!
497+
age: Int!
498+
isHero: Boolean!
499+
droids: Droid!
500+
episode: Episode!
501+
}
502+
type Droid {
503+
primaryFunction: String
504+
}
505+
enum Episode {
506+
NEWHOPE
507+
EMPIRE
508+
JEDI
509+
}
510+
`);
511+
512+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
513+
human: {
514+
weight: 1,
515+
fields: {
516+
homePlanet: {
517+
weight: 0,
518+
},
519+
age: {
520+
weight: 0,
521+
},
522+
isHero: {
523+
weight: 0,
524+
},
525+
droids: {
526+
resolveTo: 'droid',
527+
},
528+
episode: {
529+
resolveTo: 'episode',
530+
},
531+
},
532+
},
533+
droid: {
534+
weight: 1,
535+
fields: {
536+
primaryFunction: {
537+
weight: 0,
538+
},
539+
},
540+
},
541+
episode: {
542+
weight: 0,
543+
fields: {},
544+
},
545+
});
546+
});
547+
548+
test('on list types', () => {
549+
schema = buildSchema(`
550+
type Planet{
551+
droids(first: Int!): [Droid]!
552+
heroDroids(first: Int!): [Droid!]
553+
villainDroids(first: Int!):[Droid!]!
554+
}
555+
type Droid {
556+
primaryFunction: String
557+
}`);
558+
559+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
560+
planet: {
561+
weight: 1,
562+
fields: {
563+
droids: {
564+
resolveTo: 'droid',
565+
weight: expect.any(Function),
566+
},
567+
heroDroids: {
568+
resolveTo: 'droid',
569+
weight: expect.any(Function),
570+
},
571+
villainDroids: {
572+
resolveTo: 'droid',
573+
weight: expect.any(Function),
574+
},
575+
},
576+
},
577+
droid: {
578+
weight: 1,
579+
fields: {
580+
primaryFunction: {
581+
weight: 0,
582+
},
583+
},
584+
},
585+
});
586+
});
587+
});
588+
492589
// TODO: Tests should be written to account for the additional scenarios possible in a schema
493590
// Mutation type
494591
// Input types (a part of mutations?)

test/analysis/typeComplexityAnalysis.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { TypeWeightObject, Variables } from '../../src/@types/buildTypeWeights';
1313
droid(id: ID!): Droid
1414
human(id: ID!): Human
1515
scalars: Scalars
16+
nonNull: [Droid!]!
1617
}
1718
1819
enum Episode {
@@ -100,6 +101,7 @@ import { TypeWeightObject, Variables } from '../../src/@types/buildTypeWeights';
100101
const mockWeightFunction = jest.fn();
101102
const mockHumanFriendsFunction = jest.fn();
102103
const mockDroidFriendsFunction = jest.fn();
104+
const nonNullMockWeightFunction = jest.fn();
103105

104106
// this object is created by the schema above for use in all the tests below
105107
const typeWeights: TypeWeightObject = {
@@ -130,6 +132,10 @@ const typeWeights: TypeWeightObject = {
130132
scalars: {
131133
resolveTo: 'scalars',
132134
},
135+
nonNull: {
136+
resolveTo: 'droid',
137+
weight: nonNullMockWeightFunction,
138+
},
133139
},
134140
},
135141
episode: {
@@ -333,6 +339,14 @@ describe('Test getQueryTypeComplexity function', () => {
333339
expect(mockWeightFunction.mock.calls[1].length).toBe(3); // calling with arguments and variables
334340
});
335341

342+
test('with bounded lists including non-null operators', () => {
343+
query = `query {nonNull(episode: EMPIRE, first: 3) { name, id } }`;
344+
nonNullMockWeightFunction.mockReturnValueOnce(3);
345+
expect(getQueryTypeComplexity(parse(query), {}, typeWeights)).toBe(4); // 1 Query + 3 reviews
346+
expect(nonNullMockWeightFunction.mock.calls.length).toBe(1);
347+
expect(nonNullMockWeightFunction.mock.calls[0].length).toBe(3);
348+
});
349+
336350
describe('with nested lists', () => {
337351
test('and simple nesting', () => {
338352
query = `query { human(id: 1) { name, friends(first: 5) { name, friends(first: 3){ name }}}} `;

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
@@ -82,6 +84,20 @@ describe('Weight Function correctly parses Argument Nodes if', () => {
8284
});
8385
});
8486

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

0 commit comments

Comments
 (0)