Skip to content

Commit f2e5764

Browse files
authored
Merge pull request #22 from oslabs-beta/em/typeWeightsTest
Extended test suite of 'typeWeightObject' to account for lists, enums, interfaces and unions
2 parents 3384164 + b6b31cf commit f2e5764

File tree

2 files changed

+228
-19
lines changed

2 files changed

+228
-19
lines changed

src/@types/buildTypeWeights.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
interface Fields {
2-
[index: string]: number;
2+
readonly [index: string]: number | ((arg: number, type: Type) => number);
33
}
44

55
interface Type {
6-
weight: number;
7-
fields: Fields;
6+
readonly weight: number;
7+
readonly fields: Fields;
88
}
99

1010
interface TypeWeightObject {
11-
[index: string]: Type;
11+
readonly [index: string]: Type;
1212
}
1313

1414
interface TypeWeightConfig {

test/analysis/buildTypeWeights.test.ts

Lines changed: 224 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import { buildSchema } from 'graphql';
22
import { GraphQLSchema } from 'graphql/type/schema';
33
import buildTypeWeightsFromSchema from '../../src/analysis/buildTypeWeights';
44

5+
// these types allow the tests to overwite properties on the typeWeightObject
6+
interface TestFields {
7+
[index: string]: number;
8+
}
9+
interface TestType {
10+
weight: number;
11+
fields: TestFields;
12+
}
13+
interface TestTypeWeightObject {
14+
[index: string]: TestType;
15+
}
16+
517
xdescribe('Test buildTypeWeightsFromSchema function', () => {
618
let schema: GraphQLSchema;
719

@@ -28,18 +40,25 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
2840

2941
test('multiple types', () => {
3042
schema = buildSchema(`
43+
type Query {
44+
user: User,
45+
movie: Movie,
46+
}
3147
type User {
3248
name: String
3349
email: String
3450
}
35-
3651
type Movie {
3752
name: String
3853
director: String
3954
}
4055
`);
4156

4257
expect(buildTypeWeightsFromSchema(schema)).toEqual({
58+
Query: {
59+
weight: 1,
60+
fields: {},
61+
},
4362
User: {
4463
weight: 1,
4564
fields: {
@@ -63,12 +82,10 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
6382
user: User
6483
movie: Movie
6584
}
66-
6785
type User {
6886
name: String
69-
email: String
87+
film: Movie
7088
}
71-
7289
type Movie {
7390
name: String
7491
director: User
@@ -121,29 +138,220 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
121138
});
122139
});
123140

141+
test('types with arguments', () => {
142+
schema = buildSchema(`
143+
type Query {
144+
character(id: ID!): Character
145+
}
146+
type Character {
147+
id: ID!
148+
name: String!
149+
}`);
150+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
151+
Query: {
152+
weight: 1,
153+
fields: {},
154+
},
155+
Character: {
156+
weight: 1,
157+
fields: {
158+
id: 0,
159+
name: 0,
160+
},
161+
},
162+
});
163+
});
164+
165+
test('enum types', () => {
166+
schema = buildSchema(`
167+
type Query {
168+
hero(episode: Episode): Character
169+
}
170+
type Character {
171+
id: ID!
172+
name: String!
173+
}
174+
enum Episode {
175+
NEWHOPE
176+
EMPIRE
177+
JEDI
178+
}`);
179+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
180+
Query: {
181+
weight: 1,
182+
fields: {},
183+
},
184+
Character: {
185+
weight: 1,
186+
fields: {
187+
id: 0,
188+
name: 0,
189+
},
190+
},
191+
Episode: {
192+
weight: 0,
193+
fields: {},
194+
},
195+
});
196+
});
197+
198+
test('fields returning lists of objects of determinate size', () => {
199+
schema = buildSchema(`
200+
type Query {
201+
reviews(episode: Episode!, first: Int): [Review]
202+
}
203+
type Review {
204+
episode: Episode
205+
stars: Int!
206+
commentary: String
207+
}
208+
enum Episode {
209+
NEWHOPE
210+
EMPIRE
211+
JEDI
212+
}`);
213+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
214+
Query: {
215+
weight: 1,
216+
fields: {
217+
// FIXME: check the best solution during implementation and update the tests here.
218+
reviews: (arg: number, type: Type) => arg * type.weight,
219+
// code from PR review -> reviews: (type) => args[multiplierName] * typeWeightObject[type].weight
220+
},
221+
},
222+
Review: {
223+
weight: 1,
224+
fields: {
225+
stars: 0,
226+
commentary: 0,
227+
},
228+
},
229+
Episode: {
230+
weight: 0,
231+
fields: {},
232+
},
233+
});
234+
});
235+
236+
// TODO: need to figure out how to handle this situation. Skip for now.
237+
// The field friends returns a list of an unknown number of objects.
238+
xtest('fields returning lists of objects of indetermitae size', () => {
239+
schema = buildSchema(`
240+
type Human {
241+
id: ID!
242+
name: String!
243+
homePlanet: String
244+
friends: [Human]
245+
}
246+
`);
247+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
248+
Human: {
249+
weight: 1,
250+
fields: {
251+
// FIXME: check the best solution during implementation and update the tests here.
252+
friends: (arg: number, type: Type) => arg * type.weight,
253+
},
254+
},
255+
});
256+
});
257+
258+
test('interface types', () => {
259+
schema = buildSchema(`
260+
interface Character {
261+
id: ID!
262+
name: String!
263+
}
264+
265+
type Human implements Character {
266+
id: ID!
267+
name: String!
268+
homePlanet: String
269+
}
270+
271+
type Droid implements Character {
272+
id: ID!
273+
name: String!
274+
primaryFunction: String
275+
}`);
276+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
277+
Character: {
278+
weight: 1,
279+
fields: {
280+
id: 0,
281+
name: 0,
282+
},
283+
},
284+
Human: {
285+
weight: 1,
286+
fields: {
287+
id: 0,
288+
name: 0,
289+
homePlanet: 0,
290+
},
291+
},
292+
Droid: {
293+
weight: 1,
294+
fields: {
295+
id: 0,
296+
name: 0,
297+
primaryFunction: 0,
298+
},
299+
},
300+
Episode: {
301+
weight: 0,
302+
fields: {},
303+
},
304+
});
305+
});
306+
307+
test('union tyes', () => {
308+
schema = buildSchema(`
309+
union SearchResult = Human | Droid
310+
type Human{
311+
homePlanet: String
312+
}
313+
type Droid {
314+
primaryFunction: String
315+
}`);
316+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
317+
SearchResult: {
318+
weight: 1,
319+
fields: {},
320+
},
321+
human: {
322+
weight: 1,
323+
fields: {
324+
homePlanet: 0,
325+
},
326+
},
327+
droid: {
328+
weight: 1,
329+
fields: {
330+
primaryFunction: 0,
331+
},
332+
},
333+
});
334+
});
335+
124336
// TODO: Tests should be written to acount for the additional scenarios possible in a schema
125337
// Mutation type
338+
// Input types (a part of mutations?)
126339
// Subscription type
127-
// List type
128-
// Enem types
129-
// Interface
130-
// Unions
131-
// Input types
132340
});
133341

134342
describe('changes "type weight object" type weights with user configuration of...', () => {
135-
let expectedOutput: TypeWeightObject;
343+
let expectedOutput: TestTypeWeightObject;
136344

137345
beforeEach(() => {
138346
schema = buildSchema(`
139347
type Query {
140-
user: User
141-
movie: Movie
348+
user(id: ID!): User
349+
movie(id: ID!): Movie
142350
}
143351
144352
type User {
145353
name: String
146-
email: String
354+
film: Movie
147355
}
148356
149357
type Movie {
@@ -163,7 +371,6 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
163371
weight: 1,
164372
fields: {
165373
name: 0,
166-
email: 0,
167374
},
168375
},
169376
Movie: {
@@ -202,7 +409,6 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
202409
});
203410

204411
expectedOutput.user.fields.name = 2;
205-
expectedOutput.user.fields.email = 2;
206412
expectedOutput.movie.fields.name = 2;
207413

208414
expect(typeWeightObject).toEqual({ expectedOutput });
@@ -249,5 +455,8 @@ xdescribe('Test buildTypeWeightsFromSchema function', () => {
249455
'negative'
250456
);
251457
});
458+
459+
// TODO: throw validation error if schema is invalid
460+
test('schema is invalid', () => {});
252461
});
253462
});

0 commit comments

Comments
 (0)