Skip to content

Commit f222b82

Browse files
committed
added tests for directives includes and skip in complexity analysis
1 parent 8754486 commit f222b82

File tree

1 file changed

+165
-39
lines changed

1 file changed

+165
-39
lines changed

test/analysis/typeComplexityAnalysis.test.ts

Lines changed: 165 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -794,44 +794,6 @@ describe('Test getQueryTypeComplexity function', () => {
794794
expect(queryParser.processQuery(parse(query))).toBe(5);
795795
});
796796

797-
xtest('that include the "include" directive', () => {
798-
query = `
799-
query {
800-
hero(episode: EMPIRE) {
801-
...@include(if: true) {
802-
name
803-
friends(first: 3) {
804-
name
805-
}
806-
}
807-
... on Human {
808-
homePlanet
809-
}
810-
}
811-
}`;
812-
mockCharacterFriendsFunction.mockReturnValueOnce(3);
813-
// Query 1 + 1 hero + max(...Character 3, ...Human 0) = 5
814-
expect(queryParser.processQuery(parse(query))).toBe(5);
815-
816-
query = `
817-
query {
818-
hero(episode: EMPIRE) {
819-
...@include(if: false) {
820-
name
821-
friends(first: 3) {
822-
name
823-
}
824-
}
825-
... on Human {
826-
homePlanet
827-
}
828-
}
829-
}`;
830-
mockCharacterFriendsFunction.mockReturnValueOnce(3);
831-
// Query 1 + 1 hero + max(...Character 3, ...Human 0) = 5
832-
expect(queryParser.processQuery(parse(query))).toBe(2);
833-
});
834-
835797
test('and multiple fragments apply to the selection set', () => {
836798
query = `
837799
query {
@@ -857,7 +819,7 @@ describe('Test getQueryTypeComplexity function', () => {
857819
});
858820
});
859821

860-
test('with lists of unknown size', () => {
822+
test('with lists of unknown size and a custom @listCost directive is used', () => {
861823
query = `
862824
query {
863825
search(text: "hi") {
@@ -892,6 +854,170 @@ describe('Test getQueryTypeComplexity function', () => {
892854
expect(nonNullMockWeightFunction.mock.calls[0].length).toBe(3);
893855
});
894856

857+
// TODO: refine complexity analysis to consider directives includes and skip
858+
xdescribe('with directives @includes and @skip', () => {
859+
test('@includes on interfaces', () => {
860+
query = `
861+
query {
862+
hero(episode: EMPIRE) {
863+
...@include(if: true) {
864+
name
865+
friends(first: 3) {
866+
name
867+
}
868+
}
869+
... on Human {
870+
homePlanet
871+
}
872+
}
873+
}`;
874+
mockCharacterFriendsFunction.mockReturnValueOnce(3);
875+
// Query 1 + 1 hero + max(...Character 3, ...Human 0) = 5
876+
expect(queryParser.processQuery(parse(query))).toBe(5);
877+
878+
query = `
879+
query {
880+
hero(episode: EMPIRE) {
881+
...@include(if: false) {
882+
name
883+
friends(first: 3) {
884+
name
885+
}
886+
}
887+
... on Human {
888+
homePlanet
889+
}
890+
}
891+
}`;
892+
mockCharacterFriendsFunction.mockReturnValueOnce(3);
893+
// Query 1 + 1 hero = 2
894+
expect(queryParser.processQuery(parse(query))).toBe(2);
895+
});
896+
897+
test('@skip on interfaces', () => {
898+
query = `
899+
query {
900+
hero(episode: EMPIRE) {
901+
...@skip(if: true) {
902+
name
903+
friends(first: 3) {
904+
name
905+
}
906+
}
907+
... on Human {
908+
homePlanet
909+
}
910+
}
911+
}`;
912+
mockCharacterFriendsFunction.mockReturnValueOnce(3);
913+
// Query 1 + 1 hero = 2
914+
expect(queryParser.processQuery(parse(query))).toBe(2);
915+
916+
query = `
917+
query {
918+
hero(episode: EMPIRE) {
919+
...@skip(if: false) {
920+
name
921+
friends(first: 3) {
922+
name
923+
}
924+
}
925+
... on Human {
926+
homePlanet
927+
}
928+
}
929+
}`;
930+
mockCharacterFriendsFunction.mockReturnValueOnce(3);
931+
// Query 1 + 1 hero + max(...Character 3, ...Human 0) = 5
932+
expect(queryParser.processQuery(parse(query))).toBe(5);
933+
});
934+
935+
test('@includes on object types', () => {
936+
query = `query {
937+
hero(episode: EMPIRE) {
938+
id, name
939+
}
940+
human(id: 1) @include(if: true) {
941+
id,
942+
name,
943+
homePlanet
944+
}
945+
}`;
946+
// 1 query + 1 hero + 1 human
947+
expect(queryParser.processQuery(parse(query))).toBe(3);
948+
949+
query = `query {
950+
hero(episode: EMPIRE) {
951+
id, name
952+
}
953+
human(id: 1) @include(if: false) {
954+
id,
955+
name,
956+
homePlanet
957+
}
958+
}`;
959+
// 1 query + 1 hero
960+
expect(queryParser.processQuery(parse(query))).toBe(2);
961+
});
962+
963+
test('@skip on object types', () => {
964+
query = `query {
965+
hero(episode: EMPIRE) {
966+
id, name
967+
}
968+
human(id: 1) @skip(if: true) {
969+
id,
970+
name,
971+
homePlanet
972+
}
973+
}`;
974+
// 1 query + 1 hero
975+
expect(queryParser.processQuery(parse(query))).toBe(2);
976+
977+
query = `query {
978+
hero(episode: EMPIRE) {
979+
id, name
980+
}
981+
human(id: 1) @skip(if: false) {
982+
id,
983+
name,
984+
homePlanet
985+
}
986+
}`;
987+
// 1 query + 1 hero + 1 human
988+
expect(queryParser.processQuery(parse(query))).toBe(3);
989+
});
990+
test('with arguments and varibales', () => {
991+
variables = { directive: false };
992+
queryParser = new ASTParser(typeWeights, variables);
993+
query = `query (directive: $Boolean!){
994+
hero(episode: EMPIRE) {
995+
id, name
996+
}
997+
human(id: 1) @skip(if: $directive) {
998+
id,
999+
name,
1000+
homePlanet
1001+
}
1002+
}`;
1003+
// 1 query + 1 hero + 1 human
1004+
expect(queryParser.processQuery(parse(query))).toBe(3);
1005+
1006+
query = `query (directive: $Boolean!){
1007+
hero(episode: EMPIRE) {
1008+
id, name
1009+
}
1010+
human(id: 1) @includes(if: $directive) {
1011+
id,
1012+
name,
1013+
homePlanet
1014+
}
1015+
}`;
1016+
// 1 query + 1 hero
1017+
expect(queryParser.processQuery(parse(query))).toBe(2);
1018+
});
1019+
});
1020+
8951021
describe('with nested lists', () => {
8961022
test('and simple nesting', () => {
8971023
query = `query { human(id: 1) { name, friends(first: 5) { name, friends(first: 3){ name }}}} `;

0 commit comments

Comments
 (0)